Skip to content

Commit e9aec65

Browse files
committed
In clone, allow applying renamings pre-emptively
When cloning a theory and subtituting a sub-theory by some other one, it is now possible to apply renamings on the target sub-theory. The syntax is: ``` clone T ... with theory T <- U { rename ... } ``` This allows to substitute one theory by some other one that got some of its components renamed by a previous clone. For example: ``` abstract theory Word. op foo_XX = ... end Word. clone Word as W16 rename "_XX" as "_16". abstract theory UseWord. clone Word as W. end UseWord. clone UseWord as UseWord16 with theory W <- W16 { rename "_XX" as "_16" }. ```
1 parent f71e049 commit e9aec65

File tree

4 files changed

+90
-86
lines changed

4 files changed

+90
-86
lines changed

src/ecParser.mly

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3640,8 +3640,8 @@ clone_override:
36403640
| MODULE TYPE x=uqident mode=loc(opclmode) y=uqident
36413641
{ (x, PTHO_ModTyp (y, unloc mode)) }
36423642

3643-
| THEORY x=uqident mode=loc(opclmode) y=uqident
3644-
{ (x, PTHO_Theory (y, unloc mode)) }
3643+
| THEORY x=uqident mode=loc(opclmode) y=uqident renames=brace(clone_rename)?
3644+
{ (x, PTHO_Theory (y, unloc mode, odfl [] renames)) }
36453645

36463646
realize:
36473647
| REALIZE x=qident

src/ecParsetree.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1193,7 +1193,7 @@ and op_override = op_override_def genoverride * clmode
11931193
and pr_override = pr_override_def genoverride * clmode
11941194
and me_override = pqsymbol * clmode
11951195
and mt_override = pqsymbol * clmode
1196-
and th_override = pqsymbol * clmode
1196+
and th_override = pqsymbol * clmode * theory_renaming list
11971197
and ax_override = pqsymbol * clmode
11981198
and nt_override = EcPath.path * clmode
11991199

src/ecThCloning.ml

Lines changed: 82 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,53 @@ and rk_categories = {
219219
rkc_theory : bool;
220220
}
221221

222+
(* -------------------------------------------------------------------- *)
223+
type octxt = {
224+
oc_env : EcEnv.env;
225+
oc_oth : ctheory;
226+
}
227+
228+
(* -------------------------------------------------------------------- *)
229+
module Renaming : sig
230+
val rename1 : octxt -> theory_renaming -> renaming
231+
end = struct
232+
let rename1 oc (k, (r1, r2)) : renaming =
233+
let e1 =
234+
try EcRegexp.regexp (unloc r1)
235+
with EcRegexp.Error _ -> clone_error oc.oc_env (CE_InvalidRE (unloc r1)) in
236+
let e2 =
237+
try EcRegexp.subst (unloc r2)
238+
with EcRegexp.Error _ -> clone_error oc.oc_env (CE_InvalidRE (unloc r2)) in
239+
240+
Array.iter
241+
(fun m ->
242+
if EcRegexp.match_ (`S "^0+$") (oget m.(1)) then
243+
clone_error oc.oc_env (CE_InvalidRE (unloc r2)))
244+
(try EcRegexp.extract (`S "\\$([0-9]+)") (unloc r2)
245+
with Not_found -> [||]);
246+
247+
let k =
248+
if List.is_empty k then `All else
249+
250+
let update rk = function
251+
| `Lemma -> { rk with rkc_lemmas = true; }
252+
| `Type -> { rk with rkc_types = true; }
253+
| `Op -> { rk with rkc_ops = true; }
254+
| `Pred -> { rk with rkc_preds = true; }
255+
| `Module -> { rk with rkc_module = true; }
256+
| `ModType -> { rk with rkc_modtype = true; }
257+
| `Theory -> { rk with rkc_theory = true; } in
258+
259+
let init = {
260+
rkc_lemmas = false; rkc_types = false; rkc_ops = false;
261+
rkc_preds = false; rkc_module = false; rkc_modtype = false;
262+
rkc_theory = false; } in
263+
264+
`Selected (List.fold_left update init k)
265+
266+
in (k, (e1, e2))
267+
end
268+
222269
(* -------------------------------------------------------------------- *)
223270
let rename ((rk, (rex, itempl)) : renaming) (k, x) =
224271
let selected =
@@ -239,12 +286,6 @@ let rename ((rk, (rex, itempl)) : renaming) (k, x) =
239286

240287
if x = newx then None else Some newx
241288

242-
(* -------------------------------------------------------------------- *)
243-
type octxt = {
244-
oc_env : EcEnv.env;
245-
oc_oth : ctheory;
246-
}
247-
248289
(* -------------------------------------------------------------------- *)
249290
module OVRD : sig
250291
type state = theory_cloning_proof list * evclone
@@ -373,7 +414,9 @@ end = struct
373414
in (proofs, evc)
374415

375416
(* ------------------------------------------------------------------ *)
376-
let th_ovrd oc ((proofs, evc) : state) name ((thd, mode) : th_override) =
417+
let th_ovrd oc ((proofs, evc) : state) name ((thd, mode, prerenames) : th_override) =
418+
let prerenames = List.map (Renaming.rename1 oc) prerenames in
419+
377420
let { pl_loc = lc; pl_desc = ((nm, x) as name) } = name in
378421

379422
let loced x = mk_loc lc x in
@@ -406,42 +449,53 @@ end = struct
406449
let thd = let thd = EcPath.toqsymbol sp in (fst thd @ [snd thd]) in
407450
let xdth = nm @ [x] in
408451

452+
let rename (kind, name) =
453+
try
454+
List.find_map (fun rnm -> rename rnm (kind, name)) prerenames
455+
with Not_found -> name in
456+
409457
assert (not (Msym.mem x evc.evc_ths));
410458

411459
let evc = { evc with
412460
evc_ths = Msym.change (fun sub ->
413461
let sub, clear = odfl (evc_empty, false) sub in
414462
Some (sub, clear || (mode <> `Alias))) x evc.evc_ths } in
415463

416-
let rec doit_r prefix (proofs, evc) dth =
464+
let rec doit_r (prefix, tgprefix) (proofs, evc) dth =
465+
let dtpath (x : symbol) =
466+
loced (xdth @ prefix, x) in
467+
468+
let tgpath ?kind (x : symbol) =
469+
let x = Option.fold ~none:x ~some:(fun k -> rename (k, x)) kind in
470+
EcPath.fromqsymbol (thd @ tgprefix, x) in
471+
417472
match dth with
418473
| Th_type (x, _) ->
419-
let ovrd = `ByPath (EcPath.fromqsymbol (thd @ prefix, x)) in
420-
let ovrd = (ovrd, mode) in
421-
ty_ovrd oc (proofs, evc) (loced (xdth @ prefix, x)) ovrd
474+
let ovrd = `ByPath (tgpath ~kind:`Type x), mode in
475+
ty_ovrd oc (proofs, evc) (dtpath x) ovrd
422476

423477
| Th_operator (x, ({ op_kind = OB_oper _ })) ->
424-
let ovrd = `ByPath (EcPath.fromqsymbol (thd @ prefix, x)) in
425-
let ovrd = (ovrd, mode) in
426-
op_ovrd oc (proofs, evc) (loced (xdth @ prefix, x)) ovrd
478+
let ovrd = (`ByPath (tgpath ~kind:`Op x), mode) in
479+
op_ovrd oc (proofs, evc) (dtpath x) ovrd
427480

428481
| Th_operator (x, ({ op_kind = OB_pred _ })) ->
429-
let ovrd = `ByPath (EcPath.fromqsymbol (thd @ prefix, x)) in
430-
let ovrd = (ovrd, mode) in
431-
pr_ovrd oc (proofs, evc) (loced (xdth @ prefix, x)) ovrd
482+
let ovrd = (`ByPath (tgpath ~kind:`Pred x), mode) in
483+
pr_ovrd oc (proofs, evc) (dtpath x) ovrd
432484

433485
| Th_operator (x, {op_kind=OB_nott _; _ }) ->
434-
let ovrd = EcPath.fromqsymbol (thd @ prefix, x) in
435-
let ovrd = (ovrd, mode) in
436-
nt_ovrd oc (proofs, evc) (loced (xdth @ prefix, x)) ovrd
486+
let ovrd = (tgpath x, mode) in
487+
nt_ovrd oc (proofs, evc) (dtpath x) ovrd
437488

438489
| Th_axiom (x, _) ->
439-
let axd = loced (thd @ prefix, x) in
440-
let name = (loced (xdth @ prefix, x)) in
441-
ax_ovrd oc (proofs, evc) name (axd, mode)
490+
let axd = loced (EcPath.toqsymbol (tgpath ~kind:`Axiom x)) in
491+
ax_ovrd oc (proofs, evc) (dtpath x) (axd, mode)
442492

443493
| Th_theory (x, dth) when dth.cth_mode = `Concrete ->
444-
List.fold_left (doit (prefix @ [x])) (proofs, evc) dth.cth_items
494+
let tgx = rename (`Theory, x) in
495+
List.fold_left
496+
(doit (prefix @ [x], tgprefix @ [tgx]))
497+
(proofs, evc)
498+
dth.cth_items
445499

446500
| Th_theory (_, _) ->
447501
(proofs, evc)
@@ -453,9 +507,9 @@ end = struct
453507
(proofs, evc)
454508

455509
| Th_modtype (x, _) ->
456-
modtype_ovrd
457-
oc (proofs, evc) (loced (xdth @ prefix, x))
458-
(loced (thd @ prefix, x), mode)
510+
let ovrd = loced (EcPath.toqsymbol (tgpath ~kind:`ModType x)) in
511+
modtype_ovrd
512+
oc (proofs, evc) (dtpath x) (ovrd, mode)
459513

460514
| Th_instance _ -> (proofs, evc)
461515

@@ -468,7 +522,7 @@ end = struct
468522
and doit prefix (proofs, evc) dth =
469523
doit_r prefix (proofs, evc) dth.ti_item
470524

471-
in List.fold_left (doit []) (proofs, evc) dth.cth_items
525+
in List.fold_left (doit ([], [])) (proofs, evc) dth.cth_items
472526

473527
(* ------------------------------------------------------------------ *)
474528
let ovrd oc state name (ovrd : theory_override) =
@@ -492,47 +546,6 @@ end = struct
492546
th_ovrd oc state name thd
493547
end
494548

495-
(* -------------------------------------------------------------------- *)
496-
module Renaming : sig
497-
val rename1 : octxt -> theory_renaming -> renaming
498-
end = struct
499-
let rename1 oc (k, (r1, r2)) : renaming =
500-
let e1 =
501-
try EcRegexp.regexp (unloc r1)
502-
with EcRegexp.Error _ -> clone_error oc.oc_env (CE_InvalidRE (unloc r1)) in
503-
let e2 =
504-
try EcRegexp.subst (unloc r2)
505-
with EcRegexp.Error _ -> clone_error oc.oc_env (CE_InvalidRE (unloc r2)) in
506-
507-
Array.iter
508-
(fun m ->
509-
if EcRegexp.match_ (`S "^0+$") (oget m.(1)) then
510-
clone_error oc.oc_env (CE_InvalidRE (unloc r2)))
511-
(try EcRegexp.extract (`S "\\$([0-9]+)") (unloc r2)
512-
with Not_found -> [||]);
513-
514-
let k =
515-
if List.is_empty k then `All else
516-
517-
let update rk = function
518-
| `Lemma -> { rk with rkc_lemmas = true; }
519-
| `Type -> { rk with rkc_types = true; }
520-
| `Op -> { rk with rkc_ops = true; }
521-
| `Pred -> { rk with rkc_preds = true; }
522-
| `Module -> { rk with rkc_module = true; }
523-
| `ModType -> { rk with rkc_modtype = true; }
524-
| `Theory -> { rk with rkc_theory = true; } in
525-
526-
let init = {
527-
rkc_lemmas = false; rkc_types = false; rkc_ops = false;
528-
rkc_preds = false; rkc_module = false; rkc_modtype = false;
529-
rkc_theory = false; } in
530-
531-
`Selected (List.fold_left update init k)
532-
533-
in (k, (e1, e2))
534-
end
535-
536549
(* -------------------------------------------------------------------- *)
537550
module Proofs : sig
538551
val proof : intheory:bool -> octxt -> evclone -> theory_cloning_proof -> evclone

src/ecTheoryReplay.ml

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -367,23 +367,14 @@ let string_of_renaming_kind = function
367367
| `Theory -> "theory"
368368

369369
(* -------------------------------------------------------------------- *)
370-
let rename ?(fold = true) ove subst (kind, name) =
370+
let rename ove subst (kind, name) =
371371
try
372372
let newname =
373-
match fold with
374-
| false ->
375-
List.find_map
376-
(fun rnm -> EcThCloning.rename rnm (kind, name))
377-
ove.ovre_rnms
378-
| _ ->
379-
let newname =
380-
List.fold_left (* FIXME:parallel substitution *)
381-
(fun name rnm ->
382-
Option.value ~default:name (EcThCloning.rename rnm (kind, name)))
373+
List.fold_left (* FIXME:parallel substitution *)
374+
(fun name rnm ->
375+
Option.value ~default:name (EcThCloning.rename rnm (kind, name)))
383376
name ove.ovre_rnms in
384-
if newname = name then raise Not_found;
385-
newname
386-
in
377+
if newname = name then raise Not_found;
387378

388379
let nameok =
389380
match kind with

0 commit comments

Comments
 (0)