26 fun reindex_desc desc = |
26 fun reindex_desc desc = |
27 let |
27 let |
28 val kks = map fst desc; |
28 val kks = map fst desc; |
29 val perm_kks = sort int_ord kks; |
29 val perm_kks = sort int_ord kks; |
30 |
30 |
31 fun perm_dtyp (Datatype_Aux.DtType (s, Ds)) = Datatype_Aux.DtType (s, map perm_dtyp Ds) |
31 fun perm_dtyp (Old_Datatype_Aux.DtType (s, Ds)) = Old_Datatype_Aux.DtType (s, map perm_dtyp Ds) |
32 | perm_dtyp (Datatype_Aux.DtRec kk) = Datatype_Aux.DtRec (find_index (curry (op =) kk) kks) |
32 | perm_dtyp (Old_Datatype_Aux.DtRec kk) = |
|
33 Old_Datatype_Aux.DtRec (find_index (curry (op =) kk) kks) |
33 | perm_dtyp D = D |
34 | perm_dtyp D = D |
34 in |
35 in |
35 if perm_kks = kks then |
36 if perm_kks = kks then |
36 desc |
37 desc |
37 else |
38 else |
66 val As = map2 (fn s => fn TVar (_, S) => TFree (s, S)) As_names var_As; |
67 val As = map2 (fn s => fn TVar (_, S) => TFree (s, S)) As_names var_As; |
67 val fpTs = map (fn s => Type (s, As)) fpT_names; |
68 val fpTs = map (fn s => Type (s, As)) fpT_names; |
68 |
69 |
69 val nn_fp = length fpTs; |
70 val nn_fp = length fpTs; |
70 |
71 |
71 val mk_dtyp = Datatype_Aux.dtyp_of_typ (map (apsnd (map Term.dest_TFree) o dest_Type) fpTs); |
72 val mk_dtyp = Old_Datatype_Aux.dtyp_of_typ (map (apsnd (map Term.dest_TFree) o dest_Type) fpTs); |
72 |
73 |
73 fun mk_ctr_descr Ts = mk_ctr Ts #> dest_Const ##> (binder_types #> map mk_dtyp); |
74 fun mk_ctr_descr Ts = mk_ctr Ts #> dest_Const ##> (binder_types #> map mk_dtyp); |
74 fun mk_typ_descr index (Type (T_name, Ts)) ({ctrs, ...} : ctr_sugar) = |
75 fun mk_typ_descr index (Type (T_name, Ts)) ({ctrs, ...} : ctr_sugar) = |
75 (index, (T_name, map mk_dtyp Ts, map (mk_ctr_descr Ts) ctrs)); |
76 (index, (T_name, map mk_dtyp Ts, map (mk_ctr_descr Ts) ctrs)); |
76 |
77 |
77 val fp_ctr_sugars = map (#ctr_sugar o lfp_sugar_of) fpT_names; |
78 val fp_ctr_sugars = map (#ctr_sugar o lfp_sugar_of) fpT_names; |
78 val orig_descr = map3 mk_typ_descr (0 upto nn_fp - 1) fpTs fp_ctr_sugars; |
79 val orig_descr = map3 mk_typ_descr (0 upto nn_fp - 1) fpTs fp_ctr_sugars; |
79 val all_infos = Datatype_Data.get_all thy; |
80 val all_infos = Old_Datatype_Data.get_all thy; |
80 val (orig_descr' :: nested_descrs, _) = |
81 val (orig_descr' :: nested_descrs, _) = |
81 Datatype_Aux.unfold_datatypes lthy orig_descr all_infos orig_descr nn_fp; |
82 Old_Datatype_Aux.unfold_datatypes lthy orig_descr all_infos orig_descr nn_fp; |
82 |
83 |
83 fun cliquify_descr [] = [] |
84 fun cliquify_descr [] = [] |
84 | cliquify_descr [entry] = [[entry]] |
85 | cliquify_descr [entry] = [[entry]] |
85 | cliquify_descr (full_descr as (_, (T_name1, _, _)) :: _) = |
86 | cliquify_descr (full_descr as (_, (T_name1, _, _)) :: _) = |
86 let |
87 let |
88 if member (op =) fpT_names T_name1 then |
89 if member (op =) fpT_names T_name1 then |
89 nn_fp |
90 nn_fp |
90 else |
91 else |
91 (case Symtab.lookup all_infos T_name1 of |
92 (case Symtab.lookup all_infos T_name1 of |
92 SOME {descr, ...} => |
93 SOME {descr, ...} => |
93 length (filter_out (exists Datatype_Aux.is_rec_type o #2 o snd) descr) |
94 length (filter_out (exists Old_Datatype_Aux.is_rec_type o #2 o snd) descr) |
94 | NONE => raise Fail "unknown old-style datatype"); |
95 | NONE => raise Fail "unknown old-style datatype"); |
95 in |
96 in |
96 chop nn full_descr ||> cliquify_descr |> op :: |
97 chop nn full_descr ||> cliquify_descr |> op :: |
97 end; |
98 end; |
98 |
99 |
100 val descrs = burrow reindex_desc (orig_descr' :: rev nested_descrs); |
101 val descrs = burrow reindex_desc (orig_descr' :: rev nested_descrs); |
101 val (cliques, descr) = |
102 val (cliques, descr) = |
102 split_list (flat (map_index (fn (i, descr) => map (pair i) descr) |
103 split_list (flat (map_index (fn (i, descr) => map (pair i) descr) |
103 (maps cliquify_descr descrs))); |
104 (maps cliquify_descr descrs))); |
104 |
105 |
105 val dest_dtyp = Datatype_Aux.typ_of_dtyp descr; |
106 val dest_dtyp = Old_Datatype_Aux.typ_of_dtyp descr; |
106 |
107 |
107 val Ts = Datatype_Aux.get_rec_types descr; |
108 val Ts = Old_Datatype_Aux.get_rec_types descr; |
108 val nn = length Ts; |
109 val nn = length Ts; |
109 |
110 |
110 val fp_sugars0 = map (lfp_sugar_of o fst o dest_Type) Ts; |
111 val fp_sugars0 = map (lfp_sugar_of o fst o dest_Type) Ts; |
111 val ctr_Tsss = map (map (map dest_dtyp o snd) o #3 o snd) descr; |
112 val ctr_Tsss = map (map (map dest_dtyp o snd) o #3 o snd) descr; |
112 val kkssss = |
113 val kkssss = |
113 map (map (map (fn Datatype_Aux.DtRec kk => [kk] | _ => []) o snd) o #3 o snd) descr; |
114 map (map (map (fn Old_Datatype_Aux.DtRec kk => [kk] | _ => []) o snd) o #3 o snd) descr; |
114 |
115 |
115 val callers = map (fn kk => Var ((Name.uu, kk), @{typ "unit => unit"})) (0 upto nn - 1); |
116 val callers = map (fn kk => Var ((Name.uu, kk), @{typ "unit => unit"})) (0 upto nn - 1); |
116 |
117 |
117 fun apply_comps n kk = |
118 fun apply_comps n kk = |
118 mk_partial_compN n (replicate n HOLogic.unitT ---> HOLogic.unitT) (nth callers kk); |
119 mk_partial_compN n (replicate n HOLogic.unitT ---> HOLogic.unitT) (nth callers kk); |
173 in |
174 in |
174 common_notes @ notes |
175 common_notes @ notes |
175 end); |
176 end); |
176 |
177 |
177 val register_interpret = |
178 val register_interpret = |
178 Datatype_Data.register infos |
179 Old_Datatype_Data.register infos |
179 #> Datatype_Data.interpretation_data (Datatype_Aux.default_config, map fst infos) |
180 #> Old_Datatype_Data.interpretation_data (Old_Datatype_Aux.default_config, map fst infos) |
180 in |
181 in |
181 lthy |
182 lthy |
182 |> Local_Theory.raw_theory register_interpret |
183 |> Local_Theory.raw_theory register_interpret |
183 |> Local_Theory.notes all_notes |
184 |> Local_Theory.notes all_notes |
184 |> snd |
185 |> snd |