Skip to content
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

DSL for module semantics #32

Merged
merged 50 commits into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
152b2d0
Change instance representation as record
ShinWonho Jun 13, 2023
a1c51f9
Add limits check in grow reduction rules
ShinWonho Jun 13, 2023
5380d76
Add type field in eleminst
ShinWonho Jun 14, 2023
2b82dcc
Type check from updated store
Jun 15, 2023
6b6bd32
Consider free variables in premises of DefD
ShinWonho Jun 16, 2023
835bf0d
Add grow helper functions
ShinWonho Jun 16, 2023
7d757da
Fix typos
ShinWonho Jun 16, 2023
7aaff60
Fix typo in data segment
Jun 22, 2023
cecacb5
Draft for module semantics
Jun 22, 2023
2a158db
Add premises for updating store
Jun 22, 2023
066f8c3
Add premises for start function
Jun 22, 2023
6c7d379
Add export allocation
Jun 22, 2023
97d3fe6
Add import allocation
Jun 22, 2023
68c6067
Add 7-module.watsup
ShinWonho Jun 26, 2023
b8681db
Update TEST.md
ShinWonho Jun 26, 2023
9b3e6e3
Fix typo in instantiation
ShinWonho Jun 26, 2023
030a3a8
Update grow helper functions
ShinWonho Jun 28, 2023
d967107
Update spectec/src/frontend/elab.ml
ShinWonho Jun 28, 2023
8f6c590
Update spectec/src/il/validation.ml
ShinWonho Jun 28, 2023
0f48772
Refactor module semantics
ShinWonho Jun 29, 2023
35be3dd
Minor changes
Jun 29, 2023
edda75e
Minor changes
Jun 29, 2023
ff216fb
Update module semantics
Jun 30, 2023
21e0a94
Minor change
Jun 30, 2023
236de04
Minor change
ShinWonho Jul 3, 2023
1560490
Fix typo
ShinWonho Jul 4, 2023
e7a5252
Add iter with index
ShinWonho Jul 5, 2023
6bc75a6
Update elab and validation
ShinWonho Jul 5, 2023
506306e
Compute address in alloc func
ShinWonho Jul 7, 2023
eb57512
Minor changes
ShinWonho Jul 13, 2023
5aeb393
Add iter with an index
ShinWonho Jul 12, 2023
a453c69
Allow opt iter to have list iter type
ShinWonho Jul 7, 2023
88fe94b
Fix location of \land for conditions of funcdef
f52985 Jun 28, 2023
42b276f
Filter with recursive
ShinWonho Jul 14, 2023
65e6777
Minor change
ShinWonho Jul 14, 2023
edb1fbf
Merge two render_condition functions
ShinWonho Jul 19, 2023
8bb5fe0
Merge ListN
ShinWonho Jul 20, 2023
6872abe
Fix bug in iter with index
ShinWonho Jul 21, 2023
616eded
Minor changes
ShinWonho Jul 24, 2023
d764e97
Minor change
ShinWonho Jul 24, 2023
5c9aed2
Check iter with index
ShinWonho Jul 24, 2023
ba5b056
Minor changes
ShinWonho Jul 26, 2023
31213dd
Strip index variable in iter
ShinWonho Jul 26, 2023
474c65f
Add type check for index variable
ShinWonho Jul 26, 2023
384016d
Remove wrong assertion
ShinWonho Jul 26, 2023
fd8ec1f
Update spectec/src/frontend/elab.ml
ShinWonho Jul 26, 2023
97e95c3
Add validation for iter with index
ShinWonho Jul 26, 2023
0e45bef
Update spectec/src/il/validation.ml
ShinWonho Jul 26, 2023
199efd1
Minor change
ShinWonho Jul 26, 2023
cb45bce
Merge branch 'main' into module-semantics
ShinWonho Jul 26, 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
12 changes: 6 additions & 6 deletions spectec/src/backend-latex/render.ml
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ exception Arity_mismatch
let rec expand_iter args iter =
match iter with
| Opt | List | List1 -> iter
| ListN (e, opt) -> ListN (expand_exp args e, opt)
| ListN (e, id_opt) -> ListN (expand_exp args e, id_opt)

and expand_exp args e = expand_exp' args e.it $ e.at
and expand_exp' args e' =
Expand Down Expand Up @@ -442,9 +442,9 @@ and render_iter env = function
| List -> "^\\ast"
| List1 -> "^{+}"
| ListN ({it = ParenE (e, _); _}, None) | ListN (e, None) ->
"^{" ^ render_exp env e ^ "}"
"^{" ^ render_exp env e ^ "}"
| ListN (e, Some id) ->
"^(" ^ id.it ^ "<" ^ render_exp env e ^ ")"
"^(" ^ id.it ^ "<" ^ render_exp env e ^ ")"


(* Types *)
Expand Down Expand Up @@ -687,7 +687,7 @@ let render_ruledef env d =
render_rule_deco env " \\, " id1 id2 ""
| _ -> failwith "render_ruledef"

let render_condition env tabs = function
let render_conditions env tabs = function
| [] -> " & "
| [Elem {it = ElsePr; _}] -> " &\\quad\n " ^ word "otherwise"
| (Elem {it = ElsePr; _})::prems ->
Expand All @@ -708,14 +708,14 @@ let render_reddef env d =
in
render_rule_deco env "" id1 id2 " \\quad " ^ "& " ^
render_exp env e1 ^ " &" ^ render_atom env SqArrow ^ "& " ^
render_exp env e2 ^ render_condition env "&&&&" prems
render_exp env e2 ^ render_conditions env "&&&&" prems
| _ -> failwith "render_reddef"

let render_funcdef env d =
match d.it with
| DefD (id1, e1, e2, prems) ->
render_exp env (CallE (id1, e1) $ d.at) ^ " &=& " ^
render_exp env e2 ^ render_condition env "&&&" prems
render_exp env e2 ^ render_conditions env "&&&" prems
| _ -> failwith "render_funcdef"

let rec render_sep_defs ?(sep = " \\\\\n") ?(br = " \\\\[0.8ex]\n") f = function
Expand Down
2 changes: 1 addition & 1 deletion spectec/src/el/eq.ml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ let rec eq_iter iter1 iter2 =
match iter1, iter2 with
| ListN (e1, None), ListN (e2, None) -> eq_exp e1 e2
| ListN (e1, Some id1), ListN (e2, Some id2) ->
eq_exp e1 e2 && id1.it = id2.it
eq_exp e1 e2 && id1.it = id2.it
| _, _ -> false


Expand Down
3 changes: 2 additions & 1 deletion spectec/src/el/free.ml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ let union sets1 sets2 =
defid = Set.union sets1.defid sets2.defid;
}

let free_opt free_x xo = Option.(value (map free_x xo) ~default:empty)
let free_list free_x xs = List.(fold_left union empty (map free_x xs))

let free_nl_elem free_x = function Nl -> empty | Elem x -> free_x x
Expand All @@ -37,7 +38,7 @@ let free_defid id = {empty with defid = Set.singleton id.it}
let rec free_iter iter =
match iter with
| Opt | List | List1 -> empty
| ListN (e, _) -> free_exp e
| ListN (e, id_opt) -> union (free_exp e) (free_opt free_varid id_opt)


(* Types *)
Expand Down
3 changes: 1 addition & 2 deletions spectec/src/el/print.ml
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ let rec string_of_iter iter =
| List -> "*"
| List1 -> "+"
| ListN (e, None) -> "^" ^ string_of_exp e
| ListN (e, Some (id)) ->
"^(" ^ id.it ^ "<" ^ string_of_exp e ^ ")"
| ListN (e, Some (id)) -> "^(" ^ id.it ^ "<" ^ string_of_exp e ^ ")"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I missed this nit before:

Suggested change
| ListN (e, Some (id)) -> "^(" ^ id.it ^ "<" ^ string_of_exp e ^ ")"
| ListN (e, Some id) -> "^(" ^ id.it ^ "<" ^ string_of_exp e ^ ")"



(* Types *)
Expand Down
2 changes: 1 addition & 1 deletion spectec/src/frontend/elab.ml
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ let rec elab_iter env iter : Il.iter =
| Opt -> Il.Opt
| List -> Il.List
| List1 -> Il.List1
| ListN (e, opt) -> Il.ListN (elab_exp env e (NatT $ e.at), opt)
| ListN (e, id_opt) -> Il.ListN (elab_exp env e (NatT $ e.at), id_opt)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also needs to check the type of the id.



(* Types *)
Expand Down
11 changes: 8 additions & 3 deletions spectec/src/frontend/multiplicity.ml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ let iter_nl_list f xs = List.iter (function Nl -> () | Elem x -> f x) xs
let rec check_iter env ctx iter =
match iter with
| Opt | List | List1 -> ()
| ListN (e, _) ->
| ListN (e, opt) ->
Option.iter (fun id -> check_id env (iter::ctx) id) opt;
check_exp env ctx e
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
| ListN (e, opt) ->
Option.iter (fun id -> check_id env (iter::ctx) id) opt;
check_exp env ctx e
| ListN (e, id_opt) ->
Option.iter (fun id -> check_id env (ListN (e, None) :: ctx) id) id_opt;
check_exp env ctx e


and check_exp env ctx e =
Expand Down Expand Up @@ -162,9 +163,13 @@ let union = Env.union (fun _ ctx1 ctx2 -> assert (ctx1 = ctx2); Some ctx1)
let rec annot_iter env iter : Il.Ast.iter * occur =
match iter with
| Opt | List | List1 -> iter, Env.empty
| ListN (e, opt) ->
| ListN (e, None) ->
let e', occur = annot_exp env e in
ListN (e', opt), occur
ListN (e', None), occur
| ListN (e, Some id) ->
let e', occur1 = annot_exp env e in
let occur2 = Env.singleton id.it [] in
ListN (e', Some id), union occur1 occur2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
| ListN (e, None) ->
let e', occur = annot_exp env e in
ListN (e', opt), occur
ListN (e', None), occur
| ListN (e, Some id) ->
let e', occur1 = annot_exp env e in
let occur2 = Env.singleton id.it [] in
ListN (e', Some id), union occur1 occur2
| ListN (e, id_opt) ->
let e', occur1 = annot_exp env e in
let occur2 =
match id_opt with None -> Env.empty | Some id -> Env.singleton id.it [] in
ListN (e', id_opt), union occur1 occur2


and annot_exp env e : Il.Ast.exp * occur =
let it, occur =
Expand Down
4 changes: 2 additions & 2 deletions spectec/src/frontend/parser.mly
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ iter :
| STAR { List }
| UP arith_prim
{ match $2.it with
| ParenE ({ it = CmpE({ it = VarE id; _ }, LtOp, e); _}, false) ->
ListN (e, Some id)
| ParenE ({it = CmpE({it = VarE id; _}, LtOp, e); _}, false) ->
ListN (e, Some id)
| _ -> ListN ($2, None)
}

Expand Down
2 changes: 1 addition & 1 deletion spectec/src/il/eq.ml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ let rec eq_iter iter1 iter2 =
match iter1, iter2 with
| ListN (e1, None), ListN (e2, None) -> eq_exp e1 e2
| ListN (e1, Some id1), ListN (e2, Some id2) ->
eq_exp e1 e2 && id1.it = id2.it
eq_exp e1 e2 && id1.it = id2.it
| _, _ -> false


Expand Down
2 changes: 1 addition & 1 deletion spectec/src/il/free.ml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ let free_defid id = {empty with defid = Set.singleton id.it}
let rec free_iter iter =
match iter with
| Opt | List | List1 -> empty
| ListN (e, _) -> free_exp e
| ListN (e, id_opt) -> union (free_exp e) (free_opt free_varid id_opt)


(* Types *)
Expand Down
16 changes: 10 additions & 6 deletions spectec/src/il/validation.ml
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,15 @@ let valid_list valid_x_y env xs ys at =
let rec valid_iter env iter =
match iter with
| Opt | List | List1 -> ()
| ListN (e, _) -> valid_exp env e (NatT $ e.at)
| ListN (e, None) -> valid_exp env e (NatT $ e.at)
| ListN (e, Some id) ->
valid_exp env e (NatT $ e.at);
let t', dim = find "variable" env.vars id in
equiv_typ env t' (NatT $ e.at) e.at;
if dim <> [iter] then
error e.at ("use of iterated variable `" ^
id.it ^ String.concat "" (List.map string_of_iter dim) ^
"` outside suitable iteraton context")


(* Types *)
Expand Down Expand Up @@ -314,11 +322,7 @@ and infer_exp env e : typ =
| CallE (id, _) -> snd (find "function" env.defs id)
| MixE _ -> error e.at "cannot infer type of mixin notation"
| IterE (e1, iter) ->
let iter' =
match fst iter with
| ListN _ -> List
| iter' -> iter'
in
let iter' = match fst iter with ListN _ -> List | iter' -> iter' in
IterT (infer_exp env e1, iter') $ e.at
| OptE _ -> error e.at "cannot infer type of option"
| TheE e1 -> as_iter_typ Opt "option" env Check (infer_exp env e1) e1.at
Expand Down
2 changes: 1 addition & 1 deletion spectec/src/middlend/sub.ml
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ and t_exp' env = function
| SubE (e, t1, t2) -> SubE (e, t1, t2)

and t_iter env = function
| ListN (e, id) -> ListN (t_exp env e, id)
| ListN (e, id_opt) -> ListN (t_exp env e, id_opt)
| i -> i

and t_iterexp env (iter, vs) = (t_iter env iter, vs)
Expand Down
2 changes: 1 addition & 1 deletion spectec/src/middlend/totalize.ml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ and t_exp' env = function
| SubE (e, t1, t2) -> SubE (e, t1, t2)

and t_iter env = function
| ListN (e, id) -> ListN (t_exp env e, id)
| ListN (e, id_opt) -> ListN (t_exp env e, id_opt)
| i -> i

and t_iterexp env (iter, vs) = (t_iter env iter, vs)
Expand Down
2 changes: 1 addition & 1 deletion spectec/src/middlend/unthe.ml
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ and t_iterexp n (iter, vs) =
unary t_iter n iter (fun iter' -> (iter', vs))

and t_iter n iter = match iter with
| ListN (e, id) -> unary t_exp n e (fun e' -> ListN (e', id))
| ListN (e, id_opt) -> unary t_exp n e (fun e' -> ListN (e', id_opt))
| _ -> [], iter

and t_path n = phrase t_path' n
Expand Down
2 changes: 1 addition & 1 deletion spectec/src/middlend/wild.ml
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ and t_iterexp env (iter, vs) =
unary t_iter env iter (fun iter' -> (iter', vs))

and t_iter env iter = match iter with
| ListN (e, id) -> unary t_exp env e (fun e' -> ListN (e', id))
| ListN (e, id_opt) -> unary t_exp env e (fun e' -> ListN (e', id_opt))
| _ -> [], iter

and t_path env = phrase t_path' env
Expand Down