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