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