125 |
125 |
126 (* code generator basics *) |
126 (* code generator basics *) |
127 |
127 |
128 type deftab = (typ * thm) list Symtab.table; |
128 type deftab = (typ * thm) list Symtab.table; |
129 |
129 |
130 fun is_overloaded thy c = case Defs.specifications_of (Theory.defs_of thy) c |
130 fun is_overloaded thy c = case Theory.definitions_of thy c |
131 of [] => true |
131 of [] => true (* FIXME false (!?) *) |
132 | [(ty, _)] => not (Sign.typ_equiv thy (ty, Sign.the_const_type thy c)) |
132 | [{lhs = ty, ...}] => not (Sign.typ_equiv thy (ty, Sign.the_const_type thy c)) |
133 | _ => true; |
133 | _ => true; |
134 |
134 |
135 structure InstNameMangler = NameManglerFun ( |
135 structure InstNameMangler = NameManglerFun ( |
136 type ctxt = theory; |
136 type ctxt = theory; |
137 type src = string * (class * string); |
137 type src = string * (class * string); |
150 val ord = prod_ord string_ord Term.typ_ord; |
150 val ord = prod_ord string_ord Term.typ_ord; |
151 fun mk thy ((c, ty), i) = |
151 fun mk thy ((c, ty), i) = |
152 let |
152 let |
153 val c' = idf_of_name thy nsp_overl c; |
153 val c' = idf_of_name thy nsp_overl c; |
154 val prefix = |
154 val prefix = |
155 case (AList.lookup (Sign.typ_equiv thy) |
155 case (find_first (fn {lhs, ...} => Sign.typ_equiv thy (ty, lhs)) |
156 (Defs.specifications_of (Theory.defs_of thy) c)) ty |
156 (Theory.definitions_of thy c)) |
157 of SOME (_, thyname) => NameSpace.append thyname nsp_overl |
157 of SOME {module, ...} => NameSpace.append module nsp_overl |
158 | NONE => if c = "op =" |
158 | NONE => if c = "op =" |
159 then |
159 then |
160 NameSpace.append |
160 NameSpace.append |
161 (((fn s => Codegen.thyname_of_type s thy) o fst o dest_Type o hd o fst o strip_type) ty) |
161 (((fn s => Codegen.thyname_of_type s thy) o fst o dest_Type o hd o fst o strip_type) ty) |
162 nsp_overl |
162 nsp_overl |
928 fun mk_overltabs thy = |
928 fun mk_overltabs thy = |
929 (Symtab.empty, ConstNameMangler.empty) |
929 (Symtab.empty, ConstNameMangler.empty) |
930 |> Symtab.fold |
930 |> Symtab.fold |
931 (fn (c, _) => |
931 (fn (c, _) => |
932 let |
932 let |
933 val deftab = Defs.specifications_of (Theory.defs_of thy) c |
933 val deftab = Theory.definitions_of thy c |
934 val is_overl = (is_none o ClassPackage.lookup_const_class thy) c |
934 val is_overl = (is_none o ClassPackage.lookup_const_class thy) c |
935 andalso case deftab |
935 andalso case deftab (* is_overloaded (!?) *) |
936 of [] => false |
936 of [] => false |
937 | [(ty, _)] => not (Sign.typ_equiv thy (ty, Sign.the_const_type thy c)) |
937 | [{lhs = ty, ...}] => not (Sign.typ_equiv thy (ty, Sign.the_const_type thy c)) |
938 | _ => true; |
938 | _ => true; |
939 in if is_overl then (fn (overltab1, overltab2) => ( |
939 in if is_overl then (fn (overltab1, overltab2) => ( |
940 overltab1 |
940 overltab1 |
941 |> Symtab.update_new (c, map fst deftab), |
941 |> Symtab.update_new (c, map #lhs deftab), |
942 overltab2 |
942 overltab2 |
943 |> fold_map (fn (ty, _) => ConstNameMangler.declare thy (c, ty)) deftab |
943 |> fold_map (fn {lhs = ty, ...} => ConstNameMangler.declare thy (c, ty)) deftab |
944 |-> (fn _ => I))) else I |
944 |-> (fn _ => I))) else I |
945 end) ((#2 o #constants o Consts.dest o #consts o Sign.rep_sg) thy) |
945 end) ((#2 o #constants o Consts.dest o #consts o Sign.rep_sg) thy) |
946 |> (fn (overltab1, overltab2) => |
946 |> (fn (overltab1, overltab2) => |
947 let |
947 let |
948 val c = "op ="; |
948 val c = "op ="; |