| author | paulson |
| Fri, 14 Mar 2003 10:30:15 +0100 | |
| changeset 13860 | b681a3cb0beb |
| parent 13753 | 38b76f457b9c |
| child 13886 | 0b243f6e257e |
| permissions | -rw-r--r-- |
| 11520 | 1 |
(* Title: Pure/codegen.ML |
2 |
ID: $Id$ |
|
| 11539 | 3 |
Author: Stefan Berghofer, TU Muenchen |
4 |
License: GPL (GNU GENERAL PUBLIC LICENSE) |
|
| 11520 | 5 |
|
| 11539 | 6 |
Generic code generator. |
| 11520 | 7 |
*) |
8 |
||
9 |
signature CODEGEN = |
|
10 |
sig |
|
11 |
val quiet_mode : bool ref |
|
12 |
val message : string -> unit |
|
|
13753
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
13 |
val mode : string list ref |
| 11520 | 14 |
|
| 12452 | 15 |
datatype 'a mixfix = |
| 11520 | 16 |
Arg |
17 |
| Ignore |
|
18 |
| Pretty of Pretty.T |
|
| 12452 | 19 |
| Quote of 'a; |
| 11520 | 20 |
|
| 12452 | 21 |
type 'a codegen |
22 |
||
23 |
val add_codegen: string -> term codegen -> theory -> theory |
|
24 |
val add_tycodegen: string -> typ codegen -> theory -> theory |
|
|
12555
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
25 |
val add_attribute: string -> theory attribute -> theory -> theory |
| 11520 | 26 |
val print_codegens: theory -> unit |
27 |
val generate_code: theory -> (string * string) list -> string |
|
28 |
val generate_code_i: theory -> (string * term) list -> string |
|
| 12452 | 29 |
val assoc_consts: (xstring * string option * term mixfix list) list -> theory -> theory |
30 |
val assoc_consts_i: (xstring * typ option * term mixfix list) list -> theory -> theory |
|
31 |
val assoc_types: (xstring * typ mixfix list) list -> theory -> theory |
|
32 |
val get_assoc_code: theory -> string -> typ -> term mixfix list option |
|
33 |
val get_assoc_type: theory -> string -> typ mixfix list option |
|
34 |
val invoke_codegen: theory -> string -> bool -> |
|
35 |
(exn option * string) Graph.T * term -> (exn option * string) Graph.T * Pretty.T |
|
36 |
val invoke_tycodegen: theory -> string -> bool -> |
|
37 |
(exn option * string) Graph.T * typ -> (exn option * string) Graph.T * Pretty.T |
|
| 11520 | 38 |
val mk_const_id: Sign.sg -> string -> string |
39 |
val mk_type_id: Sign.sg -> string -> string |
|
40 |
val rename_term: term -> term |
|
41 |
val get_defn: theory -> string -> typ -> ((term list * term) * int option) option |
|
42 |
val is_instance: theory -> typ -> typ -> bool |
|
43 |
val parens: Pretty.T -> Pretty.T |
|
44 |
val mk_app: bool -> Pretty.T -> Pretty.T list -> Pretty.T |
|
45 |
val eta_expand: term -> term list -> int -> term |
|
|
13753
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
46 |
val mk_type: bool -> typ -> Pretty.T |
|
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
47 |
val mk_term_of: Sign.sg -> bool -> typ -> Pretty.T |
| 12452 | 48 |
val parse_mixfix: (string -> 'a) -> string -> 'a mixfix list |
| 11520 | 49 |
val parsers: OuterSyntax.parser list |
50 |
val setup: (theory -> theory) list |
|
51 |
end; |
|
52 |
||
53 |
structure Codegen : CODEGEN = |
|
54 |
struct |
|
55 |
||
56 |
val quiet_mode = ref true; |
|
57 |
fun message s = if !quiet_mode then () else writeln s; |
|
58 |
||
|
13753
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
59 |
val mode = ref ([] : string list); |
|
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
60 |
|
| 11520 | 61 |
(**** Mixfix syntax ****) |
62 |
||
| 12452 | 63 |
datatype 'a mixfix = |
| 11520 | 64 |
Arg |
65 |
| Ignore |
|
66 |
| Pretty of Pretty.T |
|
| 12452 | 67 |
| Quote of 'a; |
| 11520 | 68 |
|
69 |
fun is_arg Arg = true |
|
70 |
| is_arg Ignore = true |
|
71 |
| is_arg _ = false; |
|
72 |
||
| 12452 | 73 |
fun quotes_of [] = [] |
74 |
| quotes_of (Quote q :: ms) = q :: quotes_of ms |
|
75 |
| quotes_of (_ :: ms) = quotes_of ms; |
|
76 |
||
77 |
fun args_of [] xs = ([], xs) |
|
78 |
| args_of (Arg :: ms) (x :: xs) = apfst (cons x) (args_of ms xs) |
|
79 |
| args_of (Ignore :: ms) (_ :: xs) = args_of ms xs |
|
80 |
| args_of (_ :: ms) xs = args_of ms xs; |
|
| 11520 | 81 |
|
| 12490 | 82 |
fun num_args x = length (filter is_arg x); |
| 11520 | 83 |
|
84 |
||
85 |
(**** theory data ****) |
|
86 |
||
87 |
(* data kind 'Pure/codegen' *) |
|
88 |
||
| 12452 | 89 |
type 'a codegen = theory -> (exn option * string) Graph.T -> |
90 |
string -> bool -> 'a -> ((exn option * string) Graph.T * Pretty.T) option; |
|
91 |
||
| 11520 | 92 |
structure CodegenArgs = |
93 |
struct |
|
94 |
val name = "Pure/codegen"; |
|
95 |
type T = |
|
| 12452 | 96 |
{codegens : (string * term codegen) list,
|
97 |
tycodegens : (string * typ codegen) list, |
|
98 |
consts : ((string * typ) * term mixfix list) list, |
|
|
12555
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
99 |
types : (string * typ mixfix list) list, |
|
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
100 |
attrs: (string * theory attribute) list}; |
| 11520 | 101 |
|
|
12555
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
102 |
val empty = |
|
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
103 |
{codegens = [], tycodegens = [], consts = [], types = [], attrs = []};
|
| 11520 | 104 |
val copy = I; |
105 |
val prep_ext = I; |
|
106 |
||
|
12555
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
107 |
fun merge |
|
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
108 |
({codegens = codegens1, tycodegens = tycodegens1,
|
|
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
109 |
consts = consts1, types = types1, attrs = attrs1}, |
|
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
110 |
{codegens = codegens2, tycodegens = tycodegens2,
|
|
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
111 |
consts = consts2, types = types2, attrs = attrs2}) = |
| 11520 | 112 |
{codegens = rev (merge_alists (rev codegens1) (rev codegens2)),
|
| 12452 | 113 |
tycodegens = rev (merge_alists (rev tycodegens1) (rev tycodegens2)), |
|
12555
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
114 |
consts = merge_alists consts1 consts2, |
|
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
115 |
types = merge_alists types1 types2, |
|
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
116 |
attrs = merge_alists attrs1 attrs2}; |
| 11520 | 117 |
|
| 12452 | 118 |
fun print sg ({codegens, tycodegens, ...} : T) =
|
119 |
Pretty.writeln (Pretty.chunks |
|
120 |
[Pretty.strs ("term code generators:" :: map fst codegens),
|
|
121 |
Pretty.strs ("type code generators:" :: map fst tycodegens)]);
|
|
| 11520 | 122 |
end; |
123 |
||
124 |
structure CodegenData = TheoryDataFun(CodegenArgs); |
|
125 |
val print_codegens = CodegenData.print; |
|
126 |
||
127 |
||
| 12452 | 128 |
(**** add new code generators to theory ****) |
| 11520 | 129 |
|
130 |
fun add_codegen name f thy = |
|
|
12555
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
131 |
let val {codegens, tycodegens, consts, types, attrs} = CodegenData.get thy
|
| 11520 | 132 |
in (case assoc (codegens, name) of |
| 12452 | 133 |
None => CodegenData.put {codegens = (name, f) :: codegens,
|
|
12555
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
134 |
tycodegens = tycodegens, consts = consts, types = types, |
|
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
135 |
attrs = attrs} thy |
| 12452 | 136 |
| Some _ => error ("Code generator " ^ name ^ " already declared"))
|
137 |
end; |
|
138 |
||
139 |
fun add_tycodegen name f thy = |
|
|
12555
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
140 |
let val {codegens, tycodegens, consts, types, attrs} = CodegenData.get thy
|
| 12452 | 141 |
in (case assoc (tycodegens, name) of |
142 |
None => CodegenData.put {tycodegens = (name, f) :: tycodegens,
|
|
|
12555
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
143 |
codegens = codegens, consts = consts, types = types, |
|
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
144 |
attrs = attrs} thy |
| 11520 | 145 |
| Some _ => error ("Code generator " ^ name ^ " already declared"))
|
146 |
end; |
|
147 |
||
148 |
||
|
12555
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
149 |
(**** code attribute ****) |
|
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
150 |
|
|
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
151 |
fun add_attribute name att thy = |
|
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
152 |
let val {codegens, tycodegens, consts, types, attrs} = CodegenData.get thy
|
|
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
153 |
in (case assoc (attrs, name) of |
|
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
154 |
None => CodegenData.put {tycodegens = tycodegens,
|
|
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
155 |
codegens = codegens, consts = consts, types = types, |
|
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
156 |
attrs = (name, att) :: attrs} thy |
|
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
157 |
| Some _ => error ("Code attribute " ^ name ^ " already declared"))
|
|
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
158 |
end; |
|
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
159 |
|
|
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
160 |
val code_attr = |
|
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
161 |
Attrib.syntax (Scan.depend (fn thy => Scan.optional Args.name "" >> |
|
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
162 |
(fn name => (thy, case assoc (#attrs (CodegenData.get thy), name) of |
|
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
163 |
None => error ("Unknown code attribute: " ^ quote name)
|
|
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
164 |
| Some att => att)))); |
|
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
165 |
|
|
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
166 |
|
| 11520 | 167 |
(**** associate constants with target language code ****) |
168 |
||
169 |
fun gen_assoc_consts prep_type xs thy = foldl (fn (thy, (s, tyopt, syn)) => |
|
170 |
let |
|
171 |
val sg = sign_of thy; |
|
|
12555
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
172 |
val {codegens, tycodegens, consts, types, attrs} = CodegenData.get thy;
|
| 11520 | 173 |
val cname = Sign.intern_const sg s; |
174 |
in |
|
175 |
(case Sign.const_type sg cname of |
|
176 |
Some T => |
|
177 |
let val T' = (case tyopt of |
|
178 |
None => T |
|
179 |
| Some ty => |
|
180 |
let val U = prep_type sg ty |
|
181 |
in if Type.typ_instance (Sign.tsig_of sg, U, T) then U |
|
182 |
else error ("Illegal type constraint for constant " ^ cname)
|
|
183 |
end) |
|
184 |
in (case assoc (consts, (cname, T')) of |
|
185 |
None => CodegenData.put {codegens = codegens,
|
|
| 12452 | 186 |
tycodegens = tycodegens, |
|
12555
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
187 |
consts = ((cname, T'), syn) :: consts, |
|
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
188 |
types = types, attrs = attrs} thy |
| 11520 | 189 |
| Some _ => error ("Constant " ^ cname ^ " already associated with code"))
|
190 |
end |
|
191 |
| _ => error ("Not a constant: " ^ s))
|
|
192 |
end) (thy, xs); |
|
193 |
||
194 |
val assoc_consts_i = gen_assoc_consts (K I); |
|
195 |
val assoc_consts = gen_assoc_consts (fn sg => typ_of o read_ctyp sg); |
|
196 |
||
197 |
(**** associate types with target language types ****) |
|
198 |
||
199 |
fun assoc_types xs thy = foldl (fn (thy, (s, syn)) => |
|
200 |
let |
|
|
12555
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
201 |
val {codegens, tycodegens, consts, types, attrs} = CodegenData.get thy;
|
| 11520 | 202 |
val tc = Sign.intern_tycon (sign_of thy) s |
203 |
in |
|
204 |
(case assoc (types, tc) of |
|
| 12452 | 205 |
None => CodegenData.put {codegens = codegens,
|
206 |
tycodegens = tycodegens, consts = consts, |
|
|
12555
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
207 |
types = (tc, syn) :: types, attrs = attrs} thy |
| 11520 | 208 |
| Some _ => error ("Type " ^ tc ^ " already associated with code"))
|
209 |
end) (thy, xs); |
|
210 |
||
| 12452 | 211 |
fun get_assoc_type thy s = assoc (#types (CodegenData.get thy), s); |
| 11546 | 212 |
|
| 11520 | 213 |
|
214 |
(**** make valid ML identifiers ****) |
|
215 |
||
216 |
fun gen_mk_id kind rename sg s = |
|
217 |
let |
|
218 |
val (xs as x::_) = explode (rename (space_implode "_" |
|
219 |
(NameSpace.unpack (Sign.cond_extern sg kind s)))); |
|
220 |
fun check_str [] = "" |
|
|
12555
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
221 |
| check_str (" " :: xs) = "_" ^ check_str xs
|
|
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
222 |
| check_str (x :: xs) = |
| 12824 | 223 |
(if Symbol.is_letdig x then x |
| 11520 | 224 |
else "_" ^ string_of_int (ord x)) ^ check_str xs |
225 |
in |
|
226 |
(if not (Symbol.is_letter x) then "id" else "") ^ check_str xs |
|
227 |
end; |
|
228 |
||
|
13073
cc9d7f403a4b
mk_const_id now checks for clashes with reserved ML identifiers.
berghofe
parents:
13003
diff
changeset
|
229 |
val mk_const_id = gen_mk_id Sign.constK |
|
cc9d7f403a4b
mk_const_id now checks for clashes with reserved ML identifiers.
berghofe
parents:
13003
diff
changeset
|
230 |
(fn s => if s mem ThmDatabase.ml_reserved then s ^ "_const" else s); |
|
cc9d7f403a4b
mk_const_id now checks for clashes with reserved ML identifiers.
berghofe
parents:
13003
diff
changeset
|
231 |
|
| 11520 | 232 |
val mk_type_id = gen_mk_id Sign.typeK |
233 |
(fn s => if s mem ThmDatabase.ml_reserved then s ^ "_type" else s); |
|
234 |
||
|
13731
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
235 |
fun rename_terms ts = |
| 11520 | 236 |
let |
|
13731
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
237 |
val names = foldr add_term_names |
|
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
238 |
(ts, map (fst o fst) (Drule.vars_of_terms ts)); |
| 11520 | 239 |
val clash = names inter ThmDatabase.ml_reserved; |
240 |
val ps = clash ~~ variantlist (clash, names); |
|
241 |
||
242 |
fun rename (Var ((a, i), T)) = Var ((if_none (assoc (ps, a)) a, i), T) |
|
243 |
| rename (Free (a, T)) = Free (if_none (assoc (ps, a)) a, T) |
|
244 |
| rename (Abs (s, T, t)) = Abs (s, T, rename t) |
|
245 |
| rename (t $ u) = rename t $ rename u |
|
246 |
| rename t = t; |
|
247 |
in |
|
|
13731
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
248 |
map rename ts |
|
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
249 |
end; |
|
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
250 |
|
|
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
251 |
val rename_term = hd o rename_terms o single; |
|
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
252 |
|
|
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
253 |
|
|
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
254 |
(**** retrieve definition of constant ****) |
|
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
255 |
|
|
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
256 |
fun is_instance thy T1 T2 = |
|
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
257 |
Type.typ_instance (Sign.tsig_of (sign_of thy), T1, Type.varifyT T2); |
|
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
258 |
|
|
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
259 |
fun get_assoc_code thy s T = apsome snd (find_first (fn ((s', T'), _) => |
|
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
260 |
s = s' andalso is_instance thy T T') (#consts (CodegenData.get thy))); |
|
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
261 |
|
|
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
262 |
fun get_defn thy s T = |
|
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
263 |
let |
|
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
264 |
val axms = flat (map (Symtab.dest o #axioms o Theory.rep_theory) |
|
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
265 |
(thy :: Theory.ancestors_of thy)); |
|
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
266 |
val defs = mapfilter (fn (_, t) => |
|
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
267 |
(let |
|
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
268 |
val (lhs, rhs) = Logic.dest_equals t; |
|
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
269 |
val (c, args) = strip_comb lhs; |
|
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
270 |
val (s', T') = dest_Const c |
|
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
271 |
in if s=s' then Some (T', split_last (rename_terms (args @ [rhs]))) |
|
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
272 |
else None end) handle TERM _ => None) axms; |
|
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
273 |
val i = find_index (is_instance thy T o fst) defs |
|
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
274 |
in |
|
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
275 |
if i>=0 then Some (snd (nth_elem (i, defs)), |
|
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
276 |
if length defs = 1 then None else Some i) |
|
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
277 |
else None |
| 11520 | 278 |
end; |
279 |
||
280 |
||
| 12452 | 281 |
(**** invoke suitable code generator for term / type ****) |
| 11520 | 282 |
|
| 12452 | 283 |
fun invoke_codegen thy dep brack (gr, t) = (case get_first |
| 11520 | 284 |
(fn (_, f) => f thy gr dep brack t) (#codegens (CodegenData.get thy)) of |
285 |
None => error ("Unable to generate code for term:\n" ^
|
|
286 |
Sign.string_of_term (sign_of thy) t ^ "\nrequired by:\n" ^ |
|
287 |
commas (Graph.all_succs gr [dep])) |
|
| 12452 | 288 |
| Some x => x); |
289 |
||
290 |
fun invoke_tycodegen thy dep brack (gr, T) = (case get_first |
|
291 |
(fn (_, f) => f thy gr dep brack T) (#tycodegens (CodegenData.get thy)) of |
|
292 |
None => error ("Unable to generate code for type:\n" ^
|
|
293 |
Sign.string_of_typ (sign_of thy) T ^ "\nrequired by:\n" ^ |
|
294 |
commas (Graph.all_succs gr [dep])) |
|
295 |
| Some x => x); |
|
| 11520 | 296 |
|
297 |
||
298 |
(**** code generator for mixfix expressions ****) |
|
299 |
||
300 |
fun parens p = Pretty.block [Pretty.str "(", p, Pretty.str ")"];
|
|
301 |
||
302 |
fun pretty_fn [] p = [p] |
|
303 |
| pretty_fn (x::xs) p = Pretty.str ("fn " ^ x ^ " =>") ::
|
|
304 |
Pretty.brk 1 :: pretty_fn xs p; |
|
305 |
||
306 |
fun pretty_mixfix [] [] _ = [] |
|
307 |
| pretty_mixfix (Arg :: ms) (p :: ps) qs = p :: pretty_mixfix ms ps qs |
|
| 12452 | 308 |
| pretty_mixfix (Ignore :: ms) ps qs = pretty_mixfix ms ps qs |
| 11520 | 309 |
| pretty_mixfix (Pretty p :: ms) ps qs = p :: pretty_mixfix ms ps qs |
| 12452 | 310 |
| pretty_mixfix (Quote _ :: ms) ps (q :: qs) = q :: pretty_mixfix ms ps qs; |
| 11520 | 311 |
|
312 |
||
| 12452 | 313 |
(**** default code generators ****) |
| 11520 | 314 |
|
315 |
fun eta_expand t ts i = |
|
316 |
let |
|
317 |
val (Ts, _) = strip_type (fastype_of t); |
|
318 |
val j = i - length ts |
|
319 |
in |
|
320 |
foldr (fn (T, t) => Abs ("x", T, t))
|
|
321 |
(take (j, Ts), list_comb (t, ts @ map Bound (j-1 downto 0))) |
|
322 |
end; |
|
323 |
||
324 |
fun mk_app _ p [] = p |
|
325 |
| mk_app brack p ps = if brack then |
|
326 |
Pretty.block (Pretty.str "(" ::
|
|
327 |
separate (Pretty.brk 1) (p :: ps) @ [Pretty.str ")"]) |
|
328 |
else Pretty.block (separate (Pretty.brk 1) (p :: ps)); |
|
329 |
||
330 |
fun new_names t xs = variantlist (xs, |
|
331 |
map (fst o fst o dest_Var) (term_vars t) union |
|
332 |
add_term_names (t, ThmDatabase.ml_reserved)); |
|
333 |
||
334 |
fun new_name t x = hd (new_names t [x]); |
|
335 |
||
336 |
fun default_codegen thy gr dep brack t = |
|
337 |
let |
|
338 |
val (u, ts) = strip_comb t; |
|
| 12452 | 339 |
fun codegens brack = foldl_map (invoke_codegen thy dep brack) |
| 11520 | 340 |
in (case u of |
341 |
Var ((s, i), _) => |
|
| 12452 | 342 |
let val (gr', ps) = codegens true (gr, ts) |
| 11520 | 343 |
in Some (gr', mk_app brack (Pretty.str (s ^ |
344 |
(if i=0 then "" else string_of_int i))) ps) |
|
345 |
end |
|
346 |
||
347 |
| Free (s, _) => |
|
| 12452 | 348 |
let val (gr', ps) = codegens true (gr, ts) |
| 11520 | 349 |
in Some (gr', mk_app brack (Pretty.str s) ps) end |
350 |
||
351 |
| Const (s, T) => |
|
352 |
(case get_assoc_code thy s T of |
|
353 |
Some ms => |
|
354 |
let val i = num_args ms |
|
355 |
in if length ts < i then |
|
356 |
default_codegen thy gr dep brack (eta_expand u ts i) |
|
357 |
else |
|
358 |
let |
|
| 12452 | 359 |
val (ts1, ts2) = args_of ms ts; |
360 |
val (gr1, ps1) = codegens false (gr, ts1); |
|
361 |
val (gr2, ps2) = codegens true (gr1, ts2); |
|
362 |
val (gr3, ps3) = codegens false (gr2, quotes_of ms); |
|
| 11520 | 363 |
in |
364 |
Some (gr3, mk_app brack (Pretty.block (pretty_mixfix ms ps1 ps3)) ps2) |
|
365 |
end |
|
366 |
end |
|
367 |
| None => (case get_defn thy s T of |
|
368 |
None => None |
|
369 |
| Some ((args, rhs), k) => |
|
370 |
let |
|
371 |
val id = mk_const_id (sign_of thy) s ^ (case k of |
|
372 |
None => "" | Some i => "_def" ^ string_of_int i); |
|
| 12452 | 373 |
val (gr', ps) = codegens true (gr, ts); |
| 11520 | 374 |
in |
375 |
Some (Graph.add_edge (id, dep) gr' handle Graph.UNDEF _ => |
|
376 |
let |
|
377 |
val _ = message ("expanding definition of " ^ s);
|
|
378 |
val (Ts, _) = strip_type T; |
|
379 |
val (args', rhs') = |
|
380 |
if not (null args) orelse null Ts then (args, rhs) else |
|
381 |
let val v = Free (new_name rhs "x", hd Ts) |
|
382 |
in ([v], betapply (rhs, v)) end; |
|
| 12452 | 383 |
val (gr1, p) = invoke_codegen thy id false |
384 |
(Graph.add_edge (id, dep) |
|
385 |
(Graph.new_node (id, (None, "")) gr'), rhs'); |
|
386 |
val (gr2, xs) = codegens false (gr1, args'); |
|
|
12580
7fdc00bb2a9e
Code generator now adds type constraints to val declarations (to make
berghofe
parents:
12555
diff
changeset
|
387 |
val (gr3, ty) = invoke_tycodegen thy id false (gr2, T); |
| 11520 | 388 |
in Graph.map_node id (K (None, Pretty.string_of (Pretty.block |
|
12580
7fdc00bb2a9e
Code generator now adds type constraints to val declarations (to make
berghofe
parents:
12555
diff
changeset
|
389 |
(separate (Pretty.brk 1) (if null args' then |
|
7fdc00bb2a9e
Code generator now adds type constraints to val declarations (to make
berghofe
parents:
12555
diff
changeset
|
390 |
[Pretty.str ("val " ^ id ^ " :"), ty]
|
|
7fdc00bb2a9e
Code generator now adds type constraints to val declarations (to make
berghofe
parents:
12555
diff
changeset
|
391 |
else Pretty.str ("fun " ^ id) :: xs) @
|
|
7fdc00bb2a9e
Code generator now adds type constraints to val declarations (to make
berghofe
parents:
12555
diff
changeset
|
392 |
[Pretty.str " =", Pretty.brk 1, p, Pretty.str ";"])) ^ "\n\n")) gr3 |
| 11520 | 393 |
end, mk_app brack (Pretty.str id) ps) |
394 |
end)) |
|
395 |
||
396 |
| Abs _ => |
|
397 |
let |
|
398 |
val (bs, Ts) = ListPair.unzip (strip_abs_vars u); |
|
399 |
val t = strip_abs_body u |
|
400 |
val bs' = new_names t bs; |
|
| 12452 | 401 |
val (gr1, ps) = codegens true (gr, ts); |
402 |
val (gr2, p) = invoke_codegen thy dep false |
|
403 |
(gr1, subst_bounds (map Free (rev (bs' ~~ Ts)), t)); |
|
| 11520 | 404 |
in |
405 |
Some (gr2, mk_app brack (Pretty.block (Pretty.str "(" :: pretty_fn bs' p @
|
|
406 |
[Pretty.str ")"])) ps) |
|
407 |
end |
|
408 |
||
409 |
| _ => None) |
|
410 |
end; |
|
411 |
||
| 12452 | 412 |
fun default_tycodegen thy gr dep brack (TVar ((s, i), _)) = |
413 |
Some (gr, Pretty.str (s ^ (if i = 0 then "" else string_of_int i))) |
|
414 |
| default_tycodegen thy gr dep brack (TFree (s, _)) = Some (gr, Pretty.str s) |
|
415 |
| default_tycodegen thy gr dep brack (Type (s, Ts)) = |
|
416 |
(case assoc (#types (CodegenData.get thy), s) of |
|
417 |
None => None |
|
418 |
| Some ms => |
|
419 |
let |
|
420 |
val (gr', ps) = foldl_map |
|
421 |
(invoke_tycodegen thy dep false) (gr, fst (args_of ms Ts)); |
|
422 |
val (gr'', qs) = foldl_map |
|
423 |
(invoke_tycodegen thy dep false) (gr', quotes_of ms) |
|
424 |
in Some (gr'', Pretty.block (pretty_mixfix ms ps qs)) end); |
|
425 |
||
| 11520 | 426 |
|
427 |
fun output_code gr xs = implode (map (snd o Graph.get_node gr) |
|
428 |
(rev (Graph.all_preds gr xs))); |
|
429 |
||
430 |
fun gen_generate_code prep_term thy = Pretty.setmp_margin 80 (fn xs => |
|
431 |
let |
|
432 |
val sg = sign_of thy; |
|
433 |
val gr = Graph.new_node ("<Top>", (None, "")) Graph.empty;
|
|
434 |
val (gr', ps) = foldl_map (fn (gr, (s, t)) => apsnd (pair s) |
|
| 12452 | 435 |
(invoke_codegen thy "<Top>" false (gr, t))) |
436 |
(gr, map (apsnd (prep_term sg)) xs) |
|
| 11520 | 437 |
in |
438 |
"structure Generated =\nstruct\n\n" ^ |
|
439 |
output_code gr' ["<Top>"] ^ |
|
440 |
space_implode "\n\n" (map (fn (s', p) => Pretty.string_of (Pretty.block |
|
441 |
[Pretty.str ("val " ^ s' ^ " ="), Pretty.brk 1, p, Pretty.str ";"])) ps) ^
|
|
442 |
"\n\nend;\n\nopen Generated;\n" |
|
443 |
end); |
|
444 |
||
445 |
val generate_code_i = gen_generate_code (K I); |
|
446 |
val generate_code = gen_generate_code |
|
447 |
(fn sg => term_of o read_cterm sg o rpair TypeInfer.logicT); |
|
448 |
||
| 12452 | 449 |
|
|
13753
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
450 |
(**** Reflection ****) |
|
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
451 |
|
|
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
452 |
val strip_tname = implode o tl o explode; |
|
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
453 |
|
|
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
454 |
fun pretty_list xs = Pretty.block (Pretty.str "[" :: |
|
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
455 |
flat (separate [Pretty.str ",", Pretty.brk 1] (map single xs)) @ |
|
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
456 |
[Pretty.str "]"]); |
|
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
457 |
|
|
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
458 |
fun mk_type p (TVar ((s, i), _)) = Pretty.str |
|
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
459 |
(strip_tname s ^ (if i = 0 then "" else string_of_int i) ^ "T") |
|
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
460 |
| mk_type p (TFree (s, _)) = Pretty.str (strip_tname s ^ "T") |
|
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
461 |
| mk_type p (Type (s, Ts)) = (if p then parens else I) (Pretty.block |
|
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
462 |
[Pretty.str "Type", Pretty.brk 1, Pretty.str ("(\"" ^ s ^ "\","),
|
|
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
463 |
Pretty.brk 1, pretty_list (map (mk_type false) Ts), Pretty.str ")"]); |
|
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
464 |
|
|
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
465 |
fun mk_term_of sg p (TVar ((s, i), _)) = Pretty.str |
|
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
466 |
(strip_tname s ^ (if i = 0 then "" else string_of_int i) ^ "F") |
|
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
467 |
| mk_term_of sg p (TFree (s, _)) = Pretty.str (strip_tname s ^ "F") |
|
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
468 |
| mk_term_of sg p (Type (s, Ts)) = (if p then parens else I) (Pretty.block |
|
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
469 |
(separate (Pretty.brk 1) (Pretty.str ("term_of_" ^ mk_type_id sg s) ::
|
|
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
470 |
flat (map (fn T => [mk_term_of sg true T, mk_type true T]) Ts)))); |
|
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
471 |
|
|
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
472 |
|
| 12452 | 473 |
(**** Interface ****) |
474 |
||
|
13731
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
475 |
val str = setmp print_mode [] Pretty.str; |
|
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
476 |
|
| 11520 | 477 |
fun parse_mixfix rd s = |
478 |
(case Scan.finite Symbol.stopper (Scan.repeat |
|
479 |
( $$ "_" >> K Arg |
|
480 |
|| $$ "?" >> K Ignore |
|
481 |
|| $$ "/" |-- Scan.repeat ($$ " ") >> (Pretty o Pretty.brk o length) |
|
482 |
|| $$ "{" |-- $$ "*" |-- Scan.repeat1
|
|
483 |
( $$ "'" |-- Scan.one Symbol.not_eof |
|
484 |
|| Scan.unless ($$ "*" -- $$ "}") (Scan.one Symbol.not_eof)) --| |
|
| 12452 | 485 |
$$ "*" --| $$ "}" >> (Quote o rd o implode) |
| 11520 | 486 |
|| Scan.repeat1 |
487 |
( $$ "'" |-- Scan.one Symbol.not_eof |
|
488 |
|| Scan.unless ($$ "_" || $$ "?" || $$ "/" || $$ "{" |-- $$ "*")
|
|
|
13731
e2d17090052b
Parameters in definitions are now renamed to avoid clashes with
berghofe
parents:
13073
diff
changeset
|
489 |
(Scan.one Symbol.not_eof)) >> (Pretty o str o implode))) |
| 11520 | 490 |
(Symbol.explode s) of |
491 |
(p, []) => p |
|
492 |
| _ => error ("Malformed annotation: " ^ quote s));
|
|
493 |
||
| 11546 | 494 |
structure P = OuterParse and K = OuterSyntax.Keyword; |
| 11520 | 495 |
|
496 |
val assoc_typeP = |
|
497 |
OuterSyntax.command "types_code" |
|
| 11546 | 498 |
"associate types with target language types" K.thy_decl |
| 11520 | 499 |
(Scan.repeat1 (P.xname --| P.$$$ "(" -- P.string --| P.$$$ ")") >>
|
| 12452 | 500 |
(fn xs => Toplevel.theory (fn thy => assoc_types |
501 |
(map (fn (name, mfx) => (name, parse_mixfix |
|
502 |
(typ_of o read_ctyp (sign_of thy)) mfx)) xs) thy))); |
|
| 11520 | 503 |
|
504 |
val assoc_constP = |
|
505 |
OuterSyntax.command "consts_code" |
|
| 11546 | 506 |
"associate constants with target language code" K.thy_decl |
| 11520 | 507 |
(Scan.repeat1 |
| 13003 | 508 |
(P.xname -- (Scan.option (P.$$$ "::" |-- P.typ)) --| |
| 11520 | 509 |
P.$$$ "(" -- P.string --| P.$$$ ")") >>
|
510 |
(fn xs => Toplevel.theory (fn thy => assoc_consts |
|
511 |
(map (fn ((name, optype), mfx) => (name, optype, parse_mixfix |
|
512 |
(term_of o read_cterm (sign_of thy) o rpair TypeInfer.logicT) mfx)) |
|
513 |
xs) thy))); |
|
514 |
||
515 |
val generate_codeP = |
|
| 11546 | 516 |
OuterSyntax.command "generate_code" "generates code for terms" K.thy_decl |
| 13003 | 517 |
(Scan.option (P.$$$ "(" |-- P.name --| P.$$$ ")") --
|
|
13753
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
518 |
Scan.optional (P.$$$ "[" |-- P.enum "," P.xname --| P.$$$ "]") (!mode) -- |
| 13003 | 519 |
Scan.repeat1 (P.name --| P.$$$ "=" -- P.term) >> |
|
13753
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
520 |
(fn ((opt_fname, mode'), xs) => Toplevel.theory (fn thy => |
| 11520 | 521 |
((case opt_fname of |
522 |
None => use_text Context.ml_output false |
|
523 |
| Some fname => File.write (Path.unpack fname)) |
|
|
13753
38b76f457b9c
- Added mode reference variable (may be used to switch on and off specific
berghofe
parents:
13731
diff
changeset
|
524 |
(setmp mode mode' (generate_code thy) xs); thy)))); |
| 11520 | 525 |
|
526 |
val parsers = [assoc_typeP, assoc_constP, generate_codeP]; |
|
527 |
||
| 12452 | 528 |
val setup = |
529 |
[CodegenData.init, |
|
530 |
add_codegen "default" default_codegen, |
|
531 |
add_tycodegen "default" default_tycodegen, |
|
|
12555
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
532 |
assoc_types [("fun", parse_mixfix (K dummyT) "(_ ->/ _)")],
|
|
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
533 |
Attrib.add_attributes [("code",
|
|
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
534 |
(code_attr, K Attrib.undef_local_attribute), |
|
e6d7f040fdc7
"code" attribute is now managed by basic code generator module.
berghofe
parents:
12490
diff
changeset
|
535 |
"declare theorems for code generation")]]; |
| 11520 | 536 |
|
537 |
end; |
|
538 |
||
539 |
OuterSyntax.add_parsers Codegen.parsers; |