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