Skip to content

Commit be05f2b

Browse files
committed
Fix killing of bound/free position of outer mrbnf in composition
1 parent 2acc34f commit be05f2b

File tree

3 files changed

+29
-36
lines changed

3 files changed

+29
-36
lines changed

Tools/mrbnf_comp.ML

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ sig
3131
MRBNF_Def.mrbnf list -> unfold_set * local_theory -> MRBNF_Def.mrbnf * (unfold_set * local_theory)
3232
val permute_mrbnf: (binding -> binding) -> int list -> int list -> MRBNF_Def.mrbnf ->
3333
(comp_cache * unfold_set) * local_theory -> MRBNF_Def.mrbnf * ((comp_cache * unfold_set) * local_theory)
34-
val normalize_mrbnfs: (int -> binding -> binding) -> (string * sort) option list ->
35-
(string * sort) list list -> (string * sort) list -> ((string * sort) * MRBNF_Def.var_type) list ->
34+
val normalize_mrbnfs: (int -> binding -> binding) -> typ option list ->
35+
(string * sort) list list -> typ list -> ((string * sort) * MRBNF_Def.var_type) list ->
3636
((string * sort) list list ->
3737
(string * sort) list) -> MRBNF_Def.mrbnf option -> MRBNF_Def.mrbnf list -> (comp_cache * unfold_set) * local_theory ->
38-
(int list list * (string * sort) list) * MRBNF_Def.mrbnf option * (MRBNF_Def.mrbnf list * ((comp_cache * unfold_set) * local_theory))
38+
(int list * int list list * (string * sort) list) * MRBNF_Def.mrbnf option * (MRBNF_Def.mrbnf list * ((comp_cache * unfold_set) * local_theory))
3939
val compose_mrbnf: MRBNF_Def.inline_policy -> (int -> binding -> binding) ->
4040
((string * sort) list list -> (string * sort) list) -> MRBNF_Def.mrbnf -> MRBNF_Def.mrbnf list ->
4141
typ list -> typ list list -> typ option list -> typ list list ->
@@ -1080,49 +1080,45 @@ fun lift_and_permute_mrbnf qualify ns src dest mrbnf =
10801080
lift_mrbnf qualify ns mrbnf
10811081
#> uncurry (permute_mrbnf qualify src dest);
10821082

1083-
fun tfree_ord ((a, S), (b, S')) = case fast_string_ord (a, b) of
1084-
EQUAL => Term_Ord.sort_ord (S, S')
1085-
| ord => ord
1086-
structure Vars = Table(type key = (string * sort) val ord = tfree_ord);
1087-
1088-
fun map_option f (SOME x) = SOME (f x)
1089-
| map_option _ NONE = NONE
1090-
1091-
fun normalize_mrbnfs qualify oAs Ass Ds Xs flatten_tyargs outer_opt mrbnfs accum =
1083+
fun normalize_mrbnfs qualify (oAs: typ option list) (Ass: (string * sort) list list) (Ds: typ list) Xs flatten_tyargs outer_opt mrbnfs accum =
10921084
let
10931085
val oAs' = case outer_opt of
10941086
NONE => []
10951087
| SOME outer => map_filter (fn (x, var_type) => case x of
1096-
SOME y => SOME (y, var_type)
1088+
SOME y => (case try dest_TFree y of
1089+
SOME _ => SOME (y, var_type)
1090+
| NONE => SOME (y, MRBNF_Def.Dead_Var)
1091+
)
10971092
| NONE => NONE
10981093
) (oAs ~~ var_types_of_mrbnf outer);
1099-
val var_map = fold (fold (fn (A, var_type) => Vars.map_default (A, var_type) (fn var_type' =>
1094+
val var_map = fold (fold (fn (A, var_type) => Typtab.map_default (A, var_type) (fn var_type' =>
11001095
case var_type_ord (var_type, var_type') of LESS => var_type | _ => var_type'
1101-
))) (oAs' :: Xs :: map2 (fn As => fn mrbnf => As ~~ var_types_of_mrbnf mrbnf) Ass mrbnfs)
1102-
(Vars.make (Ds ~~ replicate (length Ds) MRBNF_Def.Dead_Var))
1096+
))) (oAs' :: map (apfst TFree) Xs :: map2 (fn As => fn mrbnf => map TFree As ~~ var_types_of_mrbnf mrbnf) Ass mrbnfs)
1097+
(Typtab.make (Ds ~~ replicate (length Ds) MRBNF_Def.Dead_Var))
11031098

11041099
val odemote_target_types = map (fn x => case x of
1105-
SOME y => the (Vars.lookup var_map y)
1100+
SOME y => the (Typtab.lookup var_map y)
11061101
| NONE => Live_Var
11071102
) oAs;
11081103
val (outer_opt', accum') = case outer_opt of
11091104
NONE => (outer_opt, accum)
11101105
| SOME outer => apfst SOME (demote_mrbnf (qualify 0) odemote_target_types outer accum)
11111106

1112-
val demote_target_types = map (map_filter (fn A => Vars.lookup var_map A)) Ass
1107+
val demote_target_types = map (map_filter (fn A => Typtab.lookup var_map (TFree A))) Ass
11131108
val (inners', accum'') = @{fold_map 3} (demote_mrbnf o qualify)
11141109
(1 upto length mrbnfs) demote_target_types mrbnfs accum'
11151110

11161111
val oAs2 = filter (fn SOME A =>
1117-
let val var_type = the (Vars.lookup var_map A)
1112+
let val var_type = the (Typtab.lookup var_map A)
11181113
in case var_type of
11191114
Dead_Var => false | _ => true
11201115
end
11211116
| NONE => true) oAs
1122-
fun map_filter_killed f = map_filter (fn A => case Vars.lookup var_map A of
1117+
fun map_filter_killed f = map_filter (fn A => case Typtab.lookup var_map (TFree A) of
11231118
SOME Dead_Var => NONE | SOME x => SOME (f A x) | NONE => SOME (f A Live_Var)
11241119
);
1125-
val As = map_filter_killed pair (flatten_tyargs (map fst oAs' :: Ass))
1120+
val oAs'' = map_filter (try dest_TFree o fst) oAs';
1121+
val As = map_filter_killed pair (flatten_tyargs (oAs'' :: Ass))
11261122
fun get_lifts vars = sort (fn (a, b) => var_type_ord (snd b, snd a)) (subtract (fn (a, b) => a = fst b) vars As)
11271123
fun count_lifts As = fold (fn (_, var_type) => fn (n1, n2, n3) => case var_type of
11281124
MRBNF_Def.Live_Var => (n1 + 1, n2, n3)
@@ -1132,11 +1128,11 @@ fun normalize_mrbnfs qualify oAs Ass Ds Xs flatten_tyargs outer_opt mrbnfs accum
11321128
) As (0, 0, 0)
11331129

11341130
val Ass' = map (map_filter_killed K) Ass
1135-
val kill_poss = map (map_filter I o map_index (fn (i, A) => case the (Vars.lookup var_map A) of
1131+
val kill_poss = map (map_filter I o map_index (fn (i, A) => case the (Typtab.lookup var_map (TFree A)) of
11361132
Dead_Var => SOME i | _ => NONE
11371133
)) Ass
11381134
val new_oDs = map_filter I (map_index (fn (i, x) => case x of
1139-
SOME y => (case the (Vars.lookup var_map y) of
1135+
SOME y => (case the (Typtab.lookup var_map y) of
11401136
Dead_Var => SOME i | _ => NONE)
11411137
| _ => NONE
11421138
) oAs)
@@ -1146,12 +1142,12 @@ fun normalize_mrbnfs qualify oAs Ass Ds Xs flatten_tyargs outer_opt mrbnfs accum
11461142
val find_indices' = map (fn x => find_index (equal x) (map fst As))
11471143
val srcs = map find_indices' (map2 (append o map fst) need_liftss Ass')
11481144

1149-
val oneed_lifts = filter (fn (_, var_type) => var_type <> MRBNF_Def.Live_Var) (get_lifts (map fst oAs'))
1145+
val oneed_lifts = filter (fn (_, var_type) => var_type <> MRBNF_Def.Live_Var) (get_lifts oAs'')
11501146
val olift_ns = count_lifts oneed_lifts
11511147
val odest = 0 upto length oAs2 + length oneed_lifts - 1
11521148
val osrc =
11531149
let
1154-
val vars = map (SOME o fst) oneed_lifts @ oAs2
1150+
val vars = map (SOME o fst) oneed_lifts @ map (Option.map dest_TFree) oAs2
11551151
val As' = map_filter (fn (var, var_type) =>
11561152
if var_type = MRBNF_Def.Free_Var orelse var_type = MRBNF_Def.Bound_Var then
11571153
SOME var
@@ -1168,7 +1164,7 @@ fun normalize_mrbnfs qualify oAs Ass Ds Xs flatten_tyargs outer_opt mrbnfs accum
11681164
NONE => (outer_opt', accum'')
11691165
| SOME outer' => apfst SOME (lift_and_permute_mrbnf (qualify (length mrbnfs)) olift_ns osrc odest outer' accum'')
11701166
in
1171-
((kill_poss, map fst As),
1167+
((new_oDs, kill_poss, map fst As),
11721168
outer_opt'',
11731169
@{fold_map 5} (lift_and_permute_mrbnf o qualify)
11741170
(if length mrbnfs = 1 then [0] else 1 upto length mrbnfs)
@@ -1179,12 +1175,12 @@ fun raw_compose_mrbnf const_policy qualify flatten_tyargs outer inners oDs Dss o
11791175
let
11801176
val b = name_of_mrbnf outer;
11811177
val Ass = map (map Term.dest_TFree) tfreess;
1182-
val oAs = map (map_option Term.dest_TFree) otfrees
1183-
val Ds = fold (fold Term.add_tfreesT) (oDs :: Dss) [];
1184-
val ((kill_poss, As), outer', (inners', ((cache', unfold_set'), lthy'))) =
1178+
val oAs = otfrees
1179+
val Ds = distinct (op=) (flat (oDs :: Dss));
1180+
val ((okill_pos, kill_poss, As), outer', (inners', ((cache', unfold_set'), lthy'))) =
11851181
normalize_mrbnfs qualify oAs Ass Ds Xs flatten_tyargs (SOME outer) inners accum;
1186-
val Ds =
1187-
oDs @ flat (@{map 3} (uncurry append oo curry swap oo map o nth) tfreess kill_poss Dss);
1182+
val Ds = (oDs @ map (the o nth oAs) okill_pos)
1183+
@ flat (@{map 3} (uncurry append oo curry swap oo map o nth) tfreess kill_poss Dss);
11881184
val As = map TFree As;
11891185
in
11901186
apfst (rpair (Ds, As))

thys/POPLmark/Labeled_FSet.thy

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,11 @@ lemma nonrep_lfset_alt:
1313
unfolding nonrep_lfset_def prod_set_defs by fastforce
1414

1515
typedef ('a, 'b) G = "UNIV :: ('a \<times> 'b) fset set" by auto
16+
1617
setup_lifting type_definition_G
1718
context notes [[bnf_internals]] begin
1819
copy_bnf ('a, 'b) G
1920
end
20-
(*lemma map_G_transfer[transfer_rule]:
21-
"rel_fun (=) (rel_fun (=) (rel_fun (pcr_G (=) (=)) (pcr_G (=) (=)))) (\<lambda>f g. (|`|) (map_prod f g)) map_G"
22-
by (tactic \<open>Local_Defs.unfold_tac @{context}
23-
[BNF_Def.bnf_of @{context} @{type_name G} |> the |> BNF_Def.map_def_of_bnf]\<close>)
24-
(simp add: rel_fun_def pcr_G_def cr_G_def prod.rel_eq fset.rel_eq relcompp_apply Abs_G_inverse)*)
2521

2622
lift_definition nonrep_G :: "('a, 'b) G \<Rightarrow> bool" is nonrep_lfset .
2723

thys/POPLmark/SystemFSub.thy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ binder_datatype 'a "typ" =
1717
TyVar 'a
1818
| Top
1919
| Fun "'a typ" "'a typ"
20+
| Rec "(string, 'a typ) lfset"
2021
| Forall \<alpha>::'a "'a typ" t::"'a typ" binds \<alpha> in t
2122

2223
declare supp_swap_bound[OF cinfinite_imp_infinite[OF typ.UNIV_cinfinite], simp]

0 commit comments

Comments
 (0)