Skip to content

Commit f9deb8d

Browse files
committed
setoid rewrite
1 parent 41d8e7b commit f9deb8d

33 files changed

+928
-207
lines changed

src/ecCommands.ml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,14 @@ and process_addrw scope (local, base, names) =
687687
and process_reduction scope name =
688688
EcScope.Reduction.add_reduction scope name
689689

690+
(* -------------------------------------------------------------------- *)
691+
and process_relation (scope : EcScope.scope) (rel : prelation) =
692+
EcScope.Setoid.add_relation scope rel
693+
694+
(* -------------------------------------------------------------------- *)
695+
and process_morphism (scope : EcScope.scope) (mph : pmorphism) =
696+
EcScope.Setoid.add_morphism scope mph
697+
690698
(* -------------------------------------------------------------------- *)
691699
and process_hint scope hint =
692700
EcScope.Auto.add_hint scope hint
@@ -789,6 +797,8 @@ and process (ld : Loader.loader) (scope : EcScope.scope) g =
789797
| Goption opt -> `Fct (fun scope -> process_option scope opt)
790798
| Gaddrw hint -> `Fct (fun scope -> process_addrw scope hint)
791799
| Greduction red -> `Fct (fun scope -> process_reduction scope red)
800+
| Grelation rel -> `Fct (fun scope -> process_relation scope rel)
801+
| Gmorphism mph -> `Fct (fun scope -> process_morphism scope mph)
792802
| Ghint hint -> `Fct (fun scope -> process_hint scope hint)
793803
| GdumpWhy3 file -> `Fct (fun scope -> process_dump_why3 scope file)
794804
with

src/ecCoreGoal.ml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ module FApi = struct
508508

509509
(* ------------------------------------------------------------------ *)
510510
let xmutate (tc : tcenv) (vx : 'a) (fp : form list) =
511-
let (tc, hds) = List.map_fold (fun tc fp -> newgoal tc fp) tc fp in
511+
let (tc, hds) = List.fold_left_map (fun tc fp -> newgoal tc fp) tc fp in
512512
close tc (VExtern (vx, hds))
513513

514514
(* ------------------------------------------------------------------ *)
@@ -518,7 +518,7 @@ module FApi = struct
518518
(* ------------------------------------------------------------------ *)
519519
let xmutate_hyps (tc : tcenv) (vx : 'a) subgoals =
520520
let (tc, hds) =
521-
List.map_fold
521+
List.fold_left_map
522522
(fun tc (hyps, fp) -> newgoal tc ~hyps fp)
523523
tc subgoals
524524
in
@@ -564,11 +564,11 @@ module FApi = struct
564564

565565
(* ------------------------------------------------------------------ *)
566566
let on_sub1i_goals (tt : int -> backward) (hds : handle list) (pe : proofenv) =
567-
let do1 i pe hd =
567+
let do1 pe i hd =
568568
let tc = tt i (tcenv1_of_penv hd pe) in
569569
assert (tc.tce_tcenv.tce_ctxt = []);
570570
(tc_penv tc, tc_opened tc) in
571-
List.mapi_fold do1 pe hds
571+
List.fold_left_mapi do1 pe hds
572572

573573
(* ------------------------------------------------------------------ *)
574574
let on_sub1_goals (tt : backward) (hds : handle list) (pe : proofenv) =
@@ -578,11 +578,11 @@ module FApi = struct
578578
let on_sub1i_map_goals
579579
(tt : int -> tcenv1 -> 'a * tcenv) (hds : handle list) (pe : proofenv)
580580
=
581-
let do1 i pe hd =
581+
let do1 pe i hd =
582582
let data, tc = tt i (tcenv1_of_penv hd pe) in
583583
assert (tc.tce_tcenv.tce_ctxt = []);
584584
(tc_penv tc, (tc_opened tc, data)) in
585-
List.mapi_fold do1 pe hds
585+
List.fold_left_mapi do1 pe hds
586586

587587
(* ------------------------------------------------------------------ *)
588588
let on_sub1_map_goals

src/ecCoreSubst.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ module Fsubst = struct
600600

601601
(* ------------------------------------------------------------------ *)
602602
and add_bindings (s : f_subst) : bindings -> f_subst * bindings =
603-
List.map_fold add_binding s
603+
List.fold_left_map add_binding s
604604

605605
(* ------------------------------------------------------------------ *)
606606
and add_mod_params (s : f_subst) (params : _) =

src/ecEnv.ml

Lines changed: 67 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ module Mp = EcPath.Mp
1919
module Sid = EcIdent.Sid
2020
module Mid = EcIdent.Mid
2121
module TC = EcTypeClass
22+
module Sint = EcMaps.Sint
2223
module Mint = EcMaps.Mint
2324

2425
(* -------------------------------------------------------------------- *)
@@ -183,6 +184,7 @@ type preenv = {
183184
env_rwbase : Sp.t Mip.t;
184185
env_atbase : atbase Msym.t;
185186
env_redbase : mredinfo;
187+
env_stdbase : setoid;
186188
env_ntbase : ntbase Mop.t;
187189
env_albase : path Mp.t; (* theory aliases *)
188190
env_modlcs : Sid.t; (* declared modules *)
@@ -226,6 +228,13 @@ and atbase0 = path * [`Rigid | `Default]
226228

227229
and atbase = atbase0 list Mint.t
228230

231+
and setoid = setoid1 Mp.t
232+
233+
and setoid1 = {
234+
spec : path;
235+
morphisms : (path Mint.t) Mp.t;
236+
}
237+
229238
(* -------------------------------------------------------------------- *)
230239
type env = preenv
231240

@@ -312,6 +321,7 @@ let empty gstate =
312321
env_rwbase = Mip.empty;
313322
env_atbase = Msym.empty;
314323
env_redbase = Mrd.empty;
324+
env_stdbase = Mp.empty;
315325
env_ntbase = Mop.empty;
316326
env_albase = Mp.empty;
317327
env_modlcs = Sid.empty;
@@ -613,7 +623,7 @@ module MC = struct
613623
let mc = lookup_mc qn env in
614624
let objs = odfl [] (mc |> omap (fun mc -> MMsym.all x (proj mc))) in
615625
let _, objs =
616-
List.map_fold
626+
List.fold_left_map
617627
(fun ps ((p, _) as obj)->
618628
if Sip.mem p ps
619629
then (ps, None)
@@ -1018,7 +1028,7 @@ module MC = struct
10181028
in
10191029

10201030
let (mc, submcs) =
1021-
List.map_fold mc1_of_module
1031+
List.fold_left_map mc1_of_module
10221032
(empty_mc
10231033
(if p2 = None then Some me.me_params else None))
10241034
me.me_comps
@@ -1113,12 +1123,13 @@ module MC = struct
11131123
(mc, None)
11141124

11151125
| Th_export _ | Th_addrw _ | Th_instance _
1116-
| Th_auto _ | Th_reduction _ ->
1126+
| Th_auto _ | Th_reduction _ | Th_relation _
1127+
| Th_morphism _ ->
11171128
(mc, None)
11181129
in
11191130

11201131
let (mc, submcs) =
1121-
List.map_fold mc1_of_theory (empty_mc None) cth.cth_items
1132+
List.fold_left_map mc1_of_theory (empty_mc None) cth.cth_items
11221133
in
11231134
((x, mc), List.rev_pmap identity submcs)
11241135

@@ -1566,6 +1577,35 @@ module Auto = struct
15661577
Msym.values env.env_atbase |> List.map flatten_db |> List.flatten
15671578
end
15681579
1580+
(* -------------------------------------------------------------------- *)
1581+
module Setoid = struct
1582+
type nonrec setoid1 = setoid1
1583+
1584+
let update_relation_db ((oppath, axpath) : path * path) (db : setoid) =
1585+
Mp.add oppath { spec = axpath; morphisms = Mp.empty; } db
1586+
1587+
let add_relation ((oppath, axpath) : path * path) (env : env) =
1588+
let item = mkitem ~import:true (Th_relation (oppath, axpath)) in
1589+
{ env with
1590+
env_stdbase = update_relation_db (oppath, axpath) env.env_stdbase;
1591+
env_item = item :: env.env_item; }
1592+
1593+
let get_relation (env : env) (oppath : path) : setoid1 option =
1594+
Mp.find_opt oppath env.env_stdbase
1595+
1596+
let update_morphism_db ((rel, op, ax, pos) : path * path * path * int) (db : setoid) =
1597+
Mp.change (fun db1 ->
1598+
Some { (oget db1) with morphisms =
1599+
Mp.change (fun m -> Some (Mint.add pos ax (odfl Mint.empty m))) op (oget db1).morphisms }
1600+
) rel db
1601+
1602+
let add_morphism ((rel, op, ax, pos) : path * path * path * int) (env : env) =
1603+
let item = mkitem ~import:true (Th_morphism (rel, op, ax, pos)) in
1604+
{ env with
1605+
env_stdbase = update_morphism_db (rel, op, ax, pos) env.env_stdbase;
1606+
env_item = item :: env.env_item; }
1607+
end
1608+
15691609
(* -------------------------------------------------------------------- *)
15701610
module Fun = struct
15711611
type t = EcModules.function_
@@ -2974,6 +3014,17 @@ module Theory = struct
29743014
29753015
in bind_base_th for1
29763016
3017+
(* ------------------------------------------------------------------ *)
3018+
let bind_std_th =
3019+
let for1 _path db = function
3020+
| Th_relation r ->
3021+
Some (Setoid.update_relation_db r db)
3022+
| Th_morphism m ->
3023+
Some (Setoid.update_morphism_db m db)
3024+
| _ -> None
3025+
3026+
in bind_base_th for1
3027+
29773028
(* ------------------------------------------------------------------ *)
29783029
let bind_nt_th =
29793030
let for1 path base = function
@@ -3021,12 +3072,14 @@ module Theory = struct
30213072
let env_tc = bind_tc_th thname env.env_tc items in
30223073
let env_rwbase = bind_br_th thname env.env_rwbase items in
30233074
let env_atbase = bind_at_th thname env.env_atbase items in
3075+
let env_stdbase = bind_std_th thname env.env_stdbase items in
30243076
let env_ntbase = bind_nt_th thname env.env_ntbase items in
30253077
let env_redbase = bind_rd_th thname env.env_redbase items in
30263078
let env =
30273079
{ env with
30283080
env_tci ; env_tc ; env_rwbase;
3029-
env_atbase; env_ntbase; env_redbase; }
3081+
env_atbase; env_stdbase; env_ntbase;
3082+
env_redbase; }
30303083
in
30313084
add_restr_th thname env items
30323085
@@ -3086,7 +3139,12 @@ module Theory = struct
30863139
| Th_alias (name, path) ->
30873140
rebind_alias name path env
30883141
3089-
| Th_addrw _ | Th_instance _ | Th_auto _ | Th_reduction _ ->
3142+
| Th_addrw _
3143+
| Th_instance _
3144+
| Th_auto _
3145+
| Th_reduction _
3146+
| Th_relation _
3147+
| Th_morphism _ ->
30903148
env
30913149
30923150
in
@@ -3103,7 +3161,7 @@ module Theory = struct
31033161
(* ------------------------------------------------------------------ *)
31043162
let rec filter clears root cleared items =
31053163
snd_map (List.pmap identity)
3106-
(List.map_fold (filter1 clears root) cleared items)
3164+
(List.fold_left_map (filter1 clears root) cleared items)
31073165
31083166
and filter_th clears root cleared items =
31093167
let mempty = List.exists (EcPath.p_equal root) clears in
@@ -3239,6 +3297,7 @@ module Theory = struct
32393297
env_tc = bind_tc_th thpath env.env_tc cth.cth_items;
32403298
env_rwbase = bind_br_th thpath env.env_rwbase cth.cth_items;
32413299
env_atbase = bind_at_th thpath env.env_atbase cth.cth_items;
3300+
env_stdbase = bind_std_th thpath env.env_stdbase cth.cth_items;
32423301
env_ntbase = bind_nt_th thpath env.env_ntbase cth.cth_items;
32433302
env_redbase = bind_rd_th thpath env.env_redbase cth.cth_items;
32443303
env_thenvs = Mp.set_union env.env_thenvs compiled.compiled; }
@@ -3442,7 +3501,7 @@ module LDecl = struct
34423501
let do1 hyps s =
34433502
let id = fresh_id hyps s in
34443503
(add_local id (LD_var (tbool, None)) hyps, id)
3445-
in List.map_fold do1 hyps names
3504+
in List.fold_left_map do1 hyps names
34463505
34473506
(* ------------------------------------------------------------------ *)
34483507
type hyps = {

src/ecEnv.mli

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,21 @@ module Reduction : sig
416416
val get : topsym -> env -> rule list
417417
end
418418

419+
(* -------------------------------------------------------------------- *)
420+
type setoid1 = {
421+
spec : path;
422+
morphisms : (path EcMaps.Mint.t) Mp.t;
423+
}
424+
425+
module Setoid : sig
426+
type nonrec setoid1 = setoid1
427+
428+
val add_relation : path * path -> env -> env
429+
val get_relation : env -> path -> setoid1 option
430+
431+
val add_morphism : path * path * path * int -> env -> env
432+
end
433+
419434
(* -------------------------------------------------------------------- *)
420435
module Auto : sig
421436
type base0 = path * [`Rigid | `Default]

src/ecHiGoal.ml

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,11 @@ module LowRewrite = struct
249249
| LRW_IdRewriting
250250
| LRW_RPatternNoMatch
251251
| LRW_RPatternNoRuleMatch
252+
| LRW_InvalidSetoidContext
252253

253254
exception RewriteError of error
254255

255-
let rec find_rewrite_patterns ~inpred (dir : rwside) pt =
256+
let rec find_rewrite_patterns ~inpred (dir : rwside) pt : (pt_ev * rwmode * (form * form)) list =
256257
let hyps = pt.PT.ptev_env.PT.pte_hy in
257258
let env = LDecl.toenv hyps in
258259
let pt = { pt with ptev_ax = snd (PT.concretize pt) } in
@@ -270,7 +271,13 @@ module LowRewrite = struct
270271
let pt' = apply_pterm_to_arg_r pt' (PVASub pt) in
271272
[(pt', `Eq, (f, f_false))]
272273

273-
| _ -> []
274+
| _ -> begin
275+
try
276+
EcSetoid.as_instance env (destr_op_app ax)
277+
|> Option.map (fun (instance, (f1, f2)) -> (pt, `Setoid instance, (f1, f2)))
278+
|> Option.to_list
279+
with DestrError _ -> []
280+
end
274281

275282
and split ax =
276283
match EcFol.sform_of_form ax with
@@ -329,7 +336,7 @@ module LowRewrite = struct
329336
type rwinfos = rwside * EcFol.form option * EcMatching.occ option
330337

331338
let t_rewrite_r ?(mode = `Full) ?target ((s, prw, o) : rwinfos) pt tc =
332-
let hyps, tgfp = FApi.tc1_flat ?target tc in
339+
let env, hyps, tgfp = FApi.tc1_eflat ?target tc in
333340

334341
let modes =
335342
match mode with
@@ -366,7 +373,7 @@ module LowRewrite = struct
366373
| PT.FindOccFailure `MatchFailure ->
367374
raise (RewriteError LRW_RPatternNoRuleMatch)
368375
| PT.FindOccFailure `IncompleteMatch ->
369-
raise (RewriteError LRW_CannotInfer)
376+
raise (RewriteError LRW_CannotInfer)
370377
end in
371378

372379
if not occmode.k_keyed then begin
@@ -376,23 +383,42 @@ module LowRewrite = struct
376383
end;
377384

378385
let pt = fst (PT.concretize pt) in
386+
387+
let exception InvalidSetoidContext in
388+
379389
let cpos =
390+
let postcheck (instance : EcSetoid.instance) (lazy ctxt) =
391+
if not (EcSetoid.valid_setoid_context env instance ctxt) then
392+
raise InvalidSetoidContext in
393+
394+
let postcheck =
395+
match mode with
396+
| `Setoid instance -> postcheck instance
397+
| _ -> fun _ -> () in
398+
380399
try FPosition.select_form
400+
~postcheck:(fun _ ctxt _ -> postcheck ctxt; true)
381401
~xconv:`AlphaEq ~keyed:occmode.k_keyed
382402
hyps o subf tgfp
383-
with InvalidOccurence -> raise (RewriteError (LRW_InvalidOccurence))
403+
with
404+
| InvalidOccurence ->
405+
raise (RewriteError (LRW_InvalidOccurence))
406+
| InvalidSetoidContext ->
407+
raise (RewriteError (LRW_InvalidSetoidContext))
384408
in
385409

386410
EcLowGoal.t_rewrite
387411
~keyed:occmode.k_keyed ?target ~mode pt (s, Some cpos) tc in
388412

389413
let rec do_first = function
390-
| [] -> raise (RewriteError LRW_NothingToRewrite)
414+
| [] ->
415+
raise (RewriteError LRW_NothingToRewrite)
416+
417+
| [pt, mode, (f1, f2)] ->
418+
for1 (pt, mode, (f1, f2))
391419

392-
| (pt, mode, (f1, f2)) :: pts ->
393-
try for1 (pt, mode, (f1, f2))
394-
with RewriteError _ ->
395-
do_first pts
420+
| pt :: pts ->
421+
try do_first [pt] with RewriteError _ -> do_first pts
396422
in
397423

398424
let pts = find_rewrite_patterns s pt in
@@ -596,6 +622,8 @@ let process_rewrite1_core ?mode ?(close = true) ?target (s, p, o) pt tc =
596622
tc_error !!tc "r-pattern does not match the goal"
597623
| LowRewrite.LRW_RPatternNoRuleMatch ->
598624
tc_error !!tc "r-pattern does not match the rewriting rule"
625+
| LowRewrite.LRW_InvalidSetoidContext ->
626+
tc_error !!tc "invalid setoid-rewrite position"
599627

600628
(* -------------------------------------------------------------------- *)
601629
let process_delta ~und_delta ?target (s, o, p) tc =
@@ -2139,7 +2167,7 @@ let process_exists args (tc : tcenv1) =
21392167
PT.check_pterm_arg pte (x, xty) f arg.ptea_arg
21402168
in
21412169

2142-
let _concl, args = List.map_fold for1 (FApi.tc1_goal tc) args in
2170+
let _concl, args = List.fold_left_map for1 (FApi.tc1_goal tc) args in
21432171

21442172
if not (PT.can_concretize pte) then
21452173
tc_error !!tc "cannot infer all placeholders";

0 commit comments

Comments
 (0)