Skip to content

Commit 8cb8dc9

Browse files
committed
[spectec] Restrict reduction of equality to values
1 parent 1b79b88 commit 8cb8dc9

File tree

2 files changed

+14
-21
lines changed

2 files changed

+14
-21
lines changed

spectec/doc/semantics/il/5-reduction.spectec

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -155,21 +155,21 @@ rule Step_exp/CMP-ctx2:
155155
-- Step_exp: S |- e_2 ~> e_2'
156156

157157
rule Step_exp/CMP-EQ-true:
158-
S |- CMP EQ e_1 e_2 ~> BOOL true
159-
-- if e_1 = e_2
158+
S |- CMP EQ val_1 val_2 ~> BOOL true
159+
-- if val_1 = val_2
160160

161161
rule Step_exp/CMP-EQ-false:
162162
S |- CMP EQ val_1 val_2 ~> BOOL false
163163
-- if val_1 =/= val_2
164164

165-
rule Step_exp/CMP-NE-false:
166-
S |- CMP NE e_1 e_2 ~> BOOL false
167-
-- if e_1 = e_2
168-
169165
rule Step_exp/CMP-NE-true:
170166
S |- CMP NE val_1 val_2 ~> BOOL true
171167
-- if val_1 =/= val_2
172168

169+
rule Step_exp/CMP-NE-false:
170+
S |- CMP NE val_1 val_2 ~> BOOL false
171+
-- if val_1 = val_2
172+
173173
rule Step_exp/CMP-NUM:
174174
S |- CMP numcmpop num_1 num_2 ~> BOOL $numcmp(numcmpop, num_1, num_2)
175175

@@ -557,6 +557,8 @@ rule Step_argmatch/EXP-fail:
557557
S |- `{q*} (EXP e / EXP e') ~>_s `{} FAIL
558558
-- Step_expmatch: S |- `{q*} (e / e') ~>_s `{} FAIL
559559

560+
;; TODO: EXP disjointness?
561+
560562

561563
rule Step_argmatch_plain/ctx1:
562564
S |- a / a' ~> a'' / a'
@@ -566,14 +568,6 @@ rule Step_argmatch_plain/ctx2:
566568
S |- a / a' ~> a / a''
567569
-- Step_arg: S |- a' ~> a''
568570

569-
rule Step_argmatch_plain/eq:
570-
S |- a / a ~> eps
571-
572-
;; TODO: disjointness
573-
;;rule Step_argmatch_plain/neq:
574-
;; S |- a / a' ~> FAIL
575-
;; -- Disj_arg: S |- a =/= a'
576-
577571

578572
rule Step_expmatch/plain:
579573
S |- `{q*} (e / e')* ~>_{} `{q*} (e'' / e''')*
@@ -616,7 +610,7 @@ rule Step_expmatch_plain/ctx2:
616610
-- Step_exp: S |- e' ~> e''
617611

618612
rule Step_expmatch_plain/eq:
619-
S |- e / e ~> eps
613+
S |- val / val ~> eps
620614

621615
rule Step_expmatch_plain/UN-PLUS:
622616
S |- num / UN PLUS e ~> num / e
@@ -721,7 +715,7 @@ rule Step_expmatch_plain/SUB-SUB:
721715
SUB e t_1 t'_1 / e'
722716
-- Sub_typ: $storeenv(S) |- t_1 <: t'_1
723717

724-
;; TODO: disjointness
718+
;; TODO: disjointness?
725719

726720
rule Step_expmatch_plain/SUB-TUP:
727721
S |- TUP e^n / SUB e' (TUP (x_1 `: t'_1)^n) (TUP (x_2 `: t'_2)^n) ~>

spectec/src/il/eval.ml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,8 @@ and reduce_exp env e : exp =
179179
let e1' = reduce_exp env e1 in
180180
let e2' = reduce_exp env e2 in
181181
(match op, e1'.it, e2'.it with
182-
| `EqOp, _, _ when Eq.eq_exp e1' e2' -> BoolE true
183-
| `NeOp, _, _ when Eq.eq_exp e1' e2' -> BoolE false
184-
| `EqOp, _, _ when is_normal_exp e1' && is_normal_exp e2' -> BoolE false
185-
| `NeOp, _, _ when is_normal_exp e1' && is_normal_exp e2' -> BoolE true
182+
| `EqOp, _, _ when is_normal_exp e1' && is_normal_exp e2' -> BoolE (Eq.eq_exp e1' e2')
183+
| `NeOp, _, _ when is_normal_exp e1' && is_normal_exp e2' -> BoolE (not (Eq.eq_exp e1' e2'))
186184
| #Num.cmpop as op', NumE n1, NumE n2 ->
187185
(match Num.cmp op' n1 n2 with
188186
| Some b -> BoolE b
@@ -686,7 +684,8 @@ and match_exp' env s e1 e2 : subst option =
686684
(fun r -> fmt "%s" (opt il_subst r))
687685
) @@ fun _ ->
688686
assert (Eq.eq_exp e1 (reduce_exp env e1));
689-
if Eq.eq_exp e1 e2 then Some s else (* HACK around subtype elim pass introducing calls on LHS's *)
687+
(* HACK around subtype elim pass introducing calls on LHS's *)
688+
if Eq.eq_exp e1 e2 && is_normal_exp e1 && is_normal_exp e2 then Some s else
690689
match e1.it, (reduce_exp env (Subst.subst_exp s e2)).it with
691690
| _, VarE id when Subst.mem_varid s id ->
692691
(* A pattern variable already in the substitution is non-linear *)

0 commit comments

Comments
 (0)