Skip to content

Commit 62f535c

Browse files
committed
[spectec] Support hints on rules
1 parent 9cac9a6 commit 62f535c

13 files changed

Lines changed: 38 additions & 19 deletions

File tree

0 Bytes
Binary file not shown.

spectec/src/backend-latex/render.ml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,8 @@ let env_hintdef env hd =
232232
env_hints "show" env.show_rel id hints;
233233
env_hints "name" env.name_rel id hints;
234234
env_hints "tabular" env.tab_rel id hints
235+
| RuleH (_id1, _id2, _hints) ->
236+
()
235237
| VarH (id, hints) ->
236238
env_hints "macro" env.macro_var id hints;
237239
env_hints "show" env.show_var id hints
@@ -1768,7 +1770,7 @@ let render_gramdef env d : row list =
17681770

17691771
let render_ruledef_infer env d =
17701772
match d.it with
1771-
| RuleD (id1, _ps, id2, e, prems) ->
1773+
| RuleD (id1, _ps, id2, e, prems, _hints) ->
17721774
let prems' = filter_nl_list (function {it = VarPr _; _} -> false | _ -> true) prems in
17731775
"\\frac{\n" ^
17741776
(if has_nl prems then "\\begin{array}{@{}c@{}}\n" else "") ^
@@ -1782,7 +1784,7 @@ let render_ruledef_infer env d =
17821784

17831785
let render_ruledef env d : row list =
17841786
match d.it with
1785-
| RuleD (id1, _ps, id2, e, prems) ->
1787+
| RuleD (id1, _ps, id2, e, prems, _hints) ->
17861788
let e1, op, e2 =
17871789
match e.it with
17881790
| InfixE (e1, op, ({it = SeqE (e21::es22); _} as e2)) when Atom.is_sub op ->
@@ -1848,7 +1850,7 @@ let rec render_defs env = function
18481850
| RelD (_, _ps, t, _) ->
18491851
"\\boxed{" ^ render_typ env t ^ "}" ^
18501852
(if ds' = [] then "" else " \\; " ^ render_defs env ds')
1851-
| RuleD (id1, _, _, _, _) ->
1853+
| RuleD (id1, _, _, _, _, _) ->
18521854
if Map.mem id1.it !(env.tab_rel) then
18531855
(* Columns: decorator & lhs & op & rhs & premise *)
18541856
let sp_deco = if env.deco_rule then sp else "@{}" in
@@ -1892,7 +1894,7 @@ let rec split_tabdefs id tabdefs = function
18921894
| [] -> List.rev tabdefs, []
18931895
| d::ds ->
18941896
match d.it with
1895-
| RuleD (id1, _, _, _, _) when id1.it = id ->
1897+
| RuleD (id1, _, _, _, _, _) when id1.it = id ->
18961898
split_tabdefs id (d::tabdefs) ds
18971899
| _ -> List.rev tabdefs, d::ds
18981900

@@ -1918,7 +1920,7 @@ let rec render_script env = function
19181920
| RelD _ ->
19191921
"$" ^ render_def env d ^ "$\n\n" ^
19201922
render_script env ds
1921-
| RuleD (id1, _, _, _, _) ->
1923+
| RuleD (id1, _, _, _, _, _) ->
19221924
if Map.mem id1.it !(env.tab_rel) then
19231925
let tabdefs, ds' = split_tabdefs id1.it [d] ds in
19241926
"$$\n" ^ render_defs env tabdefs ^ "\n$$\n\n" ^

spectec/src/backend-splice/splice.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ let env_def env def =
8787
env.gram <- Map.add id1.it {grammar with gfragments} env.gram
8888
| RelD (id, _, _, _) ->
8989
env.rel <- Map.add id.it {rdef = def; rules = []} env.rel
90-
| RuleD (id1, _, id2, _, _) ->
90+
| RuleD (id1, _, id2, _, _, _) ->
9191
let relation = Map.find id1.it env.rel in
9292
let rules = relation.rules @ [(id2.it, def, ref 0)] in
9393
env.rel <- Map.add id1.it {relation with rules} env.rel

spectec/src/el/ast.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ and def' =
174174
| TypD of id * id * arg list * typ * hint list (* `syntax` typid args hint* `=` typ *)
175175
| GramD of id * id * param list * typ * gram * hint list (* `grammar` gramid params hint* `:` type `=` gram *)
176176
| RelD of id * param list * typ * hint list (* `relation` relid params `:` typ hint* *)
177-
| RuleD of id * param list * id * exp * prem nl_list (* `rule` relid params ruleid? `:` exp (`--` prem)* *)
177+
| RuleD of id * param list * id * exp * prem nl_list * hint list (* `rule` relid params ruleid? `:` exp (`--` prem)* *)
178178
| VarD of id * typ * hint list (* `var` varid `:` typ *)
179179
| DecD of id * param list * typ * hint list (* `def` `$` defid params `:` typ hint* *)
180180
| DefD of id * arg list * exp * prem nl_list (* `def` `$` defid args `=` exp (`--` prem)* *)
@@ -195,6 +195,7 @@ and hintdef' =
195195
| TypH of id * id * hint list
196196
| GramH of id * id * hint list
197197
| RelH of id * hint list
198+
| RuleH of id * id * hint list
198199
| VarH of id * hint list
199200
| DecH of id * hint list
200201

spectec/src/el/free.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ let free_def d =
173173
| VarD (_id, t, _hints) -> free_typ t
174174
| SepD -> empty
175175
| RelD (_id, ps, t, _hints) -> free_typ t -- bound_params ps
176-
| RuleD (id1, ps, _id2, e, prems) ->
176+
| RuleD (id1, ps, _id2, e, prems, _hints) ->
177177
free_relid id1 ++ (free_exp e ++ free_prems prems -- bound_params ps)
178178
| DecD (_id, ps, t, _hints) ->
179179
free_params ps ++ free_typ t -- bound_params ps

spectec/src/el/iter.ml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ let hintdef d =
225225
| TypH (x1, x2, hs) -> typid x1; ruleid x2; hints hs
226226
| GramH (x1, x2, hs) -> gramid x1; ruleid x2; hints hs
227227
| RelH (x, hs) -> relid x; hints hs
228+
| RuleH (x1, x2, hs) -> relid x1; ruleid x2; hints hs
228229
| VarH (x, hs) -> varid x; hints hs
229230
| DecH (x, hs) -> defid x; hints hs
230231

@@ -237,7 +238,7 @@ let def d =
237238
| VarD (x, t, hs) -> varid x; typ t; hints hs
238239
| SepD -> ()
239240
| RelD (x, ps, t, hs) -> relid x; params ps; typ t; hints hs
240-
| RuleD (x1, ps, x2, e, prs) -> relid x1; params ps; ruleid x2; exp e; prems prs
241+
| RuleD (x1, ps, x2, e, prs, hs) -> relid x1; params ps; ruleid x2; exp e; prems prs; hints hs
241242
| DecD (x, ps, t, hs) -> defid x; params ps; typ t; hints hs
242243
| DefD (x, as_, e, prs) -> defid x; args as_; exp e; prems prs
243244
| HintD hd -> hintdef hd
@@ -389,6 +390,7 @@ let clone_hintdef d =
389390
| TypH (x1, x2, hs) -> TypH (x1, x2, List.map clone_hint hs)
390391
| GramH (x1, x2, hs) -> GramH (x1, x2, List.map clone_hint hs)
391392
| RelH (x, hs) -> RelH (x, List.map clone_hint hs)
393+
| RuleH (x1, x2, hs) -> RuleH (x1, x2, List.map clone_hint hs)
392394
| VarH (x, hs) -> VarH (x, List.map clone_hint hs)
393395
| DecH (x, hs) -> DecH (x, List.map clone_hint hs)
394396
) $ d.at
@@ -401,7 +403,7 @@ let clone_def d =
401403
| VarD (x, t, hs) -> VarD (x, clone_typ t, List.map clone_hint hs)
402404
| SepD -> SepD
403405
| RelD (x, ps, t, hs) -> RelD (x, List.map clone_param ps, clone_typ t, List.map clone_hint hs)
404-
| RuleD (x1, ps, x2, e, prs) -> RuleD (x1, List.map clone_param ps, x2, clone_exp e, Convert.map_nl_list clone_prem prs)
406+
| RuleD (x1, ps, x2, e, prs, hs) -> RuleD (x1, List.map clone_param ps, x2, clone_exp e, Convert.map_nl_list clone_prem prs, List.map clone_hint hs)
405407
| DecD (x, ps, t, hs) -> DecD (x, List.map clone_param ps, clone_typ t, List.map clone_hint hs)
406408
| DefD (x, as_, e, prs) -> DefD (x, List.map clone_arg as_, clone_exp e, Convert.map_nl_list clone_prem prs)
407409
| HintD hd -> HintD (clone_hintdef hd)

spectec/src/el/print.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ let string_of_def d =
310310
string_of_typ t ^ " = " ^ string_of_gram gram
311311
| RelD (id, ps, t, _hints) ->
312312
"relation " ^ string_of_relid id ^ string_of_params ps ^ ": " ^ string_of_typ t
313-
| RuleD (id1, ps, id2, e, prems) ->
313+
| RuleD (id1, ps, id2, e, prems, _hints) ->
314314
"rule " ^ string_of_relid id1 ^ string_of_params ps ^ string_of_ruleid id2 ^ ":\n " ^
315315
string_of_exp e ^
316316
string_of_nl_list "" "" (prefix "\n -- " string_of_prem) prems

spectec/src/el/subst.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ let subst_def s d =
264264
| VarD (x, t, hs) -> VarD (x, subst_typ s t, hs)
265265
| SepD -> SepD
266266
| RelD (x, ps, t, hs) -> RelD (x, List.map (subst_param s) ps, subst_typ s t, hs)
267-
| RuleD (x1, ps, x2, e, prs) -> RuleD (x1, List.map (subst_param s) ps, x2, subst_exp s e, Convert.map_nl_list (subst_prem s) prs)
267+
| RuleD (x1, ps, x2, e, prs, hs) -> RuleD (x1, List.map (subst_param s) ps, x2, subst_exp s e, Convert.map_nl_list (subst_prem s) prs, hs)
268268
| DecD (x, ps, t, hs) -> DecD (x, List.map (subst_param s) ps, subst_typ s t, hs)
269269
| DefD (x, as_, e, prs) -> DefD (x, List.map (subst_arg s) as_, subst_exp s e, Convert.map_nl_list (subst_prem s) prs)
270270
| HintD hd -> HintD hd

spectec/src/frontend/elab.ml

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2470,7 +2470,13 @@ let elab_hintdef _env (hd : hintdef) : Il.def list =
24702470
[Il.HintD (Il.DecH (id, elab_hints id "" hints) $ hd.at) $ hd.at]
24712471
| AtomH (id, atom, _hints) ->
24722472
let _ = elab_atom atom id in []
2473-
| GramH _ | VarH _ ->
2473+
| GramH (id1, _id2, hints) ->
2474+
if hints = [] then [] else
2475+
[Il.HintD (Il.GramH (id1, elab_hints id1 "" hints) $ hd.at) $ hd.at]
2476+
| RuleH (id1, id2, hints) ->
2477+
if hints = [] then [] else
2478+
[Il.HintD (Il.RuleH (id1, id2, elab_hints id1 "" hints) $ hd.at) $ hd.at]
2479+
| VarH _ ->
24742480
[]
24752481

24762482

@@ -2624,7 +2630,7 @@ let rec elab_def_pass2 env (d : def) : Il.def list =
26242630

26252631
| RelD _ -> []
26262632

2627-
| RuleD (x1, ps, x2, e, prems) ->
2633+
| RuleD (x1, ps, x2, e, prems, hints) ->
26282634
let ps', t', rules', mixop, not' = find "relation" env.rels x1 in
26292635
if List.exists (fun (x, _) -> x.it = x2.it) rules' then
26302636
error d.at ("duplicate rule name `" ^ x1.it ^
@@ -2642,7 +2648,8 @@ let rec elab_def_pass2 env (d : def) : Il.def list =
26422648
let qs = infer_quants env env' dims det [] [] [] es' [] prems' d.at in
26432649
let rule' = Il.RuleD (x2, qs, mixop, e', prems') $ d.at in
26442650
env.rels <- rebind "relation" env.rels x1 (ps', t', rules' @ [x2, rule'], mixop, not');
2645-
if not env'.pm then [] else elab_def_pass2 env Subst.(subst_def pm_snd (Iter.clone_def d))
2651+
(if not env'.pm then [] else elab_def_pass2 env Subst.(subst_def pm_snd (Iter.clone_def d)))
2652+
@ elab_hintdef env (RuleH (x1, x2, hints) $ d.at)
26462653

26472654
| VarD _ -> []
26482655

@@ -2694,6 +2701,7 @@ let populate_hint env (hd' : Il.hintdef) =
26942701
| Il.RelH (x, _) -> ignore (find "relation" env.rels x)
26952702
| Il.DecH (x, _) -> ignore (find "definition" env.defs x)
26962703
| Il.GramH (x, _) -> ignore (find "grammar" env.grams x)
2704+
| Il.RuleH (x, _, _) -> ignore (find "relation" env.rels x)
26972705

26982706
let populate_def env (d' : Il.def) : Il.def =
26992707
Debug.(log_in "el.populate_def" dline);

spectec/src/frontend/parser.mly

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,12 +1088,12 @@ def_ :
10881088
{ RelD ($2, [], $4, $5) }
10891089
| RELATION relid LPAREN comma_list(param) RPAREN COLON nottyp hint*
10901090
{ RelD ($2, $4, $7, $8) }
1091-
| RULE relid ruleid_list COLON exp prem_list
1091+
| RULE relid ruleid_list COLON exp prem_list hint*
10921092
{ let id = if $3 = "" then "" else String.sub $3 1 (String.length $3 - 1) in
1093-
RuleD ($2, [], id $ $loc($3), $5, $6) }
1094-
| RULE relid LPAREN comma_list(param) RPAREN ruleid_list COLON exp prem_list
1093+
RuleD ($2, [], id $ $loc($3), $5, $6, $7) }
1094+
| RULE relid LPAREN comma_list(param) RPAREN ruleid_list COLON exp prem_list hint*
10951095
{ let id = if $6 = "" then "" else String.sub $6 1 (String.length $6 - 1) in
1096-
RuleD ($2, $4, id $ $loc($6), $8, $9) }
1096+
RuleD ($2, $4, id $ $loc($6), $8, $9, $10) }
10971097
| VAR varid_bind COLON typ hint*
10981098
{ VarD ($2, $4, $5) }
10991099
| DEF DOLLAR defid COLON typ hint*
@@ -1122,6 +1122,9 @@ def_ :
11221122
HintD (GramH ($2, id $ $loc($3), $4) $ $sloc) }
11231123
| RELATION relid hint*
11241124
{ HintD (RelH ($2, $3) $ $sloc) }
1125+
| RULE relid ruleid_list hint*
1126+
{ let id = if $3 = "" then "" else String.sub $3 1 (String.length $3 - 1) in
1127+
HintD (RuleH ($2, id $ $loc($3), $4) $ $sloc) }
11251128
| VAR varid_bind hint*
11261129
{ HintD (VarH ($2, $3) $ $sloc) }
11271130
| DEF DOLLAR defid hint*

0 commit comments

Comments
 (0)