Skip to content

Commit a1d054c

Browse files
committed
"Expand" prose fix
1 parent adc8bef commit a1d054c

File tree

4 files changed

+120
-120
lines changed

4 files changed

+120
-120
lines changed

spectec/src/backend-interpreter/relation.ml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,23 @@ let val_type = function
141141
with exn -> raise (Exception.Invalid (exn, Printexc.get_raw_backtrace ())))
142142
| vs -> Numerics.error_values "$Val_type" vs
143143

144+
let expand = function
145+
| [ v ] ->
146+
(try
147+
v
148+
|> Construct.al_to_def_type
149+
|> Types.expand_def_type
150+
|> Construct.al_of_str_type
151+
with exn -> raise (Exception.Invalid (exn, Printexc.get_raw_backtrace ())))
152+
| vs -> Numerics.error_values "$Expand" vs
153+
144154
let manual_map =
145155
FuncMap.empty
146156
|> FuncMap.add "Ref_type" ref_type
147157
|> FuncMap.add "Module_ok" module_ok
148158
|> FuncMap.add "Val_type" val_type
149159
|> FuncMap.add "Externaddr_type" externaddr_type
160+
|> FuncMap.add "Expand" expand
150161

151162
let mem name =
152163

spectec/src/backend-prose/render.ml

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -253,25 +253,25 @@ and al_to_el_expr expr =
253253
| Some _ ->
254254
None
255255
| _ ->
256-
let elid = id $ no_region in
257-
let* elal = al_to_el_args al in
258-
(* Unwrap parenthsized args *)
259-
let elal = List.map
260-
(fun elarg ->
261-
let elarg = match elarg with
262-
| El.Ast.ExpA exp ->
263-
let exp = match exp.it with
264-
| ParenE exp' -> exp'
265-
| _ -> exp
266-
in
267-
El.Ast.ExpA exp
268-
| _ -> elarg
256+
let elid = id $ no_region in
257+
let* elal = al_to_el_args al in
258+
(* Unwrap parenthsized args *)
259+
let elal = List.map
260+
(fun elarg ->
261+
let elarg = match elarg with
262+
| El.Ast.ExpA exp ->
263+
let exp = match exp.it with
264+
| ParenE exp' -> exp'
265+
| _ -> exp
269266
in
270-
(ref elarg) $ no_region
271-
)
272-
elal
273-
in
274-
Some (El.Ast.CallE (elid, elal))
267+
El.Ast.ExpA exp
268+
| _ -> elarg
269+
in
270+
(ref elarg) $ no_region
271+
)
272+
elal
273+
in
274+
Some (El.Ast.CallE (elid, elal))
275275
)
276276
| Al.Ast.CatE (e1, e2) ->
277277
let* ele1 = al_to_el_expr e1 in
@@ -598,14 +598,20 @@ and render_expr' env expr =
598598
if id = "Eval_expr" then
599599
(match args with
600600
| [instrs] ->
601-
"the result of :ref:`evaluating <exec-expr>` " ^ instrs
601+
sprintf "the result of :ref:`evaluating <exec-expr>` %s" instrs
602602
| _ -> error expr.at (Printf.sprintf "Invalid arity for relation call: %d ([ %s ])" (List.length args) (String.concat " " args));
603603
)
604+
else if id = "Expand" then
605+
(match args with
606+
| [arg1] ->
607+
sprintf "the :ref:`expansion <aux-expand-deftype>` of %s" arg1
608+
| _ -> error expr.at "Invalid arity for relation call";
609+
)
604610
else if String.ends_with ~suffix:"_type" id || String.ends_with ~suffix:"_ok" id then
605611
(match args with
606612
| [arg1; arg2] ->
607-
arg1 ^ " is :ref:`valid <valid-val>` with type " ^ arg2
608-
| [arg] -> "the type of " ^ arg
613+
sprintf "%s is :ref:`valid <valid-val>` with type %s" arg1 arg2
614+
| [arg] -> sprintf "the type of %s" arg
609615
| _ -> error expr.at "Invalid arity for relation call";
610616
)
611617
else error expr.at ("Not supported relation call: " ^ id);

spectec/src/il2al/preprocess.ml

Lines changed: 18 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -128,43 +128,27 @@ let rec preprocess_prem prem =
128128
prem
129129
|> preprocess_prem
130130
|> List.map (fun new_prem -> IterPr (new_prem, iterexp) $ prem.at)
131-
| RulePr (id, mixop, exp) when id.it = "Expand" ->
132-
(match mixop, exp.it with
133-
(* Expand: `dt` ~~ `ct` *)
134-
| [[]; [approx]; []], TupE [dt; ct] when approx.it = Approx ->
135-
(* `$expanddt(dt) = ct` *)
136-
let expanddt =
137-
CallE ("expanddt" $ prem.at, [ExpA dt $ dt.at]) $$ prem.at % ct.note
138-
in
139-
let new_prem =
140-
IfPr (CmpE (`EqOp, `BoolT, expanddt, ct) $$ prem.at % (BoolT $ no_region))
141-
in
142-
143-
(* Add function definition to AL environment *)
144-
if not (Env.mem_def !Al.Valid.il_env id) then (
145-
let param = ExpP ("_" $ no_region, dt.note) $ dt.at in
146-
Al.Valid.il_env := Env.bind_def !Al.Valid.il_env id ([param], ct.note, [])
147-
);
148-
149-
[ new_prem $ prem.at ]
150-
(* Expand: ??? *)
151-
| _ -> [ prem ]
152-
)
153131
| RulePr (id, mixop, exp) ->
154-
(match mixop, exp.it with
155-
(* `id`: |- `lhs` : `rhs` *)
156-
| [[turnstile]; [colon]; []], TupE [lhs; rhs]
157-
(* `id`: C |- `lhs` : `rhs` *)
158-
| [[]; [turnstile]; [colon]; []], TupE [_; lhs; rhs]
159-
when turnstile.it = Turnstile && colon.it = Colon ->
160-
typing_functions := id.it :: !typing_functions;
161-
162-
(* $`id`(`lhs`) = `rhs` *)
163-
let typing_function_call =
132+
let lhs_rhs_opt =
133+
match mixop, exp.it with
134+
(* `id`: |- `lhs` : `rhs` *)
135+
| [[turnstile]; [colon]; []], TupE [lhs; rhs]
136+
(* `id`: C |- `lhs` : `rhs` *)
137+
| [[]; [turnstile]; [colon]; []], TupE [_; lhs; rhs]
138+
when turnstile.it = Turnstile && colon.it = Colon ->
139+
typing_functions := id.it :: !typing_functions;
140+
Some (lhs, rhs)
141+
(* `lhs` ~~ `rhs` *)
142+
| [[]; [approx]; []], TupE [lhs; rhs] when approx.it = Approx -> Some (lhs, rhs)
143+
| _ -> None
144+
in
145+
(match lhs_rhs_opt with
146+
| Some (lhs, rhs) ->
147+
let function_call =
164148
CallE (id, [ExpA lhs $ lhs.at]) $$ exp.at % rhs.note
165149
in
166150
let new_prem =
167-
IfPr (CmpE (`EqOp, `BoolT, typing_function_call, rhs) $$ exp.at % (BoolT $ no_region))
151+
IfPr (CmpE (`EqOp, `BoolT, function_call, rhs) $$ prem.at % (BoolT $ no_region))
168152
in
169153

170154
(* Add function definition to AL environment *)
@@ -174,8 +158,7 @@ let rec preprocess_prem prem =
174158
);
175159

176160
[ new_prem $ prem.at ]
177-
| _ -> [ prem ]
178-
)
161+
| None -> [ prem ])
179162
(* Split -- if e1 /\ e2 *)
180163
| IfPr ( { it = BinE (`AndOp, _, e1, e2); _ } ) ->
181164
preprocess_prem (IfPr e1 $ prem.at) @ preprocess_prem (IfPr e2 $ prem.at)

0 commit comments

Comments
 (0)