Skip to content

Commit

Permalink
Start adding tests, and use dune
Browse files Browse the repository at this point in the history
  • Loading branch information
cmester0 committed Apr 9, 2024
1 parent 72bd6b2 commit 7fde53b
Show file tree
Hide file tree
Showing 12 changed files with 560 additions and 339 deletions.
100 changes: 100 additions & 0 deletions .github/workflows/coq-hacspec-lib.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@

name: Hacspec - Coq Lib

on:
push:
branches:
- master
pull_request:
branches:
- '**'

jobs:
build:
# the OS must be GNU/Linux to be able to use the docker-coq-action
runs-on: ubuntu-latest
strategy:
matrix:
image:
- 'coqorg/coq:8.18'
fail-fast: false
steps:
- uses: actions/checkout@v3
- uses: DeterminateSystems/nix-installer-action@main
- uses: DeterminateSystems/magic-nix-cache-action@main
- name: Build
run: nix build -L

- name: Install the toolchain
run: |
nix profile install nixpkgs#yq
nix profile install .#rustc
nix profile install .
- name: Install tomlq
run: cargo install --git https://github.com/Techcable/tomlq

- name: Set up environment
run: |
echo "::group::Setting up problem matcher"
echo "::add-matcher::./.github/coq-errors.json"
echo "::endgroup::"
- name: Build Coq-Hacspec lib
uses: coq-community/docker-coq-action@v1
with:
opam_file: 'opam/coq-hacspec.opam'
custom_image: ${{ matrix.image }}

- name: Run Coq on Tests
working-directory: tests
run: |
paths=$(tomlq -r '.workspace.members | .[]' Cargo.toml)
for cratePath in $paths; do
crate=$(tomlq -r '.package.name' "$cratePath/Cargo.toml")
for skip in $SKIPLIST; do
if [[ "$skip" == "$crate" || "$skip" == "$crate-$backend" ]]; then
echo "⛔ $crate [$backend] (skipping)"
continue 2
fi
done
for backend in coq; do
echo "::group::$crate [$backend]"
cargo hax -C -p "$crate" \; into "$backend"
coqc $cratePath/proofs/coq/extraction/*.v
echo "::endgroup::"
done
done
env:
SKIPLIST: |
enum-struct-variant
literals
slices
naming
if-let
enum-repr
pattern-or
side-effects
v1-lib
mut_arg
fnmut
mut-ref-functionalization
generics
loops
even
odd
never-type
attributes
attribute-opaque
raw-attributes
traits
reordering
nested-derefs
minimal
basic-structs
ping-pong
noise-kkpsk0
fn-to-letfun
include-flag
recursion
# See also:
# https://github.com/coq-community/docker-coq-action#readme
# https://github.com/erikmd/docker-coq-github-action-demo
3 changes: 3 additions & 0 deletions dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
(base (>= "0.16.2"))
(coq (>= "8.18.0"))
coq-compcert
coq-coqprime
coq-quickchick
)
(tags
(topics "to describe" your project)))

42 changes: 22 additions & 20 deletions engine/backends/coq/coq/coq_backend.ml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ open AST
module CoqLibrary : Library = struct
module Notation = struct
let int_repr (x : string) (i : string) : string =
"(@repr" ^ " " ^ "WORDSIZE" ^ x ^ " " ^ i ^ ")"
"(Int"^x^".repr" ^ " " ^ i ^ ")"

let type_str : string = "Type"
let bool_str : string = "bool"
Expand Down Expand Up @@ -169,7 +169,7 @@ struct
C.AST.Product (args_ty span args)
| TApp { ident; args } ->
C.AST.AppTy
(C.AST.NameTy (pglobal_ident ident ^ "_t"), args_ty span args)
(C.AST.NameTy (pglobal_ident ident), args_ty span args)
| TArrow (inputs, output) ->
List.fold_right ~init:(pty span output)
~f:(fun x y -> C.AST.Arrow (x, y))
Expand Down Expand Up @@ -385,7 +385,7 @@ struct
| TyAlias { name; ty; _ } ->
[
C.AST.Notation
( "'" ^ pconcrete_ident name ^ "_t" ^ "'",
( "'" ^ pconcrete_ident name ^ "'",
C.AST.Type (pty span ty),
None );
]
Expand Down Expand Up @@ -437,7 +437,7 @@ struct
in
[
C.AST.Notation
( "'" ^ o.type_name ^ "_t" ^ "'",
( "'" ^ o.type_name ^ "'",
C.AST.Type
(C.AST.NatMod
( o.type_of_canvas,
Expand All @@ -449,8 +449,8 @@ struct
[],
C.AST.Var "id",
C.AST.Arrow
( C.AST.NameTy (o.type_name ^ "_t"),
C.AST.NameTy (o.type_name ^ "_t") ) );
( C.AST.NameTy (o.type_name),
C.AST.NameTy (o.type_name) ) );
]
| "bytes" ->
let open Hacspeclib_macro_parser in
Expand All @@ -459,7 +459,7 @@ struct
in
[
C.AST.Notation
( "'" ^ o.bytes_name ^ "_t" ^ "'",
( "'" ^ o.bytes_name ^ "'",
C.AST.Type
(C.AST.ArrayTy
( C.AST.Int { size = C.AST.U8; signed = false },
Expand All @@ -470,8 +470,8 @@ struct
[],
C.AST.Var "id",
C.AST.Arrow
( C.AST.NameTy (o.bytes_name ^ "_t"),
C.AST.NameTy (o.bytes_name ^ "_t") ) );
( C.AST.NameTy (o.bytes_name),
C.AST.NameTy (o.bytes_name) ) );
]
| "unsigned_public_integer" ->
let open Hacspeclib_macro_parser in
Expand All @@ -480,7 +480,7 @@ struct
in
[
C.AST.Notation
( "'" ^ o.integer_name ^ "_t" ^ "'",
( "'" ^ o.integer_name ^ "'",
C.AST.Type
(C.AST.ArrayTy
( C.AST.Int { size = C.AST.U8; signed = false },
Expand All @@ -491,8 +491,8 @@ struct
[],
C.AST.Var "id",
C.AST.Arrow
( C.AST.NameTy (o.integer_name ^ "_t"),
C.AST.NameTy (o.integer_name ^ "_t") ) );
( C.AST.NameTy (o.integer_name),
C.AST.NameTy (o.integer_name) ) );
]
| "public_bytes" ->
let open Hacspeclib_macro_parser in
Expand All @@ -506,14 +506,14 @@ struct
in
[
C.AST.Notation
("'" ^ o.bytes_name ^ "_t" ^ "'", C.AST.Type typ, None);
("'" ^ o.bytes_name ^ "'", C.AST.Type typ, None);
C.AST.Definition
( o.bytes_name,
[],
C.AST.Var "id",
C.AST.Arrow
( C.AST.NameTy (o.bytes_name ^ "_t"),
C.AST.NameTy (o.bytes_name ^ "_t") ) );
( C.AST.NameTy (o.bytes_name),
C.AST.NameTy (o.bytes_name) ) );
]
| "array" ->
let open Hacspeclib_macro_parser in
Expand All @@ -536,7 +536,7 @@ struct
in
[
C.AST.Notation
( "'" ^ o.array_name ^ "_t" ^ "'",
( "'" ^ o.array_name ^ "'",
C.AST.Type
(C.AST.ArrayTy
( C.AST.Int { size = typ; signed = false },
Expand All @@ -547,8 +547,8 @@ struct
[],
C.AST.Var "id",
C.AST.Arrow
( C.AST.NameTy (o.array_name ^ "_t"),
C.AST.NameTy (o.array_name ^ "_t") ) );
( C.AST.NameTy (o.array_name),
C.AST.NameTy (o.array_name) ) );
]
| _ -> unsupported ())
| _ -> unsupported ())
Expand Down Expand Up @@ -667,11 +667,13 @@ let string_of_items : AST.item list -> string =

let hardcoded_coq_headers =
"(* File automatically generated by Hacspec *)\n\
From Hacspec Require Import Hacspec_Lib MachineIntegers.\n\
From Hacspec Require Import Hacspec_Lib.\n\
From Coq Require Import ZArith.\n\
Import List.ListNotations.\n\
Require Import Coq.Strings.String.\n\
Open Scope Z_scope.\n\
Open Scope bool_scope.\n"
Open Scope bool_scope.\n\
Open Scope hacspec_scope.\n"

let translate _ (_bo : BackendOptions.t) (items : AST.item list) :
Types.file list =
Expand Down
4 changes: 2 additions & 2 deletions engine/backends/coq/coq_ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ functor

let literal_to_string (x : AST.literal) : string =
match x with
| Const_string s -> s
| Const_string s -> "\"" ^ s ^ "\"%string"
| Const_char c -> Int.to_string c (* TODO *)
| Const_int (i, { size; _ }) ->
Lib.Notation.int_repr (int_size_to_string size) i
Expand Down Expand Up @@ -304,7 +304,7 @@ functor
let rec term_to_string (x : AST.term) depth : string * bool =
match x with
| AST.UnitTerm -> ("tt", false)
| AST.Let { pattern = pat; value = bind; value_typ = typ; body = term; _ }
| AST.Let { pattern = AST.AscriptionPat (pat, _) | pat; value = bind; value_typ = typ; body = term; _ }
->
(* TODO: propegate type definition *)
let var_str = pat_to_string pat true depth in
Expand Down
31 changes: 31 additions & 0 deletions opam/coq-hacspec.opam
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# This file is generated by dune, edit dune-project instead
opam-version: "2.0"
synopsis: "A short synopsis"
description: "A longer description"
maintainer: ["Lasse Letager Hansen<[email protected]>"]
authors: ["Lasse Letager Hansen<[email protected]>"]
tags: ["topics" "to describe" "your" "project"]
depends: [
"ocaml"
"dune" {>= "3.8"}
"base" {>= "0.16.2"}
"coq" {>= "8.18.0"}
"coq-compcert"
"coq-coqprime"
"coq-quickchick"
"odoc" {with-doc}
]
build: [
["dune" "subst"] {dev}
[
"dune"
"build"
"-p"
name
"-j"
jobs
"@install"
"@runtest" {with-test}
"@doc" {with-doc}
]
]
1 change: 1 addition & 0 deletions proof-libs/coq/coq/_CoqProject
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

src/Hacspec_Types.v
src/Hacspec_Integers.v
src/Hacspec_TODO.v
src/Hacspec_Lib.v
src/Hacspec_lib.v
src/QuickChickLib.v
Loading

0 comments on commit 7fde53b

Please sign in to comment.