| author | wenzelm | 
| Thu, 14 Aug 2008 16:52:51 +0200 | |
| changeset 27866 | c721ea6e0eb4 | 
| parent 27675 | cb0021d56e11 | 
| child 27983 | 00e005cdceb0 | 
| permissions | -rw-r--r-- | 
| 24219 | 1  | 
(* Title: Pure/Isar/code.ML  | 
2  | 
ID: $Id$  | 
|
3  | 
Author: Florian Haftmann, TU Muenchen  | 
|
4  | 
||
5  | 
Abstract executable content of theory. Management of data dependent on  | 
|
| 25485 | 6  | 
executable content. Cache assumes non-concurrent processing of a single theory.  | 
| 24219 | 7  | 
*)  | 
8  | 
||
9  | 
signature CODE =  | 
|
10  | 
sig  | 
|
| 
24624
 
b8383b1bbae3
distinction between regular and default code theorems
 
haftmann 
parents: 
24585 
diff
changeset
 | 
11  | 
val add_func: thm -> theory -> theory  | 
| 
 
b8383b1bbae3
distinction between regular and default code theorems
 
haftmann 
parents: 
24585 
diff
changeset
 | 
12  | 
val add_liberal_func: thm -> theory -> theory  | 
| 
 
b8383b1bbae3
distinction between regular and default code theorems
 
haftmann 
parents: 
24585 
diff
changeset
 | 
13  | 
val add_default_func: thm -> theory -> theory  | 
| 
 
b8383b1bbae3
distinction between regular and default code theorems
 
haftmann 
parents: 
24585 
diff
changeset
 | 
14  | 
val add_default_func_attr: Attrib.src  | 
| 24219 | 15  | 
val del_func: thm -> theory -> theory  | 
| 26021 | 16  | 
val del_funcs: string -> theory -> theory  | 
| 
24423
 
ae9cd0e92423
overloaded definitions accompanied by explicit constants
 
haftmann 
parents: 
24283 
diff
changeset
 | 
17  | 
val add_funcl: string * thm list Susp.T -> theory -> theory  | 
| 
27557
 
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
 
haftmann 
parents: 
26970 
diff
changeset
 | 
18  | 
val map_pre: (MetaSimplifier.simpset -> MetaSimplifier.simpset) -> theory -> theory  | 
| 
 
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
 
haftmann 
parents: 
26970 
diff
changeset
 | 
19  | 
val map_post: (MetaSimplifier.simpset -> MetaSimplifier.simpset) -> theory -> theory  | 
| 24219 | 20  | 
val add_inline: thm -> theory -> theory  | 
21  | 
val del_inline: thm -> theory -> theory  | 
|
22  | 
val add_post: thm -> theory -> theory  | 
|
23  | 
val del_post: thm -> theory -> theory  | 
|
| 27609 | 24  | 
val add_functrans: string * (theory -> thm list -> thm list option) -> theory -> theory  | 
| 
27557
 
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
 
haftmann 
parents: 
26970 
diff
changeset
 | 
25  | 
val del_functrans: string -> theory -> theory  | 
| 
24423
 
ae9cd0e92423
overloaded definitions accompanied by explicit constants
 
haftmann 
parents: 
24283 
diff
changeset
 | 
26  | 
val add_datatype: (string * typ) list -> theory -> theory  | 
| 
 
ae9cd0e92423
overloaded definitions accompanied by explicit constants
 
haftmann 
parents: 
24283 
diff
changeset
 | 
27  | 
val add_datatype_cmd: string list -> theory -> theory  | 
| 25485 | 28  | 
val type_interpretation:  | 
29  | 
(string * ((string * sort) list * (string * typ list) list)  | 
|
30  | 
-> theory -> theory) -> theory -> theory  | 
|
| 
24844
 
98c006a30218
certificates for code generator case expressions
 
haftmann 
parents: 
24837 
diff
changeset
 | 
31  | 
val add_case: thm -> theory -> theory  | 
| 
 
98c006a30218
certificates for code generator case expressions
 
haftmann 
parents: 
24837 
diff
changeset
 | 
32  | 
val add_undefined: string -> theory -> theory  | 
| 27675 | 33  | 
val purge_data: theory -> theory  | 
| 24219 | 34  | 
|
35  | 
val coregular_algebra: theory -> Sorts.algebra  | 
|
36  | 
val operational_algebra: theory -> (sort -> sort) * Sorts.algebra  | 
|
| 
24423
 
ae9cd0e92423
overloaded definitions accompanied by explicit constants
 
haftmann 
parents: 
24283 
diff
changeset
 | 
37  | 
val these_funcs: theory -> string -> thm list  | 
| 24219 | 38  | 
val get_datatype: theory -> string -> ((string * sort) list * (string * typ list) list)  | 
| 
24423
 
ae9cd0e92423
overloaded definitions accompanied by explicit constants
 
haftmann 
parents: 
24283 
diff
changeset
 | 
39  | 
val get_datatype_of_constr: theory -> string -> string option  | 
| 
24844
 
98c006a30218
certificates for code generator case expressions
 
haftmann 
parents: 
24837 
diff
changeset
 | 
40  | 
val get_case_data: theory -> string -> (int * string list) option  | 
| 
 
98c006a30218
certificates for code generator case expressions
 
haftmann 
parents: 
24837 
diff
changeset
 | 
41  | 
val is_undefined: theory -> string -> bool  | 
| 26970 | 42  | 
val default_typ: theory -> string -> (string * sort) list * typ  | 
| 24219 | 43  | 
|
44  | 
val preprocess_conv: cterm -> thm  | 
|
| 24837 | 45  | 
val preprocess_term: theory -> term -> term  | 
| 24219 | 46  | 
val postprocess_conv: cterm -> thm  | 
| 24837 | 47  | 
val postprocess_term: theory -> term -> term  | 
| 24219 | 48  | 
|
49  | 
val add_attribute: string * (Args.T list -> attribute * Args.T list) -> theory -> theory  | 
|
50  | 
||
51  | 
val print_codesetup: theory -> unit  | 
|
52  | 
end;  | 
|
53  | 
||
54  | 
signature CODE_DATA_ARGS =  | 
|
55  | 
sig  | 
|
56  | 
type T  | 
|
57  | 
val empty: T  | 
|
| 27609 | 58  | 
val purge: theory -> string list -> T -> T  | 
| 24219 | 59  | 
end;  | 
60  | 
||
61  | 
signature CODE_DATA =  | 
|
62  | 
sig  | 
|
63  | 
type T  | 
|
64  | 
val get: theory -> T  | 
|
65  | 
val change: theory -> (T -> T) -> T  | 
|
66  | 
val change_yield: theory -> (T -> 'a * T) -> 'a * T  | 
|
67  | 
end;  | 
|
68  | 
||
69  | 
signature PRIVATE_CODE =  | 
|
70  | 
sig  | 
|
71  | 
include CODE  | 
|
| 27609 | 72  | 
val declare_data: Object.T -> (theory -> string list -> Object.T -> Object.T)  | 
73  | 
-> serial  | 
|
| 24219 | 74  | 
  val get_data: serial * ('a -> Object.T) * (Object.T -> 'a)
 | 
75  | 
-> theory -> 'a  | 
|
76  | 
  val change_data: serial * ('a -> Object.T) * (Object.T -> 'a)
 | 
|
77  | 
    -> theory -> ('a -> 'a) -> 'a
 | 
|
78  | 
  val change_yield_data: serial * ('a -> Object.T) * (Object.T -> 'a)
 | 
|
79  | 
    -> theory -> ('a -> 'b * 'a) -> 'b * 'a
 | 
|
80  | 
end;  | 
|
81  | 
||
82  | 
structure Code : PRIVATE_CODE =  | 
|
83  | 
struct  | 
|
84  | 
||
| 25312 | 85  | 
(** code attributes **)  | 
86  | 
||
87  | 
structure CodeAttr = TheoryDataFun (  | 
|
88  | 
type T = (string * (Args.T list -> attribute * Args.T list)) list;  | 
|
89  | 
val empty = [];  | 
|
90  | 
val copy = I;  | 
|
91  | 
val extend = I;  | 
|
| 26970 | 92  | 
fun merge _ = AList.merge (op = : string * string -> bool) (K true);  | 
| 25312 | 93  | 
);  | 
| 24219 | 94  | 
|
| 25312 | 95  | 
fun add_attribute (attr as (name, _)) =  | 
96  | 
let  | 
|
97  | 
    fun add_parser ("", parser) attrs = attrs @ [("", parser)]
 | 
|
98  | 
| add_parser (name, parser) attrs = (name, Args.$$$ name |-- parser) :: attrs;  | 
|
99  | 
    fun error "" = error ("Code attribute already declared")
 | 
|
100  | 
      | error name = error ("Code attribute " ^ name ^ " already declared")
 | 
|
101  | 
in CodeAttr.map (fn attrs => if AList.defined (op =) attrs name  | 
|
102  | 
then error name else add_parser attr attrs)  | 
|
103  | 
end;  | 
|
104  | 
||
105  | 
val _ =  | 
|
106  | 
let  | 
|
107  | 
val code_attr = Attrib.syntax (Scan.peek (fn context =>  | 
|
108  | 
List.foldr op || Scan.fail (map snd (CodeAttr.get (Context.theory_of context)))));  | 
|
109  | 
in  | 
|
| 26463 | 110  | 
Context.>> (Context.map_theory  | 
111  | 
(Attrib.add_attributes  | 
|
112  | 
        [("code", code_attr, "declare theorems for code generation")]))
 | 
|
| 25312 | 113  | 
end;  | 
114  | 
||
115  | 
||
116  | 
(** certificate theorems **)  | 
|
| 24219 | 117  | 
|
118  | 
fun string_of_lthms r = case Susp.peek r  | 
|
| 26928 | 119  | 
of SOME thms => (map Display.string_of_thm o rev) thms  | 
| 24219 | 120  | 
| NONE => ["[...]"];  | 
121  | 
||
122  | 
fun pretty_lthms ctxt r = case Susp.peek r  | 
|
123  | 
of SOME thms => map (ProofContext.pretty_thm ctxt) thms  | 
|
124  | 
| NONE => [Pretty.str "[...]"];  | 
|
125  | 
||
126  | 
fun certificate thy f r =  | 
|
127  | 
case Susp.peek r  | 
|
128  | 
of SOME thms => (Susp.value o f thy) thms  | 
|
| 
24844
 
98c006a30218
certificates for code generator case expressions
 
haftmann 
parents: 
24837 
diff
changeset
 | 
129  | 
| NONE => let  | 
| 
 
98c006a30218
certificates for code generator case expressions
 
haftmann 
parents: 
24837 
diff
changeset
 | 
130  | 
val thy_ref = Theory.check_thy thy;  | 
| 
 
98c006a30218
certificates for code generator case expressions
 
haftmann 
parents: 
24837 
diff
changeset
 | 
131  | 
in Susp.delay (fn () => (f (Theory.deref thy_ref) o Susp.force) r) end;  | 
| 24219 | 132  | 
|
133  | 
||
| 25312 | 134  | 
(** logical and syntactical specification of executable code **)  | 
| 24219 | 135  | 
|
136  | 
(* pairs of (selected, deleted) defining equations *)  | 
|
137  | 
||
138  | 
type sdthms = thm list Susp.T * thm list;  | 
|
139  | 
||
140  | 
fun add_drop_redundant thm (sels, dels) =  | 
|
141  | 
let  | 
|
142  | 
val thy = Thm.theory_of_thm thm;  | 
|
143  | 
val args_of = snd o strip_comb o fst o Logic.dest_equals o Thm.plain_prop_of;  | 
|
144  | 
val args = args_of thm;  | 
|
145  | 
fun matches [] _ = true  | 
|
146  | 
| matches (Var _ :: xs) [] = matches xs []  | 
|
147  | 
| matches (_ :: _) [] = false  | 
|
148  | 
| matches (x :: xs) (y :: ys) = Pattern.matches thy (x, y) andalso matches xs ys;  | 
|
149  | 
fun drop thm' = not (matches args (args_of thm'))  | 
|
| 
26947
 
133905a0c493
moved global pretty/string_of functions from Sign to Syntax;
 
wenzelm 
parents: 
26928 
diff
changeset
 | 
150  | 
      orelse (warning ("Code generator: dropping redundant defining equation\n" ^ Display.string_of_thm thm'); false);
 | 
| 24219 | 151  | 
val (keeps, drops) = List.partition drop sels;  | 
152  | 
in (thm :: keeps, dels |> remove Thm.eq_thm_prop thm |> fold (insert Thm.eq_thm_prop) drops) end;  | 
|
153  | 
||
154  | 
fun add_thm thm (sels, dels) =  | 
|
155  | 
apfst Susp.value (add_drop_redundant thm (Susp.force sels, dels));  | 
|
156  | 
||
157  | 
fun add_lthms lthms (sels, []) =  | 
|
158  | 
(Susp.delay (fn () => fold add_drop_redundant  | 
|
159  | 
(Susp.force lthms) (Susp.force sels, []) |> fst), [])  | 
|
160  | 
(*FIXME*)  | 
|
161  | 
| add_lthms lthms (sels, dels) =  | 
|
162  | 
fold add_thm (Susp.force lthms) (sels, dels);  | 
|
163  | 
||
164  | 
fun del_thm thm (sels, dels) =  | 
|
165  | 
(Susp.value (remove Thm.eq_thm_prop thm (Susp.force sels)), thm :: dels);  | 
|
166  | 
||
| 26021 | 167  | 
fun del_thms (sels, dels) =  | 
168  | 
let  | 
|
169  | 
val all_sels = Susp.force sels;  | 
|
170  | 
in (Susp.value [], rev all_sels @ dels) end;  | 
|
171  | 
||
| 24219 | 172  | 
fun pretty_sdthms ctxt (sels, _) = pretty_lthms ctxt sels;  | 
173  | 
||
| 25312 | 174  | 
fun melt _ ([], []) = (false, [])  | 
175  | 
| melt _ ([], ys) = (true, ys)  | 
|
176  | 
| melt eq (xs, ys) = fold_rev  | 
|
177  | 
(fn y => fn (t, xs) => (t orelse not (member eq xs y), insert eq y xs)) ys (false, xs);  | 
|
178  | 
||
179  | 
val melt_thms = melt Thm.eq_thm_prop;  | 
|
180  | 
||
181  | 
fun melt_lthms (r1, r2) =  | 
|
182  | 
if Susp.same (r1, r2)  | 
|
183  | 
then (false, r1)  | 
|
184  | 
else case Susp.peek r1  | 
|
185  | 
of SOME [] => (true, r2)  | 
|
186  | 
| _ => case Susp.peek r2  | 
|
187  | 
of SOME [] => (true, r1)  | 
|
188  | 
| _ => (apsnd (Susp.delay o K)) (melt_thms (Susp.force r1, Susp.force r2));  | 
|
189  | 
||
190  | 
fun melt_sdthms ((sels1, dels1), (sels2, dels2)) =  | 
|
| 24219 | 191  | 
let  | 
| 25312 | 192  | 
val (dels_t, dels) = melt_thms (dels1, dels2);  | 
| 24219 | 193  | 
in if dels_t  | 
194  | 
then let  | 
|
| 25312 | 195  | 
val (_, sels) = melt_thms  | 
| 24219 | 196  | 
(subtract Thm.eq_thm_prop dels2 (Susp.force sels1), Susp.force sels2);  | 
| 25312 | 197  | 
val (_, dels) = melt_thms  | 
| 24219 | 198  | 
(subtract Thm.eq_thm_prop (Susp.force sels2) dels1, dels2);  | 
| 27609 | 199  | 
in ((Susp.delay o K) sels, dels) end  | 
| 24219 | 200  | 
else let  | 
| 27609 | 201  | 
val (_, sels) = melt_lthms (sels1, sels2);  | 
202  | 
in (sels, dels) end  | 
|
| 24219 | 203  | 
end;  | 
204  | 
||
205  | 
||
| 25312 | 206  | 
(* specification data *)  | 
| 24219 | 207  | 
|
208  | 
val eq_string = op = : string * string -> bool;  | 
|
209  | 
fun eq_dtyp ((vs1, cs1), (vs2, cs2)) =  | 
|
210  | 
gen_eq_set (eq_pair eq_string (gen_eq_set eq_string)) (vs1, vs2)  | 
|
| 
24423
 
ae9cd0e92423
overloaded definitions accompanied by explicit constants
 
haftmann 
parents: 
24283 
diff
changeset
 | 
211  | 
andalso gen_eq_set (eq_fst eq_string) (cs1, cs2);  | 
| 27582 | 212  | 
fun merge_dtyps (tabs as (tab1, tab2)) =  | 
| 24219 | 213  | 
let  | 
| 27582 | 214  | 
fun join _ (cos as (_, cos2)) = if eq_dtyp cos then raise Symtab.SAME else cos2;  | 
215  | 
in Symtab.join join tabs end;  | 
|
| 24219 | 216  | 
|
217  | 
datatype spec = Spec of {
 | 
|
| 27609 | 218  | 
funcs: sdthms Symtab.table,  | 
| 
24844
 
98c006a30218
certificates for code generator case expressions
 
haftmann 
parents: 
24837 
diff
changeset
 | 
219  | 
dtyps: ((string * sort) list * (string * typ list) list) Symtab.table,  | 
| 
 
98c006a30218
certificates for code generator case expressions
 
haftmann 
parents: 
24837 
diff
changeset
 | 
220  | 
cases: (int * string list) Symtab.table * unit Symtab.table  | 
| 24219 | 221  | 
};  | 
222  | 
||
| 
24844
 
98c006a30218
certificates for code generator case expressions
 
haftmann 
parents: 
24837 
diff
changeset
 | 
223  | 
fun mk_spec (funcs, (dtyps, cases)) =  | 
| 
 
98c006a30218
certificates for code generator case expressions
 
haftmann 
parents: 
24837 
diff
changeset
 | 
224  | 
  Spec { funcs = funcs, dtyps = dtyps, cases = cases };
 | 
| 
 
98c006a30218
certificates for code generator case expressions
 
haftmann 
parents: 
24837 
diff
changeset
 | 
225  | 
fun map_spec f (Spec { funcs = funcs, dtyps = dtyps, cases = cases }) =
 | 
| 
 
98c006a30218
certificates for code generator case expressions
 
haftmann 
parents: 
24837 
diff
changeset
 | 
226  | 
mk_spec (f (funcs, (dtyps, cases)));  | 
| 27609 | 227  | 
fun merge_spec (Spec { funcs = funcs1, dtyps = dtyps1, cases = (cases1, undefs1) },
 | 
228  | 
  Spec { funcs = funcs2, dtyps = dtyps2, cases = (cases2, undefs2) }) =
 | 
|
| 24219 | 229  | 
let  | 
| 27609 | 230  | 
val funcs = Symtab.join (K melt_sdthms) (funcs1, funcs2);  | 
| 27582 | 231  | 
val dtyps = merge_dtyps (dtyps1, dtyps2);  | 
| 27609 | 232  | 
val cases = (Symtab.merge (K true) (cases1, cases2),  | 
233  | 
Symtab.merge (K true) (undefs1, undefs2));  | 
|
| 
27557
 
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
 
haftmann 
parents: 
26970 
diff
changeset
 | 
234  | 
in mk_spec (funcs, (dtyps, cases)) end;  | 
| 24219 | 235  | 
|
| 25312 | 236  | 
|
237  | 
(* pre- and postprocessor *)  | 
|
238  | 
||
239  | 
datatype thmproc = Thmproc of {
 | 
|
| 
27557
 
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
 
haftmann 
parents: 
26970 
diff
changeset
 | 
240  | 
pre: MetaSimplifier.simpset,  | 
| 
 
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
 
haftmann 
parents: 
26970 
diff
changeset
 | 
241  | 
post: MetaSimplifier.simpset,  | 
| 27609 | 242  | 
functrans: (string * (serial * (theory -> thm list -> thm list option))) list  | 
| 25312 | 243  | 
};  | 
244  | 
||
| 
27557
 
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
 
haftmann 
parents: 
26970 
diff
changeset
 | 
245  | 
fun mk_thmproc ((pre, post), functrans) =  | 
| 
 
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
 
haftmann 
parents: 
26970 
diff
changeset
 | 
246  | 
  Thmproc { pre = pre, post = post, functrans = functrans };
 | 
| 
 
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
 
haftmann 
parents: 
26970 
diff
changeset
 | 
247  | 
fun map_thmproc f (Thmproc { pre, post, functrans }) =
 | 
| 
 
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
 
haftmann 
parents: 
26970 
diff
changeset
 | 
248  | 
mk_thmproc (f ((pre, post), functrans));  | 
| 
 
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
 
haftmann 
parents: 
26970 
diff
changeset
 | 
249  | 
fun merge_thmproc (Thmproc { pre = pre1, post = post1, functrans = functrans1 },
 | 
| 
 
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
 
haftmann 
parents: 
26970 
diff
changeset
 | 
250  | 
  Thmproc { pre = pre2, post = post2, functrans = functrans2 }) =
 | 
| 25312 | 251  | 
let  | 
| 
27557
 
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
 
haftmann 
parents: 
26970 
diff
changeset
 | 
252  | 
val pre = MetaSimplifier.merge_ss (pre1, pre2);  | 
| 
 
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
 
haftmann 
parents: 
26970 
diff
changeset
 | 
253  | 
val post = MetaSimplifier.merge_ss (post1, post2);  | 
| 
 
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
 
haftmann 
parents: 
26970 
diff
changeset
 | 
254  | 
val functrans = AList.merge (op =) (eq_fst (op =)) (functrans1, functrans2);  | 
| 
 
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
 
haftmann 
parents: 
26970 
diff
changeset
 | 
255  | 
in mk_thmproc ((pre, post), functrans) end;  | 
| 25312 | 256  | 
|
| 24219 | 257  | 
datatype exec = Exec of {
 | 
258  | 
thmproc: thmproc,  | 
|
259  | 
spec: spec  | 
|
260  | 
};  | 
|
261  | 
||
262  | 
fun mk_exec (thmproc, spec) =  | 
|
263  | 
  Exec { thmproc = thmproc, spec = spec };
 | 
|
264  | 
fun map_exec f (Exec { thmproc = thmproc, spec = spec }) =
 | 
|
265  | 
mk_exec (f (thmproc, spec));  | 
|
| 
27557
 
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
 
haftmann 
parents: 
26970 
diff
changeset
 | 
266  | 
fun merge_exec (Exec { thmproc = thmproc1, spec = spec1 },
 | 
| 24219 | 267  | 
  Exec { thmproc = thmproc2, spec = spec2 }) =
 | 
268  | 
let  | 
|
| 
27557
 
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
 
haftmann 
parents: 
26970 
diff
changeset
 | 
269  | 
val thmproc = merge_thmproc (thmproc1, thmproc2);  | 
| 
 
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
 
haftmann 
parents: 
26970 
diff
changeset
 | 
270  | 
val spec = merge_spec (spec1, spec2);  | 
| 
 
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
 
haftmann 
parents: 
26970 
diff
changeset
 | 
271  | 
in mk_exec (thmproc, spec) end;  | 
| 
 
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
 
haftmann 
parents: 
26970 
diff
changeset
 | 
272  | 
val empty_exec = mk_exec (mk_thmproc ((MetaSimplifier.empty_ss, MetaSimplifier.empty_ss), []),  | 
| 
24844
 
98c006a30218
certificates for code generator case expressions
 
haftmann 
parents: 
24837 
diff
changeset
 | 
273  | 
mk_spec (Symtab.empty, (Symtab.empty, (Symtab.empty, Symtab.empty))));  | 
| 24219 | 274  | 
|
| 25312 | 275  | 
fun the_thmproc (Exec { thmproc = Thmproc x, ...}) = x;
 | 
| 24219 | 276  | 
fun the_spec (Exec { spec = Spec x, ...}) = x;
 | 
277  | 
val the_funcs = #funcs o the_spec;  | 
|
278  | 
val the_dtyps = #dtyps o the_spec;  | 
|
| 
24844
 
98c006a30218
certificates for code generator case expressions
 
haftmann 
parents: 
24837 
diff
changeset
 | 
279  | 
val the_cases = #cases o the_spec;  | 
| 24219 | 280  | 
val map_thmproc = map_exec o apfst o map_thmproc;  | 
281  | 
val map_funcs = map_exec o apsnd o map_spec o apfst;  | 
|
| 
24844
 
98c006a30218
certificates for code generator case expressions
 
haftmann 
parents: 
24837 
diff
changeset
 | 
282  | 
val map_dtyps = map_exec o apsnd o map_spec o apsnd o apfst;  | 
| 
 
98c006a30218
certificates for code generator case expressions
 
haftmann 
parents: 
24837 
diff
changeset
 | 
283  | 
val map_cases = map_exec o apsnd o map_spec o apsnd o apsnd;  | 
| 24219 | 284  | 
|
285  | 
||
286  | 
(* data slots dependent on executable content *)  | 
|
287  | 
||
288  | 
(*private copy avoids potential conflict of table exceptions*)  | 
|
289  | 
structure Datatab = TableFun(type key = int val ord = int_ord);  | 
|
290  | 
||
291  | 
local  | 
|
292  | 
||
293  | 
type kind = {
 | 
|
294  | 
empty: Object.T,  | 
|
| 27609 | 295  | 
purge: theory -> string list -> Object.T -> Object.T  | 
| 24219 | 296  | 
};  | 
297  | 
||
298  | 
val kinds = ref (Datatab.empty: kind Datatab.table);  | 
|
299  | 
val kind_keys = ref ([]: serial list);  | 
|
300  | 
||
301  | 
fun invoke f k = case Datatab.lookup (! kinds) k  | 
|
302  | 
of SOME kind => f kind  | 
|
303  | 
| NONE => sys_error "Invalid code data identifier";  | 
|
304  | 
||
305  | 
in  | 
|
306  | 
||
| 27609 | 307  | 
fun declare_data empty purge =  | 
| 24219 | 308  | 
let  | 
309  | 
val k = serial ();  | 
|
| 27609 | 310  | 
    val kind = {empty = empty, purge = purge};
 | 
| 24219 | 311  | 
val _ = change kinds (Datatab.update (k, kind));  | 
312  | 
val _ = change kind_keys (cons k);  | 
|
313  | 
in k end;  | 
|
314  | 
||
| 27609 | 315  | 
fun invoke_init k = invoke (fn kind => #empty kind) k;  | 
| 24219 | 316  | 
|
| 27609 | 317  | 
fun invoke_purge_all thy cs =  | 
| 24219 | 318  | 
fold (fn k => Datatab.map_entry k  | 
| 27609 | 319  | 
(invoke (fn kind => #purge kind thy cs) k)) (! kind_keys);  | 
| 24219 | 320  | 
|
321  | 
end; (*local*)  | 
|
322  | 
||
323  | 
||
| 25312 | 324  | 
(** theory store **)  | 
| 24219 | 325  | 
|
326  | 
local  | 
|
327  | 
||
328  | 
type data = Object.T Datatab.table;  | 
|
| 27609 | 329  | 
val empty_data = Datatab.empty : data;  | 
| 24219 | 330  | 
|
331  | 
structure CodeData = TheoryDataFun  | 
|
332  | 
(  | 
|
333  | 
type T = exec * data ref;  | 
|
| 27609 | 334  | 
val empty = (empty_exec, ref empty_data);  | 
| 24219 | 335  | 
fun copy (exec, data) = (exec, ref (! data));  | 
336  | 
val extend = copy;  | 
|
337  | 
fun merge pp ((exec1, data1), (exec2, data2)) =  | 
|
| 27609 | 338  | 
(merge_exec (exec1, exec2), ref empty_data);  | 
| 24219 | 339  | 
);  | 
340  | 
||
| 26463 | 341  | 
val _ = Context.>> (Context.map_theory CodeData.init);  | 
| 24219 | 342  | 
|
343  | 
fun thy_data f thy = f ((snd o CodeData.get) thy);  | 
|
344  | 
||
345  | 
fun get_ensure_init kind data_ref =  | 
|
346  | 
case Datatab.lookup (! data_ref) kind  | 
|
347  | 
of SOME x => x  | 
|
| 27609 | 348  | 
| NONE => let val y = invoke_init kind  | 
| 24219 | 349  | 
in (change data_ref (Datatab.update (kind, y)); y) end;  | 
350  | 
||
351  | 
in  | 
|
352  | 
||
353  | 
(* access to executable content *)  | 
|
354  | 
||
| 
24844
 
98c006a30218
certificates for code generator case expressions
 
haftmann 
parents: 
24837 
diff
changeset
 | 
355  | 
val the_exec = fst o CodeData.get;  | 
| 24219 | 356  | 
|
357  | 
fun map_exec_purge touched f thy =  | 
|
| 27609 | 358  | 
CodeData.map (fn (exec, data) => (f exec, ref (case touched  | 
359  | 
of SOME cs => invoke_purge_all thy cs (! data)  | 
|
360  | 
| NONE => empty_data))) thy;  | 
|
| 24219 | 361  | 
|
| 27675 | 362  | 
val purge_data = (CodeData.map o apsnd) (K (ref empty_data));  | 
363  | 
||
| 24219 | 364  | 
|
365  | 
(* access to data dependent on abstract executable content *)  | 
|
366  | 
||
367  | 
fun get_data (kind, _, dest) = thy_data (get_ensure_init kind #> dest);  | 
|
368  | 
||
369  | 
fun change_data (kind, mk, dest) =  | 
|
370  | 
let  | 
|
371  | 
fun chnge data_ref f =  | 
|
372  | 
let  | 
|
373  | 
val data = get_ensure_init kind data_ref;  | 
|
374  | 
val data' = f (dest data);  | 
|
375  | 
in (change data_ref (Datatab.update (kind, mk data')); data') end;  | 
|
376  | 
in thy_data chnge end;  | 
|
377  | 
||
378  | 
fun change_yield_data (kind, mk, dest) =  | 
|
379  | 
let  | 
|
380  | 
fun chnge data_ref f =  | 
|
381  | 
let  | 
|
382  | 
val data = get_ensure_init kind data_ref;  | 
|
383  | 
val (x, data') = f (dest data);  | 
|
384  | 
in (x, (change data_ref (Datatab.update (kind, mk data')); data')) end;  | 
|
385  | 
in thy_data chnge end;  | 
|
386  | 
||
387  | 
end; (*local*)  | 
|
388  | 
||
389  | 
||
390  | 
(* print executable content *)  | 
|
391  | 
||
392  | 
fun print_codesetup thy =  | 
|
393  | 
let  | 
|
394  | 
val ctxt = ProofContext.init thy;  | 
|
| 
24844
 
98c006a30218
certificates for code generator case expressions
 
haftmann 
parents: 
24837 
diff
changeset
 | 
395  | 
val exec = the_exec thy;  | 
| 24219 | 396  | 
fun pretty_func (s, lthms) =  | 
397  | 
(Pretty.block o Pretty.fbreaks) (  | 
|
398  | 
Pretty.str s :: pretty_sdthms ctxt lthms  | 
|
399  | 
);  | 
|
400  | 
fun pretty_dtyp (s, []) =  | 
|
401  | 
Pretty.str s  | 
|
402  | 
| pretty_dtyp (s, cos) =  | 
|
403  | 
(Pretty.block o Pretty.breaks) (  | 
|
404  | 
Pretty.str s  | 
|
405  | 
:: Pretty.str "="  | 
|
406  | 
:: separate (Pretty.str "|") (map (fn (c, []) => Pretty.str c  | 
|
407  | 
| (c, tys) =>  | 
|
408  | 
(Pretty.block o Pretty.breaks)  | 
|
| 
24423
 
ae9cd0e92423
overloaded definitions accompanied by explicit constants
 
haftmann 
parents: 
24283 
diff
changeset
 | 
409  | 
(Pretty.str (CodeUnit.string_of_const thy c)  | 
| 
26947
 
133905a0c493
moved global pretty/string_of functions from Sign to Syntax;
 
wenzelm 
parents: 
26928 
diff
changeset
 | 
410  | 
:: Pretty.str "of"  | 
| 
 
133905a0c493
moved global pretty/string_of functions from Sign to Syntax;
 
wenzelm 
parents: 
26928 
diff
changeset
 | 
411  | 
:: map (Pretty.quote o Syntax.pretty_typ_global thy) tys)) cos)  | 
| 24219 | 412  | 
);  | 
| 
27557
 
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
 
haftmann 
parents: 
26970 
diff
changeset
 | 
413  | 
val pre = (#pre o the_thmproc) exec;  | 
| 
 
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
 
haftmann 
parents: 
26970 
diff
changeset
 | 
414  | 
val post = (#post o the_thmproc) exec;  | 
| 
 
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
 
haftmann 
parents: 
26970 
diff
changeset
 | 
415  | 
val functrans = (map fst o #functrans o the_thmproc) exec;  | 
| 27609 | 416  | 
val funcs = the_funcs exec  | 
| 
24423
 
ae9cd0e92423
overloaded definitions accompanied by explicit constants
 
haftmann 
parents: 
24283 
diff
changeset
 | 
417  | 
|> Symtab.dest  | 
| 24219 | 418  | 
|> (map o apfst) (CodeUnit.string_of_const thy)  | 
419  | 
|> sort (string_ord o pairself fst);  | 
|
420  | 
val dtyps = the_dtyps exec  | 
|
421  | 
|> Symtab.dest  | 
|
| 
26947
 
133905a0c493
moved global pretty/string_of functions from Sign to Syntax;
 
wenzelm 
parents: 
26928 
diff
changeset
 | 
422  | 
|> map (fn (dtco, (vs, cos)) =>  | 
| 
 
133905a0c493
moved global pretty/string_of functions from Sign to Syntax;
 
wenzelm 
parents: 
26928 
diff
changeset
 | 
423  | 
(Syntax.string_of_typ_global thy (Type (dtco, map TFree vs)), cos))  | 
| 24219 | 424  | 
|> sort (string_ord o pairself fst)  | 
425  | 
in  | 
|
426  | 
(Pretty.writeln o Pretty.chunks) [  | 
|
427  | 
Pretty.block (  | 
|
428  | 
Pretty.str "defining equations:"  | 
|
429  | 
:: Pretty.fbrk  | 
|
| 27609 | 430  | 
:: (Pretty.fbreaks o map pretty_func) funcs  | 
| 24219 | 431  | 
),  | 
| 
27557
 
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
 
haftmann 
parents: 
26970 
diff
changeset
 | 
432  | 
Pretty.block [  | 
| 
 
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
 
haftmann 
parents: 
26970 
diff
changeset
 | 
433  | 
Pretty.str "preprocessing simpset:",  | 
| 
 
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
 
haftmann 
parents: 
26970 
diff
changeset
 | 
434  | 
Pretty.fbrk,  | 
| 
 
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
 
haftmann 
parents: 
26970 
diff
changeset
 | 
435  | 
MetaSimplifier.pretty_ss pre  | 
| 
 
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
 
haftmann 
parents: 
26970 
diff
changeset
 | 
436  | 
],  | 
| 
 
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
 
haftmann 
parents: 
26970 
diff
changeset
 | 
437  | 
Pretty.block [  | 
| 
 
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
 
haftmann 
parents: 
26970 
diff
changeset
 | 
438  | 
Pretty.str "postprocessing simpset:",  | 
| 
 
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
 
haftmann 
parents: 
26970 
diff
changeset
 | 
439  | 
Pretty.fbrk,  | 
| 
 
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
 
haftmann 
parents: 
26970 
diff
changeset
 | 
440  | 
MetaSimplifier.pretty_ss post  | 
| 
 
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
 
haftmann 
parents: 
26970 
diff
changeset
 | 
441  | 
],  | 
| 24219 | 442  | 
Pretty.block (  | 
| 27609 | 443  | 
Pretty.str "function transformers:"  | 
| 24219 | 444  | 
:: Pretty.fbrk  | 
| 
27557
 
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
 
haftmann 
parents: 
26970 
diff
changeset
 | 
445  | 
:: (Pretty.fbreaks o map Pretty.str) functrans  | 
| 25968 | 446  | 
),  | 
447  | 
Pretty.block (  | 
|
| 24219 | 448  | 
Pretty.str "datatypes:"  | 
449  | 
:: Pretty.fbrk  | 
|
450  | 
:: (Pretty.fbreaks o map pretty_dtyp) dtyps  | 
|
451  | 
)  | 
|
452  | 
]  | 
|
453  | 
end;  | 
|
454  | 
||
455  | 
||
456  | 
||
457  | 
(** theorem transformation and certification **)  | 
|
458  | 
||
| 26970 | 459  | 
fun const_of thy = dest_Const o fst o strip_comb o fst o Logic.dest_equals  | 
460  | 
o ObjectLogic.drop_judgment thy o Thm.plain_prop_of;  | 
|
461  | 
||
462  | 
fun const_of_func thy = AxClass.unoverload_const thy o const_of thy;  | 
|
463  | 
||
| 24219 | 464  | 
fun common_typ_funcs [] = []  | 
465  | 
| common_typ_funcs [thm] = [thm]  | 
|
| 26970 | 466  | 
| common_typ_funcs (thms as thm :: _) = (*FIXME is too general*)  | 
| 24219 | 467  | 
let  | 
468  | 
val thy = Thm.theory_of_thm thm;  | 
|
469  | 
fun incr_thm thm max =  | 
|
470  | 
let  | 
|
471  | 
val thm' = incr_indexes max thm;  | 
|
472  | 
val max' = Thm.maxidx_of thm' + 1;  | 
|
473  | 
in (thm', max') end;  | 
|
474  | 
val (thms', maxidx) = fold_map incr_thm thms 0;  | 
|
| 26970 | 475  | 
val ty1 :: tys = map (snd o const_of thy) thms';  | 
| 24219 | 476  | 
fun unify ty env = Sign.typ_unify thy (ty1, ty) env  | 
477  | 
handle Type.TUNIFY =>  | 
|
478  | 
            error ("Type unificaton failed, while unifying defining equations\n"
 | 
|
479  | 
^ (cat_lines o map Display.string_of_thm) thms  | 
|
480  | 
^ "\nwith types\n"  | 
|
481  | 
^ (cat_lines o map (CodeUnit.string_of_typ thy)) (ty1 :: tys));  | 
|
482  | 
val (env, _) = fold unify tys (Vartab.empty, maxidx)  | 
|
483  | 
val instT = Vartab.fold (fn (x_i, (sort, ty)) =>  | 
|
484  | 
cons (Thm.ctyp_of thy (TVar (x_i, sort)), Thm.ctyp_of thy ty)) env [];  | 
|
485  | 
in map (Thm.instantiate (instT, [])) thms' end;  | 
|
486  | 
||
487  | 
fun certify_const thy const thms =  | 
|
488  | 
let  | 
|
| 
24423
 
ae9cd0e92423
overloaded definitions accompanied by explicit constants
 
haftmann 
parents: 
24283 
diff
changeset
 | 
489  | 
fun cert thm = if const = const_of_func thy thm  | 
| 24219 | 490  | 
      then thm else error ("Wrong head of defining equation,\nexpected constant "
 | 
| 26928 | 491  | 
^ CodeUnit.string_of_const thy const ^ "\n" ^ Display.string_of_thm thm)  | 
| 24219 | 492  | 
in map cert thms end;  | 
493  | 
||
494  | 
||
495  | 
||
496  | 
(** operational sort algebra and class discipline **)  | 
|
497  | 
||
498  | 
local  | 
|
499  | 
||
500  | 
fun aggr_neutr f y [] = y  | 
|
501  | 
| aggr_neutr f y (x::xs) = aggr_neutr f (f y x) xs;  | 
|
502  | 
||
503  | 
fun aggregate f [] = NONE  | 
|
504  | 
| aggregate f (x::xs) = SOME (aggr_neutr f x xs);  | 
|
505  | 
||
| 25462 | 506  | 
fun inter_sorts algebra =  | 
507  | 
aggregate (map2 (curry (Sorts.inter_sort algebra)));  | 
|
| 24219 | 508  | 
|
509  | 
fun specific_constraints thy (class, tyco) =  | 
|
510  | 
let  | 
|
511  | 
val vs = Name.invents Name.context "" (Sign.arity_number thy tyco);  | 
|
| 24969 | 512  | 
val classparams = (map fst o these o try (#params o AxClass.get_info thy)) class;  | 
| 24837 | 513  | 
val funcs = classparams  | 
| 
25597
 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 
haftmann 
parents: 
25501 
diff
changeset
 | 
514  | 
|> map_filter (fn c => try (AxClass.param_of_inst thy) (c, tyco))  | 
| 
24844
 
98c006a30218
certificates for code generator case expressions
 
haftmann 
parents: 
24837 
diff
changeset
 | 
515  | 
|> map (Symtab.lookup ((the_funcs o the_exec) thy))  | 
| 27609 | 516  | 
|> (map o Option.map) (Susp.force o fst)  | 
| 24219 | 517  | 
|> maps these  | 
| 
24423
 
ae9cd0e92423
overloaded definitions accompanied by explicit constants
 
haftmann 
parents: 
24283 
diff
changeset
 | 
518  | 
|> map (Thm.transfer thy)  | 
| 
 
ae9cd0e92423
overloaded definitions accompanied by explicit constants
 
haftmann 
parents: 
24283 
diff
changeset
 | 
519  | 
fun sorts_of [Type (_, tys)] = map (snd o dest_TVar) tys  | 
| 
 
ae9cd0e92423
overloaded definitions accompanied by explicit constants
 
haftmann 
parents: 
24283 
diff
changeset
 | 
520  | 
| sorts_of tys = map (snd o dest_TVar) tys;  | 
| 26970 | 521  | 
val sorts = map (sorts_of o Sign.const_typargs thy o const_of thy) funcs;  | 
| 24219 | 522  | 
in sorts end;  | 
523  | 
||
| 25462 | 524  | 
fun weakest_constraints thy algebra (class, tyco) =  | 
| 24219 | 525  | 
let  | 
| 25462 | 526  | 
val all_superclasses = Sorts.complete_sort algebra [class];  | 
527  | 
in case inter_sorts algebra (maps (fn class => specific_constraints thy (class, tyco)) all_superclasses)  | 
|
| 24219 | 528  | 
of SOME sorts => sorts  | 
| 25462 | 529  | 
| NONE => Sorts.mg_domain algebra tyco [class]  | 
| 24219 | 530  | 
end;  | 
531  | 
||
| 25462 | 532  | 
fun strongest_constraints thy algebra (class, tyco) =  | 
| 24219 | 533  | 
let  | 
534  | 
val all_subclasses = class :: Graph.all_preds ((#classes o Sorts.rep_algebra) algebra) [class];  | 
|
535  | 
val inst_subclasses = filter (can (Sorts.mg_domain algebra tyco) o single) all_subclasses;  | 
|
| 25462 | 536  | 
in case inter_sorts algebra (maps (fn class => specific_constraints thy (class, tyco)) inst_subclasses)  | 
| 24219 | 537  | 
of SOME sorts => sorts  | 
538  | 
| NONE => replicate  | 
|
| 25462 | 539  | 
(Sign.arity_number thy tyco) (Sorts.minimize_sort algebra (Sorts.all_classes algebra))  | 
540  | 
end;  | 
|
541  | 
||
542  | 
fun get_algebra thy (class, tyco) =  | 
|
543  | 
let  | 
|
544  | 
val base_algebra = Sign.classes_of thy;  | 
|
545  | 
in if can (Sorts.mg_domain base_algebra tyco) [class]  | 
|
546  | 
then base_algebra  | 
|
547  | 
else let  | 
|
548  | 
val superclasses = Sorts.super_classes base_algebra class;  | 
|
549  | 
val sorts = inter_sorts base_algebra  | 
|
550  | 
(map_filter (fn class => try (Sorts.mg_domain base_algebra tyco) [class]) superclasses)  | 
|
551  | 
|> the_default (replicate (Sign.arity_number thy tyco) [])  | 
|
552  | 
in  | 
|
553  | 
base_algebra  | 
|
| 
26947
 
133905a0c493
moved global pretty/string_of functions from Sign to Syntax;
 
wenzelm 
parents: 
26928 
diff
changeset
 | 
554  | 
|> Sorts.add_arities (Syntax.pp_global thy) (tyco, [(class, sorts)])  | 
| 25462 | 555  | 
end  | 
| 24219 | 556  | 
end;  | 
557  | 
||
| 24837 | 558  | 
fun gen_classparam_typ constr thy class (c, tyco) =  | 
| 24219 | 559  | 
let  | 
| 25462 | 560  | 
val algebra = get_algebra thy (class, tyco);  | 
| 24969 | 561  | 
val cs = these (try (#params o AxClass.get_info thy) class);  | 
| 25462 | 562  | 
val SOME ty = AList.lookup (op =) cs c;  | 
| 24969 | 563  | 
val sort_args = Name.names (Name.declare Name.aT Name.context) Name.aT  | 
| 25462 | 564  | 
(constr thy algebra (class, tyco));  | 
| 24219 | 565  | 
val ty_inst = Type (tyco, map TFree sort_args);  | 
566  | 
in Logic.varifyT (map_type_tfree (K ty_inst) ty) end;  | 
|
567  | 
||
568  | 
fun retrieve_algebra thy operational =  | 
|
| 
26947
 
133905a0c493
moved global pretty/string_of functions from Sign to Syntax;
 
wenzelm 
parents: 
26928 
diff
changeset
 | 
569  | 
Sorts.subalgebra (Syntax.pp_global thy) operational  | 
| 25462 | 570  | 
(weakest_constraints thy (Sign.classes_of thy))  | 
| 24219 | 571  | 
(Sign.classes_of thy);  | 
572  | 
||
573  | 
in  | 
|
574  | 
||
575  | 
fun coregular_algebra thy = retrieve_algebra thy (K true) |> snd;  | 
|
576  | 
fun operational_algebra thy =  | 
|
577  | 
let  | 
|
578  | 
fun add_iff_operational class =  | 
|
| 
24928
 
3419943838f5
renamed AxClass.get_definition to AxClass.get_info (again);
 
wenzelm 
parents: 
24848 
diff
changeset
 | 
579  | 
can (AxClass.get_info thy) class ? cons class;  | 
| 24219 | 580  | 
val operational_classes = fold add_iff_operational (Sign.all_classes thy) []  | 
581  | 
in retrieve_algebra thy (member (op =) operational_classes) end;  | 
|
582  | 
||
| 24837 | 583  | 
val classparam_weakest_typ = gen_classparam_typ weakest_constraints;  | 
584  | 
val classparam_strongest_typ = gen_classparam_typ strongest_constraints;  | 
|
| 24219 | 585  | 
|
586  | 
fun assert_func_typ thm =  | 
|
587  | 
let  | 
|
588  | 
val thy = Thm.theory_of_thm thm;  | 
|
| 24837 | 589  | 
fun check_typ_classparam tyco (c, thm) =  | 
| 24219 | 590  | 
let  | 
| 
24423
 
ae9cd0e92423
overloaded definitions accompanied by explicit constants
 
haftmann 
parents: 
24283 
diff
changeset
 | 
591  | 
val SOME class = AxClass.class_of_param thy c;  | 
| 26970 | 592  | 
val (_, ty) = const_of thy thm;  | 
| 24837 | 593  | 
val ty_decl = classparam_weakest_typ thy class (c, tyco);  | 
594  | 
val ty_strongest = classparam_strongest_typ thy class (c, tyco);  | 
|
| 24219 | 595  | 
fun constrain thm =  | 
596  | 
let  | 
|
597  | 
val max = Thm.maxidx_of thm + 1;  | 
|
598  | 
val ty_decl' = Logic.incr_tvar max ty_decl;  | 
|
| 26970 | 599  | 
val (_, ty') = const_of thy thm;  | 
| 24219 | 600  | 
val (env, _) = Sign.typ_unify thy (ty_decl', ty') (Vartab.empty, max);  | 
601  | 
val instT = Vartab.fold (fn (x_i, (sort, ty)) =>  | 
|
602  | 
cons (Thm.ctyp_of thy (TVar (x_i, sort)), Thm.ctyp_of thy ty)) env [];  | 
|
603  | 
in Thm.instantiate (instT, []) thm end;  | 
|
604  | 
in if Sign.typ_instance thy (ty_strongest, ty)  | 
|
605  | 
then if Sign.typ_instance thy (ty, ty_decl)  | 
|
606  | 
then thm  | 
|
607  | 
            else (warning ("Constraining type\n" ^ CodeUnit.string_of_typ thy ty
 | 
|
608  | 
^ "\nof defining equation\n"  | 
|
| 26928 | 609  | 
^ Display.string_of_thm thm  | 
| 24219 | 610  | 
^ "\nto permitted most general type\n"  | 
611  | 
^ CodeUnit.string_of_typ thy ty_decl);  | 
|
612  | 
constrain thm)  | 
|
613  | 
            else CodeUnit.bad_thm ("Type\n" ^ CodeUnit.string_of_typ thy ty
 | 
|
614  | 
^ "\nof defining equation\n"  | 
|
| 26928 | 615  | 
^ Display.string_of_thm thm  | 
| 24219 | 616  | 
^ "\nis incompatible with permitted least general type\n"  | 
617  | 
^ CodeUnit.string_of_typ thy ty_strongest)  | 
|
| 
24423
 
ae9cd0e92423
overloaded definitions accompanied by explicit constants
 
haftmann 
parents: 
24283 
diff
changeset
 | 
618  | 
end;  | 
| 
 
ae9cd0e92423
overloaded definitions accompanied by explicit constants
 
haftmann 
parents: 
24283 
diff
changeset
 | 
619  | 
fun check_typ_fun (c, thm) =  | 
| 24219 | 620  | 
let  | 
| 26970 | 621  | 
val (_, ty) = const_of thy thm;  | 
| 24219 | 622  | 
val ty_decl = Sign.the_const_type thy c;  | 
623  | 
in if Sign.typ_equiv thy (Type.strip_sorts ty_decl, Type.strip_sorts ty)  | 
|
624  | 
then thm  | 
|
625  | 
        else CodeUnit.bad_thm ("Type\n" ^ CodeUnit.string_of_typ thy ty
 | 
|
626  | 
^ "\nof defining equation\n"  | 
|
| 26928 | 627  | 
^ Display.string_of_thm thm  | 
| 24219 | 628  | 
^ "\nis incompatible with declared function type\n"  | 
629  | 
^ CodeUnit.string_of_typ thy ty_decl)  | 
|
630  | 
end;  | 
|
| 
24423
 
ae9cd0e92423
overloaded definitions accompanied by explicit constants
 
haftmann 
parents: 
24283 
diff
changeset
 | 
631  | 
fun check_typ (c, thm) =  | 
| 
25597
 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 
haftmann 
parents: 
25501 
diff
changeset
 | 
632  | 
case AxClass.inst_of_param thy c  | 
| 24837 | 633  | 
of SOME (c, tyco) => check_typ_classparam tyco (c, thm)  | 
| 
24423
 
ae9cd0e92423
overloaded definitions accompanied by explicit constants
 
haftmann 
parents: 
24283 
diff
changeset
 | 
634  | 
| NONE => check_typ_fun (c, thm);  | 
| 
 
ae9cd0e92423
overloaded definitions accompanied by explicit constants
 
haftmann 
parents: 
24283 
diff
changeset
 | 
635  | 
in check_typ (const_of_func thy thm, thm) end;  | 
| 24219 | 636  | 
|
| 24283 | 637  | 
val mk_func = CodeUnit.error_thm (assert_func_typ o CodeUnit.mk_func);  | 
| 
24624
 
b8383b1bbae3
distinction between regular and default code theorems
 
haftmann 
parents: 
24585 
diff
changeset
 | 
638  | 
val mk_liberal_func = CodeUnit.warning_thm (assert_func_typ o CodeUnit.mk_func);  | 
| 
 
b8383b1bbae3
distinction between regular and default code theorems
 
haftmann 
parents: 
24585 
diff
changeset
 | 
639  | 
val mk_default_func = CodeUnit.try_thm (assert_func_typ o CodeUnit.mk_func);  | 
| 24219 | 640  | 
|
641  | 
end;  | 
|
642  | 
||
643  | 
||
644  | 
||
645  | 
(** interfaces and attributes **)  | 
|
646  | 
||
| 
24624
 
b8383b1bbae3
distinction between regular and default code theorems
 
haftmann 
parents: 
24585 
diff
changeset
 | 
647  | 
fun delete_force msg key xs =  | 
| 
 
b8383b1bbae3
distinction between regular and default code theorems
 
haftmann 
parents: 
24585 
diff
changeset
 | 
648  | 
if AList.defined (op =) xs key then AList.delete (op =) key xs  | 
| 
 
b8383b1bbae3
distinction between regular and default code theorems
 
haftmann 
parents: 
24585 
diff
changeset
 | 
649  | 
  else error ("No such " ^ msg ^ ": " ^ quote key);
 | 
| 
 
b8383b1bbae3
distinction between regular and default code theorems
 
haftmann 
parents: 
24585 
diff
changeset
 | 
650  | 
|
| 
24423
 
ae9cd0e92423
overloaded definitions accompanied by explicit constants
 
haftmann 
parents: 
24283 
diff
changeset
 | 
651  | 
fun get_datatype thy tyco =  | 
| 
24844
 
98c006a30218
certificates for code generator case expressions
 
haftmann 
parents: 
24837 
diff
changeset
 | 
652  | 
case Symtab.lookup ((the_dtyps o the_exec) thy) tyco  | 
| 
24423
 
ae9cd0e92423
overloaded definitions accompanied by explicit constants
 
haftmann 
parents: 
24283 
diff
changeset
 | 
653  | 
of SOME spec => spec  | 
| 
 
ae9cd0e92423
overloaded definitions accompanied by explicit constants
 
haftmann 
parents: 
24283 
diff
changeset
 | 
654  | 
| NONE => Sign.arity_number thy tyco  | 
| 24848 | 655  | 
|> Name.invents Name.context Name.aT  | 
| 
24423
 
ae9cd0e92423
overloaded definitions accompanied by explicit constants
 
haftmann 
parents: 
24283 
diff
changeset
 | 
656  | 
|> map (rpair [])  | 
| 
 
ae9cd0e92423
overloaded definitions accompanied by explicit constants
 
haftmann 
parents: 
24283 
diff
changeset
 | 
657  | 
|> rpair [];  | 
| 
 
ae9cd0e92423
overloaded definitions accompanied by explicit constants
 
haftmann 
parents: 
24283 
diff
changeset
 | 
658  | 
|
| 
 
ae9cd0e92423
overloaded definitions accompanied by explicit constants
 
haftmann 
parents: 
24283 
diff
changeset
 | 
659  | 
fun get_datatype_of_constr thy c =  | 
| 
 
ae9cd0e92423
overloaded definitions accompanied by explicit constants
 
haftmann 
parents: 
24283 
diff
changeset
 | 
660  | 
case (snd o strip_type o Sign.the_const_type thy) c  | 
| 
 
ae9cd0e92423
overloaded definitions accompanied by explicit constants
 
haftmann 
parents: 
24283 
diff
changeset
 | 
661  | 
of Type (tyco, _) => if member (op =)  | 
| 
24844
 
98c006a30218
certificates for code generator case expressions
 
haftmann 
parents: 
24837 
diff
changeset
 | 
662  | 
((the_default [] o Option.map (map fst o snd) o Symtab.lookup ((the_dtyps o the_exec) thy)) tyco) c  | 
| 
24423
 
ae9cd0e92423
overloaded definitions accompanied by explicit constants
 
haftmann 
parents: 
24283 
diff
changeset
 | 
663  | 
then SOME tyco else NONE  | 
| 
 
ae9cd0e92423
overloaded definitions accompanied by explicit constants
 
haftmann 
parents: 
24283 
diff
changeset
 | 
664  | 
| _ => NONE;  | 
| 
 
ae9cd0e92423
overloaded definitions accompanied by explicit constants
 
haftmann 
parents: 
24283 
diff
changeset
 | 
665  | 
|
| 
 
ae9cd0e92423
overloaded definitions accompanied by explicit constants
 
haftmann 
parents: 
24283 
diff
changeset
 | 
666  | 
fun get_constr_typ thy c =  | 
| 
 
ae9cd0e92423
overloaded definitions accompanied by explicit constants
 
haftmann 
parents: 
24283 
diff
changeset
 | 
667  | 
case get_datatype_of_constr thy c  | 
| 
 
ae9cd0e92423
overloaded definitions accompanied by explicit constants
 
haftmann 
parents: 
24283 
diff
changeset
 | 
668  | 
of SOME tyco => let  | 
| 
 
ae9cd0e92423
overloaded definitions accompanied by explicit constants
 
haftmann 
parents: 
24283 
diff
changeset
 | 
669  | 
val (vs, cos) = get_datatype thy tyco;  | 
| 
 
ae9cd0e92423
overloaded definitions accompanied by explicit constants
 
haftmann 
parents: 
24283 
diff
changeset
 | 
670  | 
val SOME tys = AList.lookup (op =) cos c;  | 
| 
 
ae9cd0e92423
overloaded definitions accompanied by explicit constants
 
haftmann 
parents: 
24283 
diff
changeset
 | 
671  | 
val ty = tys ---> Type (tyco, map TFree vs);  | 
| 
 
ae9cd0e92423
overloaded definitions accompanied by explicit constants
 
haftmann 
parents: 
24283 
diff
changeset
 | 
672  | 
in SOME (Logic.varifyT ty) end  | 
| 
 
ae9cd0e92423
overloaded definitions accompanied by explicit constants
 
haftmann 
parents: 
24283 
diff
changeset
 | 
673  | 
| NONE => NONE;  | 
| 
 
ae9cd0e92423
overloaded definitions accompanied by explicit constants
 
haftmann 
parents: 
24283 
diff
changeset
 | 
674  | 
|
| 
24844
 
98c006a30218
certificates for code generator case expressions
 
haftmann 
parents: 
24837 
diff
changeset
 | 
675  | 
val get_case_data = Symtab.lookup o fst o the_cases o the_exec;  | 
| 
 
98c006a30218
certificates for code generator case expressions
 
haftmann 
parents: 
24837 
diff
changeset
 | 
676  | 
|
| 
 
98c006a30218
certificates for code generator case expressions
 
haftmann 
parents: 
24837 
diff
changeset
 | 
677  | 
val is_undefined = Symtab.defined o snd o the_cases o the_exec;  | 
| 
 
98c006a30218
certificates for code generator case expressions
 
haftmann 
parents: 
24837 
diff
changeset
 | 
678  | 
|
| 
24624
 
b8383b1bbae3
distinction between regular and default code theorems
 
haftmann 
parents: 
24585 
diff
changeset
 | 
679  | 
fun add_func thm thy =  | 
| 
 
b8383b1bbae3
distinction between regular and default code theorems
 
haftmann 
parents: 
24585 
diff
changeset
 | 
680  | 
let  | 
| 
 
b8383b1bbae3
distinction between regular and default code theorems
 
haftmann 
parents: 
24585 
diff
changeset
 | 
681  | 
val func = mk_func thm;  | 
| 
 
b8383b1bbae3
distinction between regular and default code theorems
 
haftmann 
parents: 
24585 
diff
changeset
 | 
682  | 
val c = const_of_func thy func;  | 
| 
 
b8383b1bbae3
distinction between regular and default code theorems
 
haftmann 
parents: 
24585 
diff
changeset
 | 
683  | 
val _ = if (is_some o AxClass.class_of_param thy) c  | 
| 
 
b8383b1bbae3
distinction between regular and default code theorems
 
haftmann 
parents: 
24585 
diff
changeset
 | 
684  | 
      then error ("Rejected polymorphic equation for overloaded constant:\n"
 | 
| 26928 | 685  | 
^ Display.string_of_thm thm)  | 
| 
24624
 
b8383b1bbae3
distinction between regular and default code theorems
 
haftmann 
parents: 
24585 
diff
changeset
 | 
686  | 
else ();  | 
| 
 
b8383b1bbae3
distinction between regular and default code theorems
 
haftmann 
parents: 
24585 
diff
changeset
 | 
687  | 
val _ = if (is_some o get_datatype_of_constr thy) c  | 
| 
 
b8383b1bbae3
distinction between regular and default code theorems
 
haftmann 
parents: 
24585 
diff
changeset
 | 
688  | 
      then error ("Rejected equation for datatype constructor:\n"
 | 
| 26928 | 689  | 
^ Display.string_of_thm func)  | 
| 
24624
 
b8383b1bbae3
distinction between regular and default code theorems
 
haftmann 
parents: 
24585 
diff
changeset
 | 
690  | 
else ();  | 
| 
 
b8383b1bbae3
distinction between regular and default code theorems
 
haftmann 
parents: 
24585 
diff
changeset
 | 
691  | 
in  | 
| 
 
b8383b1bbae3
distinction between regular and default code theorems
 
haftmann 
parents: 
24585 
diff
changeset
 | 
692  | 
(map_exec_purge (SOME [c]) o map_funcs) (Symtab.map_default  | 
| 27609 | 693  | 
(c, (Susp.value [], [])) (add_thm func)) thy  | 
| 
24624
 
b8383b1bbae3
distinction between regular and default code theorems
 
haftmann 
parents: 
24585 
diff
changeset
 | 
694  | 
end;  | 
| 24219 | 695  | 
|
| 
24624
 
b8383b1bbae3
distinction between regular and default code theorems
 
haftmann 
parents: 
24585 
diff
changeset
 | 
696  | 
fun add_liberal_func thm thy =  | 
| 
 
b8383b1bbae3
distinction between regular and default code theorems
 
haftmann 
parents: 
24585 
diff
changeset
 | 
697  | 
case mk_liberal_func thm  | 
| 
 
b8383b1bbae3
distinction between regular and default code theorems
 
haftmann 
parents: 
24585 
diff
changeset
 | 
698  | 
of SOME func => let  | 
| 
 
b8383b1bbae3
distinction between regular and default code theorems
 
haftmann 
parents: 
24585 
diff
changeset
 | 
699  | 
val c = const_of_func thy func  | 
| 
 
b8383b1bbae3
distinction between regular and default code theorems
 
haftmann 
parents: 
24585 
diff
changeset
 | 
700  | 
in if (is_some o AxClass.class_of_param thy) c  | 
| 
 
b8383b1bbae3
distinction between regular and default code theorems
 
haftmann 
parents: 
24585 
diff
changeset
 | 
701  | 
orelse (is_some o get_datatype_of_constr thy) c  | 
| 
 
b8383b1bbae3
distinction between regular and default code theorems
 
haftmann 
parents: 
24585 
diff
changeset
 | 
702  | 
then thy  | 
| 
 
b8383b1bbae3
distinction between regular and default code theorems
 
haftmann 
parents: 
24585 
diff
changeset
 | 
703  | 
else map_exec_purge (SOME [c]) (map_funcs  | 
| 
 
b8383b1bbae3
distinction between regular and default code theorems
 
haftmann 
parents: 
24585 
diff
changeset
 | 
704  | 
(Symtab.map_default  | 
| 27609 | 705  | 
(c, (Susp.value [], [])) (add_thm func))) thy  | 
| 
24624
 
b8383b1bbae3
distinction between regular and default code theorems
 
haftmann 
parents: 
24585 
diff
changeset
 | 
706  | 
end  | 
| 
 
b8383b1bbae3
distinction between regular and default code theorems
 
haftmann 
parents: 
24585 
diff
changeset
 | 
707  | 
| NONE => thy;  | 
| 
 
b8383b1bbae3
distinction between regular and default code theorems
 
haftmann 
parents: 
24585 
diff
changeset
 | 
708  | 
|
| 
 
b8383b1bbae3
distinction between regular and default code theorems
 
haftmann 
parents: 
24585 
diff
changeset
 | 
709  | 
fun add_default_func thm thy =  | 
| 
 
b8383b1bbae3
distinction between regular and default code theorems
 
haftmann 
parents: 
24585 
diff
changeset
 | 
710  | 
case mk_default_func thm  | 
| 
 
b8383b1bbae3
distinction between regular and default code theorems
 
haftmann 
parents: 
24585 
diff
changeset
 | 
711  | 
of SOME func => let  | 
| 
 
b8383b1bbae3
distinction between regular and default code theorems
 
haftmann 
parents: 
24585 
diff
changeset
 | 
712  | 
val c = const_of_func thy func  | 
| 
 
b8383b1bbae3
distinction between regular and default code theorems
 
haftmann 
parents: 
24585 
diff
changeset
 | 
713  | 
in if (is_some o AxClass.class_of_param thy) c  | 
| 
 
b8383b1bbae3
distinction between regular and default code theorems
 
haftmann 
parents: 
24585 
diff
changeset
 | 
714  | 
orelse (is_some o get_datatype_of_constr thy) c  | 
| 
 
b8383b1bbae3
distinction between regular and default code theorems
 
haftmann 
parents: 
24585 
diff
changeset
 | 
715  | 
then thy  | 
| 
 
b8383b1bbae3
distinction between regular and default code theorems
 
haftmann 
parents: 
24585 
diff
changeset
 | 
716  | 
else map_exec_purge (SOME [c]) (map_funcs  | 
| 
 
b8383b1bbae3
distinction between regular and default code theorems
 
haftmann 
parents: 
24585 
diff
changeset
 | 
717  | 
(Symtab.map_default  | 
| 27609 | 718  | 
(c, (Susp.value [], [])) (add_thm func))) thy  | 
| 
24624
 
b8383b1bbae3
distinction between regular and default code theorems
 
haftmann 
parents: 
24585 
diff
changeset
 | 
719  | 
end  | 
| 
 
b8383b1bbae3
distinction between regular and default code theorems
 
haftmann 
parents: 
24585 
diff
changeset
 | 
720  | 
| NONE => thy;  | 
| 24219 | 721  | 
|
722  | 
fun del_func thm thy =  | 
|
| 24659 | 723  | 
case mk_liberal_func thm  | 
724  | 
of SOME func => let  | 
|
725  | 
val c = const_of_func thy func;  | 
|
726  | 
in map_exec_purge (SOME [c]) (map_funcs  | 
|
| 27609 | 727  | 
(Symtab.map_entry c (del_thm func))) thy  | 
| 24659 | 728  | 
end  | 
729  | 
| NONE => thy;  | 
|
| 24219 | 730  | 
|
| 26021 | 731  | 
fun del_funcs const = map_exec_purge (SOME [const])  | 
| 27609 | 732  | 
(map_funcs (Symtab.map_entry const del_thms));  | 
| 26021 | 733  | 
|
| 24219 | 734  | 
fun add_funcl (const, lthms) thy =  | 
735  | 
let  | 
|
736  | 
val lthms' = certificate thy (fn thy => certify_const thy const) lthms;  | 
|
737  | 
(*FIXME must check compatibility with sort algebra;  | 
|
738  | 
alas, naive checking results in non-termination!*)  | 
|
739  | 
in  | 
|
| 25312 | 740  | 
map_exec_purge (SOME [const])  | 
| 27609 | 741  | 
(map_funcs (Symtab.map_default (const, (Susp.value [], []))  | 
742  | 
(add_lthms lthms'))) thy  | 
|
| 24219 | 743  | 
end;  | 
744  | 
||
| 
24624
 
b8383b1bbae3
distinction between regular and default code theorems
 
haftmann 
parents: 
24585 
diff
changeset
 | 
745  | 
val add_default_func_attr = Attrib.internal (fn _ => Thm.declaration_attribute  | 
| 
 
b8383b1bbae3
distinction between regular and default code theorems
 
haftmann 
parents: 
24585 
diff
changeset
 | 
746  | 
(fn thm => Context.mapping (add_default_func thm) I));  | 
| 24219 | 747  | 
|
| 25485 | 748  | 
structure TypeInterpretation = InterpretationFun(type T = string * serial val eq = eq_snd (op =) : T * T -> bool);  | 
| 25462 | 749  | 
|
| 
24423
 
ae9cd0e92423
overloaded definitions accompanied by explicit constants
 
haftmann 
parents: 
24283 
diff
changeset
 | 
750  | 
fun add_datatype raw_cs thy =  | 
| 24219 | 751  | 
let  | 
| 
25597
 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 
haftmann 
parents: 
25501 
diff
changeset
 | 
752  | 
val cs = map (fn c_ty as (_, ty) => (AxClass.unoverload_const thy c_ty, ty)) raw_cs;  | 
| 
24423
 
ae9cd0e92423
overloaded definitions accompanied by explicit constants
 
haftmann 
parents: 
24283 
diff
changeset
 | 
753  | 
val (tyco, vs_cos) = CodeUnit.constrset_of_consts thy cs;  | 
| 25462 | 754  | 
val cs' = map fst (snd vs_cos);  | 
755  | 
val purge_cs = case Symtab.lookup ((the_dtyps o the_exec) thy) tyco  | 
|
756  | 
of SOME (vs, cos) => if null cos then NONE else SOME (cs' @ map fst cos)  | 
|
| 
24423
 
ae9cd0e92423
overloaded definitions accompanied by explicit constants
 
haftmann 
parents: 
24283 
diff
changeset
 | 
757  | 
| NONE => NONE;  | 
| 24219 | 758  | 
in  | 
759  | 
thy  | 
|
| 25462 | 760  | 
|> map_exec_purge purge_cs (map_dtyps (Symtab.update (tyco, vs_cos))  | 
| 
24423
 
ae9cd0e92423
overloaded definitions accompanied by explicit constants
 
haftmann 
parents: 
24283 
diff
changeset
 | 
761  | 
#> map_funcs (fold (Symtab.delete_safe o fst) cs))  | 
| 25485 | 762  | 
|> TypeInterpretation.data (tyco, serial ())  | 
| 24219 | 763  | 
end;  | 
764  | 
||
| 25485 | 765  | 
fun type_interpretation f = TypeInterpretation.interpretation  | 
766  | 
(fn (tyco, _) => fn thy => f (tyco, get_datatype thy tyco) thy);  | 
|
767  | 
||
| 
24423
 
ae9cd0e92423
overloaded definitions accompanied by explicit constants
 
haftmann 
parents: 
24283 
diff
changeset
 | 
768  | 
fun add_datatype_cmd raw_cs thy =  | 
| 
 
ae9cd0e92423
overloaded definitions accompanied by explicit constants
 
haftmann 
parents: 
24283 
diff
changeset
 | 
769  | 
let  | 
| 
 
ae9cd0e92423
overloaded definitions accompanied by explicit constants
 
haftmann 
parents: 
24283 
diff
changeset
 | 
770  | 
val cs = map (CodeUnit.read_bare_const thy) raw_cs;  | 
| 
 
ae9cd0e92423
overloaded definitions accompanied by explicit constants
 
haftmann 
parents: 
24283 
diff
changeset
 | 
771  | 
in add_datatype cs thy end;  | 
| 24219 | 772  | 
|
| 
24844
 
98c006a30218
certificates for code generator case expressions
 
haftmann 
parents: 
24837 
diff
changeset
 | 
773  | 
fun add_case thm thy =  | 
| 
 
98c006a30218
certificates for code generator case expressions
 
haftmann 
parents: 
24837 
diff
changeset
 | 
774  | 
let  | 
| 
 
98c006a30218
certificates for code generator case expressions
 
haftmann 
parents: 
24837 
diff
changeset
 | 
775  | 
val entry as (c, _) = CodeUnit.case_cert thm;  | 
| 
 
98c006a30218
certificates for code generator case expressions
 
haftmann 
parents: 
24837 
diff
changeset
 | 
776  | 
in  | 
| 
 
98c006a30218
certificates for code generator case expressions
 
haftmann 
parents: 
24837 
diff
changeset
 | 
777  | 
(map_exec_purge (SOME [c]) o map_cases o apfst) (Symtab.update entry) thy  | 
| 
 
98c006a30218
certificates for code generator case expressions
 
haftmann 
parents: 
24837 
diff
changeset
 | 
778  | 
end;  | 
| 
 
98c006a30218
certificates for code generator case expressions
 
haftmann 
parents: 
24837 
diff
changeset
 | 
779  | 
|
| 
 
98c006a30218
certificates for code generator case expressions
 
haftmann 
parents: 
24837 
diff
changeset
 | 
780  | 
fun add_undefined c thy =  | 
| 
 
98c006a30218
certificates for code generator case expressions
 
haftmann 
parents: 
24837 
diff
changeset
 | 
781  | 
(map_exec_purge (SOME [c]) o map_cases o apsnd) (Symtab.update (c, ())) thy;  | 
| 
 
98c006a30218
certificates for code generator case expressions
 
haftmann 
parents: 
24837 
diff
changeset
 | 
782  | 
|
| 
27557
 
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
 
haftmann 
parents: 
26970 
diff
changeset
 | 
783  | 
val map_pre = map_exec_purge NONE o map_thmproc o apfst o apfst;  | 
| 
 
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
 
haftmann 
parents: 
26970 
diff
changeset
 | 
784  | 
val map_post = map_exec_purge NONE o map_thmproc o apfst o apsnd;  | 
| 
 
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
 
haftmann 
parents: 
26970 
diff
changeset
 | 
785  | 
|
| 
 
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
 
haftmann 
parents: 
26970 
diff
changeset
 | 
786  | 
fun add_inline thm thy = (map_pre o MetaSimplifier.add_simp)  | 
| 
 
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
 
haftmann 
parents: 
26970 
diff
changeset
 | 
787  | 
(CodeUnit.error_thm CodeUnit.mk_rew thm) thy;  | 
| 
 
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
 
haftmann 
parents: 
26970 
diff
changeset
 | 
788  | 
(*fully applied in order to get right context for mk_rew!*)  | 
| 
 
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
 
haftmann 
parents: 
26970 
diff
changeset
 | 
789  | 
|
| 
 
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
 
haftmann 
parents: 
26970 
diff
changeset
 | 
790  | 
fun del_inline thm thy = (map_pre o MetaSimplifier.del_simp)  | 
| 
 
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
 
haftmann 
parents: 
26970 
diff
changeset
 | 
791  | 
(CodeUnit.error_thm CodeUnit.mk_rew thm) thy;  | 
| 
 
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
 
haftmann 
parents: 
26970 
diff
changeset
 | 
792  | 
(*fully applied in order to get right context for mk_rew!*)  | 
| 24219 | 793  | 
|
| 
27557
 
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
 
haftmann 
parents: 
26970 
diff
changeset
 | 
794  | 
fun add_post thm thy = (map_post o MetaSimplifier.add_simp)  | 
| 
 
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
 
haftmann 
parents: 
26970 
diff
changeset
 | 
795  | 
(CodeUnit.error_thm CodeUnit.mk_rew thm) thy;  | 
| 
 
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
 
haftmann 
parents: 
26970 
diff
changeset
 | 
796  | 
(*fully applied in order to get right context for mk_rew!*)  | 
| 24219 | 797  | 
|
| 
27557
 
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
 
haftmann 
parents: 
26970 
diff
changeset
 | 
798  | 
fun del_post thm thy = (map_post o MetaSimplifier.del_simp)  | 
| 
 
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
 
haftmann 
parents: 
26970 
diff
changeset
 | 
799  | 
(CodeUnit.error_thm CodeUnit.mk_rew thm) thy;  | 
| 
 
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
 
haftmann 
parents: 
26970 
diff
changeset
 | 
800  | 
(*fully applied in order to get right context for mk_rew!*)  | 
| 
 
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
 
haftmann 
parents: 
26970 
diff
changeset
 | 
801  | 
|
| 
 
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
 
haftmann 
parents: 
26970 
diff
changeset
 | 
802  | 
fun add_functrans (name, f) =  | 
| 
 
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
 
haftmann 
parents: 
26970 
diff
changeset
 | 
803  | 
(map_exec_purge NONE o map_thmproc o apsnd)  | 
| 24219 | 804  | 
(AList.update (op =) (name, (serial (), f)));  | 
805  | 
||
| 
27557
 
151731493264
simpsets as pre/postprocessors; generic preprocessor now named function transformators
 
haftmann 
parents: 
26970 
diff
changeset
 | 
806  | 
fun del_functrans name =  | 
| 24219 | 807  | 
(map_exec_purge NONE o map_thmproc o apsnd)  | 
| 27609 | 808  | 
(delete_force "function transformer" name);  | 
| 24219 | 809  | 
|
| 26463 | 810  | 
val _ = Context.>> (Context.map_theory  | 
| 24219 | 811  | 
(let  | 
812  | 
fun mk_attribute f = Thm.declaration_attribute (fn thm => Context.mapping (f thm) I);  | 
|
813  | 
fun add_simple_attribute (name, f) =  | 
|
814  | 
add_attribute (name, Scan.succeed (mk_attribute f));  | 
|
815  | 
fun add_del_attribute (name, (add, del)) =  | 
|
816  | 
add_attribute (name, Args.del |-- Scan.succeed (mk_attribute del)  | 
|
817  | 
|| Scan.succeed (mk_attribute add))  | 
|
818  | 
in  | 
|
| 25462 | 819  | 
TypeInterpretation.init  | 
820  | 
    #> add_del_attribute ("func", (add_func, del_func))
 | 
|
| 24219 | 821  | 
    #> add_del_attribute ("inline", (add_inline, del_inline))
 | 
822  | 
    #> add_del_attribute ("post", (add_post, del_post))
 | 
|
| 26463 | 823  | 
end));  | 
| 24219 | 824  | 
|
825  | 
||
826  | 
(** post- and preprocessing **)  | 
|
827  | 
||
828  | 
local  | 
|
829  | 
||
| 27609 | 830  | 
fun apply_functrans thy [] = []  | 
831  | 
| apply_functrans thy (thms as thm :: _) =  | 
|
| 24219 | 832  | 
let  | 
| 
24423
 
ae9cd0e92423
overloaded definitions accompanied by explicit constants
 
haftmann 
parents: 
24283 
diff
changeset
 | 
833  | 
val const = const_of_func thy thm;  | 
| 27609 | 834  | 
val functrans = (map (fn (_, (_, f)) => f thy) o #functrans  | 
835  | 
o the_thmproc o the_exec) thy;  | 
|
836  | 
val thms' = perhaps (perhaps_loop (perhaps_apply functrans)) thms;  | 
|
| 24219 | 837  | 
in certify_const thy const thms' end;  | 
838  | 
||
839  | 
fun rhs_conv conv thm =  | 
|
840  | 
let  | 
|
841  | 
val thm' = (conv o Thm.rhs_of) thm;  | 
|
842  | 
in Thm.transitive thm thm' end  | 
|
843  | 
||
| 24837 | 844  | 
fun term_of_conv thy f =  | 
845  | 
Thm.cterm_of thy  | 
|
846  | 
#> f  | 
|
847  | 
#> Thm.prop_of  | 
|
848  | 
#> Logic.dest_equals  | 
|
849  | 
#> snd;  | 
|
850  | 
||
| 24219 | 851  | 
in  | 
852  | 
||
853  | 
fun preprocess thy thms =  | 
|
| 27582 | 854  | 
let  | 
855  | 
val pre = (Simplifier.theory_context thy o #pre o the_thmproc o the_exec) thy;  | 
|
856  | 
in  | 
|
857  | 
thms  | 
|
| 27609 | 858  | 
|> apply_functrans thy  | 
| 27582 | 859  | 
|> map (CodeUnit.rewrite_func pre)  | 
860  | 
(*FIXME - must check gere: rewrite rule, defining equation, proper constant *)  | 
|
861  | 
|> map (AxClass.unoverload thy)  | 
|
862  | 
|> common_typ_funcs  | 
|
863  | 
end;  | 
|
| 26970 | 864  | 
|
| 24219 | 865  | 
|
866  | 
fun preprocess_conv ct =  | 
|
867  | 
let  | 
|
868  | 
val thy = Thm.theory_of_cterm ct;  | 
|
| 27582 | 869  | 
val pre = (Simplifier.theory_context thy o #pre o the_thmproc o the_exec) thy;  | 
| 24219 | 870  | 
in  | 
871  | 
ct  | 
|
| 27582 | 872  | 
|> Simplifier.rewrite pre  | 
| 
25597
 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 
haftmann 
parents: 
25501 
diff
changeset
 | 
873  | 
|> rhs_conv (AxClass.unoverload_conv thy)  | 
| 24219 | 874  | 
end;  | 
875  | 
||
| 24837 | 876  | 
fun preprocess_term thy = term_of_conv thy preprocess_conv;  | 
877  | 
||
| 24219 | 878  | 
fun postprocess_conv ct =  | 
879  | 
let  | 
|
880  | 
val thy = Thm.theory_of_cterm ct;  | 
|
| 27582 | 881  | 
val post = (Simplifier.theory_context thy o #post o the_thmproc o the_exec) thy;  | 
| 24219 | 882  | 
in  | 
883  | 
ct  | 
|
| 
25597
 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 
haftmann 
parents: 
25501 
diff
changeset
 | 
884  | 
|> AxClass.overload_conv thy  | 
| 27582 | 885  | 
|> rhs_conv (Simplifier.rewrite post)  | 
| 24219 | 886  | 
end;  | 
887  | 
||
| 24837 | 888  | 
fun postprocess_term thy = term_of_conv thy postprocess_conv;  | 
889  | 
||
| 24219 | 890  | 
end; (*local*)  | 
891  | 
||
| 
25597
 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 
haftmann 
parents: 
25501 
diff
changeset
 | 
892  | 
fun default_typ_proto thy c = case AxClass.inst_of_param thy c  | 
| 24837 | 893  | 
of SOME (c, tyco) => classparam_weakest_typ thy ((the o AxClass.class_of_param thy) c)  | 
| 
24423
 
ae9cd0e92423
overloaded definitions accompanied by explicit constants
 
haftmann 
parents: 
24283 
diff
changeset
 | 
894  | 
(c, tyco) |> SOME  | 
| 
 
ae9cd0e92423
overloaded definitions accompanied by explicit constants
 
haftmann 
parents: 
24283 
diff
changeset
 | 
895  | 
| NONE => (case AxClass.class_of_param thy c  | 
| 
 
ae9cd0e92423
overloaded definitions accompanied by explicit constants
 
haftmann 
parents: 
24283 
diff
changeset
 | 
896  | 
of SOME class => SOME (Term.map_type_tvar  | 
| 24848 | 897  | 
(K (TVar ((Name.aT, 0), [class]))) (Sign.the_const_type thy c))  | 
| 
24423
 
ae9cd0e92423
overloaded definitions accompanied by explicit constants
 
haftmann 
parents: 
24283 
diff
changeset
 | 
898  | 
| NONE => get_constr_typ thy c);  | 
| 24219 | 899  | 
|
900  | 
local  | 
|
901  | 
||
902  | 
fun get_funcs thy const =  | 
|
| 
24844
 
98c006a30218
certificates for code generator case expressions
 
haftmann 
parents: 
24837 
diff
changeset
 | 
903  | 
Symtab.lookup ((the_funcs o the_exec) thy) const  | 
| 27609 | 904  | 
|> Option.map (Susp.force o fst)  | 
| 24219 | 905  | 
|> these  | 
906  | 
|> map (Thm.transfer thy);  | 
|
907  | 
||
908  | 
in  | 
|
909  | 
||
910  | 
fun these_funcs thy const =  | 
|
911  | 
let  | 
|
912  | 
fun drop_refl thy = filter_out (is_equal o Term.fast_term_ord o Logic.dest_equals  | 
|
913  | 
o ObjectLogic.drop_judgment thy o Thm.plain_prop_of);  | 
|
914  | 
in  | 
|
915  | 
get_funcs thy const  | 
|
916  | 
|> preprocess thy  | 
|
917  | 
|> drop_refl thy  | 
|
918  | 
end;  | 
|
919  | 
||
| 
24423
 
ae9cd0e92423
overloaded definitions accompanied by explicit constants
 
haftmann 
parents: 
24283 
diff
changeset
 | 
920  | 
fun default_typ thy c = case default_typ_proto thy c  | 
| 26970 | 921  | 
of SOME ty => CodeUnit.typscheme thy (c, ty)  | 
| 
24423
 
ae9cd0e92423
overloaded definitions accompanied by explicit constants
 
haftmann 
parents: 
24283 
diff
changeset
 | 
922  | 
| NONE => (case get_funcs thy c  | 
| 
25597
 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 
haftmann 
parents: 
25501 
diff
changeset
 | 
923  | 
of thm :: _ => snd (CodeUnit.head_func (AxClass.unoverload thy thm))  | 
| 26970 | 924  | 
| [] => CodeUnit.typscheme thy (c, Sign.the_const_type thy c));  | 
| 24219 | 925  | 
|
926  | 
end; (*local*)  | 
|
927  | 
||
928  | 
end; (*struct*)  | 
|
929  | 
||
930  | 
||
931  | 
(** type-safe interfaces for data depedent on executable content **)  | 
|
932  | 
||
933  | 
functor CodeDataFun(Data: CODE_DATA_ARGS): CODE_DATA =  | 
|
934  | 
struct  | 
|
935  | 
||
936  | 
type T = Data.T;  | 
|
937  | 
exception Data of T;  | 
|
938  | 
fun dest (Data x) = x  | 
|
939  | 
||
940  | 
val kind = Code.declare_data (Data Data.empty)  | 
|
| 27609 | 941  | 
(fn thy => fn cs => fn Data x => Data (Data.purge thy cs x));  | 
| 24219 | 942  | 
|
943  | 
val data_op = (kind, Data, dest);  | 
|
944  | 
||
945  | 
val get = Code.get_data data_op;  | 
|
946  | 
val change = Code.change_data data_op;  | 
|
947  | 
fun change_yield thy = Code.change_yield_data data_op thy;  | 
|
948  | 
||
949  | 
end;  | 
|
950  | 
||
951  | 
structure Code : CODE =  | 
|
952  | 
struct  | 
|
953  | 
||
954  | 
open Code;  | 
|
955  | 
||
956  | 
end;  |