author | haftmann |
Fri, 16 Mar 2007 21:32:22 +0100 | |
changeset 22464 | 164e7be27736 |
parent 22396 | 6c7f9207fa9e |
child 22476 | 088e141084a6 |
permissions | -rw-r--r-- |
22355 | 1 |
(* Title: Pure/Tools/codegen_serializer.ML |
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
2 |
ID: $Id$ |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
3 |
Author: Florian Haftmann, TU Muenchen |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
4 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
5 |
Serializer from intermediate language ("Thin-gol") to |
20699 | 6 |
target languages (like SML or Haskell). |
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
7 |
*) |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
8 |
|
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
9 |
signature CODEGEN_SERIALIZER = |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
10 |
sig |
20456 | 11 |
include BASIC_CODEGEN_THINGOL; |
20931 | 12 |
|
22355 | 13 |
val add_syntax_class: string -> class |
14 |
-> (string * ((string * typ list) * string) list) option -> theory -> theory; |
|
15 |
val add_syntax_inst: string -> string * class -> bool -> theory -> theory; |
|
16 |
val add_syntax_tycoP: string -> string -> OuterParse.token list |
|
17 |
-> (theory -> theory) * OuterParse.token list; |
|
18 |
val add_syntax_constP: string -> string -> OuterParse.token list |
|
19 |
-> (theory -> theory) * OuterParse.token list; |
|
20 |
||
20699 | 21 |
val add_pretty_list: string -> string -> string -> (Pretty.T list -> Pretty.T) |
22 |
-> ((string -> string) * (string -> string)) option -> int * string |
|
23 |
-> theory -> theory; |
|
24 |
val add_pretty_ml_string: string -> string -> string -> string |
|
25 |
-> (string -> string) -> (string -> string) -> string -> theory -> theory; |
|
26 |
val add_undefined: string -> string -> string -> theory -> theory; |
|
21837 | 27 |
val add_pretty_imperative_monad_bind: string -> string -> theory -> theory; |
20896 | 28 |
|
29 |
type serializer; |
|
22306
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
30 |
val add_serializer: string * serializer -> theory -> theory; |
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
31 |
val get_serializer: theory -> string -> Args.T list -> (theory -> string -> string) |
20931 | 32 |
-> string list option -> CodegenThingol.code -> unit; |
21082 | 33 |
val assert_serializer: theory -> string -> string; |
20931 | 34 |
|
35 |
val const_has_serialization: theory -> string list -> string -> bool; |
|
36 |
||
37 |
val eval_verbose: bool ref; |
|
22306
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
38 |
val eval_term: theory -> (theory -> string -> string) -> CodegenThingol.code |
22464 | 39 |
-> (string * 'a option ref) * CodegenThingol.iterm -> string list -> 'a; |
22022 | 40 |
val code_width: int ref; |
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
41 |
end; |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
42 |
|
22355 | 43 |
structure CodegenSerializer : CODEGEN_SERIALIZER = |
18169
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
44 |
struct |
45def66f86cb
added modules for code generator generation two, not operational yet
haftmann
parents:
diff
changeset
|
45 |
|
19167
f237c0cb3882
refined representation of codegen intermediate language
haftmann
parents:
19150
diff
changeset
|
46 |
open BasicCodegenThingol; |
20845 | 47 |
val tracing = CodegenThingol.tracing; |
18850 | 48 |
|
22022 | 49 |
(** basics **) |
18702 | 50 |
|
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
51 |
infixr 5 @@; |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
52 |
infixr 5 @|; |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
53 |
fun x @@ y = [x, y]; |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
54 |
fun xs @| y = xs @ [y]; |
22022 | 55 |
val str = setmp print_mode [] Pretty.str; |
56 |
val concat = Pretty.block o Pretty.breaks; |
|
22061 | 57 |
val brackets = Pretty.enclose "(" ")" o Pretty.breaks; |
22022 | 58 |
fun semicolon ps = Pretty.block [concat ps, str ";"]; |
59 |
||
22061 | 60 |
|
22022 | 61 |
(** syntax **) |
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
62 |
|
18216 | 63 |
datatype lrx = L | R | X; |
64 |
||
18516 | 65 |
datatype fixity = |
18216 | 66 |
BR |
67 |
| NOBR |
|
68 |
| INFX of (int * lrx); |
|
69 |
||
22022 | 70 |
val APP = INFX (~1, L); |
71 |
||
18216 | 72 |
fun eval_lrx L L = false |
73 |
| eval_lrx R R = false |
|
74 |
| eval_lrx _ _ = true; |
|
75 |
||
21882 | 76 |
fun eval_fxy NOBR NOBR = false |
77 |
| eval_fxy BR NOBR = false |
|
78 |
| eval_fxy NOBR BR = false |
|
18704
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
79 |
| eval_fxy (INFX (pr, lr)) (INFX (pr_ctxt, lr_ctxt)) = |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
80 |
pr < pr_ctxt |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
81 |
orelse pr = pr_ctxt |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
82 |
andalso eval_lrx lr lr_ctxt |
22022 | 83 |
orelse pr_ctxt = ~1 |
21882 | 84 |
| eval_fxy _ (INFX _) = false |
22022 | 85 |
| eval_fxy (INFX _) NOBR = false |
21882 | 86 |
| eval_fxy _ _ = true; |
18216 | 87 |
|
18704
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
88 |
fun gen_brackify _ [p] = p |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
89 |
| gen_brackify true (ps as _::_) = Pretty.enclose "(" ")" ps |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
90 |
| gen_brackify false (ps as _::_) = Pretty.block ps; |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
91 |
|
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
92 |
fun brackify fxy_ctxt ps = |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
93 |
gen_brackify (eval_fxy BR fxy_ctxt) (Pretty.breaks ps); |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
94 |
|
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
95 |
fun brackify_infix infx fxy_ctxt ps = |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
96 |
gen_brackify (eval_fxy (INFX infx) fxy_ctxt) (Pretty.breaks ps); |
18216 | 97 |
|
22306
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
98 |
type class_syntax = string * (string -> string option); |
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
99 |
type typ_syntax = int * ((fixity -> itype -> Pretty.T) |
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
100 |
-> fixity -> itype list -> Pretty.T); |
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
101 |
type term_syntax = int * ((CodegenNames.var_ctxt -> fixity -> iterm -> Pretty.T) |
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
102 |
-> CodegenNames.var_ctxt -> fixity -> (iterm * itype) list -> Pretty.T); |
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
103 |
|
20699 | 104 |
|
20896 | 105 |
(* user-defined syntax *) |
20699 | 106 |
|
107 |
datatype 'a mixfix = |
|
108 |
Arg of fixity |
|
20931 | 109 |
| Pretty of Pretty.T; |
18865 | 110 |
|
22306
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
111 |
fun mk_mixfix prep_arg (fixity_this, mfx) = |
18702 | 112 |
let |
21122
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
113 |
fun is_arg (Arg _) = true |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
114 |
| is_arg _ = false; |
21882 | 115 |
val i = (length o filter is_arg) mfx; |
21122
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
116 |
fun fillin _ [] [] = |
19008 | 117 |
[] |
21122
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
118 |
| fillin pr (Arg fxy :: mfx) (a :: args) = |
22306
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
119 |
(pr fxy o prep_arg) a :: fillin pr mfx args |
21122
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
120 |
| fillin pr (Pretty p :: mfx) args = |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
121 |
p :: fillin pr mfx args |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
122 |
| fillin _ [] _ = |
20389 | 123 |
error ("Inconsistent mixfix: too many arguments") |
21122
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
124 |
| fillin _ _ [] = |
20389 | 125 |
error ("Inconsistent mixfix: too less arguments"); |
21122
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
126 |
in |
22061 | 127 |
(i, fn pr => fn fixity_ctxt => fn args => |
21122
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
128 |
gen_brackify (eval_fxy fixity_this fixity_ctxt) (fillin pr mfx args)) |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
129 |
end; |
18702 | 130 |
|
22306
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
131 |
fun parse_infix prep_arg (x, i) s = |
18702 | 132 |
let |
21122
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
133 |
val l = case x of L => INFX (i, L) | _ => INFX (i, X); |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
134 |
val r = case x of R => INFX (i, R) | _ => INFX (i, X); |
18702 | 135 |
in |
22306
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
136 |
mk_mixfix prep_arg (INFX (i, x), [Arg l, (Pretty o Pretty.brk) 1, (Pretty o str) s, (Pretty o Pretty.brk) 1, Arg r]) |
18702 | 137 |
end; |
138 |
||
22306
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
139 |
fun parse_mixfix prep_arg s = |
18335 | 140 |
let |
20931 | 141 |
val sym_any = Scan.one Symbol.not_eof; |
21122
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
142 |
val parse = Scan.optional ($$ "!" >> K true) false -- Scan.repeat ( |
21130 | 143 |
($$ "(" -- $$ "_" -- $$ ")" >> K (Arg NOBR)) |
20931 | 144 |
|| ($$ "_" >> K (Arg BR)) |
145 |
|| ($$ "/" |-- Scan.repeat ($$ " ") >> (Pretty o Pretty.brk o length)) |
|
18702 | 146 |
|| (Scan.repeat1 |
20931 | 147 |
( $$ "'" |-- sym_any |
21130 | 148 |
|| Scan.unless ($$ "_" || $$ "/" || $$ "(" |-- $$ "_" |-- $$ ")") |
18702 | 149 |
sym_any) >> (Pretty o str o implode))); |
20931 | 150 |
in case Scan.finite Symbol.stopper parse (Symbol.explode s) |
22306
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
151 |
of ((_, p as [_]), []) => mk_mixfix prep_arg (NOBR, p) |
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
152 |
| ((b, p as _ :: _ :: _), []) => mk_mixfix prep_arg (if b then NOBR else BR, p) |
21122
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
153 |
| _ => Scan.!! (the_default ("malformed mixfix annotation: " ^ quote s) o snd) Scan.fail () |
18702 | 154 |
end; |
155 |
||
21082 | 156 |
fun parse_args f args = |
21895 | 157 |
case Scan.read Args.stopper f args |
158 |
of SOME x => x |
|
22197 | 159 |
| NONE => error "Bad serializer arguments"; |
21082 | 160 |
|
18702 | 161 |
|
22061 | 162 |
(* generic serializer combinators *) |
163 |
||
22306
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
164 |
fun gen_pr_app pr_app' pr_term const_syntax vars fxy (app as ((c, (_, tys)), ts)) = |
22061 | 165 |
case const_syntax c |
22070 | 166 |
of NONE => brackify fxy (pr_app' vars app) |
22061 | 167 |
| SOME (i, pr) => |
168 |
let |
|
22306
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
169 |
val k = if i < 0 then length tys else i; |
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
170 |
fun pr' fxy ts = pr pr_term vars fxy (ts ~~ curry Library.take k tys); |
22061 | 171 |
in if k = length ts |
22306
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
172 |
then pr' fxy ts |
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
173 |
else if k < length ts |
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
174 |
then case chop k ts of (ts1, ts2) => brackify fxy (pr' APP ts1 :: map (pr_term vars BR) ts2) |
22070 | 175 |
else pr_term vars fxy (CodegenThingol.eta_expand app k) |
22061 | 176 |
end; |
177 |
||
178 |
fun gen_pr_bind pr_bind' pr_term fxy ((v, pat), ty) vars = |
|
179 |
let |
|
180 |
val vs = case pat |
|
181 |
of SOME pat => CodegenThingol.fold_varnames (insert (op =)) pat [] |
|
182 |
| NONE => []; |
|
183 |
val vars' = CodegenNames.intro_vars (the_list v) vars; |
|
184 |
val vars'' = CodegenNames.intro_vars vs vars'; |
|
185 |
val v' = Option.map (CodegenNames.lookup_var vars') v; |
|
186 |
val pat' = Option.map (pr_term vars'' fxy) pat; |
|
187 |
in (pr_bind' ((v', pat'), ty), vars'') end; |
|
188 |
||
189 |
||
190 |
(* list, string and monad serializers *) |
|
191 |
||
192 |
fun implode_list c_nil c_cons t = |
|
193 |
let |
|
194 |
fun dest_cons (IConst (c, _) `$ t1 `$ t2) = |
|
195 |
if c = c_cons |
|
196 |
then SOME (t1, t2) |
|
197 |
else NONE |
|
198 |
| dest_cons _ = NONE; |
|
199 |
val (ts, t') = CodegenThingol.unfoldr dest_cons t; |
|
200 |
in case t' |
|
201 |
of IConst (c, _) => if c = c_nil then SOME ts else NONE |
|
202 |
| _ => NONE |
|
203 |
end; |
|
204 |
||
205 |
fun implode_string mk_char mk_string ts = |
|
206 |
if forall (fn IChar _ => true | _ => false) ts |
|
207 |
then (SOME o str o mk_string o implode o map (fn IChar c => mk_char c)) ts |
|
208 |
else NONE; |
|
209 |
||
210 |
fun implode_monad c_mbind c_kbind t = |
|
211 |
let |
|
212 |
fun dest_monad (IConst (c, _) `$ t1 `$ t2) = |
|
213 |
if c = c_mbind |
|
214 |
then case CodegenThingol.split_abs t2 |
|
215 |
of SOME (((v, pat), ty), t') => SOME ((SOME (((SOME v, pat), ty), true), t1), t') |
|
216 |
| NONE => NONE |
|
217 |
else if c = c_kbind |
|
218 |
then SOME ((NONE, t1), t2) |
|
219 |
else NONE |
|
220 |
| dest_monad t = case CodegenThingol.split_let t |
|
221 |
of SOME (((pat, ty), tbind), t') => SOME ((SOME (((NONE, SOME pat), ty), false), tbind), t') |
|
222 |
| NONE => NONE; |
|
223 |
in CodegenThingol.unfoldr dest_monad t end; |
|
224 |
||
225 |
fun pretty_ml_string c_nil c_cons mk_char mk_string target_implode = |
|
226 |
let |
|
22306
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
227 |
fun pretty pr vars fxy [(t, _)] = |
22061 | 228 |
case implode_list c_nil c_cons t |
229 |
of SOME ts => (case implode_string mk_char mk_string ts |
|
230 |
of SOME p => p |
|
22070 | 231 |
| NONE => Pretty.block [str target_implode, Pretty.brk 1, pr vars BR t]) |
232 |
| NONE => Pretty.block [str target_implode, Pretty.brk 1, pr vars BR t] |
|
22061 | 233 |
in (1, pretty) end; |
234 |
||
235 |
fun pretty_list c_nil c_cons mk_list mk_char_string (target_fxy, target_cons) = |
|
236 |
let |
|
237 |
fun default pr fxy t1 t2 = |
|
238 |
brackify_infix (target_fxy, R) fxy [ |
|
239 |
pr (INFX (target_fxy, X)) t1, |
|
240 |
str target_cons, |
|
241 |
pr (INFX (target_fxy, R)) t2 |
|
242 |
]; |
|
22306
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
243 |
fun pretty pr vars fxy [(t1, _), (t2, _)] = |
22061 | 244 |
case Option.map (cons t1) (implode_list c_nil c_cons t2) |
245 |
of SOME ts => |
|
246 |
(case mk_char_string |
|
247 |
of SOME (mk_char, mk_string) => |
|
248 |
(case implode_string mk_char mk_string ts |
|
249 |
of SOME p => p |
|
22070 | 250 |
| NONE => mk_list (map (pr vars NOBR) ts)) |
251 |
| NONE => mk_list (map (pr vars NOBR) ts)) |
|
252 |
| NONE => default (pr vars) fxy t1 t2; |
|
22061 | 253 |
in (2, pretty) end; |
254 |
||
255 |
val pretty_imperative_monad_bind = |
|
256 |
let |
|
22306
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
257 |
fun pretty (pr : CodegenNames.var_ctxt -> fixity -> iterm -> Pretty.T) |
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
258 |
vars fxy [(t1, _), ((v, ty) `|-> t2, _)] = |
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
259 |
pr vars fxy (ICase ((t1, ty), ([(IVar v, t2)]))) |
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
260 |
| pretty pr vars fxy [(t1, _), (t2, ty2)] = |
22248 | 261 |
let |
262 |
(*this code suffers from the lack of a proper concept for bindings*) |
|
263 |
val vs = CodegenThingol.fold_varnames cons t2 []; |
|
264 |
val v = Name.variant vs "x"; |
|
265 |
val vars' = CodegenNames.intro_vars [v] vars; |
|
266 |
val var = IVar v; |
|
22306
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
267 |
val ty = (hd o fst o CodegenThingol.unfold_fun) ty2; |
22248 | 268 |
in pr vars' fxy (ICase ((t1, ty), ([(var, t2 `$ var)]))) end; |
22061 | 269 |
in (2, pretty) end; |
270 |
||
271 |
||
272 |
||
273 |
(** name auxiliary **) |
|
274 |
||
275 |
val first_upper = implode o nth_map 0 Symbol.to_ascii_upper o explode; |
|
276 |
val first_lower = implode o nth_map 0 Symbol.to_ascii_lower o explode; |
|
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
277 |
|
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
278 |
val dest_name = |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
279 |
apfst NameSpace.implode o split_last o fst o split_last o NameSpace.explode; |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
280 |
|
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
281 |
fun mk_modl_name_tab empty_names prefix module_alias code = |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
282 |
let |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
283 |
fun nsp_map f = NameSpace.explode #> map f #> NameSpace.implode; |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
284 |
fun mk_alias name = |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
285 |
case module_alias name |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
286 |
of SOME name' => name' |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
287 |
| NONE => nsp_map (fn name => (the_single o fst) |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
288 |
(Name.variants [name] empty_names)) name; |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
289 |
fun mk_prefix name = |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
290 |
case prefix |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
291 |
of SOME prefix => NameSpace.append prefix name |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
292 |
| NONE => name; |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
293 |
val tab = |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
294 |
Symtab.empty |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
295 |
|> Graph.fold ((fn name => Symtab.default (name, (mk_alias #> mk_prefix) name)) |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
296 |
o fst o dest_name o fst) |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
297 |
code |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
298 |
in (fn name => (the o Symtab.lookup tab) name) end; |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
299 |
|
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
300 |
|
21015 | 301 |
|
22022 | 302 |
(** SML/OCaml serializer **) |
20896 | 303 |
|
304 |
datatype ml_def = |
|
305 |
MLFuns of (string * ((iterm list * iterm) list * typscheme)) list |
|
306 |
| MLDatas of (string * ((vname * sort) list * (string * itype list) list)) list |
|
22076
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
307 |
| MLClass of string * ((class * string) list * (vname * (string * itype) list)) |
20896 | 308 |
| MLClassinst of string * ((class * (string * (vname * sort) list)) |
22076
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
309 |
* ((class * (string * (string * dict list list))) list |
20896 | 310 |
* (string * iterm) list)); |
311 |
||
22306
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
312 |
fun pr_sml tyco_syntax const_syntax labelled_name keyword_vars deresolv is_cons ml_def = |
20896 | 313 |
let |
22396 | 314 |
val pr_label_classrel = translate_string (fn "." => "__" | c => c) o NameSpace.qualifier; |
315 |
val pr_label_classop = NameSpace.base o NameSpace.qualifier; |
|
22076
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
316 |
fun pr_dicts fxy ds = |
18885 | 317 |
let |
22076
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
318 |
fun pr_dictvar (v, (_, 1)) = first_upper v ^ "_" |
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
319 |
| pr_dictvar (v, (i, _)) = first_upper v ^ string_of_int (i+1) ^ "_"; |
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
320 |
fun pr_proj [] p = |
20401 | 321 |
p |
22076
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
322 |
| pr_proj [p'] p = |
22061 | 323 |
brackets [p', p] |
22076
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
324 |
| pr_proj (ps as _ :: _) p = |
22061 | 325 |
brackets [Pretty.enum " o" "(" ")" ps, p]; |
22076
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
326 |
fun pr_dictc fxy (DictConst (inst, dss)) = |
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
327 |
brackify fxy ((str o deresolv) inst :: map (pr_dicts BR) dss) |
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
328 |
| pr_dictc fxy (DictVar (classrels, v)) = |
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
329 |
pr_proj (map (str o deresolv) classrels) ((str o pr_dictvar) v) |
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
330 |
in case ds |
19253 | 331 |
of [] => str "()" |
22076
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
332 |
| [d] => pr_dictc fxy d |
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
333 |
| _ :: _ => (Pretty.list "(" ")" o map (pr_dictc NOBR)) ds |
18885 | 334 |
end; |
22076
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
335 |
fun pr_tyvars vs = |
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
336 |
vs |
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
337 |
|> map (fn (v, sort) => map_index (fn (i, _) => DictVar ([], (v, (i, length sort)))) sort) |
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
338 |
|> map (pr_dicts BR); |
20896 | 339 |
fun pr_tycoexpr fxy (tyco, tys) = |
18963 | 340 |
let |
20896 | 341 |
val tyco' = (str o deresolv) tyco |
342 |
in case map (pr_typ BR) tys |
|
18963 | 343 |
of [] => tyco' |
344 |
| [p] => Pretty.block [p, Pretty.brk 1, tyco'] |
|
345 |
| (ps as _::_) => Pretty.block [Pretty.list "(" ")" ps, Pretty.brk 1, tyco'] |
|
346 |
end |
|
20896 | 347 |
and pr_typ fxy (tyco `%% tys) = |
18702 | 348 |
(case tyco_syntax tyco |
20896 | 349 |
of NONE => pr_tycoexpr fxy (tyco, tys) |
20699 | 350 |
| SOME (i, pr) => |
351 |
if not (i = length tys) |
|
20389 | 352 |
then error ("Number of argument mismatch in customary serialization: " |
18865 | 353 |
^ (string_of_int o length) tys ^ " given, " |
20699 | 354 |
^ string_of_int i ^ " expected") |
22061 | 355 |
else pr pr_typ fxy tys) |
20896 | 356 |
| pr_typ fxy (ITyVar v) = |
18885 | 357 |
str ("'" ^ v); |
20896 | 358 |
fun pr_term vars fxy (IConst c) = |
359 |
pr_app vars fxy (c, []) |
|
360 |
| pr_term vars fxy (IVar v) = |
|
22036 | 361 |
str (CodegenNames.lookup_var vars v) |
20896 | 362 |
| pr_term vars fxy (t as t1 `$ t2) = |
363 |
(case CodegenThingol.unfold_const_app t |
|
364 |
of SOME c_ts => pr_app vars fxy c_ts |
|
18865 | 365 |
| NONE => |
20976 | 366 |
brackify fxy [pr_term vars NOBR t1, pr_term vars BR t2]) |
20896 | 367 |
| pr_term vars fxy (t as _ `|-> _) = |
20105 | 368 |
let |
22061 | 369 |
val (binds, t') = CodegenThingol.unfold_abs t; |
370 |
fun pr ((v, pat), ty) = |
|
371 |
pr_bind NOBR ((SOME v, pat), ty) |
|
372 |
#>> (fn p => concat [str "fn", p, str "=>"]); |
|
373 |
val (ps, vars') = fold_map pr binds vars; |
|
374 |
in brackets (ps @ [pr_term vars' NOBR t']) end |
|
21015 | 375 |
| pr_term vars fxy (INum n) = |
22061 | 376 |
brackets [(str o IntInf.toString) n, str ":", str "IntInf.int"] |
21015 | 377 |
| pr_term vars _ (IChar c) = |
19607
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
378 |
(str o prefix "#" o quote) |
20203 | 379 |
(let val i = ord c |
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
380 |
in if i < 32 orelse i = 34 orelse i = 92 |
20105 | 381 |
then prefix "\\" (string_of_int i) |
19607
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
382 |
else c |
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
383 |
end) |
21015 | 384 |
| pr_term vars fxy (t as ICase (_, [_])) = |
18216 | 385 |
let |
22061 | 386 |
val (binds, t') = CodegenThingol.unfold_let t; |
387 |
fun pr ((pat, ty), t) vars = |
|
388 |
vars |
|
389 |
|> pr_bind NOBR ((NONE, SOME pat), ty) |
|
390 |
|>> (fn p => semicolon [str "val", p, str "=", pr_term vars NOBR t]) |
|
391 |
val (ps, vars') = fold_map pr binds vars; |
|
20896 | 392 |
in |
393 |
Pretty.chunks [ |
|
22061 | 394 |
[str ("let"), Pretty.fbrk, Pretty.chunks ps] |> Pretty.block, |
20976 | 395 |
[str ("in"), Pretty.fbrk, pr_term vars' NOBR t'] |> Pretty.block, |
20896 | 396 |
str ("end") |
22061 | 397 |
] |
398 |
end |
|
21015 | 399 |
| pr_term vars fxy (ICase ((td, ty), b::bs)) = |
18216 | 400 |
let |
22061 | 401 |
fun pr delim (pat, t) = |
20699 | 402 |
let |
22061 | 403 |
val (p, vars') = pr_bind NOBR ((NONE, SOME pat), ty) vars; |
20699 | 404 |
in |
22061 | 405 |
concat [str delim, p, str "=>", pr_term vars' NOBR t] |
20896 | 406 |
end; |
407 |
in |
|
408 |
(Pretty.enclose "(" ")" o single o brackify fxy) ( |
|
409 |
str "case" |
|
410 |
:: pr_term vars NOBR td |
|
411 |
:: pr "of" b |
|
412 |
:: map (pr "|") bs |
|
413 |
) |
|
414 |
end |
|
22306
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
415 |
and pr_app' vars (app as ((c, (iss, tys)), ts)) = |
20976 | 416 |
if is_cons c then let |
22306
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
417 |
val k = length tys |
20976 | 418 |
in if k < 2 then |
419 |
(str o deresolv) c :: map (pr_term vars BR) ts |
|
420 |
else if k = length ts then |
|
421 |
[(str o deresolv) c, Pretty.enum "," "(" ")" (map (pr_term vars NOBR) ts)] |
|
422 |
else [pr_term vars BR (CodegenThingol.eta_expand app k)] end else |
|
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
423 |
(str o deresolv) c |
22076
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
424 |
:: ((map (pr_dicts BR) o filter_out null) iss @ map (pr_term vars BR) ts) |
22070 | 425 |
and pr_app vars = gen_pr_app pr_app' pr_term const_syntax vars |
22061 | 426 |
and pr_bind' ((NONE, NONE), _) = str "_" |
427 |
| pr_bind' ((SOME v, NONE), _) = str v |
|
428 |
| pr_bind' ((NONE, SOME p), _) = p |
|
429 |
| pr_bind' ((SOME v, SOME p), _) = concat [str v, str "as", p] |
|
430 |
and pr_bind fxy = gen_pr_bind pr_bind' pr_term fxy; |
|
21882 | 431 |
fun pr_def (MLFuns (funns as (funn :: funns'))) = |
20896 | 432 |
let |
433 |
val definer = |
|
434 |
let |
|
435 |
fun mk [] [] = "val" |
|
436 |
| mk (_::_) _ = "fun" |
|
437 |
| mk [] vs = if (null o filter_out (null o snd)) vs then "val" else "fun"; |
|
438 |
fun chk (_, ((ts, _) :: _, (vs, _))) NONE = SOME (mk ts vs) |
|
439 |
| chk (_, ((ts, _) :: _, (vs, _))) (SOME defi) = |
|
440 |
if defi = mk ts vs then SOME defi |
|
22306
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
441 |
else error ("Mixing simultaneous vals and funs not implemented: " |
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
442 |
^ commas (map (labelled_name o fst) funns)); |
20896 | 443 |
in the (fold chk funns NONE) end; |
444 |
fun pr_funn definer (name, (eqs as eq::eqs', (raw_vs, ty))) = |
|
445 |
let |
|
446 |
val vs = filter_out (null o snd) raw_vs; |
|
447 |
val shift = if null eqs' then I else |
|
448 |
map (Pretty.block o single o Pretty.block o single); |
|
449 |
fun pr_eq definer (ts, t) = |
|
450 |
let |
|
451 |
val consts = map_filter |
|
452 |
(fn c => if (is_some o const_syntax) c |
|
453 |
then NONE else (SOME o NameSpace.base o deresolv) c) |
|
454 |
((fold o CodegenThingol.fold_constnames) (insert (op =)) (t :: ts) []); |
|
455 |
val vars = keyword_vars |
|
22036 | 456 |
|> CodegenNames.intro_vars consts |
457 |
|> CodegenNames.intro_vars ((fold o CodegenThingol.fold_unbound_varnames) |
|
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
458 |
(insert (op =)) ts []); |
20896 | 459 |
in |
22022 | 460 |
concat ( |
20896 | 461 |
[str definer, (str o deresolv) name] |
462 |
@ (if null ts andalso null vs |
|
463 |
andalso not (ty = ITyVar "_")(*for evaluation*) |
|
464 |
then [str ":", pr_typ NOBR ty] |
|
465 |
else |
|
22076
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
466 |
pr_tyvars vs |
20896 | 467 |
@ map (pr_term vars BR) ts) |
468 |
@ [str "=", pr_term vars NOBR t] |
|
469 |
) |
|
470 |
end |
|
471 |
in |
|
472 |
(Pretty.block o Pretty.fbreaks o shift) ( |
|
473 |
pr_eq definer eq |
|
474 |
:: map (pr_eq "|") eqs' |
|
475 |
) |
|
476 |
end; |
|
477 |
val (ps, p) = split_last (pr_funn definer funn :: map (pr_funn "and") funns'); |
|
478 |
in Pretty.chunks (ps @ [Pretty.block ([p, str ";"])]) end |
|
479 |
| pr_def (MLDatas (datas as (data :: datas'))) = |
|
480 |
let |
|
481 |
fun pr_co (co, []) = |
|
482 |
str (deresolv co) |
|
483 |
| pr_co (co, tys) = |
|
22022 | 484 |
concat [ |
20896 | 485 |
str (deresolv co), |
486 |
str "of", |
|
21882 | 487 |
Pretty.enum " *" "" "" (map (pr_typ (INFX (2, X))) tys) |
20896 | 488 |
]; |
22396 | 489 |
fun pr_data definer (tyco, (vs, [])) = |
490 |
concat ( |
|
491 |
str definer |
|
492 |
:: pr_tycoexpr NOBR (tyco, map (ITyVar o fst) vs) |
|
493 |
:: str "=" |
|
494 |
@@ str "EMPTY__" |
|
495 |
) |
|
496 |
| pr_data definer (tyco, (vs, cos)) = |
|
497 |
concat ( |
|
498 |
str definer |
|
499 |
:: pr_tycoexpr NOBR (tyco, map (ITyVar o fst) vs) |
|
500 |
:: str "=" |
|
501 |
:: separate (str "|") (map pr_co cos) |
|
502 |
); |
|
20896 | 503 |
val (ps, p) = split_last (pr_data "datatype" data :: map (pr_data "and") datas'); |
504 |
in Pretty.chunks (ps @ [Pretty.block ([p, str ";"])]) end |
|
505 |
| pr_def (MLClass (class, (superclasses, (v, classops)))) = |
|
506 |
let |
|
22076
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
507 |
val w = first_upper v ^ "_"; |
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
508 |
fun pr_superclass_field (class, classrel) = |
22022 | 509 |
(concat o map str) [ |
22396 | 510 |
pr_label_classrel classrel, ":", "'" ^ v, deresolv class |
20896 | 511 |
]; |
22076
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
512 |
fun pr_classop_field (classop, ty) = |
22022 | 513 |
concat [ |
22396 | 514 |
(str o pr_label_classop) classop, str ":", pr_typ NOBR ty |
20896 | 515 |
]; |
22076
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
516 |
fun pr_classop_proj (classop, _) = |
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
517 |
semicolon [ |
20896 | 518 |
str "fun", |
519 |
(str o deresolv) classop, |
|
520 |
Pretty.enclose "(" ")" [str (w ^ ":'" ^ v ^ " " ^ deresolv class)], |
|
521 |
str "=", |
|
22396 | 522 |
str ("#" ^ pr_label_classop classop), |
22076
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
523 |
str w |
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
524 |
]; |
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
525 |
fun pr_superclass_proj (_, classrel) = |
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
526 |
semicolon [ |
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
527 |
str "fun", |
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
528 |
(str o deresolv) classrel, |
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
529 |
Pretty.enclose "(" ")" [str (w ^ ":'" ^ v ^ " " ^ deresolv class)], |
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
530 |
str "=", |
22396 | 531 |
str ("#" ^ pr_label_classrel classrel), |
22076
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
532 |
str w |
20896 | 533 |
]; |
534 |
in |
|
535 |
Pretty.chunks ( |
|
22022 | 536 |
concat [ |
20896 | 537 |
str ("type '" ^ v), |
538 |
(str o deresolv) class, |
|
539 |
str "=", |
|
540 |
Pretty.enum "," "{" "};" ( |
|
22076
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
541 |
map pr_superclass_field superclasses @ map pr_classop_field classops |
20896 | 542 |
) |
543 |
] |
|
22076
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
544 |
:: map pr_superclass_proj superclasses |
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
545 |
@ map pr_classop_proj classops |
20896 | 546 |
) |
547 |
end |
|
548 |
| pr_def (MLClassinst (inst, ((class, (tyco, arity)), (superarities, classop_defs)))) = |
|
549 |
let |
|
22076
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
550 |
fun pr_superclass (_, (classrel, dss)) = |
22022 | 551 |
concat [ |
22396 | 552 |
(str o pr_label_classrel) classrel, |
20896 | 553 |
str "=", |
22076
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
554 |
pr_dicts NOBR [DictConst dss] |
20896 | 555 |
]; |
22076
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
556 |
fun pr_classop (classop, t) = |
20896 | 557 |
let |
558 |
val consts = map_filter |
|
559 |
(fn c => if (is_some o const_syntax) c |
|
560 |
then NONE else (SOME o NameSpace.base o deresolv) c) |
|
561 |
(CodegenThingol.fold_constnames (insert (op =)) t []); |
|
22076
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
562 |
val vars = CodegenNames.intro_vars consts keyword_vars; |
20896 | 563 |
in |
22022 | 564 |
concat [ |
22396 | 565 |
(str o pr_label_classop) classop, |
20896 | 566 |
str "=", |
567 |
pr_term vars NOBR t |
|
568 |
] |
|
569 |
end; |
|
570 |
in |
|
22076
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
571 |
semicolon ([ |
20896 | 572 |
str (if null arity then "val" else "fun"), |
573 |
(str o deresolv) inst ] @ |
|
22076
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
574 |
pr_tyvars arity @ [ |
20896 | 575 |
str "=", |
22076
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
576 |
Pretty.enum "," "{" "}" (map pr_superclass superarities @ map pr_classop classop_defs), |
20896 | 577 |
str ":", |
22076
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
578 |
pr_tycoexpr NOBR (class, [tyco `%% map (ITyVar o fst) arity]) |
20896 | 579 |
]) |
580 |
end; |
|
581 |
in pr_def ml_def end; |
|
19042
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
582 |
|
21837 | 583 |
fun pr_sml_modl name content = |
584 |
Pretty.chunks ([ |
|
585 |
str ("structure " ^ name ^ " = "), |
|
586 |
str "struct", |
|
587 |
str "" |
|
588 |
] @ content @ [ |
|
589 |
str "", |
|
590 |
str ("end; (*struct " ^ name ^ "*)") |
|
591 |
]); |
|
592 |
||
22306
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
593 |
fun pr_ocaml tyco_syntax const_syntax labelled_name keyword_vars deresolv is_cons ml_def = |
21837 | 594 |
let |
22076
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
595 |
fun pr_dicts fxy ds = |
21837 | 596 |
let |
22076
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
597 |
fun pr_dictvar (v, (_, 1)) = "_" ^ first_upper v |
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
598 |
| pr_dictvar (v, (i, _)) = "_" ^ first_upper v ^ string_of_int (i+1); |
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
599 |
fun pr_proj ps p = |
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
600 |
fold_rev (fn p2 => fn p1 => Pretty.block [p1, str ".", str p2]) ps p |
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
601 |
fun pr_dictc fxy (DictConst (inst, dss)) = |
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
602 |
brackify fxy ((str o deresolv) inst :: map (pr_dicts BR) dss) |
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
603 |
| pr_dictc fxy (DictVar (classrels, v)) = |
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
604 |
pr_proj (map deresolv classrels) ((str o pr_dictvar) v) |
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
605 |
in case ds |
21837 | 606 |
of [] => str "()" |
22076
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
607 |
| [d] => pr_dictc fxy d |
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
608 |
| _ :: _ => (Pretty.list "(" ")" o map (pr_dictc NOBR)) ds |
21837 | 609 |
end; |
22076
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
610 |
fun pr_tyvars vs = |
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
611 |
vs |
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
612 |
|> map (fn (v, sort) => map_index (fn (i, _) => DictVar ([], (v, (i, length sort)))) sort) |
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
613 |
|> map (pr_dicts BR); |
21837 | 614 |
fun pr_tycoexpr fxy (tyco, tys) = |
615 |
let |
|
616 |
val tyco' = (str o deresolv) tyco |
|
617 |
in case map (pr_typ BR) tys |
|
618 |
of [] => tyco' |
|
619 |
| [p] => Pretty.block [p, Pretty.brk 1, tyco'] |
|
620 |
| (ps as _::_) => Pretty.block [Pretty.list "(" ")" ps, Pretty.brk 1, tyco'] |
|
621 |
end |
|
622 |
and pr_typ fxy (tyco `%% tys) = |
|
623 |
(case tyco_syntax tyco |
|
624 |
of NONE => pr_tycoexpr fxy (tyco, tys) |
|
625 |
| SOME (i, pr) => |
|
626 |
if not (i = length tys) |
|
627 |
then error ("Number of argument mismatch in customary serialization: " |
|
628 |
^ (string_of_int o length) tys ^ " given, " |
|
629 |
^ string_of_int i ^ " expected") |
|
22061 | 630 |
else pr pr_typ fxy tys) |
21837 | 631 |
| pr_typ fxy (ITyVar v) = |
632 |
str ("'" ^ v); |
|
633 |
fun pr_term vars fxy (IConst c) = |
|
634 |
pr_app vars fxy (c, []) |
|
635 |
| pr_term vars fxy (IVar v) = |
|
22036 | 636 |
str (CodegenNames.lookup_var vars v) |
21837 | 637 |
| pr_term vars fxy (t as t1 `$ t2) = |
638 |
(case CodegenThingol.unfold_const_app t |
|
639 |
of SOME c_ts => pr_app vars fxy c_ts |
|
640 |
| NONE => |
|
641 |
brackify fxy [pr_term vars NOBR t1, pr_term vars BR t2]) |
|
642 |
| pr_term vars fxy (t as _ `|-> _) = |
|
643 |
let |
|
22061 | 644 |
val (binds, t') = CodegenThingol.unfold_abs t; |
645 |
fun pr ((v, pat), ty) = pr_bind BR ((SOME v, pat), ty); |
|
646 |
val (ps, vars') = fold_map pr binds vars; |
|
647 |
in brackets (str "fun" :: ps @ str "->" @@ pr_term vars' NOBR t') end |
|
21837 | 648 |
| pr_term vars fxy (INum n) = |
22186 | 649 |
if n > 0 then |
650 |
brackify fxy [str "Big_int.big_int_of_int", (str o IntInf.toString) n] |
|
651 |
else |
|
652 |
brackify fxy [str "Big_int.big_int_of_int", |
|
653 |
(str o enclose "(" ")" o prefix "-" o IntInf.toString o op ~) n] |
|
21837 | 654 |
| pr_term vars _ (IChar c) = |
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
655 |
(str o enclose "'" "'") |
21837 | 656 |
(let val i = ord c |
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
657 |
in if i < 32 orelse i = 39 orelse i = 92 |
21837 | 658 |
then prefix "\\" (string_of_int i) |
659 |
else c |
|
660 |
end) |
|
661 |
| pr_term vars fxy (t as ICase (_, [_])) = |
|
662 |
let |
|
22061 | 663 |
val (binds, t') = CodegenThingol.unfold_let t; |
664 |
fun pr ((pat, ty), t) vars = |
|
665 |
vars |
|
666 |
|> pr_bind NOBR ((NONE, SOME pat), ty) |
|
667 |
|>> (fn p => concat [str "let", p, str "=", pr_term vars NOBR t, str "in"]) |
|
668 |
val (ps, vars') = fold_map pr binds vars; |
|
669 |
in Pretty.chunks (ps @| pr_term vars' NOBR t') end |
|
21837 | 670 |
| pr_term vars fxy (ICase ((td, ty), b::bs)) = |
671 |
let |
|
22061 | 672 |
fun pr delim (pat, t) = |
21837 | 673 |
let |
22061 | 674 |
val (p, vars') = pr_bind NOBR ((NONE, SOME pat), ty) vars; |
675 |
in concat [str delim, p, str "->", pr_term vars' NOBR t] end; |
|
21837 | 676 |
in |
677 |
(Pretty.enclose "(" ")" o single o brackify fxy) ( |
|
678 |
str "match" |
|
679 |
:: pr_term vars NOBR td |
|
680 |
:: pr "with" b |
|
681 |
:: map (pr "|") bs |
|
682 |
) |
|
683 |
end |
|
22306
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
684 |
and pr_app' vars (app as ((c, (iss, tys)), ts)) = |
21952 | 685 |
if is_cons c then |
22306
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
686 |
if length tys = length ts |
21952 | 687 |
then case ts |
688 |
of [] => [(str o deresolv) c] |
|
689 |
| [t] => [(str o deresolv) c, pr_term vars BR t] |
|
690 |
| _ => [(str o deresolv) c, Pretty.enum "," "(" ")" (map (pr_term vars NOBR) ts)] |
|
22306
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
691 |
else [pr_term vars BR (CodegenThingol.eta_expand app (length tys))] |
21952 | 692 |
else (str o deresolv) c |
22076
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
693 |
:: ((map (pr_dicts BR) o filter_out null) iss @ map (pr_term vars BR) ts) |
22070 | 694 |
and pr_app vars = gen_pr_app pr_app' pr_term const_syntax vars |
22061 | 695 |
and pr_bind' ((NONE, NONE), _) = str "_" |
696 |
| pr_bind' ((SOME v, NONE), _) = str v |
|
697 |
| pr_bind' ((NONE, SOME p), _) = p |
|
698 |
| pr_bind' ((SOME v, SOME p), _) = brackets [p, str "as", str v] |
|
699 |
and pr_bind fxy = gen_pr_bind pr_bind' pr_term fxy; |
|
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
700 |
fun pr_def (MLFuns (funns as funn :: funns')) = |
21837 | 701 |
let |
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
702 |
fun fish_parm _ (w as SOME _) = w |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
703 |
| fish_parm (IVar v) NONE = SOME v |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
704 |
| fish_parm _ NONE = NONE; |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
705 |
fun fillup_parm _ (_, SOME v) = v |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
706 |
| fillup_parm x (i, NONE) = x ^ string_of_int i; |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
707 |
fun fish_parms vars eqs = |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
708 |
let |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
709 |
val raw_fished = fold (map2 fish_parm) eqs (replicate (length (hd eqs)) NONE); |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
710 |
val x = Name.variant (map_filter I raw_fished) "x"; |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
711 |
val fished = map_index (fillup_parm x) raw_fished; |
22036 | 712 |
val vars' = CodegenNames.intro_vars fished vars; |
713 |
in map (CodegenNames.lookup_var vars') fished end; |
|
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
714 |
fun pr_eq (ts, t) = |
21837 | 715 |
let |
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
716 |
val consts = map_filter |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
717 |
(fn c => if (is_some o const_syntax) c |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
718 |
then NONE else (SOME o NameSpace.base o deresolv) c) |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
719 |
((fold o CodegenThingol.fold_constnames) (insert (op =)) (t :: ts) []); |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
720 |
val vars = keyword_vars |
22036 | 721 |
|> CodegenNames.intro_vars consts |
722 |
|> CodegenNames.intro_vars ((fold o CodegenThingol.fold_unbound_varnames) |
|
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
723 |
(insert (op =)) ts []); |
22022 | 724 |
in concat [ |
21952 | 725 |
(Pretty.block o Pretty.commas) (map (pr_term vars NOBR) ts), |
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
726 |
str "->", |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
727 |
pr_term vars NOBR t |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
728 |
] end; |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
729 |
fun pr_eqs [(ts, t)] = |
21837 | 730 |
let |
731 |
val consts = map_filter |
|
732 |
(fn c => if (is_some o const_syntax) c |
|
733 |
then NONE else (SOME o NameSpace.base o deresolv) c) |
|
734 |
((fold o CodegenThingol.fold_constnames) (insert (op =)) (t :: ts) []); |
|
735 |
val vars = keyword_vars |
|
22036 | 736 |
|> CodegenNames.intro_vars consts |
737 |
|> CodegenNames.intro_vars ((fold o CodegenThingol.fold_unbound_varnames) |
|
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
738 |
(insert (op =)) ts []); |
21837 | 739 |
in |
22022 | 740 |
concat ( |
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
741 |
map (pr_term vars BR) ts |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
742 |
@ str "=" |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
743 |
@@ pr_term vars NOBR t |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
744 |
) |
21837 | 745 |
end |
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
746 |
| pr_eqs (eqs as (eq as ([_], _)) :: eqs') = |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
747 |
Pretty.block ( |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
748 |
str "=" |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
749 |
:: Pretty.brk 1 |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
750 |
:: str "function" |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
751 |
:: Pretty.brk 1 |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
752 |
:: pr_eq eq |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
753 |
:: maps (append [Pretty.fbrk, str "|", Pretty.brk 1] o single o pr_eq) eqs' |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
754 |
) |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
755 |
| pr_eqs (eqs as eq :: eqs') = |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
756 |
let |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
757 |
val consts = map_filter |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
758 |
(fn c => if (is_some o const_syntax) c |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
759 |
then NONE else (SOME o NameSpace.base o deresolv) c) |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
760 |
((fold o CodegenThingol.fold_constnames) (insert (op =)) (map snd eqs) []); |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
761 |
val vars = keyword_vars |
22036 | 762 |
|> CodegenNames.intro_vars consts; |
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
763 |
val dummy_parms = (map str o fish_parms vars o map fst) eqs; |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
764 |
in |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
765 |
Pretty.block ( |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
766 |
Pretty.breaks dummy_parms |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
767 |
@ Pretty.brk 1 |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
768 |
:: str "=" |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
769 |
:: Pretty.brk 1 |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
770 |
:: str "match" |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
771 |
:: Pretty.brk 1 |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
772 |
:: (Pretty.block o Pretty.commas) dummy_parms |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
773 |
:: Pretty.brk 1 |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
774 |
:: str "with" |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
775 |
:: Pretty.brk 1 |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
776 |
:: pr_eq eq |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
777 |
:: maps (append [Pretty.fbrk, str "|", Pretty.brk 1] o single o pr_eq) eqs' |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
778 |
) |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
779 |
end; |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
780 |
fun pr_funn definer (name, (eqs, (vs, ty))) = |
22022 | 781 |
concat ( |
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
782 |
str definer |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
783 |
:: (str o deresolv) name |
22076
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
784 |
:: pr_tyvars (filter_out (null o snd) vs) |
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
785 |
@| pr_eqs eqs |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
786 |
); |
21837 | 787 |
val (ps, p) = split_last (pr_funn "let rec" funn :: map (pr_funn "and") funns'); |
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
788 |
in Pretty.chunks (ps @ [Pretty.block ([p, str ";;"])]) end |
21837 | 789 |
| pr_def (MLDatas (datas as (data :: datas'))) = |
790 |
let |
|
791 |
fun pr_co (co, []) = |
|
792 |
str (deresolv co) |
|
793 |
| pr_co (co, tys) = |
|
22022 | 794 |
concat [ |
21837 | 795 |
str (deresolv co), |
796 |
str "of", |
|
21882 | 797 |
Pretty.enum " *" "" "" (map (pr_typ (INFX (2, X))) tys) |
21837 | 798 |
]; |
22396 | 799 |
fun pr_data definer (tyco, (vs, [])) = |
800 |
concat ( |
|
801 |
str definer |
|
802 |
:: pr_tycoexpr NOBR (tyco, map (ITyVar o fst) vs) |
|
803 |
:: str "=" |
|
804 |
@@ str "EMPTY_" |
|
805 |
) |
|
806 |
| pr_data definer (tyco, (vs, cos)) = |
|
807 |
concat ( |
|
808 |
str definer |
|
809 |
:: pr_tycoexpr NOBR (tyco, map (ITyVar o fst) vs) |
|
810 |
:: str "=" |
|
811 |
:: separate (str "|") (map pr_co cos) |
|
812 |
); |
|
21837 | 813 |
val (ps, p) = split_last (pr_data "type" data :: map (pr_data "and") datas'); |
814 |
in Pretty.chunks (ps @ [Pretty.block ([p, str ";;"])]) end |
|
815 |
| pr_def (MLClass (class, (superclasses, (v, classops)))) = |
|
816 |
let |
|
22076
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
817 |
val w = "_" ^ first_upper v; |
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
818 |
fun pr_superclass_field (class, classrel) = |
22022 | 819 |
(concat o map str) [ |
22076
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
820 |
deresolv classrel, ":", "'" ^ v, deresolv class |
21837 | 821 |
]; |
22076
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
822 |
fun pr_classop_field (classop, ty) = |
22022 | 823 |
concat [ |
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
824 |
(str o deresolv) classop, str ":", pr_typ NOBR ty |
21837 | 825 |
]; |
22076
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
826 |
fun pr_classop_proj (classop, _) = |
22022 | 827 |
concat [ |
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
828 |
str "let", |
21837 | 829 |
(str o deresolv) classop, |
22076
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
830 |
str w, |
21837 | 831 |
str "=", |
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
832 |
str (w ^ "." ^ deresolv classop ^ ";;") |
21837 | 833 |
]; |
22076
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
834 |
in Pretty.chunks ( |
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
835 |
concat [ |
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
836 |
str ("type '" ^ v), |
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
837 |
(str o deresolv) class, |
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
838 |
str "=", |
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
839 |
Pretty.enum ";" "{" "};;" ( |
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
840 |
map pr_superclass_field superclasses @ map pr_classop_field classops |
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
841 |
) |
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
842 |
] |
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
843 |
:: map pr_classop_proj classops |
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
844 |
) end |
21837 | 845 |
| pr_def (MLClassinst (inst, ((class, (tyco, arity)), (superarities, classop_defs)))) = |
846 |
let |
|
22076
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
847 |
fun pr_superclass (_, (classrel, dss)) = |
22022 | 848 |
concat [ |
22076
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
849 |
(str o deresolv) classrel, |
21837 | 850 |
str "=", |
22076
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
851 |
pr_dicts NOBR [DictConst dss] |
21837 | 852 |
]; |
853 |
fun pr_classop_def (classop, t) = |
|
854 |
let |
|
855 |
val consts = map_filter |
|
856 |
(fn c => if (is_some o const_syntax) c |
|
857 |
then NONE else (SOME o NameSpace.base o deresolv) c) |
|
858 |
(CodegenThingol.fold_constnames (insert (op =)) t []); |
|
22076
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
859 |
val vars = CodegenNames.intro_vars consts keyword_vars; |
21837 | 860 |
in |
22022 | 861 |
concat [ |
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
862 |
(str o deresolv) classop, |
21837 | 863 |
str "=", |
864 |
pr_term vars NOBR t |
|
865 |
] |
|
866 |
end; |
|
867 |
in |
|
22022 | 868 |
concat ( |
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
869 |
str "let" |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
870 |
:: (str o deresolv) inst |
22076
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
871 |
:: pr_tyvars arity |
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
872 |
@ str "=" |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
873 |
@@ (Pretty.enclose "(" ");;" o Pretty.breaks) [ |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
874 |
Pretty.enum ";" "{" "}" (map pr_superclass superarities @ map pr_classop_def classop_defs), |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
875 |
str ":", |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
876 |
pr_tycoexpr NOBR (class, [tyco `%% map (ITyVar o fst) arity]) |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
877 |
] |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
878 |
) |
21837 | 879 |
end; |
880 |
in pr_def ml_def end; |
|
881 |
||
882 |
fun pr_ocaml_modl name content = |
|
883 |
Pretty.chunks ([ |
|
884 |
str ("module " ^ name ^ " = "), |
|
885 |
str "struct", |
|
886 |
str "" |
|
887 |
] @ content @ [ |
|
888 |
str "", |
|
889 |
str ("end;; (*struct " ^ name ^ "*)") |
|
890 |
]); |
|
891 |
||
22022 | 892 |
val code_width = ref 80; |
893 |
fun code_output p = Pretty.setmp_margin (!code_width) Pretty.output p ^ "\n"; |
|
21122
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
894 |
|
22306
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
895 |
fun seri_ml pr_def pr_modl output labelled_name reserved_user module_alias module_prolog |
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
896 |
(_ : string -> class_syntax option) tyco_syntax const_syntax code = |
21122
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
897 |
let |
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
898 |
val is_cons = fn node => case CodegenThingol.get_def code node |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
899 |
of CodegenThingol.Datatypecons _ => true |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
900 |
| _ => false; |
21122
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
901 |
datatype node = |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
902 |
Def of string * ml_def option |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
903 |
| Module of string * ((Name.context * Name.context) * node Graph.T); |
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
904 |
val empty_names = ML_Syntax.reserved |> fold Name.declare reserved_user; |
21122
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
905 |
val empty_module = ((empty_names, empty_names), Graph.empty); |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
906 |
fun map_node [] f = f |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
907 |
| map_node (m::ms) f = |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
908 |
Graph.default_node (m, Module (m, empty_module)) |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
909 |
#> Graph.map_node m (fn (Module (dmodlname, (nsp, nodes))) => Module (dmodlname, (nsp, map_node ms f nodes))); |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
910 |
fun map_nsp_yield [] f (nsp, nodes) = |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
911 |
let |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
912 |
val (x, nsp') = f nsp |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
913 |
in (x, (nsp', nodes)) end |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
914 |
| map_nsp_yield (m::ms) f (nsp, nodes) = |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
915 |
let |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
916 |
val (x, nodes') = |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
917 |
nodes |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
918 |
|> Graph.default_node (m, Module (m, empty_module)) |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
919 |
|> Graph.map_node_yield m (fn Module (dmodlname, nsp_nodes) => |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
920 |
let |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
921 |
val (x, nsp_nodes') = map_nsp_yield ms f nsp_nodes |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
922 |
in (x, Module (dmodlname, nsp_nodes')) end) |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
923 |
in (x, (nsp, nodes')) end; |
22036 | 924 |
val init_vars = CodegenNames.make_vars (ML_Syntax.reserved_names @ reserved_user); |
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
925 |
val name_modl = mk_modl_name_tab empty_names NONE module_alias code; |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
926 |
fun name_def upper name nsp = |
21122
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
927 |
let |
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
928 |
val (_, base) = dest_name name; |
21122
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
929 |
val base' = if upper then first_upper base else base; |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
930 |
val ([base''], nsp') = Name.variants [base'] nsp; |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
931 |
in (base'', nsp') end; |
21130 | 932 |
fun map_nsp_fun f (nsp_fun, nsp_typ) = |
933 |
let |
|
934 |
val (x, nsp_fun') = f nsp_fun |
|
935 |
in (x, (nsp_fun', nsp_typ)) end; |
|
936 |
fun map_nsp_typ f (nsp_fun, nsp_typ) = |
|
937 |
let |
|
938 |
val (x, nsp_typ') = f nsp_typ |
|
939 |
in (x, (nsp_fun, nsp_typ')) end; |
|
940 |
fun mk_funs defs = |
|
21122
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
941 |
fold_map |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
942 |
(fn (name, CodegenThingol.Fun info) => |
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
943 |
map_nsp_fun (name_def false name) >> (fn base => (base, (name, info))) |
22306
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
944 |
| (name, def) => error ("Function block containing illegal definition: " ^ labelled_name name) |
21130 | 945 |
) defs |
946 |
>> (split_list #> apsnd MLFuns); |
|
947 |
fun mk_datatype defs = |
|
948 |
fold_map |
|
949 |
(fn (name, CodegenThingol.Datatype info) => |
|
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
950 |
map_nsp_typ (name_def false name) >> (fn base => (base, SOME (name, info))) |
21130 | 951 |
| (name, CodegenThingol.Datatypecons _) => |
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
952 |
map_nsp_fun (name_def true name) >> (fn base => (base, NONE)) |
22306
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
953 |
| (name, def) => error ("Datatype block containing illegal definition: " ^ labelled_name name) |
21122
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
954 |
) defs |
21130 | 955 |
>> (split_list #> apsnd (map_filter I |
22306
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
956 |
#> (fn [] => error ("Datatype block without data definition: " ^ (commas o map (labelled_name o fst)) defs) |
21130 | 957 |
| infos => MLDatas infos))); |
958 |
fun mk_class defs = |
|
959 |
fold_map |
|
960 |
(fn (name, CodegenThingol.Class info) => |
|
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
961 |
map_nsp_typ (name_def false name) >> (fn base => (base, SOME (name, info))) |
22076
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
962 |
| (name, CodegenThingol.Classrel _) => |
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
963 |
map_nsp_fun (name_def false name) >> (fn base => (base, NONE)) |
21130 | 964 |
| (name, CodegenThingol.Classop _) => |
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
965 |
map_nsp_fun (name_def false name) >> (fn base => (base, NONE)) |
22306
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
966 |
| (name, def) => error ("Class block containing illegal definition: " ^ labelled_name name) |
21122
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
967 |
) defs |
21130 | 968 |
>> (split_list #> apsnd (map_filter I |
22306
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
969 |
#> (fn [] => error ("Class block without class definition: " ^ (commas o map (labelled_name o fst)) defs) |
21130 | 970 |
| [info] => MLClass info))); |
971 |
fun mk_inst [(name, CodegenThingol.Classinst info)] = |
|
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
972 |
map_nsp_fun (name_def false name) |
21130 | 973 |
>> (fn base => ([base], MLClassinst (name, info))); |
21122
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
974 |
fun add_group mk defs nsp_nodes = |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
975 |
let |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
976 |
val names as (name :: names') = map fst defs; |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
977 |
val deps = |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
978 |
[] |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
979 |
|> fold (fold (insert (op =)) o Graph.imm_succs code) names |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
980 |
|> subtract (op =) names; |
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
981 |
val (modls, _) = (split_list o map dest_name) names; |
21122
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
982 |
val modl = (the_single o distinct (op =)) modls |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
983 |
handle Empty => |
22306
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
984 |
error ("Illegal mutual dependencies: " ^ commas (map labelled_name names)); |
21122
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
985 |
val modl' = name_modl modl; |
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
986 |
val modl_explode = NameSpace.explode modl'; |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
987 |
fun add_dep name name'' = |
21122
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
988 |
let |
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
989 |
val modl'' = (name_modl o fst o dest_name) name''; |
21122
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
990 |
in if modl' = modl'' then |
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
991 |
map_node modl_explode |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
992 |
(Graph.add_edge (name, name'')) |
21122
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
993 |
else let |
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
994 |
val (common, (diff1::_, diff2::_)) = chop_prefix (op =) (modl_explode, NameSpace.explode modl''); |
21122
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
995 |
in |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
996 |
map_node common |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
997 |
(fn gr => Graph.add_edge_acyclic (diff1, diff2) gr |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
998 |
handle Graph.CYCLES _ => error ("Dependency " |
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
999 |
^ quote name |
21122
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1000 |
^ " -> " ^ quote name'' ^ " would result in module dependency cycle")) |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1001 |
end end; |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1002 |
in |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1003 |
nsp_nodes |
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
1004 |
|> map_nsp_yield modl_explode (mk defs) |
21122
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1005 |
|-> (fn (base' :: bases', def') => |
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
1006 |
apsnd (map_node modl_explode (Graph.new_node (name, (Def (base', SOME def'))) |
21122
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1007 |
#> fold2 (fn name' => fn base' => Graph.new_node (name', (Def (base', NONE)))) names' bases'))) |
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
1008 |
|> apsnd (fold (fn name => fold (add_dep name) deps) names) |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
1009 |
|> apsnd (fold (map_node modl_explode o Graph.add_edge) (product names names)) |
21122
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1010 |
end; |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1011 |
fun group_defs [(_, CodegenThingol.Bot)] = |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1012 |
I |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1013 |
| group_defs ((defs as (_, CodegenThingol.Fun _)::_)) = |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1014 |
add_group mk_funs defs |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1015 |
| group_defs ((defs as (_, CodegenThingol.Datatypecons _)::_)) = |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1016 |
add_group mk_datatype defs |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1017 |
| group_defs ((defs as (_, CodegenThingol.Datatype _)::_)) = |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1018 |
add_group mk_datatype defs |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1019 |
| group_defs ((defs as (_, CodegenThingol.Class _)::_)) = |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1020 |
add_group mk_class defs |
22076
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
1021 |
| group_defs ((defs as (_, CodegenThingol.Classrel _)::_)) = |
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
1022 |
add_group mk_class defs |
21122
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1023 |
| group_defs ((defs as (_, CodegenThingol.Classop _)::_)) = |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1024 |
add_group mk_class defs |
21130 | 1025 |
| group_defs ((defs as [(_, CodegenThingol.Classinst _)])) = |
1026 |
add_group mk_inst defs |
|
22306
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
1027 |
| group_defs defs = error ("Illegal mutual dependencies: " ^ (commas o map (labelled_name o fst)) defs) |
21130 | 1028 |
val (_, nodes) = |
1029 |
empty_module |
|
1030 |
|> fold group_defs (map (AList.make (Graph.get_node code)) |
|
1031 |
(rev (Graph.strong_conn code))) |
|
1032 |
fun deresolver prefix name = |
|
21122
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1033 |
let |
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
1034 |
val modl = (fst o dest_name) name; |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
1035 |
val modl' = (NameSpace.explode o name_modl) modl; |
21130 | 1036 |
val (_, (_, remainder)) = chop_prefix (op =) (prefix, modl'); |
1037 |
val defname' = |
|
1038 |
nodes |
|
1039 |
|> fold (fn m => fn g => case Graph.get_node g m |
|
1040 |
of Module (_, (_, g)) => g) modl' |
|
1041 |
|> (fn g => case Graph.get_node g name of Def (defname, _) => defname); |
|
22197 | 1042 |
in |
1043 |
NameSpace.implode (remainder @ [defname']) |
|
1044 |
end handle Graph.UNDEF _ => |
|
22306
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
1045 |
error ("Unknown definition name: " ^ labelled_name name); |
21122
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1046 |
fun the_prolog modlname = case module_prolog modlname |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1047 |
of NONE => [] |
21130 | 1048 |
| SOME p => [p, str ""]; |
21122
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1049 |
fun pr_node prefix (Def (_, NONE)) = |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1050 |
NONE |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1051 |
| pr_node prefix (Def (_, SOME def)) = |
22306
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
1052 |
SOME (pr_def tyco_syntax const_syntax labelled_name init_vars (deresolver prefix) is_cons def) |
21122
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1053 |
| pr_node prefix (Module (dmodlname, (_, nodes))) = |
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
21837
diff
changeset
|
1054 |
SOME (pr_modl dmodlname (the_prolog (NameSpace.implode (prefix @ [dmodlname])) |
21122
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1055 |
@ separate (str "") ((map_filter (pr_node (prefix @ [dmodlname]) o Graph.get_node nodes) |
21837 | 1056 |
o rev o flat o Graph.strong_conn) nodes))); |
1057 |
val p = pr_modl "ROOT" (the_prolog "" @ separate (str "") ((map_filter |
|
1058 |
(pr_node [] o Graph.get_node nodes) o rev o flat o Graph.strong_conn) nodes)) |
|
21122
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1059 |
in output p end; |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1060 |
|
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1061 |
val isar_seri_sml = |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1062 |
let |
22022 | 1063 |
fun output_file file = File.write (Path.explode file) o code_output; |
1064 |
val output_diag = writeln o code_output; |
|
22197 | 1065 |
val output_internal = use_text "generated code" Output.ml_output false o code_output; |
21122
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1066 |
in |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1067 |
parse_args ((Args.$$$ "-" >> K output_diag |
21548 | 1068 |
|| Args.$$$ "#" >> K output_internal |
21122
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1069 |
|| Args.name >> output_file) |
21837 | 1070 |
>> (fn output => seri_ml pr_sml pr_sml_modl output)) |
21122
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1071 |
end; |
20896 | 1072 |
|
21837 | 1073 |
val isar_seri_ocaml = |
1074 |
let |
|
22022 | 1075 |
fun output_file file = File.write (Path.explode file) o code_output; |
1076 |
val output_diag = writeln o code_output; |
|
21837 | 1077 |
in |
1078 |
parse_args ((Args.$$$ "-" >> K output_diag |
|
1079 |
|| Args.name >> output_file) |
|
1080 |
>> (fn output => seri_ml pr_ocaml pr_ocaml_modl output)) |
|
1081 |
end; |
|
20896 | 1082 |
|
21162 | 1083 |
|
20896 | 1084 |
(** Haskell serializer **) |
1085 |
||
22070 | 1086 |
local |
1087 |
||
1088 |
fun pr_bind' ((NONE, NONE), _) = str "_" |
|
1089 |
| pr_bind' ((SOME v, NONE), _) = str v |
|
1090 |
| pr_bind' ((NONE, SOME p), _) = p |
|
1091 |
| pr_bind' ((SOME v, SOME p), _) = brackets [str v, str "@", p] |
|
1092 |
||
1093 |
val pr_bind_haskell = gen_pr_bind pr_bind'; |
|
1094 |
||
1095 |
in |
|
1096 |
||
22306
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
1097 |
fun pr_haskell class_syntax tyco_syntax const_syntax labelled_name keyword_vars deresolv_here deresolv deriving_show def = |
19042
630b8dd0b31a
exported some interfaces useful for other code generator approaches
haftmann
parents:
19038
diff
changeset
|
1098 |
let |
20896 | 1099 |
fun class_name class = case class_syntax class |
1100 |
of NONE => deresolv class |
|
1101 |
| SOME (class, _) => class; |
|
1102 |
fun classop_name class classop = case class_syntax class |
|
22022 | 1103 |
of NONE => deresolv_here classop |
20896 | 1104 |
| SOME (_, classop_syntax) => case classop_syntax classop |
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
1105 |
of NONE => (snd o dest_name) classop |
20896 | 1106 |
| SOME classop => classop |
1107 |
fun pr_typparms tyvars vs = |
|
1108 |
case maps (fn (v, sort) => map (pair v) sort) vs |
|
1109 |
of [] => str "" |
|
1110 |
| xs => Pretty.block [ |
|
1111 |
Pretty.enum "," "(" ")" ( |
|
1112 |
map (fn (v, class) => str |
|
22036 | 1113 |
(class_name class ^ " " ^ CodegenNames.lookup_var tyvars v)) xs |
20896 | 1114 |
), |
1115 |
str " => " |
|
1116 |
]; |
|
1117 |
fun pr_tycoexpr tyvars fxy (tyco, tys) = |
|
1118 |
brackify fxy (str tyco :: map (pr_typ tyvars BR) tys) |
|
1119 |
and pr_typ tyvars fxy (tycoexpr as tyco `%% tys) = |
|
1120 |
(case tyco_syntax tyco |
|
1121 |
of NONE => |
|
1122 |
pr_tycoexpr tyvars fxy (deresolv tyco, tys) |
|
1123 |
| SOME (i, pr) => |
|
1124 |
if not (i = length tys) |
|
1125 |
then error ("Number of argument mismatch in customary serialization: " |
|
1126 |
^ (string_of_int o length) tys ^ " given, " |
|
1127 |
^ string_of_int i ^ " expected") |
|
22061 | 1128 |
else pr (pr_typ tyvars) fxy tys) |
20896 | 1129 |
| pr_typ tyvars fxy (ITyVar v) = |
22036 | 1130 |
(str o CodegenNames.lookup_var tyvars) v; |
20896 | 1131 |
fun pr_typscheme_expr tyvars (vs, tycoexpr) = |
22061 | 1132 |
Pretty.block (pr_typparms tyvars vs @@ pr_tycoexpr tyvars NOBR tycoexpr); |
20896 | 1133 |
fun pr_typscheme tyvars (vs, ty) = |
22061 | 1134 |
Pretty.block (pr_typparms tyvars vs @@ pr_typ tyvars NOBR ty); |
20896 | 1135 |
fun pr_term vars fxy (IConst c) = |
1136 |
pr_app vars fxy (c, []) |
|
1137 |
| pr_term vars fxy (t as (t1 `$ t2)) = |
|
1138 |
(case CodegenThingol.unfold_const_app t |
|
1139 |
of SOME app => pr_app vars fxy app |
|
1140 |
| _ => |
|
1141 |
brackify fxy [ |
|
1142 |
pr_term vars NOBR t1, |
|
1143 |
pr_term vars BR t2 |
|
1144 |
]) |
|
1145 |
| pr_term vars fxy (IVar v) = |
|
22036 | 1146 |
(str o CodegenNames.lookup_var vars) v |
20896 | 1147 |
| pr_term vars fxy (t as _ `|-> _) = |
19607
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19597
diff
changeset
|
1148 |
let |
22061 | 1149 |
val (binds, t') = CodegenThingol.unfold_abs t; |
1150 |
fun pr ((v, pat), ty) = pr_bind BR ((SOME v, pat), ty); |
|
1151 |
val (ps, vars') = fold_map pr binds vars; |
|
1152 |
in brackets (str "\\" :: ps @ str "->" @@ pr_term vars' NOBR t') end |
|
21015 | 1153 |
| pr_term vars fxy (INum n) = |
20896 | 1154 |
if n > 0 then |
1155 |
(str o IntInf.toString) n |
|
1156 |
else |
|
22061 | 1157 |
(str o enclose "(" ")" o Library.prefix "-" o IntInf.toString o IntInf.~) n |
21015 | 1158 |
| pr_term vars fxy (IChar c) = |
20896 | 1159 |
(str o enclose "'" "'") |
1160 |
(let val i = (Char.ord o the o Char.fromString) c |
|
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
1161 |
in if i < 32 orelse i = 39 orelse i = 92 |
20896 | 1162 |
then Library.prefix "\\" (string_of_int i) |
1163 |
else c |
|
1164 |
end) |
|
21015 | 1165 |
| pr_term vars fxy (t as ICase (_, [_])) = |
18216 | 1166 |
let |
22061 | 1167 |
val (binds, t) = CodegenThingol.unfold_let t; |
1168 |
fun pr ((pat, ty), t) vars = |
|
1169 |
vars |
|
1170 |
|> pr_bind BR ((NONE, SOME pat), ty) |
|
1171 |
|>> (fn p => semicolon [p, str "=", pr_term vars NOBR t]) |
|
1172 |
val (ps, vars') = fold_map pr binds vars; |
|
22022 | 1173 |
in |
1174 |
Pretty.block_enclose ( |
|
1175 |
str "let {", |
|
22061 | 1176 |
concat [str "}", str "in", pr_term vars' NOBR t] |
1177 |
) ps |
|
22022 | 1178 |
end |
22061 | 1179 |
| pr_term vars fxy (ICase ((td, ty), bs)) = |
20896 | 1180 |
let |
22061 | 1181 |
fun pr (pat, t) = |
20896 | 1182 |
let |
22061 | 1183 |
val (p, vars') = pr_bind NOBR ((NONE, SOME pat), ty) vars; |
1184 |
in semicolon [p, str "->", pr_term vars' NOBR t] end; |
|
20896 | 1185 |
in |
22022 | 1186 |
Pretty.block_enclose ( |
1187 |
concat [str "(case", pr_term vars NOBR td, str "of", str "{"], |
|
1188 |
str "})" |
|
1189 |
) (map pr bs) |
|
20896 | 1190 |
end |
20976 | 1191 |
and pr_app' vars ((c, _), ts) = |
20896 | 1192 |
(str o deresolv) c :: map (pr_term vars BR) ts |
22070 | 1193 |
and pr_app vars = gen_pr_app pr_app' pr_term const_syntax vars |
1194 |
and pr_bind fxy = pr_bind_haskell pr_term fxy; |
|
21882 | 1195 |
fun pr_def (name, CodegenThingol.Fun (eqs, (vs, ty))) = |
20896 | 1196 |
let |
22036 | 1197 |
val tyvars = CodegenNames.intro_vars (map fst vs) keyword_vars; |
20896 | 1198 |
fun pr_eq (ts, t) = |
20699 | 1199 |
let |
1200 |
val consts = map_filter |
|
1201 |
(fn c => if (is_some o const_syntax) c |
|
20896 | 1202 |
then NONE else (SOME o NameSpace.base o deresolv) c) |
1203 |
((fold o CodegenThingol.fold_constnames) (insert (op =)) (t :: ts) []); |
|
1204 |
val vars = keyword_vars |
|
22036 | 1205 |
|> CodegenNames.intro_vars consts |
1206 |
|> CodegenNames.intro_vars ((fold o CodegenThingol.fold_unbound_varnames) |
|
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
1207 |
(insert (op =)) ts []); |
20699 | 1208 |
in |
22022 | 1209 |
semicolon ( |
20896 | 1210 |
(str o deresolv_here) name |
1211 |
:: map (pr_term vars BR) ts |
|
22022 | 1212 |
@ str "=" |
1213 |
@@ pr_term vars NOBR t |
|
20699 | 1214 |
) |
20896 | 1215 |
end; |
1216 |
in |
|
1217 |
Pretty.chunks ( |
|
1218 |
Pretty.block [ |
|
1219 |
(str o suffix " ::" o deresolv_here) name, |
|
1220 |
Pretty.brk 1, |
|
22022 | 1221 |
pr_typscheme tyvars (vs, ty), |
1222 |
str ";" |
|
20896 | 1223 |
] |
21882 | 1224 |
:: map pr_eq eqs |
20896 | 1225 |
) |
21082 | 1226 |
end |
22396 | 1227 |
| pr_def (name, CodegenThingol.Datatype (vs, [])) = |
1228 |
let |
|
1229 |
val tyvars = CodegenNames.intro_vars (map fst vs) keyword_vars; |
|
1230 |
in |
|
1231 |
semicolon [ |
|
1232 |
str "data", |
|
1233 |
pr_typscheme_expr tyvars (vs, (deresolv_here name, map (ITyVar o fst) vs)) |
|
1234 |
] |
|
1235 |
end |
|
20896 | 1236 |
| pr_def (name, CodegenThingol.Datatype (vs, [(co, [ty])])) = |
1237 |
let |
|
22036 | 1238 |
val tyvars = CodegenNames.intro_vars (map fst vs) keyword_vars; |
18216 | 1239 |
in |
22022 | 1240 |
semicolon ( |
1241 |
str "newtype" |
|
1242 |
:: pr_typscheme_expr tyvars (vs, (deresolv_here name, map (ITyVar o fst) vs)) |
|
1243 |
:: str "=" |
|
1244 |
:: (str o deresolv_here) co |
|
1245 |
:: pr_typ tyvars BR ty |
|
1246 |
:: (if deriving_show name then [str "deriving (Read, Show)"] else []) |
|
1247 |
) |
|
21082 | 1248 |
end |
20896 | 1249 |
| pr_def (name, CodegenThingol.Datatype (vs, co :: cos)) = |
1250 |
let |
|
22036 | 1251 |
val tyvars = CodegenNames.intro_vars (map fst vs) keyword_vars; |
20896 | 1252 |
fun pr_co (co, tys) = |
22022 | 1253 |
concat ( |
20896 | 1254 |
(str o deresolv_here) co |
1255 |
:: map (pr_typ tyvars BR) tys |
|
1256 |
) |
|
1257 |
in |
|
22022 | 1258 |
semicolon ( |
20896 | 1259 |
str "data" |
1260 |
:: pr_typscheme_expr tyvars (vs, (deresolv_here name, map (ITyVar o fst) vs)) |
|
1261 |
:: str "=" |
|
1262 |
:: pr_co co |
|
1263 |
:: map ((fn p => Pretty.block [str "| ", p]) o pr_co) cos |
|
22022 | 1264 |
@ (if deriving_show name then [str "deriving (Read, Show)"] else []) |
1265 |
) |
|
21082 | 1266 |
end |
20896 | 1267 |
| pr_def (name, CodegenThingol.Class (superclasss, (v, classops))) = |
1268 |
let |
|
22036 | 1269 |
val tyvars = CodegenNames.intro_vars [v] keyword_vars; |
20896 | 1270 |
fun pr_classop (classop, ty) = |
22022 | 1271 |
semicolon [ |
1272 |
(str o classop_name name) classop, |
|
1273 |
str "::", |
|
20896 | 1274 |
pr_typ tyvars NOBR ty |
19136 | 1275 |
] |
20896 | 1276 |
in |
22022 | 1277 |
Pretty.block_enclose ( |
1278 |
Pretty.block [ |
|
1279 |
str "class ", |
|
22076
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
1280 |
pr_typparms tyvars [(v, map fst superclasss)], |
22036 | 1281 |
str (deresolv_here name ^ " " ^ CodegenNames.lookup_var tyvars v), |
22022 | 1282 |
str " where {" |
1283 |
], |
|
1284 |
str "};" |
|
1285 |
) (map pr_classop classops) |
|
21082 | 1286 |
end |
20896 | 1287 |
| pr_def (_, CodegenThingol.Classinst ((class, (tyco, vs)), (_, classop_defs))) = |
1288 |
let |
|
22036 | 1289 |
val tyvars = CodegenNames.intro_vars (map fst vs) keyword_vars; |
22022 | 1290 |
fun pr_instdef (classop, t) = |
20896 | 1291 |
let |
1292 |
val consts = map_filter |
|
1293 |
(fn c => if (is_some o const_syntax) c |
|
1294 |
then NONE else (SOME o NameSpace.base o deresolv) c) |
|
1295 |
(CodegenThingol.fold_constnames (insert (op =)) t []); |
|
1296 |
val vars = keyword_vars |
|
22036 | 1297 |
|> CodegenNames.intro_vars consts; |
20896 | 1298 |
in |
22022 | 1299 |
semicolon [ |
20896 | 1300 |
(str o classop_name class) classop, |
1301 |
str "=", |
|
1302 |
pr_term vars NOBR t |
|
1303 |
] |
|
22022 | 1304 |
end; |
1305 |
in |
|
1306 |
Pretty.block_enclose ( |
|
1307 |
Pretty.block [ |
|
1308 |
str "instance ", |
|
1309 |
pr_typparms tyvars vs, |
|
1310 |
str (class_name class ^ " "), |
|
1311 |
pr_typ tyvars BR (tyco `%% map (ITyVar o fst) vs), |
|
1312 |
str " where {" |
|
1313 |
], |
|
1314 |
str "};" |
|
1315 |
) (map pr_instdef classop_defs) |
|
21082 | 1316 |
end; |
20940 | 1317 |
in pr_def def end; |
20896 | 1318 |
|
22070 | 1319 |
fun pretty_haskell_monad c_mbind c_kbind = |
1320 |
let |
|
22306
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
1321 |
fun pretty pr vars fxy [(t, _)] = |
22070 | 1322 |
let |
1323 |
val pr_bind = pr_bind_haskell pr; |
|
1324 |
fun pr_mbind (NONE, t) vars = |
|
1325 |
(semicolon [pr vars NOBR t], vars) |
|
1326 |
| pr_mbind (SOME (bind, true), t) vars = vars |
|
1327 |
|> pr_bind NOBR bind |
|
1328 |
|>> (fn p => semicolon [p, str "<-", pr vars NOBR t]) |
|
1329 |
| pr_mbind (SOME (bind, false), t) vars = vars |
|
1330 |
|> pr_bind NOBR bind |
|
1331 |
|>> (fn p => semicolon [str "let", p, str "=", pr vars NOBR t]); |
|
1332 |
val (binds, t) = implode_monad c_mbind c_kbind t; |
|
1333 |
val (ps, vars') = fold_map pr_mbind binds vars; |
|
1334 |
fun brack p = if eval_fxy BR fxy then Pretty.block [str "(", p, str ")"] else p; |
|
1335 |
in (brack o Pretty.block_enclose (str "do {", str "}")) (ps @| pr vars' NOBR t) end; |
|
1336 |
in (1, pretty) end; |
|
1337 |
||
1338 |
end; (*local*) |
|
1339 |
||
21015 | 1340 |
val reserved_haskell = [ |
1341 |
"hiding", "deriving", "where", "case", "of", "infix", "infixl", "infixr", |
|
1342 |
"import", "default", "forall", "let", "in", "class", "qualified", "data", |
|
1343 |
"newtype", "instance", "if", "then", "else", "type", "as", "do", "module" |
|
1344 |
]; |
|
1345 |
||
22306
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
1346 |
fun seri_haskell module_prefix destination string_classes labelled_name reserved_user module_alias module_prolog |
21015 | 1347 |
class_syntax tyco_syntax const_syntax code = |
1348 |
let |
|
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
21837
diff
changeset
|
1349 |
val _ = Option.map File.check destination; |
21082 | 1350 |
val empty_names = Name.make_context (reserved_haskell @ reserved_user); |
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
1351 |
val name_modl = mk_modl_name_tab empty_names module_prefix module_alias code |
22306
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
1352 |
fun add_def (name, (def, deps)) = |
21082 | 1353 |
let |
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
1354 |
val (modl, base) = dest_name name; |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
1355 |
fun name_def base = Name.variants [base] #>> the_single; |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
1356 |
fun add_fun upper (nsp_fun, nsp_typ) = |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
1357 |
let |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
1358 |
val (base', nsp_fun') = name_def (if upper then first_upper base else base) nsp_fun |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
1359 |
in (base', (nsp_fun', nsp_typ)) end; |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
1360 |
fun add_typ (nsp_fun, nsp_typ) = |
21122
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1361 |
let |
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
1362 |
val (base', nsp_typ') = name_def (first_upper base) nsp_typ |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
1363 |
in (base', (nsp_fun, nsp_typ')) end; |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
1364 |
val add_name = |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
1365 |
case def |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
1366 |
of CodegenThingol.Bot => pair base |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
1367 |
| CodegenThingol.Fun _ => add_fun false |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
1368 |
| CodegenThingol.Datatype _ => add_typ |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
1369 |
| CodegenThingol.Datatypecons _ => add_fun true |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
1370 |
| CodegenThingol.Class _ => add_typ |
22076
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
1371 |
| CodegenThingol.Classrel _ => pair base |
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
1372 |
| CodegenThingol.Classop _ => add_fun false |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
1373 |
| CodegenThingol.Classinst _ => pair base; |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
1374 |
val modlname' = name_modl modl; |
21082 | 1375 |
fun add_def base' = |
1376 |
case def |
|
21093 | 1377 |
of CodegenThingol.Bot => I |
22076
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
1378 |
| CodegenThingol.Datatypecons _ => |
21082 | 1379 |
cons (name, ((NameSpace.append modlname' base', base'), NONE)) |
22076
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
1380 |
| CodegenThingol.Classrel _ => I |
21082 | 1381 |
| CodegenThingol.Classop _ => |
1382 |
cons (name, ((NameSpace.append modlname' base', base'), NONE)) |
|
1383 |
| _ => cons (name, ((NameSpace.append modlname' base', base'), SOME def)); |
|
1384 |
in |
|
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
1385 |
Symtab.map_default (modlname', ([], ([], (empty_names, empty_names)))) |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
1386 |
(apfst (fold (insert (op =)) deps)) |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
1387 |
#> `(fn code => add_name ((snd o snd o the o Symtab.lookup code) modlname')) |
21082 | 1388 |
#-> (fn (base', names) => |
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
1389 |
(Symtab.map_entry modlname' o apsnd) (fn (defs, _) => |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
1390 |
(add_def base' defs, names))) |
21082 | 1391 |
end; |
1392 |
val code' = |
|
1393 |
fold add_def (AList.make (fn name => (Graph.get_node code name, Graph.imm_succs code name)) |
|
1394 |
(Graph.strong_conn code |> flat)) Symtab.empty; |
|
22036 | 1395 |
val init_vars = CodegenNames.make_vars (reserved_haskell @ reserved_user); |
21082 | 1396 |
fun deresolv name = |
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
1397 |
(fst o fst o the o AList.lookup (op =) ((fst o snd o the |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
1398 |
o Symtab.lookup code') ((name_modl o fst o dest_name) name))) name |
22306
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
1399 |
handle Option => error ("Unknown definition name: " ^ labelled_name name); |
21082 | 1400 |
fun deresolv_here name = |
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
1401 |
(snd o fst o the o AList.lookup (op =) ((fst o snd o the |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
1402 |
o Symtab.lookup code') ((name_modl o fst o dest_name) name))) name |
22306
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
1403 |
handle Option => error ("Unknown definition name: " ^ labelled_name name); |
21082 | 1404 |
fun deriving_show tyco = |
1405 |
let |
|
21122
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1406 |
fun deriv _ "fun" = false |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1407 |
| deriv tycos tyco = member (op =) tycos tyco orelse |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1408 |
case the_default CodegenThingol.Bot (try (Graph.get_node code) tyco) |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1409 |
of CodegenThingol.Bot => true |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1410 |
| CodegenThingol.Datatype (_, cs) => forall (deriv' (tyco :: tycos)) |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1411 |
(maps snd cs) |
21082 | 1412 |
and deriv' tycos (tyco `%% tys) = deriv tycos tyco |
1413 |
andalso forall (deriv' tycos) tys |
|
1414 |
| deriv' _ (ITyVar _) = true |
|
1415 |
in deriv [] tyco end; |
|
22306
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
1416 |
fun seri_def qualified = pr_haskell class_syntax tyco_syntax const_syntax labelled_name init_vars |
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
1417 |
deresolv_here (if qualified then deresolv else deresolv_here) |
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
1418 |
(if string_classes then deriving_show else K false); |
22022 | 1419 |
fun write_module (SOME destination) modlname = |
21082 | 1420 |
let |
1421 |
val filename = case modlname |
|
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
21837
diff
changeset
|
1422 |
of "" => Path.explode "Main.hs" |
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
21837
diff
changeset
|
1423 |
| _ => (Path.ext "hs" o Path.explode o implode o separate "/" o NameSpace.explode) modlname; |
21082 | 1424 |
val pathname = Path.append destination filename; |
1425 |
val _ = File.mkdir (Path.dir pathname); |
|
22022 | 1426 |
in File.write pathname end |
1427 |
| write_module NONE _ = writeln; |
|
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
1428 |
fun seri_module (modlname', (imports, (defs, _))) = |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
1429 |
let |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
1430 |
val imports' = |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
1431 |
imports |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
1432 |
|> map (name_modl o fst o dest_name) |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
1433 |
|> distinct (op =) |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
1434 |
|> remove (op =) modlname'; |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
1435 |
val qualified = |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
1436 |
imports |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
1437 |
|> map_filter (try deresolv) |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
1438 |
|> map NameSpace.base |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
1439 |
|> has_duplicates (op =); |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
1440 |
val mk_import = str o (if qualified |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
1441 |
then prefix "import qualified " |
22022 | 1442 |
else prefix "import ") o suffix ";"; |
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
1443 |
in |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
1444 |
Pretty.chunks ( |
22022 | 1445 |
str ("module " ^ modlname' ^ " where {") |
1446 |
:: str "" |
|
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
1447 |
:: map mk_import imports' |
22022 | 1448 |
@ str "" |
1449 |
:: separate (str "") ((case module_prolog modlname' |
|
1450 |
of SOME prolog => [prolog] |
|
1451 |
| NONE => []) |
|
1452 |
@ map_filter |
|
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
1453 |
(fn (name, (_, SOME def)) => SOME (seri_def qualified (name, def)) |
22022 | 1454 |
| (_, (_, NONE)) => NONE) defs) |
1455 |
@ str "" |
|
1456 |
@@ str "}" |
|
1457 |
) |
|
1458 |
|> code_output |
|
1459 |
|> write_module destination modlname' |
|
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
1460 |
end; |
21082 | 1461 |
in Symtab.fold (fn modl => fn () => seri_module modl) code' () end; |
21015 | 1462 |
|
21082 | 1463 |
val isar_seri_haskell = |
1464 |
parse_args (Scan.option (Args.$$$ "root" -- Args.colon |-- Args.name) |
|
1465 |
-- Scan.optional (Args.$$$ "string_classes" >> K true) false |
|
1466 |
-- (Args.$$$ "-" >> K NONE || Args.name >> SOME) |
|
1467 |
>> (fn ((module_prefix, string_classes), destination) => |
|
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
21837
diff
changeset
|
1468 |
seri_haskell module_prefix (Option.map Path.explode destination) string_classes)); |
21015 | 1469 |
|
1470 |
||
1471 |
(** diagnosis serializer **) |
|
1472 |
||
22306
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
1473 |
fun seri_diagnosis labelled_name _ _ _ _ _ code = |
21015 | 1474 |
let |
22036 | 1475 |
val init_vars = CodegenNames.make_vars reserved_haskell; |
22306
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
1476 |
val pr = pr_haskell (K NONE) (K NONE) (K NONE) labelled_name init_vars I I (K false); |
21015 | 1477 |
in |
1478 |
[] |
|
21093 | 1479 |
|> Graph.fold (fn (name, (def, _)) => case try pr (name, def) of SOME p => cons p | NONE => I) code |
22022 | 1480 |
|> Pretty.chunks2 |
1481 |
|> code_output |
|
1482 |
|> writeln |
|
21015 | 1483 |
end; |
1484 |
||
1485 |
||
20896 | 1486 |
|
1487 |
(** theory data **) |
|
1488 |
||
1489 |
datatype syntax_expr = SyntaxExpr of { |
|
1490 |
class: ((string * (string -> string option)) * serial) Symtab.table, |
|
1491 |
inst: unit Symtab.table, |
|
22061 | 1492 |
tyco: (typ_syntax * serial) Symtab.table, |
1493 |
const: (term_syntax * serial) Symtab.table |
|
20896 | 1494 |
}; |
18702 | 1495 |
|
20896 | 1496 |
fun mk_syntax_expr ((class, inst), (tyco, const)) = |
1497 |
SyntaxExpr { class = class, inst = inst, tyco = tyco, const = const }; |
|
1498 |
fun map_syntax_expr f (SyntaxExpr { class, inst, tyco, const }) = |
|
1499 |
mk_syntax_expr (f ((class, inst), (tyco, const))); |
|
1500 |
fun merge_syntax_expr (SyntaxExpr { class = class1, inst = inst1, tyco = tyco1, const = const1 }, |
|
1501 |
SyntaxExpr { class = class2, inst = inst2, tyco = tyco2, const = const2 }) = |
|
1502 |
mk_syntax_expr ( |
|
1503 |
(Symtab.merge (eq_snd (op =)) (class1, class2), |
|
1504 |
Symtab.merge (op =) (inst1, inst2)), |
|
1505 |
(Symtab.merge (eq_snd (op =)) (tyco1, tyco2), |
|
1506 |
Symtab.merge (eq_snd (op =)) (const1, const2)) |
|
1507 |
); |
|
1508 |
||
1509 |
datatype syntax_modl = SyntaxModl of { |
|
21015 | 1510 |
alias: string Symtab.table, |
20896 | 1511 |
prolog: Pretty.T Symtab.table |
1512 |
}; |
|
20699 | 1513 |
|
21015 | 1514 |
fun mk_syntax_modl (alias, prolog) = |
1515 |
SyntaxModl { alias = alias, prolog = prolog }; |
|
1516 |
fun map_syntax_modl f (SyntaxModl { alias, prolog }) = |
|
1517 |
mk_syntax_modl (f (alias, prolog)); |
|
1518 |
fun merge_syntax_modl (SyntaxModl { alias = alias1, prolog = prolog1 }, |
|
1519 |
SyntaxModl { alias = alias2, prolog = prolog2 }) = |
|
20896 | 1520 |
mk_syntax_modl ( |
21015 | 1521 |
Symtab.merge (op =) (alias1, alias2), |
20896 | 1522 |
Symtab.merge (op =) (prolog1, prolog2) |
1523 |
); |
|
20699 | 1524 |
|
21015 | 1525 |
type serializer = Args.T list |
22306
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
1526 |
-> (string -> string) |
21015 | 1527 |
-> string list |
1528 |
-> (string -> string option) |
|
1529 |
-> (string -> Pretty.T option) |
|
22306
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
1530 |
-> (string -> class_syntax option) |
22061 | 1531 |
-> (string -> typ_syntax option) |
1532 |
-> (string -> term_syntax option) |
|
21015 | 1533 |
-> CodegenThingol.code -> unit; |
20896 | 1534 |
|
1535 |
datatype target = Target of { |
|
1536 |
serial: serial, |
|
1537 |
serializer: serializer, |
|
1538 |
syntax_expr: syntax_expr, |
|
21015 | 1539 |
syntax_modl: syntax_modl, |
1540 |
reserved: string list |
|
20896 | 1541 |
}; |
20699 | 1542 |
|
21015 | 1543 |
fun mk_target (serial, ((serializer, reserved), (syntax_expr, syntax_modl))) = |
1544 |
Target { serial = serial, reserved = reserved, serializer = serializer, syntax_expr = syntax_expr, syntax_modl = syntax_modl }; |
|
1545 |
fun map_target f ( Target { serial, serializer, reserved, syntax_expr, syntax_modl } ) = |
|
1546 |
mk_target (f (serial, ((serializer, reserved), (syntax_expr, syntax_modl)))); |
|
1547 |
fun merge_target target (Target { serial = serial1, serializer = serializer, reserved = reserved1, |
|
1548 |
syntax_expr = syntax_expr1, syntax_modl = syntax_modl1 }, |
|
1549 |
Target { serial = serial2, serializer = _, reserved = reserved2, |
|
1550 |
syntax_expr = syntax_expr2, syntax_modl = syntax_modl2 }) = |
|
20896 | 1551 |
if serial1 = serial2 then |
21015 | 1552 |
mk_target (serial1, ((serializer, merge (op =) (reserved1, reserved2)), |
20896 | 1553 |
(merge_syntax_expr (syntax_expr1, syntax_expr2), |
1554 |
merge_syntax_modl (syntax_modl1, syntax_modl2)) |
|
1555 |
)) |
|
1556 |
else |
|
1557 |
error ("Incompatible serializers: " ^ quote target); |
|
1558 |
||
1559 |
structure CodegenSerializerData = TheoryDataFun |
|
1560 |
(struct |
|
1561 |
val name = "Pure/codegen_serializer"; |
|
1562 |
type T = target Symtab.table; |
|
1563 |
val empty = Symtab.empty; |
|
1564 |
val copy = I; |
|
1565 |
val extend = I; |
|
1566 |
fun merge _ = Symtab.join merge_target; |
|
1567 |
fun print _ _ = (); |
|
1568 |
end); |
|
1569 |
||
1570 |
fun the_serializer (Target { serializer, ... }) = serializer; |
|
21015 | 1571 |
fun the_reserved (Target { reserved, ... }) = reserved; |
20896 | 1572 |
fun the_syntax_expr (Target { syntax_expr = SyntaxExpr x, ... }) = x; |
1573 |
fun the_syntax_modl (Target { syntax_modl = SyntaxModl x, ... }) = x; |
|
1574 |
||
1575 |
fun add_serializer (target, seri) thy = |
|
18702 | 1576 |
let |
20896 | 1577 |
val _ = case Symtab.lookup (CodegenSerializerData.get thy) target |
1578 |
of SOME _ => warning ("overwriting existing serializer " ^ quote target) |
|
1579 |
| NONE => (); |
|
20699 | 1580 |
in |
20896 | 1581 |
thy |
1582 |
|> (CodegenSerializerData.map oo Symtab.map_default) |
|
21015 | 1583 |
(target, mk_target (serial (), ((seri, []), |
20896 | 1584 |
(mk_syntax_expr ((Symtab.empty, Symtab.empty), (Symtab.empty, Symtab.empty)), |
1585 |
mk_syntax_modl (Symtab.empty, Symtab.empty))))) |
|
21015 | 1586 |
(map_target (fn (serial, ((_, keywords), syntax)) => (serial, ((seri, keywords), syntax)))) |
20699 | 1587 |
end; |
1588 |
||
21015 | 1589 |
fun map_seri_data target f thy = |
1590 |
let |
|
1591 |
val _ = if is_some (Symtab.lookup (CodegenSerializerData.get thy) target) |
|
1592 |
then () |
|
1593 |
else error ("Unknown code target language: " ^ quote target); |
|
1594 |
in |
|
1595 |
thy |
|
1596 |
|> (CodegenSerializerData.map o Symtab.map_entry target o map_target) f |
|
1597 |
end; |
|
1598 |
||
22070 | 1599 |
val target_SML = "SML"; |
1600 |
val target_OCaml = "OCaml"; |
|
1601 |
val target_Haskell = "Haskell"; |
|
21015 | 1602 |
val target_diag = "diag"; |
1603 |
||
20896 | 1604 |
val _ = Context.add_setup ( |
1605 |
CodegenSerializerData.init |
|
22070 | 1606 |
#> add_serializer (target_SML, isar_seri_sml) |
1607 |
#> add_serializer (target_OCaml, isar_seri_ocaml) |
|
1608 |
#> add_serializer (target_Haskell, isar_seri_haskell) |
|
21015 | 1609 |
#> add_serializer (target_diag, (fn _ => fn _ => seri_diagnosis)) |
20896 | 1610 |
); |
1611 |
||
22306
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
1612 |
fun get_serializer thy target args labelled_name = fn cs => |
20896 | 1613 |
let |
1614 |
val data = case Symtab.lookup (CodegenSerializerData.get thy) target |
|
1615 |
of SOME data => data |
|
1616 |
| NONE => error ("Unknown code target language: " ^ quote target); |
|
1617 |
val seri = the_serializer data; |
|
21015 | 1618 |
val reserved = the_reserved data; |
1619 |
val { alias, prolog } = the_syntax_modl data; |
|
20896 | 1620 |
val { class, inst, tyco, const } = the_syntax_expr data; |
1621 |
fun fun_of sys = (Option.map fst oo Symtab.lookup) sys; |
|
21015 | 1622 |
val project = if target = target_diag then I |
1623 |
else CodegenThingol.project_code |
|
1624 |
(Symtab.keys class @ Symtab.keys inst @ Symtab.keys tyco @ Symtab.keys const) cs; |
|
20896 | 1625 |
in |
22306
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
1626 |
project #> seri args (labelled_name thy) reserved (Symtab.lookup alias) (Symtab.lookup prolog) |
21015 | 1627 |
(fun_of class) (fun_of tyco) (fun_of const) |
20896 | 1628 |
end; |
1629 |
||
21162 | 1630 |
val eval_verbose = ref false; |
1631 |
||
22464 | 1632 |
fun eval_term thy labelled_name code ((ref_name, reff), t) args = |
21162 | 1633 |
let |
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
1634 |
val val_name = "eval.EVAL.EVAL"; |
21162 | 1635 |
val val_name' = "ROOT.eval.EVAL"; |
22464 | 1636 |
val val_name'_args = space_implode " " (val_name' :: map (enclose "(" ")") args); |
21162 | 1637 |
val data = (the o Symtab.lookup (CodegenSerializerData.get thy)) "SML" |
1638 |
val reserved = the_reserved data; |
|
1639 |
val { alias, prolog } = the_syntax_modl data; |
|
1640 |
val { class, inst, tyco, const } = the_syntax_expr data; |
|
1641 |
fun fun_of sys = (Option.map fst oo Symtab.lookup) sys; |
|
1642 |
fun eval p = ( |
|
1643 |
reff := NONE; |
|
1644 |
if !eval_verbose then Pretty.writeln p else (); |
|
22197 | 1645 |
use_text "generated code for evaluation" Output.ml_output (!eval_verbose) |
21162 | 1646 |
((Pretty.output o Pretty.chunks) [p, |
22464 | 1647 |
str ("val _ = (" ^ ref_name ^ " := SOME (" ^ val_name'_args ^ "))") |
21162 | 1648 |
]); |
1649 |
case !reff |
|
1650 |
of NONE => error ("Could not retrieve value of ML reference " ^ quote ref_name |
|
1651 |
^ " (reference probably has been shadowed)") |
|
1652 |
| SOME value => value |
|
1653 |
); |
|
1654 |
in |
|
1655 |
code |
|
1656 |
|> CodegenThingol.add_eval_def (val_name, t) |
|
1657 |
|> CodegenThingol.project_code |
|
1658 |
(Symtab.keys class @ Symtab.keys inst @ Symtab.keys tyco @ Symtab.keys const) |
|
1659 |
(SOME [val_name]) |
|
22306
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
1660 |
|> seri_ml pr_sml pr_sml_modl I (labelled_name thy) reserved (Symtab.lookup alias) (Symtab.lookup prolog) |
21162 | 1661 |
(fun_of class) (fun_of tyco) (fun_of const) |
1662 |
|> eval |
|
1663 |
end; |
|
1664 |
||
21082 | 1665 |
fun assert_serializer thy target = |
1666 |
case Symtab.lookup (CodegenSerializerData.get thy) target |
|
1667 |
of SOME data => target |
|
1668 |
| NONE => error ("Unknown code target language: " ^ quote target); |
|
1669 |
||
20896 | 1670 |
fun has_serialization f thy targets name = |
1671 |
forall ( |
|
1672 |
is_some o (fn tab => Symtab.lookup tab name) o f o the_syntax_expr o the |
|
22007 | 1673 |
o Symtab.lookup (CodegenSerializerData.get thy) |
20896 | 1674 |
) targets; |
1675 |
||
1676 |
val const_has_serialization = has_serialization #const; |
|
1677 |
||
1678 |
||
20699 | 1679 |
|
20931 | 1680 |
(** ML and toplevel interface **) |
20699 | 1681 |
|
1682 |
local |
|
1683 |
||
21015 | 1684 |
fun map_syntax_exprs target = |
1685 |
map_seri_data target o (apsnd o apsnd o apfst o map_syntax_expr); |
|
1686 |
fun map_syntax_modls target = |
|
1687 |
map_seri_data target o (apsnd o apsnd o apsnd o map_syntax_modl); |
|
1688 |
fun map_reserveds target = |
|
1689 |
map_seri_data target o (apsnd o apfst o apsnd); |
|
20896 | 1690 |
|
21122
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1691 |
fun gen_add_syntax_class prep_class prep_const target raw_class raw_syn thy = |
20699 | 1692 |
let |
21122
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1693 |
val cls = prep_class thy raw_class; |
20699 | 1694 |
val class = CodegenNames.class thy cls; |
20896 | 1695 |
fun mk_classop (const as (c, _)) = case AxClass.class_of_param thy c |
1696 |
of SOME class' => if cls = class' then CodegenNames.const thy const |
|
20699 | 1697 |
else error ("Not a class operation for class " ^ quote class ^ ": " ^ quote c) |
21122
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1698 |
| NONE => error ("Not a class operation: " ^ quote c); |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1699 |
fun mk_syntax_ops raw_ops = AList.lookup (op =) |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1700 |
((map o apfst) (mk_classop o prep_const thy) raw_ops); |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1701 |
in case raw_syn |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1702 |
of SOME (syntax, raw_ops) => |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1703 |
thy |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1704 |
|> (map_syntax_exprs target o apfst o apfst) |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1705 |
(Symtab.update (class, ((syntax, mk_syntax_ops raw_ops), serial ()))) |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1706 |
| NONE => |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1707 |
thy |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1708 |
|> (map_syntax_exprs target o apfst o apfst) |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1709 |
(Symtab.delete_safe class) |
20699 | 1710 |
end; |
1711 |
||
21122
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1712 |
fun gen_add_syntax_inst prep_class prep_tyco target (raw_tyco, raw_class) add_del thy = |
20699 | 1713 |
let |
1714 |
val inst = CodegenNames.instance thy (prep_class thy raw_class, prep_tyco thy raw_tyco); |
|
21122
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1715 |
in if add_del then |
20699 | 1716 |
thy |
20896 | 1717 |
|> (map_syntax_exprs target o apfst o apsnd) |
1718 |
(Symtab.update (inst, ())) |
|
21122
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1719 |
else |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1720 |
thy |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1721 |
|> (map_syntax_exprs target o apfst o apsnd) |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1722 |
(Symtab.delete_safe inst) |
20699 | 1723 |
end; |
1724 |
||
21122
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1725 |
fun gen_add_syntax_tyco prep_tyco target raw_tyco raw_syn thy = |
20699 | 1726 |
let |
21015 | 1727 |
val tyco = prep_tyco thy raw_tyco; |
21122
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1728 |
val tyco' = if tyco = "fun" then "fun" else CodegenNames.tyco thy tyco; |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1729 |
fun check_args (syntax as (n, _)) = if n <> Sign.arity_number thy tyco |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1730 |
then error ("Number of arguments mismatch in syntax for type constructor " ^ quote tyco) |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1731 |
else syntax |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1732 |
in case raw_syn |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1733 |
of SOME syntax => |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1734 |
thy |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1735 |
|> (map_syntax_exprs target o apsnd o apfst) |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1736 |
(Symtab.update (tyco', (check_args syntax, serial ()))) |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1737 |
| NONE => |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1738 |
thy |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1739 |
|> (map_syntax_exprs target o apsnd o apfst) |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1740 |
(Symtab.delete_safe tyco') |
20699 | 1741 |
end; |
1742 |
||
21122
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1743 |
fun gen_add_syntax_const prep_const target raw_c raw_syn thy = |
20699 | 1744 |
let |
21015 | 1745 |
val c = prep_const thy raw_c; |
1746 |
val c' = CodegenNames.const thy c; |
|
21122
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1747 |
fun check_args (syntax as (n, _)) = if n > (length o fst o strip_type o Sign.the_const_type thy o fst) c |
21015 | 1748 |
then error ("Too many arguments in syntax for constant " ^ (quote o fst) c) |
21122
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1749 |
else syntax; |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1750 |
in case raw_syn |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1751 |
of SOME syntax => |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1752 |
thy |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1753 |
|> (map_syntax_exprs target o apsnd o apsnd) |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1754 |
(Symtab.update (c', (check_args syntax, serial ()))) |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1755 |
| NONE => |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1756 |
thy |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1757 |
|> (map_syntax_exprs target o apsnd o apsnd) |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1758 |
(Symtab.delete_safe c') |
20699 | 1759 |
end; |
1760 |
||
22355 | 1761 |
fun cert_class thy class = |
1762 |
let |
|
1763 |
val _ = AxClass.get_definition thy class; |
|
1764 |
in class end; |
|
1765 |
||
21463
42dd50268c8b
completed class parameter handling in axclass.ML
haftmann
parents:
21389
diff
changeset
|
1766 |
fun read_class thy raw_class = |
42dd50268c8b
completed class parameter handling in axclass.ML
haftmann
parents:
21389
diff
changeset
|
1767 |
let |
42dd50268c8b
completed class parameter handling in axclass.ML
haftmann
parents:
21389
diff
changeset
|
1768 |
val class = Sign.intern_class thy raw_class; |
42dd50268c8b
completed class parameter handling in axclass.ML
haftmann
parents:
21389
diff
changeset
|
1769 |
val _ = AxClass.get_definition thy class; |
42dd50268c8b
completed class parameter handling in axclass.ML
haftmann
parents:
21389
diff
changeset
|
1770 |
in class end; |
42dd50268c8b
completed class parameter handling in axclass.ML
haftmann
parents:
21389
diff
changeset
|
1771 |
|
22355 | 1772 |
fun cert_tyco thy tyco = |
1773 |
let |
|
1774 |
val _ = if Sign.declared_tyname thy tyco then () |
|
1775 |
else error ("No such type constructor: " ^ quote tyco); |
|
1776 |
in tyco end; |
|
1777 |
||
1778 |
fun read_tyco thy raw_tyco = |
|
20699 | 1779 |
let |
1780 |
val tyco = Sign.intern_type thy raw_tyco; |
|
1781 |
val _ = if Sign.declared_tyname thy tyco then () |
|
1782 |
else error ("No such type constructor: " ^ quote raw_tyco); |
|
1783 |
in tyco end; |
|
1784 |
||
22070 | 1785 |
fun idfs_of_const thy c = |
20699 | 1786 |
let |
21837 | 1787 |
val c' = (c, Sign.the_const_type thy c); |
1788 |
val c'' = CodegenConsts.norm_of_typ thy c'; |
|
1789 |
in (c'', CodegenNames.const thy c'') end; |
|
20699 | 1790 |
|
22076
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
1791 |
fun no_bindings x = (Option.map o apsnd) |
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
1792 |
(fn pretty => fn pr => fn vars => pretty (pr vars)) x; |
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
1793 |
|
22070 | 1794 |
fun gen_add_haskell_monad prep_const c_run c_mbind c_kbind thy = |
1795 |
let |
|
1796 |
val c_run' = |
|
1797 |
(CodegenConsts.norm thy o prep_const thy) c_run; |
|
22076
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
1798 |
val c_mbind' = |
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
1799 |
(CodegenConsts.norm thy o prep_const thy) c_mbind; |
22070 | 1800 |
val c_mbind'' = |
22076
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
1801 |
CodegenNames.const thy c_mbind'; |
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
1802 |
val c_kbind' = |
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
1803 |
(CodegenConsts.norm thy o prep_const thy) c_kbind; |
22070 | 1804 |
val c_kbind'' = |
22076
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
1805 |
CodegenNames.const thy c_kbind'; |
22070 | 1806 |
val pr = pretty_haskell_monad c_mbind'' c_kbind'' |
1807 |
in |
|
1808 |
thy |
|
1809 |
|> gen_add_syntax_const (K I) target_Haskell c_run' (SOME pr) |
|
22076
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
1810 |
|> gen_add_syntax_const (K I) target_Haskell c_mbind' |
22306
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
1811 |
(no_bindings (SOME (parse_infix fst (L, 1) ">>="))) |
22076
42ae57200d96
changed dictionary representation to explicit classrel witnesses
haftmann
parents:
22070
diff
changeset
|
1812 |
|> gen_add_syntax_const (K I) target_Haskell c_kbind' |
22306
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
1813 |
(no_bindings (SOME (parse_infix fst (L, 1) ">>"))) |
22070 | 1814 |
end; |
1815 |
||
21015 | 1816 |
fun add_reserved target = |
1817 |
map_reserveds target o insert (op =); |
|
1818 |
||
1819 |
fun add_modl_alias target = |
|
1820 |
map_syntax_modls target o apfst o Symtab.update o apsnd CodegenNames.check_modulename; |
|
1821 |
||
1822 |
fun add_modl_prolog target = |
|
1823 |
map_syntax_modls target o apsnd o |
|
1824 |
(fn (modl, NONE) => Symtab.delete modl | (modl, SOME prolog) => |
|
1825 |
Symtab.update (modl, Pretty.str prolog)); |
|
20931 | 1826 |
|
1827 |
fun zip_list (x::xs) f g = |
|
21015 | 1828 |
f |
1829 |
#-> (fn y => |
|
1830 |
fold_map (fn x => g |-- f >> pair x) xs |
|
20931 | 1831 |
#-> (fn xys => pair ((x, y) :: xys))); |
20699 | 1832 |
|
20931 | 1833 |
structure P = OuterParse |
1834 |
and K = OuterKeyword |
|
20699 | 1835 |
|
20931 | 1836 |
fun parse_multi_syntax parse_thing parse_syntax = |
1837 |
P.and_list1 parse_thing |
|
21015 | 1838 |
#-> (fn things => Scan.repeat1 (P.$$$ "(" |-- P.name -- |
1839 |
(zip_list things parse_syntax (P.$$$ "and")) --| P.$$$ ")")); |
|
20699 | 1840 |
|
21122
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1841 |
val (infixK, infixlK, infixrK) = ("infix", "infixl", "infixr"); |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1842 |
|
22306
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
1843 |
fun parse_syntax prep_arg xs = |
21122
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1844 |
Scan.option (( |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1845 |
((P.$$$ infixK >> K X) |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1846 |
|| (P.$$$ infixlK >> K L) |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1847 |
|| (P.$$$ infixrK >> K R)) |
22306
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
1848 |
-- P.nat >> parse_infix prep_arg |
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
1849 |
|| Scan.succeed (parse_mixfix prep_arg)) |
21122
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1850 |
-- P.string |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1851 |
>> (fn (parse, s) => parse s)) xs; |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1852 |
|
21285 | 1853 |
val (code_classK, code_instanceK, code_typeK, code_constK, code_monadK, |
21015 | 1854 |
code_reservedK, code_modulenameK, code_moduleprologK) = |
21285 | 1855 |
("code_class", "code_instance", "code_type", "code_const", "code_monad", |
21015 | 1856 |
"code_reserved", "code_modulename", "code_moduleprolog"); |
20699 | 1857 |
|
21015 | 1858 |
in |
1859 |
||
22355 | 1860 |
val parse_syntax = parse_syntax; |
1861 |
||
1862 |
val add_syntax_class = gen_add_syntax_class cert_class (K I); |
|
1863 |
val add_syntax_inst = gen_add_syntax_inst cert_class cert_tyco; |
|
1864 |
val add_syntax_tyco = gen_add_syntax_tyco cert_tyco; |
|
1865 |
val add_syntax_const = gen_add_syntax_const (K I); |
|
1866 |
||
1867 |
val add_syntax_class_cmd = gen_add_syntax_class read_class CodegenConsts.read_const; |
|
1868 |
val add_syntax_inst_cmd = gen_add_syntax_inst read_class read_tyco; |
|
1869 |
val add_syntax_tyco_cmd = gen_add_syntax_tyco read_tyco; |
|
1870 |
val add_syntax_const_cmd = gen_add_syntax_const CodegenConsts.read_const; |
|
1871 |
||
1872 |
fun add_syntax_tycoP target tyco = parse_syntax I >> add_syntax_tyco_cmd target tyco; |
|
1873 |
fun add_syntax_constP target c = parse_syntax fst >> (add_syntax_const_cmd target c o no_bindings); |
|
1874 |
||
21015 | 1875 |
fun add_pretty_list target nill cons mk_list mk_char_string target_cons thy = |
20699 | 1876 |
let |
22070 | 1877 |
val (_, nil'') = idfs_of_const thy nill; |
1878 |
val (cons', cons'') = idfs_of_const thy cons; |
|
21015 | 1879 |
val pr = pretty_list nil'' cons'' mk_list mk_char_string target_cons; |
20699 | 1880 |
in |
21015 | 1881 |
thy |
22355 | 1882 |
|> add_syntax_const target cons' (SOME pr) |
20699 | 1883 |
end; |
1884 |
||
21015 | 1885 |
fun add_pretty_ml_string target nill cons str mk_char mk_string target_implode thy = |
20699 | 1886 |
let |
22070 | 1887 |
val (_, nil'') = idfs_of_const thy nill; |
1888 |
val (_, cons'') = idfs_of_const thy cons; |
|
1889 |
val (str', _) = idfs_of_const thy str; |
|
21015 | 1890 |
val pr = pretty_ml_string nil'' cons'' mk_char mk_string target_implode; |
20699 | 1891 |
in |
21015 | 1892 |
thy |
22355 | 1893 |
|> add_syntax_const target str' (SOME pr) |
20699 | 1894 |
end; |
1895 |
||
21015 | 1896 |
fun add_undefined target undef target_undefined thy = |
1897 |
let |
|
22070 | 1898 |
val (undef', _) = idfs_of_const thy undef; |
1899 |
fun pr _ _ _ _ = str target_undefined; |
|
21015 | 1900 |
in |
1901 |
thy |
|
22355 | 1902 |
|> add_syntax_const target undef' (SOME (~1, pr)) |
21837 | 1903 |
end; |
1904 |
||
1905 |
fun add_pretty_imperative_monad_bind target bind thy = |
|
1906 |
let |
|
22070 | 1907 |
val (bind', _) = idfs_of_const thy bind; |
22036 | 1908 |
val pr = pretty_imperative_monad_bind |
21837 | 1909 |
in |
1910 |
thy |
|
22355 | 1911 |
|> add_syntax_const target bind' (SOME pr) |
21015 | 1912 |
end; |
20931 | 1913 |
|
22070 | 1914 |
val add_haskell_monad = gen_add_haskell_monad CodegenConsts.read_const; |
1915 |
||
20931 | 1916 |
val code_classP = |
1917 |
OuterSyntax.command code_classK "define code syntax for class" K.thy_decl ( |
|
1918 |
parse_multi_syntax P.xname |
|
21122
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1919 |
(Scan.option (P.string -- Scan.optional (P.$$$ "where" |-- Scan.repeat1 |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1920 |
(P.term --| (P.$$$ "\\<equiv>" || P.$$$ "==") -- P.string)) [])) |
20931 | 1921 |
>> (Toplevel.theory oo fold) (fn (target, syns) => |
22355 | 1922 |
fold (fn (raw_class, syn) => add_syntax_class_cmd target raw_class syn) syns) |
20931 | 1923 |
); |
1924 |
||
1925 |
val code_instanceP = |
|
1926 |
OuterSyntax.command code_instanceK "define code syntax for instance" K.thy_decl ( |
|
1927 |
parse_multi_syntax (P.xname --| P.$$$ "::" -- P.xname) |
|
21122
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1928 |
((P.minus >> K true) || Scan.succeed false) |
20931 | 1929 |
>> (Toplevel.theory oo fold) (fn (target, syns) => |
22355 | 1930 |
fold (fn (raw_inst, add_del) => add_syntax_inst_cmd target raw_inst add_del) syns) |
20931 | 1931 |
); |
1932 |
||
1933 |
val code_typeP = |
|
1934 |
OuterSyntax.command code_typeK "define code syntax for type constructor" K.thy_decl ( |
|
22306
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
1935 |
parse_multi_syntax P.xname (parse_syntax I) |
21015 | 1936 |
>> (Toplevel.theory oo fold) (fn (target, syns) => |
22355 | 1937 |
fold (fn (raw_tyco, syn) => add_syntax_tyco_cmd target raw_tyco syn) syns) |
20931 | 1938 |
); |
1939 |
||
1940 |
val code_constP = |
|
1941 |
OuterSyntax.command code_constK "define code syntax for constant" K.thy_decl ( |
|
22306
a532c39c8917
changed representation of constants; consistent name handling
haftmann
parents:
22248
diff
changeset
|
1942 |
parse_multi_syntax P.term (parse_syntax fst) |
21015 | 1943 |
>> (Toplevel.theory oo fold) (fn (target, syns) => |
22355 | 1944 |
fold (fn (raw_const, syn) => add_syntax_const_cmd target raw_const (no_bindings syn)) syns) |
20931 | 1945 |
); |
1946 |
||
22070 | 1947 |
val code_monadP = |
1948 |
OuterSyntax.command code_monadK "define code syntax for Haskell monads" K.thy_decl ( |
|
1949 |
P.term -- P.term -- P.term |
|
1950 |
>> (fn ((raw_run, raw_mbind), raw_kbind) => Toplevel.theory |
|
1951 |
(add_haskell_monad raw_run raw_mbind raw_kbind)) |
|
1952 |
); |
|
21285 | 1953 |
|
21015 | 1954 |
val code_reservedP = |
1955 |
OuterSyntax.command code_reservedK "declare words as reserved for target language" K.thy_decl ( |
|
1956 |
P.name -- Scan.repeat1 P.name |
|
1957 |
>> (fn (target, reserveds) => (Toplevel.theory o fold (add_reserved target)) reserveds) |
|
1958 |
) |
|
20699 | 1959 |
|
21015 | 1960 |
val code_modulenameP = |
21082 | 1961 |
OuterSyntax.command code_modulenameK "alias module to other name" K.thy_decl ( |
21015 | 1962 |
P.name -- Scan.repeat1 (P.name -- P.name) |
1963 |
>> (fn (target, modlnames) => (Toplevel.theory o fold (add_modl_alias target)) modlnames) |
|
1964 |
) |
|
20699 | 1965 |
|
21015 | 1966 |
val code_moduleprologP = |
21082 | 1967 |
OuterSyntax.command code_moduleprologK "add prolog to module" K.thy_decl ( |
21122
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1968 |
P.name -- Scan.repeat1 (P.name -- (P.text >> (fn "-" => NONE | s => SOME s))) |
21015 | 1969 |
>> (fn (target, prologs) => (Toplevel.theory o fold (add_modl_prolog target)) prologs) |
1970 |
) |
|
1971 |
||
21122
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1972 |
val _ = OuterSyntax.add_keywords [infixK, infixlK, infixrK]; |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1973 |
|
21015 | 1974 |
val _ = OuterSyntax.add_parsers [code_classP, code_instanceP, code_typeP, code_constP, |
22070 | 1975 |
code_reservedP, code_modulenameP, code_moduleprologP, code_monadP]; |
20699 | 1976 |
|
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
1977 |
(*including serializer defaults*) |
21122
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1978 |
val _ = Context.add_setup ( |
22355 | 1979 |
add_syntax_tyco "SML" "fun" (SOME (2, fn pr_typ => fn fxy => fn [ty1, ty2] => |
21463
42dd50268c8b
completed class parameter handling in axclass.ML
haftmann
parents:
21389
diff
changeset
|
1980 |
(gen_brackify (case fxy of NOBR => false | _ => eval_fxy (INFX (1, R)) fxy) o Pretty.breaks) [ |
21122
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1981 |
pr_typ (INFX (1, X)) ty1, |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1982 |
str "->", |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1983 |
pr_typ (INFX (1, R)) ty2 |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1984 |
])) |
22355 | 1985 |
#> add_syntax_tyco "OCaml" "fun" (SOME (2, fn pr_typ => fn fxy => fn [ty1, ty2] => |
21837 | 1986 |
(gen_brackify (case fxy of NOBR => false | _ => eval_fxy (INFX (1, R)) fxy) o Pretty.breaks) [ |
1987 |
pr_typ (INFX (1, X)) ty1, |
|
1988 |
str "->", |
|
1989 |
pr_typ (INFX (1, R)) ty2 |
|
1990 |
])) |
|
22355 | 1991 |
#> add_syntax_tyco "Haskell" "fun" (SOME (2, fn pr_typ => fn fxy => fn [ty1, ty2] => |
21122
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1992 |
brackify_infix (1, R) fxy [ |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1993 |
pr_typ (INFX (1, X)) ty1, |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1994 |
str "->", |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1995 |
pr_typ (INFX (1, R)) ty2 |
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
1996 |
])) |
21952 | 1997 |
(*IntInt resp. Big_int are added later when code extraction for numerals is set up*) |
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
1998 |
#> add_reserved "SML" "o" (*dictionary projections use it already*) |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
1999 |
#> fold (add_reserved "Haskell") [ |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
2000 |
"Prelude", "Main", "Bool", "Maybe", "Either", "Ordering", "Char", "String", "Int", |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
2001 |
"Integer", "Float", "Double", "Rational", "IO", "Eq", "Ord", "Enum", "Bounded", |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
2002 |
"Num", "Real", "Integral", "Fractional", "Floating", "RealFloat", "Monad", "Functor", |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
2003 |
"AlreadyExists", "ArithException", "ArrayException", "AssertionFailed", "AsyncException", |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
2004 |
"BlockedOnDeadMVar", "Deadlock", "Denormal", "DivideByZero", "DotNetException", "DynException", |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
2005 |
"Dynamic", "EOF", "EQ", "EmptyRec", "ErrorCall", "ExitException", "ExitFailure", |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
2006 |
"ExitSuccess", "False", "GT", "HeapOverflow", |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
2007 |
"IO", "IOError", "IOException", "IllegalOperation", |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
2008 |
"IndexOutOfBounds", "Just", "Key", "LT", "Left", "LossOfPrecision", "NoMethodError", |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
2009 |
"NoSuchThing", "NonTermination", "Nothing", "Obj", "OtherError", "Overflow", |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
2010 |
"PatternMatchFail", "PermissionDenied", "ProtocolError", "RecConError", "RecSelError", |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
2011 |
"RecUpdError", "ResourceBusy", "ResourceExhausted", "Right", "StackOverflow", |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
2012 |
"ThreadKilled", "True", "TyCon", "TypeRep", "UndefinedElement", "Underflow", |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
2013 |
"UnsupportedOperation", "UserError", "abs", "absReal", "acos", "acosh", "all", |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
2014 |
"and", "any", "appendFile", "asTypeOf", "asciiTab", "asin", "asinh", "atan", |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
2015 |
"atan2", "atanh", "basicIORun", "blockIO", "boundedEnumFrom", "boundedEnumFromThen", |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
2016 |
"boundedEnumFromThenTo", "boundedEnumFromTo", "boundedPred", "boundedSucc", "break", |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
2017 |
"catch", "catchException", "ceiling", "compare", "concat", "concatMap", "const", |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
2018 |
"cos", "cosh", "curry", "cycle", "decodeFloat", "denominator", "div", "divMod", |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
2019 |
"doubleToRatio", "doubleToRational", "drop", "dropWhile", "either", "elem", |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
2020 |
"emptyRec", "encodeFloat", "enumFrom", "enumFromThen", "enumFromThenTo", |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
2021 |
"enumFromTo", "error", "even", "exp", "exponent", "fail", "filter", "flip", |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
2022 |
"floatDigits", "floatProperFraction", "floatRadix", "floatRange", "floatToRational", |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
2023 |
"floor", "fmap", "foldl", "foldl'", "foldl1", "foldr", "foldr1", "fromDouble", |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
2024 |
"fromEnum", "fromEnum_0", "fromInt", "fromInteger", "fromIntegral", "fromObj", |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
2025 |
"fromRational", "fst", "gcd", "getChar", "getContents", "getLine", "head", |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
2026 |
"id", "inRange", "index", "init", "intToRatio", "interact", "ioError", "isAlpha", |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
2027 |
"isAlphaNum", "isDenormalized", "isDigit", "isHexDigit", "isIEEE", "isInfinite", |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
2028 |
"isLower", "isNaN", "isNegativeZero", "isOctDigit", "isSpace", "isUpper", "iterate", "iterate'", |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
2029 |
"last", "lcm", "length", "lex", "lexDigits", "lexLitChar", "lexmatch", "lines", "log", |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
2030 |
"logBase", "lookup", "loop", "map", "mapM", "mapM_", "max", "maxBound", "maximum", |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
2031 |
"maybe", "min", "minBound", "minimum", "mod", "negate", "nonnull", "not", "notElem", |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
2032 |
"null", "numerator", "numericEnumFrom", "numericEnumFromThen", "numericEnumFromThenTo", |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
2033 |
"numericEnumFromTo", "odd", "or", "otherwise", "pi", "pred", |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
2034 |
"print", "product", "properFraction", "protectEsc", "putChar", "putStr", "putStrLn", |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
2035 |
"quot", "quotRem", "range", "rangeSize", "rationalToDouble", "rationalToFloat", |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
2036 |
"rationalToRealFloat", "read", "readDec", "readField", "readFieldName", "readFile", |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
2037 |
"readFloat", "readHex", "readIO", "readInt", "readList", "readLitChar", "readLn", |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
2038 |
"readOct", "readParen", "readSigned", "reads", "readsPrec", "realFloatToRational", |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
2039 |
"realToFrac", "recip", "reduce", "rem", "repeat", "replicate", "return", "reverse", |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
2040 |
"round", "scaleFloat", "scanl", "scanl1", "scanr", "scanr1", "seq", "sequence", |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
2041 |
"sequence_", "show", "showChar", "showException", "showField", "showList", |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
2042 |
"showLitChar", "showParen", "showString", "shows", "showsPrec", "significand", |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
2043 |
"signum", "signumReal", "sin", "sinh", "snd", "span", "splitAt", "sqrt", "subtract", |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
2044 |
"succ", "sum", "tail", "take", "takeWhile", "takeWhile1", "tan", "tanh", "threadToIOResult", |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
2045 |
"throw", "toEnum", "toInt", "toInteger", "toObj", "toRational", "truncate", "uncurry", |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
2046 |
"undefined", "unlines", "unsafeCoerce", "unsafeIndex", "unsafeRangeSize", "until", "unwords", |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
2047 |
"unzip", "unzip3", "userError", "words", "writeFile", "zip", "zip3", "zipWith", "zipWith3" |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
2048 |
] (*due to weird handling of ':', we can't do anything else than to import *all* prelude symbols*) |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21895
diff
changeset
|
2049 |
|
21122
b1fdd08e0ea3
new serialization syntax; experimental extensions
haftmann
parents:
21094
diff
changeset
|
2050 |
) |
21082 | 2051 |
|
20699 | 2052 |
end; (*local*) |
18702 | 2053 |
|
21093 | 2054 |
end; (*struct*) |