author | haftmann |
Tue, 15 Jun 2010 08:32:32 +0200 | |
changeset 37437 | 4202e11ae7dc |
parent 37384 | 5aba26803073 |
child 37439 | c72a43a7d2c5 |
permissions | -rw-r--r-- |
28054 | 1 |
(* Title: Tools/code/code_haskell.ML |
2 |
Author: Florian Haftmann, TU Muenchen |
|
3 |
||
4 |
Serializer for Haskell. |
|
5 |
*) |
|
6 |
||
7 |
signature CODE_HASKELL = |
|
8 |
sig |
|
9 |
val setup: theory -> theory |
|
10 |
end; |
|
11 |
||
12 |
structure Code_Haskell : CODE_HASKELL = |
|
13 |
struct |
|
14 |
||
15 |
val target = "Haskell"; |
|
16 |
||
17 |
open Basic_Code_Thingol; |
|
18 |
open Code_Printer; |
|
19 |
||
20 |
infixr 5 @@; |
|
21 |
infixr 5 @|; |
|
22 |
||
23 |
||
24 |
(** Haskell serializer **) |
|
25 |
||
33991 | 26 |
fun print_haskell_stmt labelled_name syntax_class syntax_tyco syntax_const |
32924
d2e9b2dab760
dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents:
32913
diff
changeset
|
27 |
reserved deresolve contr_classparam_typs deriving_show = |
28054 | 28 |
let |
30364
577edc39b501
moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents:
30280
diff
changeset
|
29 |
val deresolve_base = Long_Name.base_name o deresolve; |
28054 | 30 |
fun class_name class = case syntax_class class |
31 |
of NONE => deresolve class |
|
28687 | 32 |
| SOME class => class; |
33991 | 33 |
fun print_typcontext tyvars vs = case maps (fn (v, sort) => map (pair v) sort) vs |
28054 | 34 |
of [] => [] |
37384
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37242
diff
changeset
|
35 |
| constraints => enum "," "(" ")" ( |
28054 | 36 |
map (fn (v, class) => |
37384
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37242
diff
changeset
|
37 |
str (class_name class ^ " " ^ lookup_var tyvars v)) constraints) |
28054 | 38 |
@@ str " => "; |
33991 | 39 |
fun print_typforall tyvars vs = case map fst vs |
28054 | 40 |
of [] => [] |
41 |
| vnames => str "forall " :: Pretty.breaks |
|
32924
d2e9b2dab760
dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents:
32913
diff
changeset
|
42 |
(map (str o lookup_var tyvars) vnames) @ str "." @@ Pretty.brk 1; |
33991 | 43 |
fun print_tyco_expr tyvars fxy (tyco, tys) = |
44 |
brackify fxy (str tyco :: map (print_typ tyvars BR) tys) |
|
45 |
and print_typ tyvars fxy (tycoexpr as tyco `%% tys) = (case syntax_tyco tyco |
|
46 |
of NONE => print_tyco_expr tyvars fxy (deresolve tyco, tys) |
|
47 |
| SOME (i, print) => print (print_typ tyvars) fxy tys) |
|
48 |
| print_typ tyvars fxy (ITyVar v) = (str o lookup_var tyvars) v; |
|
49 |
fun print_typdecl tyvars (vs, tycoexpr) = |
|
50 |
Pretty.block (print_typcontext tyvars vs @| print_tyco_expr tyvars NOBR tycoexpr); |
|
51 |
fun print_typscheme tyvars (vs, ty) = |
|
52 |
Pretty.block (print_typforall tyvars vs @ print_typcontext tyvars vs @| print_typ tyvars NOBR ty); |
|
35228 | 53 |
fun print_term tyvars some_thm vars fxy (IConst c) = |
54 |
print_app tyvars some_thm vars fxy (c, []) |
|
55 |
| print_term tyvars some_thm vars fxy (t as (t1 `$ t2)) = |
|
28054 | 56 |
(case Code_Thingol.unfold_const_app t |
35228 | 57 |
of SOME app => print_app tyvars some_thm vars fxy app |
28054 | 58 |
| _ => |
59 |
brackify fxy [ |
|
35228 | 60 |
print_term tyvars some_thm vars NOBR t1, |
61 |
print_term tyvars some_thm vars BR t2 |
|
28054 | 62 |
]) |
35228 | 63 |
| print_term tyvars some_thm vars fxy (IVar NONE) = |
31889 | 64 |
str "_" |
35228 | 65 |
| print_term tyvars some_thm vars fxy (IVar (SOME v)) = |
32924
d2e9b2dab760
dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents:
32913
diff
changeset
|
66 |
(str o lookup_var vars) v |
35228 | 67 |
| print_term tyvars some_thm vars fxy (t as _ `|=> _) = |
28054 | 68 |
let |
31874 | 69 |
val (binds, t') = Code_Thingol.unfold_pat_abs t; |
35228 | 70 |
val (ps, vars') = fold_map (print_bind tyvars some_thm BR o fst) binds vars; |
71 |
in brackets (str "\\" :: ps @ str "->" @@ print_term tyvars some_thm vars' NOBR t') end |
|
72 |
| print_term tyvars some_thm vars fxy (ICase (cases as (_, t0))) = |
|
28054 | 73 |
(case Code_Thingol.unfold_const_app t0 |
74 |
of SOME (c_ts as ((c, _), _)) => if is_none (syntax_const c) |
|
35228 | 75 |
then print_case tyvars some_thm vars fxy cases |
76 |
else print_app tyvars some_thm vars fxy c_ts |
|
77 |
| NONE => print_case tyvars some_thm vars fxy cases) |
|
78 |
and print_app_expr tyvars some_thm vars ((c, (_, tys)), ts) = case contr_classparam_typs c |
|
79 |
of [] => (str o deresolve) c :: map (print_term tyvars some_thm vars BR) ts |
|
28054 | 80 |
| fingerprint => let |
33957 | 81 |
val ts_fingerprint = ts ~~ take (length ts) fingerprint; |
28054 | 82 |
val needs_annotation = forall (fn (_, NONE) => true | (t, SOME _) => |
83 |
(not o Code_Thingol.locally_monomorphic) t) ts_fingerprint; |
|
35228 | 84 |
fun print_term_anno (t, NONE) _ = print_term tyvars some_thm vars BR t |
33991 | 85 |
| print_term_anno (t, SOME _) ty = |
35228 | 86 |
brackets [print_term tyvars some_thm vars NOBR t, str "::", print_typ tyvars NOBR ty]; |
28054 | 87 |
in |
88 |
if needs_annotation then |
|
33994 | 89 |
(str o deresolve) c :: map2 print_term_anno ts_fingerprint (take (length ts) tys) |
35228 | 90 |
else (str o deresolve) c :: map (print_term tyvars some_thm vars BR) ts |
28054 | 91 |
end |
33991 | 92 |
and print_app tyvars = gen_print_app (print_app_expr tyvars) (print_term tyvars) syntax_const |
35228 | 93 |
and print_bind tyvars some_thm fxy p = gen_print_bind (print_term tyvars) some_thm fxy p |
94 |
and print_case tyvars some_thm vars fxy (cases as ((_, [_]), _)) = |
|
28054 | 95 |
let |
29952
9aed85067721
unified variable names in case expressions; no exponential fork in translation of case expressions
haftmann
parents:
29832
diff
changeset
|
96 |
val (binds, body) = Code_Thingol.unfold_let (ICase cases); |
33991 | 97 |
fun print_match ((pat, ty), t) vars = |
28054 | 98 |
vars |
35228 | 99 |
|> print_bind tyvars some_thm BR pat |
100 |
|>> (fn p => semicolon [p, str "=", print_term tyvars some_thm vars NOBR t]) |
|
33991 | 101 |
val (ps, vars') = fold_map print_match binds vars; |
31665 | 102 |
in brackify_block fxy (str "let {") |
103 |
ps |
|
35228 | 104 |
(concat [str "}", str "in", print_term tyvars some_thm vars' NOBR body]) |
28054 | 105 |
end |
35228 | 106 |
| print_case tyvars some_thm vars fxy (((t, ty), clauses as _ :: _), _) = |
28054 | 107 |
let |
33991 | 108 |
fun print_select (pat, body) = |
28054 | 109 |
let |
35228 | 110 |
val (p, vars') = print_bind tyvars some_thm NOBR pat vars; |
111 |
in semicolon [p, str "->", print_term tyvars some_thm vars' NOBR body] end; |
|
36576 | 112 |
in Pretty.block_enclose |
113 |
(concat [str "(case", print_term tyvars some_thm vars NOBR t, str "of", str "{"], str "})") |
|
33991 | 114 |
(map print_select clauses) |
28054 | 115 |
end |
35228 | 116 |
| print_case tyvars some_thm vars fxy ((_, []), _) = |
31376 | 117 |
(brackify fxy o Pretty.breaks o map str) ["error", "\"empty case\""]; |
37437 | 118 |
fun print_stmt (name, Code_Thingol.Fun (_, (((vs, ty), raw_eqs), _))) = |
28054 | 119 |
let |
32924
d2e9b2dab760
dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents:
32913
diff
changeset
|
120 |
val tyvars = intro_vars (map fst vs) reserved; |
34269 | 121 |
fun print_err n = |
33991 | 122 |
semicolon ( |
28054 | 123 |
(str o deresolve_base) name |
124 |
:: map str (replicate n "_") |
|
125 |
@ str "=" |
|
126 |
:: str "error" |
|
33991 | 127 |
@@ (str o ML_Syntax.print_string |
30364
577edc39b501
moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents:
30280
diff
changeset
|
128 |
o Long_Name.base_name o Long_Name.qualifier) name |
34269 | 129 |
); |
35228 | 130 |
fun print_eqn ((ts, t), (some_thm, _)) = |
28054 | 131 |
let |
32913 | 132 |
val consts = fold Code_Thingol.add_constnames (t :: ts) []; |
32924
d2e9b2dab760
dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents:
32913
diff
changeset
|
133 |
val vars = reserved |
d2e9b2dab760
dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents:
32913
diff
changeset
|
134 |
|> intro_base_names |
32913 | 135 |
(is_none o syntax_const) deresolve consts |
32924
d2e9b2dab760
dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents:
32913
diff
changeset
|
136 |
|> intro_vars ((fold o Code_Thingol.fold_varnames) |
32925 | 137 |
(insert (op =)) ts []); |
28054 | 138 |
in |
139 |
semicolon ( |
|
140 |
(str o deresolve_base) name |
|
35228 | 141 |
:: map (print_term tyvars some_thm vars BR) ts |
28054 | 142 |
@ str "=" |
35228 | 143 |
@@ print_term tyvars some_thm vars NOBR t |
28054 | 144 |
) |
145 |
end; |
|
146 |
in |
|
147 |
Pretty.chunks ( |
|
33991 | 148 |
semicolon [ |
28054 | 149 |
(str o suffix " ::" o deresolve_base) name, |
33991 | 150 |
print_typscheme tyvars (vs, ty) |
28054 | 151 |
] |
34269 | 152 |
:: (case filter (snd o snd) raw_eqs |
153 |
of [] => [print_err ((length o fst o Code_Thingol.unfold_fun) ty)] |
|
154 |
| eqs => map print_eqn eqs) |
|
28054 | 155 |
) |
156 |
end |
|
33991 | 157 |
| print_stmt (name, Code_Thingol.Datatype (_, (vs, []))) = |
28054 | 158 |
let |
32924
d2e9b2dab760
dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents:
32913
diff
changeset
|
159 |
val tyvars = intro_vars (map fst vs) reserved; |
28054 | 160 |
in |
161 |
semicolon [ |
|
162 |
str "data", |
|
33991 | 163 |
print_typdecl tyvars (vs, (deresolve_base name, map (ITyVar o fst) vs)) |
28054 | 164 |
] |
165 |
end |
|
33991 | 166 |
| print_stmt (name, Code_Thingol.Datatype (_, (vs, [(co, [ty])]))) = |
28054 | 167 |
let |
32924
d2e9b2dab760
dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents:
32913
diff
changeset
|
168 |
val tyvars = intro_vars (map fst vs) reserved; |
28054 | 169 |
in |
170 |
semicolon ( |
|
171 |
str "newtype" |
|
33991 | 172 |
:: print_typdecl tyvars (vs, (deresolve_base name, map (ITyVar o fst) vs)) |
28054 | 173 |
:: str "=" |
174 |
:: (str o deresolve_base) co |
|
33991 | 175 |
:: print_typ tyvars BR ty |
28054 | 176 |
:: (if deriving_show name then [str "deriving (Read, Show)"] else []) |
177 |
) |
|
178 |
end |
|
33991 | 179 |
| print_stmt (name, Code_Thingol.Datatype (_, (vs, co :: cos))) = |
28054 | 180 |
let |
32924
d2e9b2dab760
dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents:
32913
diff
changeset
|
181 |
val tyvars = intro_vars (map fst vs) reserved; |
33991 | 182 |
fun print_co (co, tys) = |
28054 | 183 |
concat ( |
184 |
(str o deresolve_base) co |
|
33991 | 185 |
:: map (print_typ tyvars BR) tys |
28054 | 186 |
) |
187 |
in |
|
188 |
semicolon ( |
|
189 |
str "data" |
|
33991 | 190 |
:: print_typdecl tyvars (vs, (deresolve_base name, map (ITyVar o fst) vs)) |
28054 | 191 |
:: str "=" |
33991 | 192 |
:: print_co co |
193 |
:: map ((fn p => Pretty.block [str "| ", p]) o print_co) cos |
|
28054 | 194 |
@ (if deriving_show name then [str "deriving (Read, Show)"] else []) |
195 |
) |
|
196 |
end |
|
37384
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37242
diff
changeset
|
197 |
| print_stmt (name, Code_Thingol.Class (_, (v, (super_classes, classparams)))) = |
28054 | 198 |
let |
32924
d2e9b2dab760
dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents:
32913
diff
changeset
|
199 |
val tyvars = intro_vars [v] reserved; |
33991 | 200 |
fun print_classparam (classparam, ty) = |
28054 | 201 |
semicolon [ |
28687 | 202 |
(str o deresolve_base) classparam, |
28054 | 203 |
str "::", |
33991 | 204 |
print_typ tyvars NOBR ty |
28054 | 205 |
] |
206 |
in |
|
207 |
Pretty.block_enclose ( |
|
208 |
Pretty.block [ |
|
209 |
str "class ", |
|
37384
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37242
diff
changeset
|
210 |
Pretty.block (print_typcontext tyvars [(v, map fst super_classes)]), |
32924
d2e9b2dab760
dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents:
32913
diff
changeset
|
211 |
str (deresolve_base name ^ " " ^ lookup_var tyvars v), |
28054 | 212 |
str " where {" |
213 |
], |
|
214 |
str "};" |
|
33991 | 215 |
) (map print_classparam classparams) |
28054 | 216 |
end |
37384
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37242
diff
changeset
|
217 |
| print_stmt (_, Code_Thingol.Classinst ((class, (tyco, vs)), (_, classparam_instances))) = |
28054 | 218 |
let |
32924
d2e9b2dab760
dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents:
32913
diff
changeset
|
219 |
val tyvars = intro_vars (map fst vs) reserved; |
37384
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37242
diff
changeset
|
220 |
fun print_classparam_instance ((classparam, const), (thm, _)) = case syntax_const classparam |
28687 | 221 |
of NONE => semicolon [ |
222 |
(str o deresolve_base) classparam, |
|
223 |
str "=", |
|
37384
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37242
diff
changeset
|
224 |
print_app tyvars (SOME thm) reserved NOBR (const, []) |
28687 | 225 |
] |
226 |
| SOME (k, pr) => |
|
227 |
let |
|
37384
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37242
diff
changeset
|
228 |
val (c, (_, tys)) = const; |
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37242
diff
changeset
|
229 |
val (vs, rhs) = (apfst o map) fst |
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37242
diff
changeset
|
230 |
(Code_Thingol.unfold_abs (Code_Thingol.eta_expand k (const, []))); |
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37242
diff
changeset
|
231 |
val s = if (is_some o syntax_const) c |
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37242
diff
changeset
|
232 |
then NONE else (SOME o Long_Name.base_name o deresolve) c; |
32924
d2e9b2dab760
dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents:
32913
diff
changeset
|
233 |
val vars = reserved |
37384
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37242
diff
changeset
|
234 |
|> intro_vars (map_filter I (s :: vs)); |
31889 | 235 |
val lhs = IConst (classparam, (([], []), tys)) `$$ map IVar vs; |
28687 | 236 |
(*dictionaries are not relevant at this late stage*) |
237 |
in |
|
238 |
semicolon [ |
|
35228 | 239 |
print_term tyvars (SOME thm) vars NOBR lhs, |
28687 | 240 |
str "=", |
35228 | 241 |
print_term tyvars (SOME thm) vars NOBR rhs |
28687 | 242 |
] |
243 |
end; |
|
28054 | 244 |
in |
245 |
Pretty.block_enclose ( |
|
246 |
Pretty.block [ |
|
247 |
str "instance ", |
|
33991 | 248 |
Pretty.block (print_typcontext tyvars vs), |
28054 | 249 |
str (class_name class ^ " "), |
33991 | 250 |
print_typ tyvars BR (tyco `%% map (ITyVar o fst) vs), |
28054 | 251 |
str " where {" |
252 |
], |
|
253 |
str "};" |
|
37384
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37242
diff
changeset
|
254 |
) (map print_classparam_instance classparam_instances) |
28054 | 255 |
end; |
33991 | 256 |
in print_stmt end; |
28054 | 257 |
|
32924
d2e9b2dab760
dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents:
32913
diff
changeset
|
258 |
fun haskell_program_of_program labelled_name module_name module_prefix reserved raw_module_alias program = |
28054 | 259 |
let |
260 |
val module_alias = if is_some module_name then K module_name else raw_module_alias; |
|
32924
d2e9b2dab760
dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents:
32913
diff
changeset
|
261 |
val reserved = Name.make_context reserved; |
d2e9b2dab760
dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents:
32913
diff
changeset
|
262 |
val mk_name_module = mk_name_module reserved module_prefix module_alias program; |
28054 | 263 |
fun add_stmt (name, (stmt, deps)) = |
264 |
let |
|
32924
d2e9b2dab760
dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents:
32913
diff
changeset
|
265 |
val (module_name, base) = dest_name name; |
28054 | 266 |
val module_name' = mk_name_module module_name; |
267 |
val mk_name_stmt = yield_singleton Name.variants; |
|
268 |
fun add_fun upper (nsp_fun, nsp_typ) = |
|
269 |
let |
|
270 |
val (base', nsp_fun') = |
|
32924
d2e9b2dab760
dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents:
32913
diff
changeset
|
271 |
mk_name_stmt (if upper then first_upper base else base) nsp_fun |
28054 | 272 |
in (base', (nsp_fun', nsp_typ)) end; |
273 |
fun add_typ (nsp_fun, nsp_typ) = |
|
274 |
let |
|
32924
d2e9b2dab760
dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents:
32913
diff
changeset
|
275 |
val (base', nsp_typ') = mk_name_stmt (first_upper base) nsp_typ |
28054 | 276 |
in (base', (nsp_fun, nsp_typ')) end; |
277 |
val add_name = case stmt |
|
278 |
of Code_Thingol.Fun _ => add_fun false |
|
279 |
| Code_Thingol.Datatype _ => add_typ |
|
280 |
| Code_Thingol.Datatypecons _ => add_fun true |
|
281 |
| Code_Thingol.Class _ => add_typ |
|
282 |
| Code_Thingol.Classrel _ => pair base |
|
283 |
| Code_Thingol.Classparam _ => add_fun false |
|
284 |
| Code_Thingol.Classinst _ => pair base; |
|
285 |
fun add_stmt' base' = case stmt |
|
286 |
of Code_Thingol.Datatypecons _ => |
|
30364
577edc39b501
moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents:
30280
diff
changeset
|
287 |
cons (name, (Long_Name.append module_name' base', NONE)) |
28054 | 288 |
| Code_Thingol.Classrel _ => I |
289 |
| Code_Thingol.Classparam _ => |
|
30364
577edc39b501
moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents:
30280
diff
changeset
|
290 |
cons (name, (Long_Name.append module_name' base', NONE)) |
577edc39b501
moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents:
30280
diff
changeset
|
291 |
| _ => cons (name, (Long_Name.append module_name' base', SOME stmt)); |
28054 | 292 |
in |
32924
d2e9b2dab760
dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents:
32913
diff
changeset
|
293 |
Symtab.map_default (module_name', ([], ([], (reserved, reserved)))) |
28054 | 294 |
(apfst (fold (insert (op = : string * string -> bool)) deps)) |
295 |
#> `(fn program => add_name ((snd o snd o the o Symtab.lookup program) module_name')) |
|
296 |
#-> (fn (base', names) => |
|
297 |
(Symtab.map_entry module_name' o apsnd) (fn (stmts, _) => |
|
298 |
(add_stmt' base' stmts, names))) |
|
299 |
end; |
|
300 |
val hs_program = fold add_stmt (AList.make (fn name => |
|
301 |
(Graph.get_node program name, Graph.imm_succs program name)) |
|
302 |
(Graph.strong_conn program |> flat)) Symtab.empty; |
|
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
303 |
fun deresolver name = (fst o the o AList.lookup (op =) ((fst o snd o the |
32924
d2e9b2dab760
dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents:
32913
diff
changeset
|
304 |
o Symtab.lookup hs_program) ((mk_name_module o fst o dest_name) name))) name |
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
305 |
handle Option => error ("Unknown statement name: " ^ labelled_name name); |
28054 | 306 |
in (deresolver, hs_program) end; |
307 |
||
308 |
fun serialize_haskell module_prefix raw_module_name string_classes labelled_name |
|
32924
d2e9b2dab760
dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents:
32913
diff
changeset
|
309 |
raw_reserved includes raw_module_alias |
36535 | 310 |
syntax_class syntax_tyco syntax_const (code_of_pretty, code_writeln) program stmt_names destination = |
28054 | 311 |
let |
36535 | 312 |
val presentation_stmt_names = Code_Target.stmt_names_of_destination destination; |
313 |
val module_name = if null presentation_stmt_names then raw_module_name else SOME "Code"; |
|
32924
d2e9b2dab760
dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents:
32913
diff
changeset
|
314 |
val reserved = fold (insert (op =) o fst) includes raw_reserved; |
28054 | 315 |
val (deresolver, hs_program) = haskell_program_of_program labelled_name |
32924
d2e9b2dab760
dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents:
32913
diff
changeset
|
316 |
module_name module_prefix reserved raw_module_alias program; |
28054 | 317 |
val contr_classparam_typs = Code_Thingol.contr_classparam_typs program; |
318 |
fun deriving_show tyco = |
|
319 |
let |
|
320 |
fun deriv _ "fun" = false |
|
34085 | 321 |
| deriv tycos tyco = not (tyco = Code_Thingol.fun_tyco) |
322 |
andalso (member (op =) tycos tyco |
|
323 |
orelse case try (Graph.get_node program) tyco |
|
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
324 |
of SOME (Code_Thingol.Datatype (_, (_, cs))) => forall (deriv' (tyco :: tycos)) |
28054 | 325 |
(maps snd cs) |
34085 | 326 |
| NONE => true) |
28054 | 327 |
and deriv' tycos (tyco `%% tys) = deriv tycos tyco |
328 |
andalso forall (deriv' tycos) tys |
|
329 |
| deriv' _ (ITyVar _) = true |
|
330 |
in deriv [] tyco end; |
|
32924
d2e9b2dab760
dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents:
32913
diff
changeset
|
331 |
val reserved = make_vars reserved; |
33991 | 332 |
fun print_stmt qualified = print_haskell_stmt labelled_name |
32924
d2e9b2dab760
dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents:
32913
diff
changeset
|
333 |
syntax_class syntax_tyco syntax_const reserved |
30364
577edc39b501
moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents:
30280
diff
changeset
|
334 |
(if qualified then deresolver else Long_Name.base_name o deresolver) |
32903 | 335 |
contr_classparam_typs |
28054 | 336 |
(if string_classes then deriving_show else K false); |
33991 | 337 |
fun print_module name content = |
28054 | 338 |
(name, Pretty.chunks [ |
339 |
str ("module " ^ name ^ " where {"), |
|
340 |
str "", |
|
341 |
content, |
|
342 |
str "", |
|
343 |
str "}" |
|
344 |
]); |
|
345 |
fun serialize_module1 (module_name', (deps, (stmts, _))) = |
|
346 |
let |
|
347 |
val stmt_names = map fst stmts; |
|
33421
3789fe962a08
always be qualified -- suspected smartness in fact never worked as expected
haftmann
parents:
32925
diff
changeset
|
348 |
val qualified = is_none module_name; |
3789fe962a08
always be qualified -- suspected smartness in fact never worked as expected
haftmann
parents:
32925
diff
changeset
|
349 |
val imports = subtract (op =) stmt_names deps |
28054 | 350 |
|> distinct (op =) |
33421
3789fe962a08
always be qualified -- suspected smartness in fact never worked as expected
haftmann
parents:
32925
diff
changeset
|
351 |
|> map_filter (try deresolver) |
30364
577edc39b501
moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents:
30280
diff
changeset
|
352 |
|> map Long_Name.qualifier |
28054 | 353 |
|> distinct (op =); |
33991 | 354 |
fun print_import_include (name, _) = str ("import qualified " ^ name ^ ";"); |
34049 | 355 |
fun print_import_module name = str ((if qualified |
356 |
then "import qualified " |
|
357 |
else "import ") ^ name ^ ";"); |
|
33991 | 358 |
val import_ps = map print_import_include includes @ map print_import_module imports |
34073
7b0bf55adecd
repaired accident: do not forget module contents if there are no imports
haftmann
parents:
34049
diff
changeset
|
359 |
val content = Pretty.chunks2 ((if null import_ps then [] else [Pretty.chunks import_ps]) |
33991 | 360 |
@ map_filter |
361 |
(fn (name, (_, SOME stmt)) => SOME (print_stmt qualified (name, stmt)) |
|
362 |
| (_, (_, NONE)) => NONE) stmts |
|
363 |
); |
|
364 |
in print_module module_name' content end; |
|
365 |
fun serialize_module2 (_, (_, (stmts, _))) = Pretty.chunks2 (map_filter |
|
36535 | 366 |
(fn (name, (_, SOME stmt)) => if null presentation_stmt_names |
367 |
orelse member (op =) presentation_stmt_names name |
|
33991 | 368 |
then SOME (print_stmt false (name, stmt)) |
28054 | 369 |
else NONE |
33991 | 370 |
| (_, (_, NONE)) => NONE) stmts); |
28054 | 371 |
val serialize_module = |
36535 | 372 |
if null presentation_stmt_names then serialize_module1 else pair "" o serialize_module2; |
29832
b4919260eaec
check for destination directory, do not allocate it gratuitously
haftmann
parents:
29794
diff
changeset
|
373 |
fun check_destination destination = |
b4919260eaec
check for destination directory, do not allocate it gratuitously
haftmann
parents:
29794
diff
changeset
|
374 |
(File.check destination; destination); |
28054 | 375 |
fun write_module destination (modlname, content) = |
376 |
let |
|
377 |
val filename = case modlname |
|
378 |
of "" => Path.explode "Main.hs" |
|
379 |
| _ => (Path.ext "hs" o Path.explode o implode o separate "/" |
|
30364
577edc39b501
moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents:
30280
diff
changeset
|
380 |
o Long_Name.explode) modlname; |
28054 | 381 |
val pathname = Path.append destination filename; |
382 |
val _ = File.mkdir (Path.dir pathname); |
|
29135 | 383 |
in File.write pathname |
384 |
("{-# OPTIONS_GHC -fglasgow-exts #-}\n\n" |
|
34073
7b0bf55adecd
repaired accident: do not forget module contents if there are no imports
haftmann
parents:
34049
diff
changeset
|
385 |
^ code_of_pretty content) |
29135 | 386 |
end |
28054 | 387 |
in |
388 |
Code_Target.mk_serialization target NONE |
|
34073
7b0bf55adecd
repaired accident: do not forget module contents if there are no imports
haftmann
parents:
34049
diff
changeset
|
389 |
(fn NONE => K () o map (code_writeln o snd) | SOME file => K () o map |
29832
b4919260eaec
check for destination directory, do not allocate it gratuitously
haftmann
parents:
29794
diff
changeset
|
390 |
(write_module (check_destination file))) |
34073
7b0bf55adecd
repaired accident: do not forget module contents if there are no imports
haftmann
parents:
34049
diff
changeset
|
391 |
(rpair [] o cat_lines o map (code_of_pretty o snd)) |
33991 | 392 |
(map (uncurry print_module) includes |
28054 | 393 |
@ map serialize_module (Symtab.dest hs_program)) |
394 |
destination |
|
395 |
end; |
|
396 |
||
28064 | 397 |
val literals = let |
398 |
fun char_haskell c = |
|
399 |
let |
|
400 |
val s = ML_Syntax.print_char c; |
|
401 |
in if s = "'" then "\\'" else s end; |
|
34944
970e1466028d
code literals: distinguish numeral classes by different entries
haftmann
parents:
34269
diff
changeset
|
402 |
fun numeral_haskell k = if k >= 0 then string_of_int k |
970e1466028d
code literals: distinguish numeral classes by different entries
haftmann
parents:
34269
diff
changeset
|
403 |
else Library.enclose "(" ")" (signed_string_of_int k); |
28064 | 404 |
in Literals { |
34178
a78b8d5b91cb
take care for destructive print mode properly using dedicated pretty builders
haftmann
parents:
34085
diff
changeset
|
405 |
literal_char = Library.enclose "'" "'" o char_haskell, |
28064 | 406 |
literal_string = quote o translate_string char_haskell, |
34944
970e1466028d
code literals: distinguish numeral classes by different entries
haftmann
parents:
34269
diff
changeset
|
407 |
literal_numeral = numeral_haskell, |
970e1466028d
code literals: distinguish numeral classes by different entries
haftmann
parents:
34269
diff
changeset
|
408 |
literal_positive_numeral = numeral_haskell, |
970e1466028d
code literals: distinguish numeral classes by different entries
haftmann
parents:
34269
diff
changeset
|
409 |
literal_naive_numeral = numeral_haskell, |
34178
a78b8d5b91cb
take care for destructive print mode properly using dedicated pretty builders
haftmann
parents:
34085
diff
changeset
|
410 |
literal_list = enum "," "[" "]", |
28064 | 411 |
infix_cons = (5, ":") |
412 |
} end; |
|
413 |
||
28054 | 414 |
|
415 |
(** optional monad syntax **) |
|
416 |
||
417 |
fun pretty_haskell_monad c_bind = |
|
418 |
let |
|
31874 | 419 |
fun dest_bind t1 t2 = case Code_Thingol.split_pat_abs t2 |
31889 | 420 |
of SOME ((pat, ty), t') => |
421 |
SOME ((SOME ((pat, ty), true), t1), t') |
|
28145 | 422 |
| NONE => NONE; |
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
423 |
fun dest_monad c_bind_name (IConst (c, _) `$ t1 `$ t2) = |
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
424 |
if c = c_bind_name then dest_bind t1 t2 |
28145 | 425 |
else NONE |
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
426 |
| dest_monad _ t = case Code_Thingol.split_let t |
28145 | 427 |
of SOME (((pat, ty), tbind), t') => |
31889 | 428 |
SOME ((SOME ((pat, ty), false), tbind), t') |
28145 | 429 |
| NONE => NONE; |
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
430 |
fun implode_monad c_bind_name = Code_Thingol.unfoldr (dest_monad c_bind_name); |
33991 | 431 |
fun print_monad print_bind print_term (NONE, t) vars = |
432 |
(semicolon [print_term vars NOBR t], vars) |
|
433 |
| print_monad print_bind print_term (SOME ((bind, _), true), t) vars = vars |
|
434 |
|> print_bind NOBR bind |
|
435 |
|>> (fn p => semicolon [p, str "<-", print_term vars NOBR t]) |
|
436 |
| print_monad print_bind print_term (SOME ((bind, _), false), t) vars = vars |
|
437 |
|> print_bind NOBR bind |
|
438 |
|>> (fn p => semicolon [str "let", p, str "=", print_term vars NOBR t]); |
|
439 |
fun pretty _ [c_bind'] print_term thm vars fxy [(t1, _), (t2, _)] = case dest_bind t1 t2 |
|
28145 | 440 |
of SOME (bind, t') => let |
31054 | 441 |
val (binds, t'') = implode_monad c_bind' t' |
33991 | 442 |
val (ps, vars') = fold_map (print_monad (gen_print_bind (K print_term) thm) print_term) |
443 |
(bind :: binds) vars; |
|
444 |
in |
|
34178
a78b8d5b91cb
take care for destructive print mode properly using dedicated pretty builders
haftmann
parents:
34085
diff
changeset
|
445 |
(brackify fxy o single o enclose "do {" "}" o Pretty.breaks) |
33991 | 446 |
(ps @| print_term vars' NOBR t'') |
447 |
end |
|
28145 | 448 |
| NONE => brackify_infix (1, L) fxy |
37242
97097e589715
brackify_infix etc.: no break before infix operator -- eases survival in Scala
haftmann
parents:
36960
diff
changeset
|
449 |
(print_term vars (INFX (1, L)) t1, str ">>=", print_term vars (INFX (1, X)) t2) |
31054 | 450 |
in (2, ([c_bind], pretty)) end; |
28054 | 451 |
|
28145 | 452 |
fun add_monad target' raw_c_bind thy = |
28054 | 453 |
let |
31156 | 454 |
val c_bind = Code.read_const thy raw_c_bind; |
28054 | 455 |
in if target = target' then |
456 |
thy |
|
28145 | 457 |
|> Code_Target.add_syntax_const target c_bind |
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
458 |
(SOME (pretty_haskell_monad c_bind)) |
28054 | 459 |
else error "Only Haskell target allows for monad syntax" end; |
460 |
||
461 |
||
462 |
(** Isar setup **) |
|
463 |
||
33991 | 464 |
fun isar_seri_haskell module_name = |
28054 | 465 |
Code_Target.parse_args (Scan.option (Args.$$$ "root" -- Args.colon |-- Args.name) |
466 |
-- Scan.optional (Args.$$$ "string_classes" >> K true) false |
|
467 |
>> (fn (module_prefix, string_classes) => |
|
33991 | 468 |
serialize_haskell module_prefix module_name string_classes)); |
28054 | 469 |
|
470 |
val _ = |
|
36960
01594f816e3a
prefer structure Keyword, Parse, Parse_Spec, Outer_Syntax;
wenzelm
parents:
36576
diff
changeset
|
471 |
Outer_Syntax.command "code_monad" "define code syntax for monads" Keyword.thy_decl ( |
01594f816e3a
prefer structure Keyword, Parse, Parse_Spec, Outer_Syntax;
wenzelm
parents:
36576
diff
changeset
|
472 |
Parse.term_group -- Parse.name >> (fn (raw_bind, target) => |
28145 | 473 |
Toplevel.theory (add_monad target raw_bind)) |
28054 | 474 |
); |
475 |
||
476 |
val setup = |
|
28064 | 477 |
Code_Target.add_target (target, (isar_seri_haskell, literals)) |
33991 | 478 |
#> Code_Target.add_syntax_tyco target "fun" (SOME (2, fn print_typ => fn fxy => fn [ty1, ty2] => |
37242
97097e589715
brackify_infix etc.: no break before infix operator -- eases survival in Scala
haftmann
parents:
36960
diff
changeset
|
479 |
brackify_infix (1, R) fxy ( |
33991 | 480 |
print_typ (INFX (1, X)) ty1, |
28054 | 481 |
str "->", |
33991 | 482 |
print_typ (INFX (1, R)) ty2 |
37242
97097e589715
brackify_infix etc.: no break before infix operator -- eases survival in Scala
haftmann
parents:
36960
diff
changeset
|
483 |
))) |
28054 | 484 |
#> fold (Code_Target.add_reserved target) [ |
485 |
"hiding", "deriving", "where", "case", "of", "infix", "infixl", "infixr", |
|
486 |
"import", "default", "forall", "let", "in", "class", "qualified", "data", |
|
487 |
"newtype", "instance", "if", "then", "else", "type", "as", "do", "module" |
|
488 |
] |
|
489 |
#> fold (Code_Target.add_reserved target) [ |
|
490 |
"Prelude", "Main", "Bool", "Maybe", "Either", "Ordering", "Char", "String", "Int", |
|
491 |
"Integer", "Float", "Double", "Rational", "IO", "Eq", "Ord", "Enum", "Bounded", |
|
492 |
"Num", "Real", "Integral", "Fractional", "Floating", "RealFloat", "Monad", "Functor", |
|
493 |
"AlreadyExists", "ArithException", "ArrayException", "AssertionFailed", "AsyncException", |
|
494 |
"BlockedOnDeadMVar", "Deadlock", "Denormal", "DivideByZero", "DotNetException", "DynException", |
|
495 |
"Dynamic", "EOF", "EQ", "EmptyRec", "ErrorCall", "ExitException", "ExitFailure", |
|
496 |
"ExitSuccess", "False", "GT", "HeapOverflow", |
|
497 |
"IOError", "IOException", "IllegalOperation", |
|
498 |
"IndexOutOfBounds", "Just", "Key", "LT", "Left", "LossOfPrecision", "NoMethodError", |
|
499 |
"NoSuchThing", "NonTermination", "Nothing", "Obj", "OtherError", "Overflow", |
|
500 |
"PatternMatchFail", "PermissionDenied", "ProtocolError", "RecConError", "RecSelError", |
|
501 |
"RecUpdError", "ResourceBusy", "ResourceExhausted", "Right", "StackOverflow", |
|
502 |
"ThreadKilled", "True", "TyCon", "TypeRep", "UndefinedElement", "Underflow", |
|
503 |
"UnsupportedOperation", "UserError", "abs", "absReal", "acos", "acosh", "all", |
|
504 |
"and", "any", "appendFile", "asTypeOf", "asciiTab", "asin", "asinh", "atan", |
|
505 |
"atan2", "atanh", "basicIORun", "blockIO", "boundedEnumFrom", "boundedEnumFromThen", |
|
506 |
"boundedEnumFromThenTo", "boundedEnumFromTo", "boundedPred", "boundedSucc", "break", |
|
507 |
"catch", "catchException", "ceiling", "compare", "concat", "concatMap", "const", |
|
508 |
"cos", "cosh", "curry", "cycle", "decodeFloat", "denominator", "div", "divMod", |
|
509 |
"doubleToRatio", "doubleToRational", "drop", "dropWhile", "either", "elem", |
|
510 |
"emptyRec", "encodeFloat", "enumFrom", "enumFromThen", "enumFromThenTo", |
|
511 |
"enumFromTo", "error", "even", "exp", "exponent", "fail", "filter", "flip", |
|
512 |
"floatDigits", "floatProperFraction", "floatRadix", "floatRange", "floatToRational", |
|
513 |
"floor", "fmap", "foldl", "foldl'", "foldl1", "foldr", "foldr1", "fromDouble", |
|
514 |
"fromEnum", "fromEnum_0", "fromInt", "fromInteger", "fromIntegral", "fromObj", |
|
515 |
"fromRational", "fst", "gcd", "getChar", "getContents", "getLine", "head", |
|
516 |
"id", "inRange", "index", "init", "intToRatio", "interact", "ioError", "isAlpha", |
|
517 |
"isAlphaNum", "isDenormalized", "isDigit", "isHexDigit", "isIEEE", "isInfinite", |
|
518 |
"isLower", "isNaN", "isNegativeZero", "isOctDigit", "isSpace", "isUpper", "iterate", "iterate'", |
|
519 |
"last", "lcm", "length", "lex", "lexDigits", "lexLitChar", "lexmatch", "lines", "log", |
|
520 |
"logBase", "lookup", "loop", "map", "mapM", "mapM_", "max", "maxBound", "maximum", |
|
521 |
"maybe", "min", "minBound", "minimum", "mod", "negate", "nonnull", "not", "notElem", |
|
522 |
"null", "numerator", "numericEnumFrom", "numericEnumFromThen", "numericEnumFromThenTo", |
|
523 |
"numericEnumFromTo", "odd", "or", "otherwise", "pi", "pred", |
|
524 |
"print", "product", "properFraction", "protectEsc", "putChar", "putStr", "putStrLn", |
|
525 |
"quot", "quotRem", "range", "rangeSize", "rationalToDouble", "rationalToFloat", |
|
526 |
"rationalToRealFloat", "read", "readDec", "readField", "readFieldName", "readFile", |
|
527 |
"readFloat", "readHex", "readIO", "readInt", "readList", "readLitChar", "readLn", |
|
528 |
"readOct", "readParen", "readSigned", "reads", "readsPrec", "realFloatToRational", |
|
529 |
"realToFrac", "recip", "reduce", "rem", "repeat", "replicate", "return", "reverse", |
|
530 |
"round", "scaleFloat", "scanl", "scanl1", "scanr", "scanr1", "seq", "sequence", |
|
531 |
"sequence_", "show", "showChar", "showException", "showField", "showList", |
|
532 |
"showLitChar", "showParen", "showString", "shows", "showsPrec", "significand", |
|
533 |
"signum", "signumReal", "sin", "sinh", "snd", "span", "splitAt", "sqrt", "subtract", |
|
534 |
"succ", "sum", "tail", "take", "takeWhile", "takeWhile1", "tan", "tanh", "threadToIOResult", |
|
535 |
"throw", "toEnum", "toInt", "toInteger", "toObj", "toRational", "truncate", "uncurry", |
|
536 |
"undefined", "unlines", "unsafeCoerce", "unsafeIndex", "unsafeRangeSize", "until", "unwords", |
|
537 |
"unzip", "unzip3", "userError", "words", "writeFile", "zip", "zip3", "zipWith", "zipWith3" |
|
538 |
] (*due to weird handling of ':', we can't do anything else than to import *all* prelude symbols*); |
|
539 |
||
540 |
end; (*struct*) |