Skip to content

Commit d374a16

Browse files
committed
Fix lingering issues of mrsbnf composition
1 parent 408b033 commit d374a16

File tree

8 files changed

+71
-50
lines changed

8 files changed

+71
-50
lines changed

Tools/mrbnf_sugar.ML

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,14 @@ fun create_binder_datatype (spec : spec) lthy =
603603
])] end
604604
) ctors_tys) ctors_tys));
605605

606+
fun find_bnfs lthy (Type (n, Ts)) xs =
607+
let
608+
val insert = case BNF_Def.bnf_of lthy n of
609+
SOME bnf => insert ((op=) o apply2 fst) (n, bnf) | NONE => I
610+
in insert (fold (find_bnfs lthy) Ts xs) end
611+
| find_bnfs _ _ xs = xs
612+
val bnfs = fold (find_bnfs lthy) (maps (map (Term.typ_subst_atomic replace) o snd) ctors_tys) [];
613+
606614
val injects = @{map_filter 2} (fn ((ctor, ctor_def), tys) => fn setss => if null tys then NONE else
607615
let
608616
val bound_tys = inter (op=) bounds (rev (map TFree (fold Term.add_tfreesT tys [])));
@@ -677,6 +685,7 @@ fun create_binder_datatype (spec : spec) lthy =
677685
K (Local_Defs.unfold0_tac ctxt (
678686
@{thms map_sum.simps map_prod_simp sum.inject prod.inject id_apply}
679687
@ set_simp_thms
688+
@ map (BNF_Def.map_id_of_bnf o snd) bnfs
680689
@ maps MRBNF_Def.set_map_of_mrbnf fp_nesting_mrbnfs
681690
@ map MRBNF_Def.map_id_of_mrbnf fp_nesting_mrbnfs
682691
@ MRBNF_Def.set_defs_of_mrbnf pre_mrbnf
@@ -1052,7 +1061,8 @@ fun create_binder_datatype (spec : spec) lthy =
10521061
) (MRBNF_Def.map_of_mrbnf rec_mrbnf);
10531062
fun tac ctxt prems = EVERY1 [
10541063
rtac ctxt (trans OF [#vvsubst_ctor vvsubst_res]),
1055-
K (Local_Defs.unfold0_tac ctxt (thms @ MRBNF_Def.set_defs_of_mrbnf pre_mrbnf @ [
1064+
K (Local_Defs.unfold0_tac ctxt (thms @ MRBNF_Def.set_defs_of_mrbnf pre_mrbnf
1065+
@ map (BNF_Def.map_id_of_bnf o snd) bnfs @ [
10561066
MRBNF_Def.map_def_of_mrbnf pre_mrbnf,
10571067
#Abs_inverse (snd info) OF @{thms UNIV_I},
10581068
snd (#noclash (hd (#quotient_fps res)))
@@ -1086,13 +1096,14 @@ fun create_binder_datatype (spec : spec) lthy =
10861096
val mapx = Term.subst_atomic_types (Ts' ~~ (vars @ passives)) (#permute quotient);
10871097
fun tac ctxt prems = EVERY1 [
10881098
rtac ctxt (trans OF [#permute_ctor quotient]),
1089-
K (Local_Defs.unfold0_tac ctxt (thms @ MRBNF_Def.set_defs_of_mrbnf pre_mrbnf @ [
1099+
K (Local_Defs.unfold0_tac ctxt (thms @ MRBNF_Def.set_defs_of_mrbnf pre_mrbnf
1100+
@ map (BNF_Def.map_id_of_bnf o snd) bnfs @ [
10901101
MRBNF_Def.map_def_of_mrbnf pre_mrbnf,
10911102
#Abs_inverse (snd info) OF @{thms UNIV_I}
10921103
])),
10931104
REPEAT_DETERM o resolve_tac ctxt prems,
10941105
REPEAT_DETERM o EVERY' [
1095-
EqSubst.eqsubst_tac ctxt [0] (map #map_permute nesting_binder_sugars),
1106+
EqSubst.eqsubst_tac ctxt [0] (@{print}(map #map_permute nesting_binder_sugars)),
10961107
REPEAT_DETERM o resolve_tac ctxt prems
10971108
],
10981109
K (Local_Defs.unfold0_tac ctxt @{thms id_apply}),
@@ -1139,6 +1150,7 @@ fun create_binder_datatype (spec : spec) lthy =
11391150
REPEAT_DETERM o resolve_tac ctxt prems,
11401151
K (Local_Defs.unfold0_tac ctxt (thms
11411152
@ MRBNF_Def.set_defs_of_mrbnf pre_mrbnf
1153+
@ map (BNF_Def.map_id_of_bnf o snd) bnfs
11421154
@ #isVVrs tvsubst_res @ map snd (#VVrs tvsubst_res)
11431155
@ [#Abs_inverse (snd info) OF @{thms UNIV_I}, snd (#noclash quotient)]
11441156
)),
@@ -1162,7 +1174,9 @@ fun create_binder_datatype (spec : spec) lthy =
11621174
])),
11631175
eresolve_tac ctxt @{thms sum.distinct[THEN notE]}
11641176
],
1165-
K (Local_Defs.unfold0_tac ctxt (map MRBNF_Def.map_id0_of_mrbnf fp_nesting_mrbnfs)),
1177+
K (Local_Defs.unfold0_tac ctxt (map MRBNF_Def.map_id0_of_mrbnf fp_nesting_mrbnfs
1178+
@ map (BNF_Def.map_id_of_bnf o snd) bnfs
1179+
)),
11661180
K (Local_Defs.unfold0_tac ctxt @{thms id_def}),
11671181
rtac ctxt refl
11681182
]

Tools/mrsbnf_comp.ML

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,17 @@ fun seal_mrsbnf qualify (bmv_unfolds, mrbnf_unfolds) name Xs tys mrsbnf info_opt
5151
val bmv = MRSBNF_Def.bmv_monad_of_mrsbnf mrsbnf;
5252
val mrbnf' = nth (MRSBNF_Def.mrbnfs_of_mrsbnf mrsbnf) (BMV_Monad_Def.leader_of_bmv_monad bmv);
5353

54-
val (_, _, (mrbnfs', (_, lthy))) = MRBNF_Comp.normalize_mrbnfs (K I) [] [map dest_TFree (snd tys)]
54+
val ((_, _, new_tys), _, (mrbnfs', (_, lthy))) = MRBNF_Comp.normalize_mrbnfs (K I) [] [map dest_TFree (snd tys)]
5555
[] Xs (K (map fst Xs)) NONE [mrbnf'] ((MRBNF_Comp.empty_comp_cache, mrbnf_unfolds), lthy);
5656
val mrbnf' = hd mrbnfs';
5757
val ((mrbnf, info, (Ds, absT_info)), lthy) = MRBNF_Comp.seal_mrbnf qualify mrbnf_unfolds name true Ds Ds mrbnf' info_opt lthy;
5858

5959
val var_class = MRBNF_Def.class_of_mrbnf mrbnf;
6060
val var_types = MRBNF_Def.var_types_of_mrbnf mrbnf';
61-
val (lives, bounds, frees) = MRBNF_Def.deinterlace (snd tys) var_types;
61+
val (lives, bounds, frees) = MRBNF_Def.deinterlace (map TFree new_tys) var_types;
6262
val bounds = map (resort_tfree_or_tvar var_class) bounds;
6363
val frees = map (resort_tfree_or_tvar var_class) frees;
64+
6465
val rep_T = MRBNF_Def.mk_T_of_mrbnf Ds lives bounds frees mrbnf';
6566

6667
val (lives', _) = lthy
@@ -137,7 +138,8 @@ fun seal_mrsbnf qualify (bmv_unfolds, mrbnf_unfolds) name Xs tys mrsbnf info_opt
137138
rtac ctxt copy,
138139
rtac ctxt copy,
139140
K (Local_Defs.unfold0_tac ctxt (comp_assocs @ [@{thm type_copy_Rep_o_Abs_o} OF [copy]])),
140-
rtac ctxt (unfold_defs thm) THEN_ALL_NEW assume_tac ctxt
141+
rtac ctxt (unfold_defs thm),
142+
REPEAT_DETERM o assume_tac ctxt
141143
]) (#map_Sb axioms),
142144
map_Injs = Option.map (map (fn thm => fn ctxt => EVERY1 [
143145
K (Local_Defs.unfold0_tac ctxt defs),
@@ -153,7 +155,8 @@ fun seal_mrsbnf qualify (bmv_unfolds, mrbnf_unfolds) name Xs tys mrsbnf info_opt
153155
rtac ctxt copy,
154156
K (Local_Defs.unfold0_tac ctxt (comp_assocs @ [@{thm type_copy_Rep_o_Abs_o} OF [copy]]))
155157
],
156-
rtac ctxt (unfold_defs (#map_is_Sb axioms)) THEN_ALL_NEW assume_tac ctxt
158+
rtac ctxt (unfold_defs (#map_is_Sb axioms)),
159+
REPEAT_DETERM o (assume_tac ctxt ORELSE' resolve_tac ctxt @{thms supp_id_bound bij_id})
157160
],
158161
set_Sb = map (fn _ => fn ctxt => EVERY1 [
159162
K (Local_Defs.unfold0_tac ctxt (defs @ [@{thm SSupp_type_copy} OF [copy]])),
@@ -163,15 +166,17 @@ fun seal_mrsbnf qualify (bmv_unfolds, mrbnf_unfolds) name Xs tys mrsbnf info_opt
163166
REPEAT_DETERM o assume_tac ctxt,
164167
K (Local_Defs.unfold0_tac ctxt @{thms comp_def}),
165168
rtac ctxt refl
166-
]) (#set_Sb axioms),
167-
set_Vrs = map (fn thm => fn ctxt => EVERY1 [
169+
]) (bounds @ lives),
170+
set_Vrs = map (K (fn ctxt => EVERY1 [
168171
K (Local_Defs.unfold_tac ctxt (defs @ [@{thm SSupp_type_copy} OF [copy]])),
169172
rtac ctxt @{thm trans[OF comp_apply]},
170-
rtac ctxt trans,
171-
rtac ctxt (unfold_defs thm),
172-
K (Local_Defs.unfold0_tac ctxt @{thms comp_def}),
173-
rtac ctxt refl
174-
]) (#set_Vrs axioms)
173+
rtac ctxt refl ORELSE' EVERY' [
174+
rtac ctxt trans,
175+
resolve_tac ctxt (map unfold_defs (#set_Vrs axioms)),
176+
K (Local_Defs.unfold0_tac ctxt @{thms comp_def}),
177+
rtac ctxt refl
178+
]
179+
])) (#set_Vrs axioms)
175180
}
176181
) (MRSBNF_Def.axioms_of_mrsbnf mrsbnf)) lthy;
177182
in ((mrsbnf, (snd (dest_Type T), absT_info)), lthy) end
@@ -216,8 +221,10 @@ fun compose_mrsbnfs inline_policy fact_policy qualify outer inners oDs Dss oAs A
216221
val outer_mrbnf = nth (MRSBNF_Def.mrbnfs_of_mrsbnf outer) leader;
217222
val inner_mrbnfs = map (fn mrsbnf => nth (MRSBNF_Def.mrbnfs_of_mrsbnf mrsbnf) leader) inners;
218223

224+
val _ = @{print} "wat"
219225
val ((mrbnf, tys), (mrbnf_unfolds, lthy)) = MRBNF_Comp.compose_mrbnf MRBNF_Def.Smart_Inline
220226
qualify flatten_tyargs outer_mrbnf inner_mrbnfs oDs Dss oAs Ass Xs (accum, lthy);
227+
val _ = @{print} (tys, MRBNF_Def.T_of_mrbnf mrbnf)
221228

222229
val mrbnf =
223230
let

Tools/mrsbnf_def.ML

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -860,18 +860,27 @@ fun mk_mrsbnf_axioms mrbnfs bmv lthy =
860860
val (fs, names_lthy) = names_lthy
861861
|> mk_Frees "f" (MRBNF_Def.interlace (map2 (curry (op-->)) As As') (map (fn a => a --> a) Bs) (map (fn a => a --> a) Fs) (MRBNF_Def.var_types_of_mrbnf lmrbnf));
862862

863+
val subst =
864+
(MRBNF_Def.frees_of_mrbnf lmrbnf ~~ Fs)
865+
@ (MRBNF_Def.bounds_of_mrbnf lmrbnf ~~ Bs)
866+
@ (MRBNF_Def.lives_of_mrbnf lmrbnf ~~ As)
867+
@ (MRBNF_Def.lives'_of_mrbnf lmrbnf ~~ As')
868+
@ (ldeads ~~ deads)
869+
val subst_phi = MRBNF_Util.subst_typ_morphism subst;
870+
val mrbnfs = map (MRBNF_Def.morph_mrbnf subst_phi) mrbnfs;
871+
863872
local
864-
val subst =
865-
(MRBNF_Def.frees_of_mrbnf lmrbnf ~~ Fs)
866-
@ (MRBNF_Def.bounds_of_mrbnf lmrbnf ~~ Bs)
867-
@ (MRBNF_Def.lives_of_mrbnf lmrbnf ~~ As)
868-
@ (MRBNF_Def.lives'_of_mrbnf lmrbnf ~~ As')
869-
@ (ldeads ~~ deads)
870-
val phi = MRBNF_Util.subst_typ_morphism subst;
873+
val T = fastype_of (MRBNF_Def.map_of_mrbnf (hd mrbnfs));
874+
val T' = case BMV_Monad_Def.leader BMV_Monad_Def.Maps_of_bmv_monad bmv of
875+
SOME Map => fastype_of Map
876+
| NONE => fastype_of (BMV_Monad_Def.leader BMV_Monad_Def.Sbs_of_bmv_monad bmv)
877+
val tyenv = try (Sign.typ_match (Proof_Context.theory_of lthy)
878+
(snd (split_last (binder_types T')) --> body_type T', snd (split_last (binder_types T)) --> body_type T)
879+
) Vartab.empty;
880+
val phi = Option.map (fn tyenv => MRBNF_Util.subst_typ_morphism (map (fn (n, (s, T)) => (TVar (n, s), T)) (Vartab.dest tyenv))) tyenv;
871881
in
872-
val mrbnfs = map (MRBNF_Def.morph_mrbnf phi) mrbnfs;
873-
val bmv = BMV_Monad_Def.morph_bmv_monad phi bmv;
874-
end;
882+
val bmv = BMV_Monad_Def.morph_bmv_monad (the_default subst_phi phi) bmv;
883+
end
875884

876885
val axioms = @{map 7} (fn mrbnf => fn Sb => fn Injs => fn RVrs => fn Vrs => fn Map_opt => fn lives =>
877886
let
@@ -888,14 +897,15 @@ fun mk_mrsbnf_axioms mrbnfs bmv lthy =
888897
val free = length frees;
889898
val free_fs = take free free_fs';
890899

900+
val free_prems' = map (HOLogic.mk_Trueprop o mk_supp_bound) free_fs';
891901
val free_prems = map (HOLogic.mk_Trueprop o mk_supp_bound) free_fs;
892902
val live_fs' = filter (member (op=) lives o domain_type o fastype_of) live_fs;
893903

894-
val map_is_Sb = fold_rev Logic.all (free_fs @ live_fs') (fold_rev (curry Logic.mk_implies) free_prems (mk_Trueprop_eq (
904+
val map_is_Sb = fold_rev Logic.all (free_fs' @ live_fs') (fold_rev (curry Logic.mk_implies) free_prems' (mk_Trueprop_eq (
895905
Term.list_comb (Term.subst_atomic_types (filter_out (member (op=) lives o snd) (As' ~~ As)) mapx, MRBNF_Def.interlace
896906
(map (fn a => the_default (HOLogic.id_const a) (
897907
List.find (curry (op=) a o domain_type o fastype_of) live_fs'
898-
)) As) (map HOLogic.id_const Bs) (free_fs @ map HOLogic.id_const (drop (length frees) Fs))
908+
)) As) (map HOLogic.id_const Bs) free_fs'
899909
(MRBNF_Def.var_types_of_mrbnf mrbnf)
900910
),
901911
let val add_Map = case Map_opt of
@@ -905,9 +915,11 @@ fun mk_mrsbnf_axioms mrbnfs bmv lthy =
905915
the (List.find (curry (op=) T o fastype_of) live_fs')
906916
) (fst (split_last (binder_types (fastype_of Map)))))
907917
)
908-
in add_Map (Term.list_comb (Sb, map (fn RVr => the (List.find (fn f =>
909-
HOLogic.dest_setT (body_type (fastype_of RVr)) = domain_type (fastype_of f)
910-
) fs)) RVrs @ map (fn Inj =>
918+
in add_Map (Term.list_comb (Sb, map (fn RVr =>
919+
let val T = HOLogic.dest_setT (body_type (fastype_of RVr));
920+
in the_default (HOLogic.id_const T) (List.find (fn f =>
921+
T = domain_type (fastype_of f)
922+
) free_fs') end) RVrs @ map (fn Inj =>
911923
HOLogic.mk_comp (Inj, the (List.find (fn f => (op=) (apply2 (domain_type o fastype_of) (Inj, f))) fs))
912924
) Injs)
913925
) end
@@ -921,14 +933,13 @@ fun mk_mrsbnf_axioms mrbnfs bmv lthy =
921933
||>> apfst hd o mk_Frees "x" [body_type (fastype_of Sb)];
922934

923935
val live = MRBNF_Def.live_of_mrbnf mrbnf;
924-
val pfree_fs = drop free free_fs';
925936
val other_prems = flat (MRBNF_Def.interlace (replicate live [])
926937
(map (fn f => map HOLogic.mk_Trueprop [mk_bij f, mk_supp_bound f]) bound_fs)
927-
(replicate free [] @ map (fn f => [HOLogic.mk_Trueprop (mk_supp_bound f)]) pfree_fs)
938+
(replicate (length free_fs') [])
928939
var_types
929940
);
930941
val other_fs = flat (MRBNF_Def.interlace (map single live_fs) (map single bound_fs)
931-
(replicate free [] @ map single pfree_fs) var_types);
942+
(replicate (length free_fs') []) var_types);
932943

933944
val g_prems = map2 (fn Inj => fn g => HOLogic.mk_Trueprop (uncurry mk_ordLess (
934945
mk_card_of (mk_SSupp Inj $ g), mk_card_of (HOLogic.mk_UNIV (domain_type (fastype_of g)))
@@ -938,7 +949,7 @@ fun mk_mrsbnf_axioms mrbnfs bmv lthy =
938949
val count = live + MRBNF_Def.free_of_mrbnf mrbnf + MRBNF_Def.bound_of_mrbnf mrbnf;
939950
val map_Sb = if count - free = 0 then NONE else
940951
let
941-
val map_t = Term.list_comb (mapx, MRBNF_Def.interlace live_fs bound_fs (map HOLogic.id_const frees @ pfree_fs) var_types);
952+
val map_t = Term.list_comb (mapx, MRBNF_Def.interlace live_fs bound_fs (map (HOLogic.id_const o domain_type o fastype_of) free_fs') var_types);
942953
val Sb_t = Term.list_comb (Sb, hs @ gs);
943954
in SOME (fold_rev Logic.all (other_fs @ hs @ gs) (fold_rev (curry Logic.mk_implies) (other_prems @ h_prems @ g_prems) (mk_Trueprop_eq (
944955
HOLogic.mk_comp (map_t, Sb_t),
@@ -966,7 +977,7 @@ fun mk_mrsbnf_axioms mrbnfs bmv lthy =
966977
val set_Sbs =
967978
let
968979
val sets' = flat (MRBNF_Def.interlace (map single live_sets) (map single bound_sets)
969-
(replicate free [] @ map single (drop free free_sets)) var_types);
980+
(replicate (length free_fs') []) var_types);
970981
in map (fn set => fold_rev Logic.all (hs @ gs @ [x]) (fold_rev (curry Logic.mk_implies) (h_prems @ g_prems) (
971982
mk_Trueprop_eq (set $ (Term.list_comb (Sb, hs @ gs) $ x), foldl1 mk_Un ((set $ x) ::
972983
@{map_filter 2} (fn Vrs => fn g => Option.mapPartial (fn mrbnf => Option.map (fn set =>

case_studies/POPLmark/POPLmark_2B.thy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ theory POPLmark_2B
22
imports Pattern "HOL-Library.List_Lexorder" "HOL-Library.Char_ord"
33
begin
44

5+
declare [[ML_print_depth=1000]]
56
binder_datatype (FTVars: 'tv, FVars: 'v) trm =
67
Var 'v
78
| Abs x::'v "'tv typ" t::"('tv, 'v) trm" binds x in t

operations/BMV_Composition.thy

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,7 @@ consts Inj_2_T4 :: "'b \<Rightarrow> ('a::var, 'b::var) T4"
4949

5050
ML_file \<open>../Tools/bmv_monad_def.ML\<close>
5151

52-
ML \<open>
53-
Multithreading.parallel_proofs := 0
54-
\<close>
55-
5652
declare [[goals_limit=1000]]
57-
5853
declare [[ML_print_depth=1000]]
5954

6055
pbmv_monad "('a, 'b, 'c, 'd, 'e, 'f, 'g) T1"
@@ -800,4 +795,4 @@ let
800795
in lthy end
801796
\<close>
802797

803-
end
798+
end

operations/BMV_Fixpoint.thy

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ type_synonym ('tv, 'v, 'btv, 'bv, 'c, 'd) FTerm_pre' =
1212

1313
ML_file \<open>../Tools/mrsbnf_comp.ML\<close>
1414

15-
ML \<open>
16-
Multithreading.parallel_proofs := 0
17-
\<close>
18-
1915
local_setup \<open>fn lthy =>
2016
let
2117
val T = @{typ "('tv, 'v, 'btv, 'bv, 'c, 'd) FTerm_pre'"};
@@ -1570,4 +1566,4 @@ val x = TVSubst.create_tvsubst_of_mrsbnf
15701566
15711567
in lthy end\<close>
15721568

1573-
end
1569+
end

operations/BMV_Monad.thy

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,6 @@ axiomatization Sb_LM :: "('a::var \<Rightarrow> 'a) \<Rightarrow> ('a \<Rightarr
170170
and Sb_LM_simp3[simp]: "Sb_LM f1 f2 (App t1 t2) = App (Sb_LM f1 f2 t1) (Sb_LM f1 f2 t2)"
171171
and Sb_LM_simp4[simp]: "x \<notin> imsupp f1 \<Longrightarrow> x \<notin> IImsupp_LM f2 \<Longrightarrow> Sb_LM f1 f2 (Lam x t) = Lam x (Sb_LM f1 f2 t)"
172172

173-
ML \<open>
174-
Multithreading.parallel_proofs := 0
175-
\<close>
176-
177173
lemma Vrs_Un: "FVars_LM t = Vrs_1 t \<union> Vrs_2 t"
178174
apply (induction t rule: LM.induct)
179175
apply auto

tests/Regression_Tests.thy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ binder_datatype 'a LLC =
1414
| Abs x::'a t::"'a LLC" binds x in t
1515
| Let "(x::'a, t::'a LLC) alist" u::"'a LLC" binds x in t u
1616

17+
declare [[ML_print_depth=1000]]
1718
(* #70 *)
1819
datatype ('tv, 'ev, 'rv) type = Type 'tv 'ev 'rv
1920
binder_datatype ('tv, 'ev, 'rv) type_scheme =

0 commit comments

Comments
 (0)