author | haftmann |
Fri, 26 Jan 2007 13:59:04 +0100 | |
changeset 22197 | 461130ccfef4 |
parent 22183 | 0e6c0aeb04ec |
child 22400 | cb0b1bbf7e91 |
permissions | -rw-r--r-- |
20387 | 1 |
(* Title: Pure/Tools/codegen_consts.ML |
2 |
ID: $Id$ |
|
3 |
Author: Florian Haftmann, TU Muenchen |
|
4 |
||
20855 | 5 |
Identifying constants by name plus normalized type instantantiation schemes. |
22197 | 6 |
Convenient data structures for constants. Auxiliary. |
20387 | 7 |
*) |
8 |
||
9 |
signature CODEGEN_CONSTS = |
|
10 |
sig |
|
11 |
type const = string * typ list (*type instantiations*) |
|
12 |
val const_ord: const * const -> order |
|
13 |
val eq_const: const * const -> bool |
|
14 |
structure Consttab: TABLE |
|
20600 | 15 |
val inst_of_typ: theory -> string * typ -> const |
16 |
val typ_of_inst: theory -> const -> string * typ |
|
17 |
val norm: theory -> const -> const |
|
18 |
val norm_of_typ: theory -> string * typ -> const |
|
22197 | 19 |
val typ_sort_inst: Sorts.algebra -> typ * sort -> sort Vartab.table -> sort Vartab.table |
20 |
val typargs: theory -> string * typ -> typ list |
|
22183 | 21 |
val find_def: theory -> const -> (string (*theory name*) * thm) option |
21118 | 22 |
val consts_of: theory -> term -> const list |
20387 | 23 |
val read_const: theory -> string -> const |
24 |
val string_of_const: theory -> const -> string |
|
20600 | 25 |
val raw_string_of_const: const -> string |
20387 | 26 |
val string_of_const_typ: theory -> string * typ -> string |
22197 | 27 |
val string_of_typ: theory -> typ -> string |
20387 | 28 |
end; |
29 |
||
30 |
structure CodegenConsts: CODEGEN_CONSTS = |
|
31 |
struct |
|
32 |
||
33 |
||
34 |
(* basic data structures *) |
|
35 |
||
36 |
type const = string * typ list (*type instantiations*); |
|
37 |
val const_ord = prod_ord fast_string_ord (list_ord Term.typ_ord); |
|
38 |
val eq_const = is_equal o const_ord; |
|
39 |
||
40 |
structure Consttab = |
|
41 |
TableFun( |
|
42 |
type key = string * typ list; |
|
43 |
val ord = const_ord; |
|
44 |
); |
|
45 |
||
46 |
||
21463
42dd50268c8b
completed class parameter handling in axclass.ML
haftmann
parents:
21118
diff
changeset
|
47 |
(* type instantiations, overloading, dictionary values *) |
20387 | 48 |
|
22197 | 49 |
fun string_of_typ thy = setmp show_sorts true (Sign.string_of_typ thy); |
50 |
||
20600 | 51 |
fun inst_of_typ thy (c_ty as (c, ty)) = |
22197 | 52 |
(c, Sign.const_typargs thy c_ty); |
20387 | 53 |
|
20600 | 54 |
fun typ_of_inst thy (c_tys as (c, tys)) = |
22197 | 55 |
(c, Sign.const_instance thy c_tys); |
20387 | 56 |
|
20389 | 57 |
fun find_def thy (c, tys) = |
20387 | 58 |
let |
59 |
val specs = Defs.specifications_of (Theory.defs_of thy) c; |
|
20389 | 60 |
val typ_instance = case AxClass.class_of_param thy c |
61 |
of SOME _ => let |
|
62 |
fun instance_tycos (Type (tyco1, _), Type (tyco2, _)) = tyco1 = tyco2 |
|
63 |
| instance_tycos (_ , TVar _) = true |
|
64 |
| instance_tycos ty_ty = Sign.typ_instance thy ty_ty; |
|
65 |
in instance_tycos end |
|
20600 | 66 |
| NONE => Sign.typ_instance thy; |
67 |
fun get_def (_, { is_def, thyname, name, lhs, rhs }) = |
|
68 |
if is_def andalso forall typ_instance (tys ~~ lhs) then |
|
69 |
case try (Thm.get_axiom_i thy) name |
|
22183 | 70 |
of SOME thm => SOME (thyname, thm) |
20600 | 71 |
| NONE => NONE |
72 |
else NONE |
|
20387 | 73 |
in |
20600 | 74 |
get_first get_def specs |
20387 | 75 |
end; |
76 |
||
20600 | 77 |
fun norm thy (c, insts) = |
20387 | 78 |
let |
22183 | 79 |
fun disciplined class [ty as Type (tyco, _)] = |
22197 | 80 |
let |
81 |
val sorts = Sorts.mg_domain (Sign.classes_of thy) tyco [class]; |
|
82 |
val vs = Name.invents Name.context "'a" (length sorts); |
|
83 |
in |
|
84 |
(c, [Type (tyco, map (fn v => TVar ((v, 0), [])) vs)]) |
|
85 |
end |
|
22183 | 86 |
| disciplined class _ = |
87 |
(c, [TVar (("'a", 0), [class])]); |
|
20600 | 88 |
in case AxClass.class_of_param thy c |
22183 | 89 |
of SOME class => disciplined class insts |
22197 | 90 |
| NONE => inst_of_typ thy (c, Sign.the_const_type thy c) |
20387 | 91 |
end; |
92 |
||
20600 | 93 |
fun norm_of_typ thy (c, ty) = |
22197 | 94 |
norm thy (c, Sign.const_typargs thy (c, ty)); |
20600 | 95 |
|
20704 | 96 |
fun consts_of thy t = |
21118 | 97 |
fold_aterms (fn Const c => cons (norm_of_typ thy c) | _ => I) t [] |
20704 | 98 |
|
22197 | 99 |
fun typ_sort_inst algebra = |
100 |
let |
|
101 |
val inters = Sorts.inter_sort algebra; |
|
102 |
fun match _ [] = I |
|
103 |
| match (TVar (v, S)) S' = Vartab.map_default (v, []) (fn S'' => inters (S, inters (S', S''))) |
|
104 |
| match (Type (a, Ts)) S = |
|
105 |
fold2 match Ts (Sorts.mg_domain algebra a S) |
|
106 |
in uncurry match end; |
|
107 |
||
108 |
fun typargs thy (c_ty as (c, ty)) = |
|
109 |
let |
|
110 |
val opt_class = AxClass.class_of_param thy c; |
|
111 |
val tys = Sign.const_typargs thy (c, ty); |
|
112 |
in case (opt_class, tys) |
|
113 |
of (SOME _, [Type (_, tys')]) => tys' |
|
114 |
| _ => tys |
|
115 |
end; |
|
116 |
||
20387 | 117 |
|
118 |
(* reading constants as terms *) |
|
119 |
||
120 |
fun read_const_typ thy raw_t = |
|
121 |
let |
|
122 |
val t = Sign.read_term thy raw_t |
|
123 |
in case try dest_Const t |
|
20600 | 124 |
of SOME c_ty => (typ_of_inst thy o norm_of_typ thy) c_ty |
20389 | 125 |
| NONE => error ("Not a constant: " ^ Sign.string_of_term thy t) |
20387 | 126 |
end; |
127 |
||
128 |
fun read_const thy = |
|
20600 | 129 |
norm_of_typ thy o read_const_typ thy; |
20387 | 130 |
|
131 |
||
22197 | 132 |
(* printing *) |
20387 | 133 |
|
134 |
fun string_of_const thy (c, tys) = |
|
135 |
space_implode " " (Sign.extern_const thy c |
|
136 |
:: map (enclose "[" "]" o Sign.string_of_typ thy) tys); |
|
137 |
||
20600 | 138 |
fun raw_string_of_const (c, tys) = |
139 |
space_implode " " (c |
|
140 |
:: map (enclose "[" "]" o Display.raw_string_of_typ) tys); |
|
141 |
||
20387 | 142 |
fun string_of_const_typ thy (c, ty) = |
143 |
string_of_const thy (c, Consts.typargs (Sign.consts_of thy) (c, ty)); |
|
144 |
||
145 |
end; |