Skip to content

Joachim’s Haskell and lean4 experiments #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 146 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
146 commits
Select commit Hold shift + click to select a range
f678ea9
Nix shell setup
nomeata Apr 3, 2023
c28fca5
First stabs at Haskell stuff
nomeata Apr 3, 2023
7208f10
IL: Keep full path of types in CaseE
nomeata Apr 3, 2023
c5c1eea
Haskell: Function definitions
nomeata Apr 3, 2023
d37facb
Records
nomeata Apr 3, 2023
89d2891
include_input flag
nomeata Apr 3, 2023
dd39b7a
More comments
nomeata Apr 3, 2023
c8bbec5
Lean experiments
nomeata Apr 4, 2023
9056af3
Add partiality hints
nomeata Apr 4, 2023
46e98a7
Mor Lean-ing
nomeata Apr 4, 2023
f0e6952
OptE
nomeata Apr 4, 2023
639ba5a
More expressions
nomeata Apr 4, 2023
0aca04d
Record monoid instance
nomeata Apr 4, 2023
e0ac8f8
Mutual definitions
nomeata Apr 4, 2023
3e53cdd
Merge branch 'main' into joachim
nomeata Apr 5, 2023
a7e8577
Undo change to pretty-printer, to make testsuite pass
nomeata Apr 5, 2023
215cd3e
Set up test
nomeata Apr 5, 2023
88e5f23
Include lean build output in test
nomeata Apr 5, 2023
ea68e0b
No need for std4
nomeata Apr 5, 2023
b001a34
More keywords
nomeata Apr 5, 2023
331fd2f
Remove locations in expected test output to reduc diffs
nomeata Apr 5, 2023
29f27bf
Simplify lean test setup
nomeata Apr 5, 2023
e04dc57
Add haskell and lean to CI setup
nomeata Apr 5, 2023
832fe23
Avoid bashim
nomeata Apr 5, 2023
8ed71a7
Test the Pull Request, not the merge commit
nomeata Apr 5, 2023
9bdda96
Try this path
nomeata Apr 5, 2023
ddf5814
Lower case lean
nomeata Apr 5, 2023
dc7d569
Try manually
nomeata Apr 5, 2023
c54c40c
Merge remote-tracking branch 'origin/main' into joachim
nomeata Apr 5, 2023
4a7fd54
Update for multiplicity variables
nomeata Apr 5, 2023
db0f53b
Sigh, curl
nomeata Apr 5, 2023
27384c9
Include multiplicity as comments
nomeata Apr 5, 2023
6ca971b
Add variant flattening IR pass
nomeata Apr 5, 2023
3a8f317
More keywords
nomeata Apr 5, 2023
9750a79
Generate some variant injections
nomeata Apr 5, 2023
89238e9
Generate some more using hints
nomeata Apr 5, 2023
95935b7
Merge remote-tracking branch 'origin/main' into joachim
nomeata Apr 6, 2023
e102bcc
Namespace functions separately
nomeata Apr 6, 2023
b44d2bc
Escape functions differently
nomeata Apr 6, 2023
c7188db
More variant injections
nomeata Apr 6, 2023
f52b26c
More injections
nomeata Apr 6, 2023
55001a9
Merge remote-tracking branch 'origin/main' into joachim
nomeata Apr 7, 2023
dfc4c62
Normalize better
nomeata Apr 7, 2023
7147fc7
Unary IterE
nomeata Apr 7, 2023
f5d5788
Some promoted premises
nomeata Apr 7, 2023
5dc8377
More option iteration
nomeata Apr 7, 2023
73d074b
Work around iteration over no variables
nomeata Apr 7, 2023
da8b2dc
More debug printing
nomeata Apr 7, 2023
ba84926
Treat e*{} as list injection
nomeata Apr 7, 2023
8a97b51
Do not warn about unused variables. Down to three errors!
nomeata Apr 7, 2023
53fcc98
Work around «$admininstr_globalinst»
nomeata Apr 7, 2023
158c1c5
Fix haskell escaping
nomeata Apr 7, 2023
5f77afd
Haskell: Handle declartions without clauses
nomeata Apr 7, 2023
176c587
Merge remote-tracking branch 'origin/main' into joachim
nomeata Apr 7, 2023
747c561
Update output
nomeata Apr 7, 2023
63b092f
Merge commit '69e814ffa8feaafacbfeefdc1b16efb38b6a006e' into joachim
nomeata Apr 8, 2023
2e33fe4
Merge commit 'ccecfdaf7dfc80feab2d165bb93d46e35dfaff8e' into joachim
nomeata Apr 8, 2023
8450cf2
Merge remote-tracking branch 'origin/main' into joachim
nomeata Apr 8, 2023
6c323ea
Add more hints until we infer SubE occurrences
nomeata Apr 8, 2023
572c691
A pass to totalize functions. Introduces TheE
nomeata Apr 8, 2023
27d31ca
Introduce middlend
nomeata Apr 8, 2023
4ee9b57
Remove nix files (until they are welcome on main) easier to switch br…
nomeata Apr 8, 2023
b0c87a1
Move TheE near OptE
nomeata Apr 8, 2023
779c421
Merge remote-tracking branch 'origin/main' into joachim
nomeata Apr 8, 2023
57fe5eb
Merge branch 'totalize' into joachim
nomeata Apr 8, 2023
6ec28e3
New Middleend pass: Making side-conditions explicit
nomeata Apr 8, 2023
1f90e4e
Merge branch 'main' into joachim
nomeata Apr 8, 2023
60e697c
Merge branch 'eq_improvements' into joachim
nomeata Apr 8, 2023
0fad493
Merge remote-tracking branch 'origin/main' into joachim
nomeata Apr 9, 2023
50661c7
Update output post-merge
nomeata Apr 9, 2023
ac2dbcd
Run middle end as part of test-frontend
nomeata Apr 9, 2023
7b8cd81
Side conditions: Now also side-conditions from iteration
nomeata Apr 9, 2023
e194c11
Unify binary
nomeata Apr 9, 2023
10857b0
Add printing flags
nomeata Apr 9, 2023
977a21a
Separte test-middlend directory
nomeata Apr 9, 2023
9112e41
Merge branch 'exe-middlend' into joachim
nomeata Apr 9, 2023
cb22d24
No-change test output update
nomeata Apr 9, 2023
e6095b7
Else elimination pass
nomeata Apr 9, 2023
72459af
Try to recognize apart expression (does not work yet)
nomeata Apr 9, 2023
b10eb9c
Merge branch 'exe-middlend' into joachim
nomeata Apr 9, 2023
0de4e50
Correctly detect apartness
nomeata Apr 9, 2023
e9b2979
Eta-contract some lamdas
nomeata Apr 9, 2023
cef6357
Improve backend tests, so that golden value is a file of its own
nomeata Apr 9, 2023
84abc0c
Add negated premises
nomeata Apr 9, 2023
e97f561
Merge branch 'NegPr' into joachim
nomeata Apr 9, 2023
44a4446
eq_prem: Add NegPr case
nomeata Apr 9, 2023
4b6dc62
Remove implied premises by prior premises, cleans up some noise
nomeata Apr 9, 2023
ab04eb6
CompE is actually flipped concatenation
nomeata Apr 10, 2023
c6f30d9
Lean backend: Unop MinusOp
nomeata Apr 10, 2023
cff7223
Implement UpdE, and fix lean4 test runner
nomeata Apr 10, 2023
d917eb2
Implement ExtE. All occuring expressions are translated!
nomeata Apr 10, 2023
f3df355
Rename flat.ml → sub.ml
nomeata Apr 11, 2023
d0b839b
Correctly infer which injections need to be produced
nomeata Apr 11, 2023
dad82c3
Remove hints
nomeata Apr 11, 2023
3f2422c
Merge branch 'exe-middlend' into joachim
nomeata Apr 11, 2023
0a8b9f4
Merge branch 'exe-middlend' into joachim
nomeata Apr 11, 2023
125f5ee
Some lean4 code cleanup
nomeata Apr 11, 2023
9453e21
Il.Print: Do not add comments when `at = no_region`
nomeata Apr 11, 2023
2ce3562
Il.Print: Do not add comments when `at = no_region`
nomeata Apr 11, 2023
aaebd82
Merge branch 'print-no-region' into joachim
nomeata Apr 11, 2023
ae45be3
Clean up prelude
nomeata Apr 12, 2023
0c11a8f
Forgot to promote
nomeata Apr 12, 2023
aa779e7
Merge branch 'sideconditions' into joachim
nomeata Apr 13, 2023
9fcb5c0
Merge branch 'sub-elim' into joachim
nomeata Apr 13, 2023
83695fb
Merge branch 'sub-elim' into joachim
nomeata Apr 21, 2023
c69cf55
Merge branch 'sideconditions' into joachim
nomeata Apr 21, 2023
5b6bd22
Prepare --the-elimination pass
nomeata Apr 21, 2023
fcefbe6
Implement --the-elimination
nomeata Apr 21, 2023
b5d64de
Merge branch 'the-elimination' into joachim
nomeata Apr 21, 2023
43dce98
Merge branch 'the-elimination' into joachim
nomeata Apr 22, 2023
22936e6
Merge branch 'the-elimination' into joachim
nomeata Apr 22, 2023
fac0188
Merge branch 'the-elimination' into joachim
nomeata Apr 22, 2023
6655af0
Merge remote-tracking branch 'origin/master' into print-no-region
nomeata Apr 24, 2023
f4c5215
Merge remote-tracking branch 'origin/master' into NegPr
nomeata Apr 24, 2023
ca57e9e
Update sideconditions
nomeata Apr 24, 2023
0b5d6d9
Merge branch 'the-elimination' into joachim
nomeata Apr 24, 2023
396fd39
Merge branch 'print-no-region' into joachim
nomeata Apr 24, 2023
01b0395
Merge branch 'sub-elim' into joachim
nomeata Apr 24, 2023
02a69b9
Merge branch 'NegPr' into joachim
nomeata Apr 24, 2023
efe04b6
Fix merge blunder
nomeata Apr 24, 2023
4c539fb
Adjust coding style
nomeata Apr 24, 2023
4aec85d
Assume totalize has run
nomeata Apr 24, 2023
a901743
Merge branch 'main' into NegPr
nomeata Apr 26, 2023
70fb291
Merge branch 'main' into print-no-region
nomeata Apr 26, 2023
b47a88e
Partially merge branch 'NegPr' into joachim
nomeata Apr 26, 2023
0e6c4e0
Merge branch 'print-no-region' into joachim
nomeata Apr 26, 2023
b649ac8
Partially merge branch 'sub-elim' into joachim
nomeata Apr 26, 2023
e639da3
Partially merge branch 'the-elimination' into joachim
nomeata Apr 26, 2023
cd51aa7
Finish merge
nomeata Apr 26, 2023
11308af
Merge remote-tracking branch 'origin/main' into joachim
nomeata Apr 26, 2023
e3dddae
IL: (Conservative) apartness checker
nomeata May 3, 2023
fd3484a
Refactor a bit
nomeata May 3, 2023
3d73939
Merge branch 'apart' into joachim
nomeata May 3, 2023
860fab5
Merge branch 'main' of github.com:Wasm-DSL/spectec into joachim
nomeata May 9, 2023
b3a369e
Il-to-il-pass: Replace wildcards with variables
nomeata Jun 9, 2023
dc81e1c
Merge branch 'wild' into joachim
nomeata Jun 9, 2023
447dd0a
Promote output
nomeata Jun 9, 2023
1fabac8
Undo spec change about MUT?
nomeata Jun 9, 2023
7beb979
Lean: Implement MulOp
nomeata Jun 9, 2023
66045ab
Merge branch 'main' of github.com:Wasm-DSL/spectec into wild
nomeata Jun 9, 2023
db69789
Merge branch 'wild' into joachim
nomeata Jun 9, 2023
87e36ce
Merge branch 'master' of github.com:Wasm-DSL/spectec into joachim
nomeata Jun 12, 2023
2fe7e13
Merge branch 'main' into joachim
nomeata Jun 12, 2023
b794380
Implement SliceE and SliceP
nomeata Jun 29, 2023
cd4e3a6
Merge branch 'master' of github.com:Wasm-DSL/spectec into joachim
nomeata Jun 29, 2023
8a83393
Forgot to promote
nomeata Jun 29, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion .github/workflows/ci-spectec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,23 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
ref: ${{ github.event.pull_request.head.sha }}
- name: Setup OCaml
uses: ocaml/setup-ocaml@v2
with:
ocaml-compiler: 4.14.x
- name: Setup Haskell
uses: haskell/actions/setup@v2
with:
ghc-version: 9.2
- name: Setup Lean
run: |
cd /tmp
curl -LO https://github.com/leanprover/lean4-nightly/releases/download/nightly-2023-02-10/lean-4.0.0-nightly-2023-02-10-linux.tar.zst
tar xaf lean-4.0.0-nightly-2023-02-10-linux.tar.zst
echo "/tmp/lean-4.0.0-nightly-2023-02-10-linux/bin" >> $GITHUB_PATH
- name: Setup Dune
run: opam install --yes dune menhir mdx
- name: Setup Latex
Expand Down
8 changes: 8 additions & 0 deletions spectec/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
watsup
watsup-haskell
watsup-lean4

# lean stuff
build
lake-packages

.direnv
13 changes: 7 additions & 6 deletions spectec/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@
NAME = watsup
EXT = $(NAME)

TESTDIRS = test-frontend test-latex
TESTDIRS = test-frontend test-middlend test-latex


DUNE_OPTS ?=

# Main targets

.PHONY: default all ci

default: exe
default: exe exe-haskell
all: exe latex test
ci: all test-frontend test-latex
ci: all test-frontend test-middlend test-latex


# Executable
Expand All @@ -24,9 +26,8 @@ OUTDIR = _build/default/src
.PHONY: exe

exe:
dune build $(SRCDIR)/$(EXE)
ln -f $(OUTDIR)/$(EXE) ./$(NAME)

dune build $(DUNE_OPTS) $(SRCDIR)/$(EXE)
ln -sf $(OUTDIR)/$(EXE) ./$(NAME)

# Latex

Expand Down
1 change: 1 addition & 0 deletions spectec/lake-manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"version": 4, "packagesDir": "lake-packages", "packages": []}
10 changes: 10 additions & 0 deletions spectec/lakefile.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import Lake

open Lake DSL

package spectec

@[default_target]
lean_lib SpecTec where

/- require std from git "https://github.com/leanprover/std4" @ "main" -/
1 change: 1 addition & 0 deletions spectec/lean-toolchain
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
leanprover/lean4:nightly-2023-02-10
5 changes: 5 additions & 0 deletions spectec/src/backend-haskell/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(library
(name backend_haskell)
(libraries util el frontend str)
(modules gen)
)
157 changes: 157 additions & 0 deletions spectec/src/backend-haskell/gen.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
open Il.Ast

let include_input = false

let parens s = "(" ^ s ^ ")"
let ($$) s1 s2 = parens (s1 ^ " " ^ s2)
let render_tuple how tys = parens (String.concat ", " (List.map how tys))

let render_type_name (id : id) = String.capitalize_ascii id.it

let render_rec_con (id : id) = "Mk" ^ render_type_name id

let is_reserved = function
| "data"
-> true
| _ -> false

let make_id s = match s with
| s when is_reserved s -> s ^ "_"
| s -> String.map (function
| '.' -> '_'
| '-' -> '_'
| c -> c
) s

let render_con_name id : atom -> string = function
| Atom s ->
(* Lets disambiguate always, to start with
if String.starts_with ~prefix:"_" s
then render_type_name id ^ s
else String.capitalize_ascii s
*)
if String.starts_with ~prefix:"_" s
then render_type_name id ^ make_id s
else render_type_name id ^ "_" ^ make_id s
| a -> "{- render_con_name: TODO -} " ^ Il.Print.string_of_atom a

let render_con_name' (typ : typ) a = match typ.it with
| VarT id -> render_con_name id a
| _ -> "_ {- render_con_name': Typ not id " ^ Il.Print.string_of_typ typ ^ " -}"

let render_field_name : atom -> string = function
| Atom s -> String.uncapitalize_ascii (make_id s)
| a -> "{- render_field_name: TODO -} " ^ Il.Print.string_of_atom a


let rec render_typ (ty : typ) = match ty.it with
| VarT id -> render_type_name id
| BoolT -> "Bool"
| NatT -> "Natural"
| TextT -> "String"
| TupT tys -> render_tuple render_typ tys
| IterT (ty, Opt) -> "Maybe" $$ render_typ ty
| IterT (ty, _) -> "[" ^ render_typ ty ^ "]"

let _unsupported_def d =
"{- " ^
Il.Print.string_of_def d ^
"\n-}"

let rec prepend first rest = function
| [] -> ""
| (x::xs) -> first ^ x ^ prepend rest rest xs


let render_variant_inj id1 id2 =
render_type_name id1 ^ "_of_" ^ render_type_name id2

let render_variant_inj' (typ1 : typ) (typ2 : typ) = match typ1.it, typ2.it with
| VarT id1, VarT id2 -> render_variant_inj id1 id2
| _, _ -> "_ {- render_variant_inj': Typs not ids -}"

let render_variant_case id (case : typcase) = match case with
| (a, {it = TupT [];_}, _hints) -> render_con_name id a
| (a, ty, _hints) -> render_con_name id a ^ " " ^ render_typ ty

let rec render_exp (exp : exp) = match exp.it with
| VarE v -> v.it
| BoolE true -> "True"
| BoolE false -> "Frue"
| NatE n -> string_of_int n
| TextE t -> "\"" ^ String.escaped t ^ "\""
| MixE (_, e) -> render_exp e
| TupE es -> render_tuple render_exp es
| IterE (e, _) -> render_exp e
| CaseE (a, e) -> render_case a e exp.note
| SubE (e, typ1, typ2) -> render_variant_inj' typ2 typ1 $$ render_exp e
| DotE (e, a) -> render_exp e ^ "." ^ render_field_name a
| IdxE (e1, e2) -> parens (render_exp e1 ^ " !! " ^ ("fromIntegral" $$ render_exp e2))
| BinE (AddOp, e1, e2) -> parens (render_exp e1 ^ " + " ^ render_exp e2)
| _ -> "undefined {- " ^ Il.Print.string_of_exp exp ^ " -}"

and render_case a e typ =
if e.it = TupE []
then render_con_name' typ a
else render_con_name' typ a $$ render_exp e

let render_clause (id : id) (clause : clause) = match clause.it with
| DefD (_binds, lhs, rhs, premise) ->
(if premise <> [] then "-- Premises ignored! \n" else "") ^
make_id id.it ^ " " ^ render_exp lhs ^ " = " ^ render_exp rhs

let rec render_def (d : def) =
begin
if include_input then
"{- " (* ^ Util.Source.string_of_region d.at ^ "\n"*) ^
Il.Print.string_of_def d ^
"\n-}\n"
else ""
end ^
match d.it with
| SynD (id, deftyp) ->
begin match deftyp.it with
| AliasT ty ->
"type " ^ render_type_name id ^ " = " ^ render_typ ty
| NotationT (mop, ty) ->
"type " ^ render_type_name id ^ " = {- mixop: " ^ Il.Print.string_of_mixop mop ^ " -} " ^ render_typ ty
| VariantT cases ->
"data " ^ render_type_name id ^ prepend "\n = " "\n | " (
List.map (render_variant_case id) cases
)
| StructT fields ->
(*
"type " ^ render_type_name id ^ " = " ^ render_tuple render_typ (
List.map (fun (_a, ty, _hints) -> ty) fields
)
*)
"data " ^ render_type_name id ^ " = " ^ render_rec_con id ^ prepend "\n { " "\n , " (
List.map (fun (a, ty, _hints) -> render_field_name a ^ " :: " ^ render_typ ty) fields
) ^ "\n }"
end
| DecD (id, typ1, typ2, clauses) ->
make_id id.it ^ " :: " ^ render_typ typ1 ^ " -> " ^ render_typ typ2 ^ "\n" ^
if clauses = []
then make_id id.it ^ " = error \"function without clauses\""
else String.concat "\n" (List.map (render_clause id) clauses)
| RecD defs ->
String.concat "\n" (List.map render_def defs)
| _ -> ""

let render_script (el : script) =
String.concat "\n\n" (List.map render_def el)

let gen_string (el : script) =
"{-# LANGUAGE OverloadedRecordDot #-}\n" ^
"{-# LANGUAGE DuplicateRecordFields #-}\n" ^
"module Test where\n" ^
"import Prelude (Bool, String, undefined, error, Maybe, fromIntegral, (+), (!!))\n" ^
"import Numeric.Natural (Natural)\n" ^
render_script el


let gen_file file el =
let haskell_code = gen_string el in
let oc = Out_channel.open_text file in
Fun.protect (fun () -> Out_channel.output_string oc haskell_code)
~finally:(fun () -> Out_channel.close oc)
2 changes: 2 additions & 0 deletions spectec/src/backend-haskell/gen.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
val gen_string : Il.Ast.script -> string
val gen_file : string -> Il.Ast.script -> unit
5 changes: 5 additions & 0 deletions spectec/src/backend-lean4/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(library
(name backend_lean4)
(libraries util el frontend str)
(modules gen)
)
Loading