74 | mk_inst (Type (_, tys1)) (Type (_, tys2)) = fold2 mk_inst tys1 tys2; |
74 | mk_inst (Type (_, tys1)) (Type (_, tys2)) = fold2 mk_inst tys1 tys2; |
75 fun of_sort_deriv (ty, sort) = |
75 fun of_sort_deriv (ty, sort) = |
76 Sorts.of_sort_derivation (Sign.pp thy) algebra |
76 Sorts.of_sort_derivation (Sign.pp thy) algebra |
77 { class_relation = class_relation, type_constructor = type_constructor, |
77 { class_relation = class_relation, type_constructor = type_constructor, |
78 type_variable = type_variable } |
78 type_variable = type_variable } |
79 (ty, sort) |
79 (ty, sort) handle Sorts.CLASS_ERROR _ => [] (*permissive!*) |
80 in |
80 in |
81 flat (maps of_sort_deriv (fold2 mk_inst tys tys_decl [])) |
81 flat (maps of_sort_deriv (fold2 mk_inst tys tys_decl [])) |
82 end; |
82 end; |
83 |
83 |
84 fun drop_classes thy tfrees thm = |
84 fun drop_classes thy tfrees thm = |
108 | resort_thms algebra tap_typ (thms as thm :: _) = |
108 | resort_thms algebra tap_typ (thms as thm :: _) = |
109 let |
109 let |
110 val thy = Thm.theory_of_thm thm; |
110 val thy = Thm.theory_of_thm thm; |
111 val pp = Sign.pp thy; |
111 val pp = Sign.pp thy; |
112 val cs = fold_consts (insert (op =)) thms []; |
112 val cs = fold_consts (insert (op =)) thms []; |
113 fun class_error (c, ty_decl) e = |
|
114 error ; |
|
115 fun match_const c (ty, ty_decl) = |
113 fun match_const c (ty, ty_decl) = |
116 let |
114 let |
117 val tys = Sign.const_typargs thy (c, ty); |
115 val tys = Sign.const_typargs thy (c, ty); |
118 val sorts = map (snd o dest_TVar) (Sign.const_typargs thy (c, ty_decl)); |
116 val sorts = map (snd o dest_TVar) (Sign.const_typargs thy (c, ty_decl)); |
119 in fn tab => fold2 (Type.typ_of_sort algebra) tys sorts tab |
117 in fn tab => fold2 (curry (Sorts.meet_sort algebra)) tys sorts tab |
120 handle Sorts.CLASS_ERROR e => raise CLASS_ERROR ([c], Sorts.msg_class_error pp e ^ ",\n" |
118 handle Sorts.CLASS_ERROR e => raise CLASS_ERROR ([c], Sorts.msg_class_error pp e ^ ",\n" |
121 ^ "for constant " ^ CodeUnit.string_of_const thy c |
119 ^ "for constant " ^ CodeUnit.string_of_const thy c |
122 ^ "\nin defining equations(s)\n" |
120 ^ "\nin defining equations(s)\n" |
123 ^ (cat_lines o map string_of_thm) thms) |
121 ^ (cat_lines o map string_of_thm) thms) |
|
122 (*handle Sorts.CLASS_ERROR _ => tab (*permissive!*)*) |
124 end; |
123 end; |
125 fun match (c, ty) = |
124 fun match (c, ty) = case tap_typ c |
126 case tap_typ c |
|
127 of SOME ty_decl => match_const c (ty, ty_decl) |
125 of SOME ty_decl => match_const c (ty, ty_decl) |
128 | NONE => I; |
126 | NONE => I; |
129 val tvars = fold match cs Vartab.empty; |
127 val tvars = fold match cs Vartab.empty; |
130 in map (CodeUnit.inst_thm tvars) thms end; |
128 in map (CodeUnit.inst_thm tvars) thms end; |
131 |
129 |
159 fun instances_of thy algebra insts = |
157 fun instances_of thy algebra insts = |
160 let |
158 let |
161 val thy_classes = (#classes o Sorts.rep_algebra o Sign.classes_of) thy; |
159 val thy_classes = (#classes o Sorts.rep_algebra o Sign.classes_of) thy; |
162 fun all_classparams tyco class = |
160 fun all_classparams tyco class = |
163 these (try (#params o AxClass.get_info thy) class) |
161 these (try (#params o AxClass.get_info thy) class) |
164 |> map (fn (c, _) => AxClass.param_of_inst thy (c, tyco)) |
162 |> map_filter (fn (c, _) => try (AxClass.param_of_inst thy) (c, tyco)) |
165 in |
163 in |
166 Symtab.empty |
164 Symtab.empty |
167 |> fold (fn (tyco, class) => |
165 |> fold (fn (tyco, class) => |
168 Symtab.map_default (tyco, []) (insert (op =) class)) insts |
166 Symtab.map_default (tyco, []) (insert (op =) class)) insts |
169 |> (fn tab => Symtab.fold (fn (tyco, classes) => append (maps (all_classparams tyco) |
167 |> (fn tab => Symtab.fold (fn (tyco, classes) => append (maps (all_classparams tyco) |
171 end; |
169 end; |
172 |
170 |
173 fun instances_of_consts thy algebra funcgr consts = |
171 fun instances_of_consts thy algebra funcgr consts = |
174 let |
172 let |
175 fun inst (cexpr as (c, ty)) = insts_of thy algebra c |
173 fun inst (cexpr as (c, ty)) = insts_of thy algebra c |
176 ((fst o Graph.get_node funcgr) c) ty handle Sorts.CLASS_ERROR _ => []; |
174 ((fst o Graph.get_node funcgr) c) ty; |
177 in |
175 in |
178 [] |
176 [] |
179 |> fold (fold (insert (op =)) o inst) consts |
177 |> fold (fold (insert (op =)) o inst) consts |
180 |> instances_of thy algebra |
178 |> instances_of thy algebra |
181 end; |
179 end; |