Skip to content

Commit 87fe196

Browse files
committed
Save vars per op in bmv monad, add set_Vrs to mrsbnf axioms
1 parent ee89977 commit 87fe196

File tree

4 files changed

+137
-111
lines changed

4 files changed

+137
-111
lines changed

Tools/bmv_monad_def.ML

Lines changed: 65 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ signature BMV_MONAD_DEF = sig
4747
ops: typ list,
4848
var_class: class,
4949
bmv_ops: bmv_monad list,
50-
frees: typ list,
51-
deads: typ list,
50+
frees: typ list list,
51+
deads: typ list list,
5252
leader: int,
53-
lives: typ list,
54-
lives': typ list,
53+
lives: typ list list,
54+
lives': typ list list,
5555
consts: (term option) bmv_monad_consts,
5656
params: 'a bmv_monad_param option list,
5757
bd_infinite_regular_card_order: 'a,
@@ -64,10 +64,10 @@ signature BMV_MONAD_DEF = sig
6464
val bd_infinite_regular_card_order_of_bmv_monad: bmv_monad -> thm;
6565
val var_class_of_bmv_monad: bmv_monad -> class;
6666
val leader_of_bmv_monad: bmv_monad -> int;
67-
val frees_of_bmv_monad: bmv_monad -> typ list;
68-
val lives_of_bmv_monad: bmv_monad -> typ list;
69-
val lives'_of_bmv_monad: bmv_monad -> typ list;
70-
val deads_of_bmv_monad: bmv_monad -> typ list;
67+
val frees_of_bmv_monad: bmv_monad -> typ list list;
68+
val lives_of_bmv_monad: bmv_monad -> typ list list;
69+
val lives'_of_bmv_monad: bmv_monad -> typ list list;
70+
val deads_of_bmv_monad: bmv_monad -> typ list list;
7171
val Injs_of_bmv_monad: bmv_monad -> term list list;
7272
val SSupps_of_bmv_monad: bmv_monad -> (term * thm) list list;
7373
val Sbs_of_bmv_monad: bmv_monad -> term list;
@@ -218,10 +218,10 @@ datatype bmv_monad = BMV of {
218218
ops: typ list,
219219
var_class: class,
220220
leader: int,
221-
frees: typ list,
222-
lives: typ list,
223-
lives': typ list,
224-
deads: typ list,
221+
frees: typ list list,
222+
lives: typ list list,
223+
lives': typ list list,
224+
deads: typ list list,
225225
consts: (term * thm) bmv_monad_consts,
226226
params: thm bmv_monad_param option list,
227227
bd_infinite_regular_card_order: thm,
@@ -236,10 +236,10 @@ fun morph_bmv_monad phi (BMV {
236236
ops = map (Morphism.typ phi) ops,
237237
leader = leader,
238238
var_class = var_class,
239-
frees = map (Morphism.typ phi) frees,
240-
lives = map (Morphism.typ phi) lives,
241-
lives' = map (Morphism.typ phi) lives',
242-
deads = map (Morphism.typ phi) deads,
239+
frees = map (map (Morphism.typ phi)) frees,
240+
lives = map (map (Morphism.typ phi)) lives,
241+
lives' = map (map (Morphism.typ phi)) lives',
242+
deads = map (map (Morphism.typ phi)) deads,
243243
consts = morph_bmv_monad_consts phi (map_prod (Morphism.term phi) (Morphism.thm phi)) consts,
244244
params = map (Option.map (map_bmv_monad_param (Morphism.thm phi))) params,
245245
axioms = map (morph_bmv_monad_axioms phi) axioms,
@@ -271,10 +271,10 @@ val bd_infinite_regular_card_order_of_bmv_monad = #bd_infinite_regular_card_orde
271271
type 'a bmv_monad_model = {
272272
ops: typ list,
273273
var_class: class,
274-
frees: typ list,
275-
lives: typ list,
276-
lives': typ list,
277-
deads: typ list,
274+
frees: typ list list,
275+
lives: typ list list,
276+
lives': typ list list,
277+
deads: typ list list,
278278
consts: (term option) bmv_monad_consts,
279279
params: 'a bmv_monad_param option list,
280280
bmv_ops: bmv_monad list,
@@ -289,10 +289,10 @@ fun morph_bmv_monad_model phi f ({ ops, var_class, frees, lives, lives', consts,
289289
) = {
290290
ops = map (Morphism.typ phi) ops,
291291
var_class = var_class,
292-
frees = map (Morphism.typ phi) frees,
293-
lives = map (Morphism.typ phi) lives,
294-
lives' = map (Morphism.typ phi) lives',
295-
deads = map (Morphism.typ phi) deads,
292+
frees = map (map (Morphism.typ phi)) frees,
293+
lives = map (map (Morphism.typ phi)) lives,
294+
lives' = map (map (Morphism.typ phi)) lives',
295+
deads = map (map (Morphism.typ phi)) deads,
296296
consts = morph_bmv_monad_consts phi (Option.map (Morphism.term phi)) consts,
297297
params = params,
298298
bmv_ops = map (morph_bmv_monad phi) bmv_ops,
@@ -625,7 +625,7 @@ fun define_bmv_monad_consts const_policy fact_policy qualify leader ops lives' (
625625
val (lthy, old_lthy) = `Local_Theory.end_nested lthy;
626626
val phi = Proof_Context.export_morphism old_lthy lthy;
627627

628-
val vars = map TFree (rev (Term.add_tfreesT (nth ops leader) [])) @ lives';
628+
val vars = map TFree (rev (Term.add_tfreesT (nth ops leader) [])) @ flat lives';
629629
val subst = (map (Morphism.typ phi) vars ~~ vars);
630630

631631
val phi' = Morphism.term_morphism "bmv_monad_export" (Term.subst_atomic_types subst o Morphism.term phi)
@@ -770,10 +770,10 @@ fun mk_bmv_monad const_policy fact_policy qualify SSupp_defs bmv_b_opt (model: t
770770
ops = #ops model @ maps (#ops o Rep_bmv) (#bmv_ops model),
771771
var_class = #var_class model,
772772
leader = #leader model,
773-
frees = #frees model,
774-
lives = #lives model,
775-
lives' = #lives' model,
776-
deads = #deads model,
773+
frees = #frees model @ maps (#frees o Rep_bmv) (#bmv_ops model),
774+
lives = #lives model @ maps (#lives o Rep_bmv) (#bmv_ops model),
775+
lives' = #lives' model @ maps (#lives' o Rep_bmv) (#bmv_ops model),
776+
deads = #deads model @ maps (#deads o Rep_bmv) (#bmv_ops model),
777777
consts = consts,
778778
params = #params model @ maps (#params o Rep_bmv) (#bmv_ops model),
779779
axioms = axioms,
@@ -840,8 +840,8 @@ fun bmv_monad_def const_policy fact_policy qualify bmv_b_opt (model: (Proof.cont
840840
let
841841
val frees = map (fn T => TFree (apsnd (
842842
Sign.minimize_sort (Proof_Context.theory_of lthy) o cons (#var_class model)
843-
) (dest_TFree T))) (#frees model);
844-
val model = morph_bmv_monad_model (MRBNF_Util.subst_typ_morphism (#frees model ~~ frees)) I model;
843+
) (dest_TFree T))) (nth (#frees model) (#leader model));
844+
val model = morph_bmv_monad_model (MRBNF_Util.subst_typ_morphism (nth (#frees model) (#leader model) ~~ frees)) I model;
845845

846846
val (consts, unfold_set, SSupp_defs, lthy) = define_bmv_monad_consts const_policy (fact_policy lthy) qualify
847847
(#leader model) (#ops model) (#lives' model) (#consts model) lthy;
@@ -872,10 +872,10 @@ fun pbmv_monad_of_bnf bnf lthy =
872872
ops = [T],
873873
var_class = var_class,
874874
leader = 0,
875-
frees = [],
876-
lives = lives,
877-
lives' = lives',
878-
deads = deads,
875+
frees = [[]],
876+
lives = [lives],
877+
lives' = [lives'],
878+
deads = [deads],
879879
bmv_ops = [],
880880
consts = {
881881
bd = BNF_Def.bd_of_bnf bnf,
@@ -942,10 +942,10 @@ fun slice_bmv_monad n bmv =
942942
ops = [f (ops_of_bmv_monad bmv)],
943943
var_class = var_class_of_bmv_monad bmv,
944944
leader = 0,
945-
frees = filter (member (op=) vars) (frees_of_bmv_monad bmv),
946-
lives = lives_of_bmv_monad bmv,
947-
lives' = lives'_of_bmv_monad bmv,
948-
deads = deads_of_bmv_monad bmv,
945+
frees = [f (frees_of_bmv_monad bmv)],
946+
lives = [f (lives_of_bmv_monad bmv)],
947+
lives' = [f (lives'_of_bmv_monad bmv)],
948+
deads = [f (deads_of_bmv_monad bmv)],
949949
consts = {
950950
bd = bd_of_bmv_monad bmv,
951951
params = [@{map_option 2} (fn Map => fn Supps => {
@@ -964,7 +964,7 @@ fun slice_bmv_monad n bmv =
964964

965965
fun compose_bmv_monad qualify (outer : bmv_monad) (inners : (bmv_monad, typ) either list) lthy =
966966
let
967-
val _ = if length (lives_of_bmv_monad outer) <> length inners then
967+
val _ = if length (nth (lives_of_bmv_monad outer) (leader_of_bmv_monad outer)) <> length inners then
968968
error "Outer needs exactly as many lives as there are inners" else ()
969969

970970
val filter_bmvs = map_filter (fn Inl x => SOME x | _ => NONE);
@@ -1000,7 +1000,8 @@ fun compose_bmv_monad qualify (outer : bmv_monad) (inners : (bmv_monad, typ) eit
10001000

10011001
val subst =
10021002
let val Ts = map (sum_collapse o map_sum (hd o ops_of_bmv_monad) I) inner_leaders'
1003-
in (lives_of_bmv_monad outer ~~ Ts) @ (lives'_of_bmv_monad outer ~~ Ts) end;
1003+
in (nth (lives_of_bmv_monad outer) (leader_of_bmv_monad outer) ~~ Ts)
1004+
@ (nth (lives'_of_bmv_monad outer) (leader_of_bmv_monad outer) ~~ Ts) end;
10041005
val new_leader = Term.typ_subst_atomic subst (nth (ops_of_bmv_monad outer) (leader_of_bmv_monad outer));
10051006

10061007
val new_Injs = distinct (op=) (
@@ -1063,7 +1064,7 @@ fun compose_bmv_monad qualify (outer : bmv_monad) (inners : (bmv_monad, typ) eit
10631064
val ops = new_leader :: map (hd o ops_of_bmv_monad) minions;
10641065

10651066
val vars = distinct (op=) (map TFree (fold Term.add_tfreesT ops []));
1066-
val lives = distinct (op=) (maps lives_of_bmv_monad inners');
1067+
val lives = distinct (op=) (flat (maps lives_of_bmv_monad inners'));
10671068

10681069
val consts = {
10691070
bd = bd_of_bmv_monad outer, (* TODO: compose bounds *)
@@ -1081,10 +1082,10 @@ fun compose_bmv_monad qualify (outer : bmv_monad) (inners : (bmv_monad, typ) eit
10811082
bmv_ops = minions,
10821083
bd_infinite_regular_card_order = fn ctxt => rtac ctxt (bd_infinite_regular_card_order_of_bmv_monad outer) 1,
10831084
var_class = var_class_of_bmv_monad outer,
1084-
frees = frees,
1085-
lives = lives,
1086-
lives' = distinct (op=) (maps lives'_of_bmv_monad inners'),
1087-
deads = subtract (op=) (lives @ frees) vars,
1085+
frees = [frees],
1086+
lives = [lives],
1087+
lives' = [distinct (op=) (flat (maps lives'_of_bmv_monad inners'))],
1088+
deads = [subtract (op=) (lives @ frees) vars],
10881089
consts = consts,
10891090
params = [NONE],
10901091
leader = 0,
@@ -1186,7 +1187,7 @@ fun compose_bmv_monad qualify (outer : bmv_monad) (inners : (bmv_monad, typ) eit
11861187
Subgoal.FOCUS_PREMS (fn {context=ctxt, prems, ...} => EVERY1 [
11871188
rtac ctxt @{thm trans[rotated]},
11881189
rtac ctxt (
1189-
let val n = length (lives_of_bmv_monad outer);
1190+
let val n = length (nth (lives_of_bmv_monad outer) (leader_of_bmv_monad outer));
11901191
in mk_arg_cong lthy (n + 1) Map OF (replicate n refl) end
11911192
),
11921193
K (prefer_tac 2),
@@ -1242,9 +1243,7 @@ fun pbmv_monad_cmd (((((((b, ops), Sbs), Injs), SSupps_opt), Vrs), param_opt), b
12421243
map (apply2 TFree) (Term.add_tfreesT (body_type (fastype_of Sb)) [] ~~ Term.add_tfreesT T [])
12431244
@ map (apply2 TFree) (Term.add_tfreesT (snd (split_last (binder_types (fastype_of Sb)))) [] ~~ Term.add_tfreesT T [])
12441245
) Sb) (map (Syntax.read_term lthy) Sbs) ops;
1245-
val frees = distinct (op=) (maps (
1246-
map (fst o dest_funT) o fst o split_last o binder_types o fastype_of
1247-
) Sbs);
1246+
val frees = map (distinct (op=) o map (fst o dest_funT) o fst o split_last o binder_types o fastype_of) Sbs;
12481247
val Injs = map (map (Syntax.read_term lthy)) Injs;
12491248
val SSupps = case SSupps_opt of
12501249
SOME SSupps => map (map (fn "_" => NONE | t => SOME (Syntax.read_term lthy t))) SSupps
@@ -1262,24 +1261,25 @@ fun pbmv_monad_cmd (((((((b, ops), Sbs), Injs), SSupps_opt), Vrs), param_opt), b
12621261
|> fold Variable.declare_typ vars
12631262

12641263
val (lives, lives', param_consts) = case param_opt of
1265-
NONE => ([], [], replicate (length ops) NONE)
1264+
NONE => (replicate (length ops) [], replicate (length ops) [], replicate (length ops) NONE)
12661265
| SOME (Maps, Suppss) =>
12671266
let
12681267
val Maps = map (fn "_" => NONE | s => SOME (Syntax.read_term lthy s)) Maps;
12691268
val Suppss = map (fn [] => NONE | xs => SOME (map (Syntax.read_term lthy) xs)) Suppss;
1269+
val Maps = Maps @ replicate (length ops - length Maps) NONE;
12701270

1271-
val lives = the_default [] (Option.map (fn Map =>
1271+
val lives = map (the_default [] o Option.map (fn Map =>
12721272
let
12731273
val Map = Term.subst_atomic_types (map (apply2 TFree) (
12741274
Term.add_tfreesT (snd (split_last (binder_types (fastype_of Map)))) []
12751275
~~ Term.add_tfreesT (hd ops) []
12761276
)) Map;
12771277
in map (fst o dest_funT) (fst (split_last (binder_types (fastype_of Map)))) end
1278-
) (hd Maps));
1278+
)) Maps;
12791279
val (lives', _) = names_lthy
1280-
|> mk_TFrees' (map Type.sort_of_atyp lives);
1280+
|> fold_map mk_TFrees' (map (map Type.sort_of_atyp) lives);
12811281

1282-
val Maps = map2 (fn T => Option.map (fn Map =>
1282+
val Maps = @{map 4} (fn T => fn lives => fn lives' => Option.map (fn Map =>
12831283
let
12841284
val l' = map (snd o dest_funT) (fst (split_last (binder_types (fastype_of Map))));
12851285
val TA = snd (split_last (binder_types (fastype_of Map)));
@@ -1294,7 +1294,7 @@ fun pbmv_monad_cmd (((((((b, ops), Sbs), Injs), SSupps_opt), Vrs), param_opt), b
12941294
subtract (op=) l' old_vars ~~ subtract (op=) lives (map TFree (Term.add_tfreesT TA []))
12951295
)
12961296
) Map end
1297-
)) ops (Maps @ replicate (length ops - length Maps) NONE);
1297+
)) ops lives lives' Maps;
12981298

12991299
val Suppss = map2 (fn T => Option.map (map (fn Supp => Term.subst_atomic_types (map (apply2 TFree) (
13001300
Term.add_tfreesT (hd (binder_types (fastype_of Supp))) [] ~~ Term.add_tfreesT T []
@@ -1304,6 +1304,10 @@ fun pbmv_monad_cmd (((((((b, ops), Sbs), Injs), SSupps_opt), Vrs), param_opt), b
13041304
map2 (@{map_option 2} (fn Map => fn Supps => { Map = Map, Supps = Supps })) Maps Suppss
13051305
) end;
13061306

1307+
val Vrs = map2 (fn frees => map (fn Vrs => map (fn var => Option.join (
1308+
List.find (fn SOME Vrs => HOLogic.dest_setT (body_type (fastype_of Vrs)) = var | NONE => false) Vrs
1309+
)) frees)) frees Vrs;
1310+
13071311
val consts = {
13081312
bd = bd,
13091313
Injs = Injs,
@@ -1375,14 +1379,15 @@ fun pbmv_monad_cmd (((((((b, ops), Sbs), Injs), SSupps_opt), Vrs), param_opt), b
13751379
}: thm bmv_monad_axioms, SSupp_eq), param), thms) end
13761380
) goals SSupp_eq_goals param_goals param_consts (tl thms));
13771381

1382+
val _ = @{print} (lives, frees)
13781383
val model = {
13791384
ops = ops,
13801385
var_class = @{class var}, (* TODO: change *)
13811386
leader = 0,
13821387
frees = frees,
13831388
lives = lives,
13841389
lives' = lives',
1385-
deads = subtract (op=) (lives @ frees) vars,
1390+
deads = map2 (fn lives => fn frees => subtract (op=) (lives @ frees) vars) lives frees,
13861391
bmv_ops = [],
13871392
consts = consts,
13881393
params = params,
@@ -1416,15 +1421,15 @@ fun print_pbmv_monads ctxt =
14161421
fun map_filter_end [] _ = []
14171422
| map_filter_end (SOME x::xs) ys = ys @ [SOME x] @ map_filter_end xs ys
14181423
| map_filter_end (NONE::xs) ys = map_filter_end xs (NONE::ys)
1419-
fun pretty_mrbnf (key, bmv as BMV {ops, frees, lives, consts, ...}) =
1424+
fun pretty_mrbnf (key, bmv as BMV {ops, frees, lives, consts, leader, ...}) =
14201425
Pretty.big_list
14211426
(Pretty.string_of (Pretty.block ([Pretty.str key, Pretty.str ":", Pretty.brk 1] @
14221427
and_list (map (Pretty.quote o Syntax.pretty_typ ctxt) ops))))
14231428
([Pretty.block [Pretty.str "frees:", Pretty.brk 1, Pretty.str (string_of_int (length frees)),
1424-
Pretty.brk 3, Pretty.list "[" "]" (List.map (Syntax.pretty_typ ctxt) frees)]] @
1429+
Pretty.brk 3, Pretty.list "[" "]" (List.map (Syntax.pretty_typ ctxt) (nth frees leader))]] @
14251430
(if length lives > 0 then
14261431
[Pretty.block [Pretty.str "live:", Pretty.brk 1, Pretty.str (string_of_int (length lives)),
1427-
Pretty.brk 3, Pretty.list "[" "]" (List.map (Syntax.pretty_typ ctxt) lives)]]
1432+
Pretty.brk 3, Pretty.list "[" "]" (List.map (Syntax.pretty_typ ctxt) (nth lives leader))]]
14281433
else []) @
14291434
[ Pretty.block ([Pretty.str "Sb:", Pretty.brk 1] @ and_list (map (Pretty.quote o Syntax.pretty_term ctxt) (#Sbs consts)))
14301435
] @ (case map_filter I (Maps_of_bmv_monad bmv) of [] => [] | _ => [

0 commit comments

Comments
 (0)