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