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