author | blanchet |
Mon, 26 Nov 2012 13:35:05 +0100 | |
changeset 50222 | 40e3c3be6bca |
parent 48568 | 084cd758a8ab |
child 51143 | 0a2371e7ced3 |
permissions | -rw-r--r-- |
37745
6315b6426200
checking generated code for various target languages
haftmann
parents:
37669
diff
changeset
|
1 |
(* Title: Tools/Code/code_haskell.ML |
28054 | 2 |
Author: Florian Haftmann, TU Muenchen |
3 |
||
4 |
Serializer for Haskell. |
|
5 |
*) |
|
6 |
||
7 |
signature CODE_HASKELL = |
|
8 |
sig |
|
44926
de3ed037c9a5
create central list for language extensions used by the haskell code generator
noschinl
parents:
44852
diff
changeset
|
9 |
val language_params: string |
37745
6315b6426200
checking generated code for various target languages
haftmann
parents:
37669
diff
changeset
|
10 |
val target: string |
28054 | 11 |
val setup: theory -> theory |
12 |
end; |
|
13 |
||
14 |
structure Code_Haskell : CODE_HASKELL = |
|
15 |
struct |
|
16 |
||
17 |
val target = "Haskell"; |
|
18 |
||
44926
de3ed037c9a5
create central list for language extensions used by the haskell code generator
noschinl
parents:
44852
diff
changeset
|
19 |
val language_extensions = |
44940 | 20 |
["EmptyDataDecls", "RankNTypes", "ScopedTypeVariables"]; |
44926
de3ed037c9a5
create central list for language extensions used by the haskell code generator
noschinl
parents:
44852
diff
changeset
|
21 |
|
de3ed037c9a5
create central list for language extensions used by the haskell code generator
noschinl
parents:
44852
diff
changeset
|
22 |
val language_pragma = |
44940 | 23 |
"{-# LANGUAGE " ^ commas language_extensions ^ " #-}"; |
44926
de3ed037c9a5
create central list for language extensions used by the haskell code generator
noschinl
parents:
44852
diff
changeset
|
24 |
|
de3ed037c9a5
create central list for language extensions used by the haskell code generator
noschinl
parents:
44852
diff
changeset
|
25 |
val language_params = |
44940 | 26 |
space_implode " " (map (prefix "-X") language_extensions); |
44926
de3ed037c9a5
create central list for language extensions used by the haskell code generator
noschinl
parents:
44852
diff
changeset
|
27 |
|
28054 | 28 |
open Basic_Code_Thingol; |
29 |
open Code_Printer; |
|
30 |
||
31 |
infixr 5 @@; |
|
32 |
infixr 5 @|; |
|
33 |
||
34 |
||
35 |
(** Haskell serializer **) |
|
36 |
||
47609 | 37 |
fun print_haskell_stmt class_syntax tyco_syntax const_syntax |
44793
fddb09e6f84d
removing previous crude approximation to add type annotations to disambiguate types
bulwahn
parents:
44792
diff
changeset
|
38 |
reserved deresolve deriving_show = |
28054 | 39 |
let |
38923 | 40 |
fun class_name class = case class_syntax class |
28054 | 41 |
of NONE => deresolve class |
28687 | 42 |
| SOME class => class; |
33991 | 43 |
fun print_typcontext tyvars vs = case maps (fn (v, sort) => map (pair v) sort) vs |
28054 | 44 |
of [] => [] |
37384
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37242
diff
changeset
|
45 |
| constraints => enum "," "(" ")" ( |
28054 | 46 |
map (fn (v, class) => |
37384
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37242
diff
changeset
|
47 |
str (class_name class ^ " " ^ lookup_var tyvars v)) constraints) |
28054 | 48 |
@@ str " => "; |
33991 | 49 |
fun print_typforall tyvars vs = case map fst vs |
28054 | 50 |
of [] => [] |
51 |
| vnames => str "forall " :: Pretty.breaks |
|
32924
d2e9b2dab760
dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents:
32913
diff
changeset
|
52 |
(map (str o lookup_var tyvars) vnames) @ str "." @@ Pretty.brk 1; |
33991 | 53 |
fun print_tyco_expr tyvars fxy (tyco, tys) = |
54 |
brackify fxy (str tyco :: map (print_typ tyvars BR) tys) |
|
47609 | 55 |
and print_typ tyvars fxy (tyco `%% tys) = (case tyco_syntax tyco |
33991 | 56 |
of NONE => print_tyco_expr tyvars fxy (deresolve tyco, tys) |
47609 | 57 |
| SOME (_, print) => print (print_typ tyvars) fxy tys) |
33991 | 58 |
| print_typ tyvars fxy (ITyVar v) = (str o lookup_var tyvars) v; |
48003
1d11af40b106
dropped sort constraints on datatype specifications
haftmann
parents:
47609
diff
changeset
|
59 |
fun print_typdecl tyvars (tyco, vs) = |
1d11af40b106
dropped sort constraints on datatype specifications
haftmann
parents:
47609
diff
changeset
|
60 |
print_tyco_expr tyvars NOBR (tyco, map ITyVar vs); |
33991 | 61 |
fun print_typscheme tyvars (vs, ty) = |
62 |
Pretty.block (print_typforall tyvars vs @ print_typcontext tyvars vs @| print_typ tyvars NOBR ty); |
|
48072
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
63 |
fun print_term tyvars some_thm vars fxy (IConst const) = |
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
64 |
print_app tyvars some_thm vars fxy (const, []) |
35228 | 65 |
| print_term tyvars some_thm vars fxy (t as (t1 `$ t2)) = |
28054 | 66 |
(case Code_Thingol.unfold_const_app t |
35228 | 67 |
of SOME app => print_app tyvars some_thm vars fxy app |
28054 | 68 |
| _ => |
69 |
brackify fxy [ |
|
35228 | 70 |
print_term tyvars some_thm vars NOBR t1, |
71 |
print_term tyvars some_thm vars BR t2 |
|
28054 | 72 |
]) |
35228 | 73 |
| print_term tyvars some_thm vars fxy (IVar NONE) = |
31889 | 74 |
str "_" |
35228 | 75 |
| 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
|
76 |
(str o lookup_var vars) v |
35228 | 77 |
| print_term tyvars some_thm vars fxy (t as _ `|=> _) = |
28054 | 78 |
let |
31874 | 79 |
val (binds, t') = Code_Thingol.unfold_pat_abs t; |
35228 | 80 |
val (ps, vars') = fold_map (print_bind tyvars some_thm BR o fst) binds vars; |
81 |
in brackets (str "\\" :: ps @ str "->" @@ print_term tyvars some_thm vars' NOBR t') end |
|
48072
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
82 |
| print_term tyvars some_thm vars fxy (ICase case_expr) = |
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
83 |
(case Code_Thingol.unfold_const_app (#primitive case_expr) |
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
84 |
of SOME (app as ({ name = c, ... }, _)) => if is_none (const_syntax c) |
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
85 |
then print_case tyvars some_thm vars fxy case_expr |
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
86 |
else print_app tyvars some_thm vars fxy app |
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
87 |
| NONE => print_case tyvars some_thm vars fxy case_expr) |
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
88 |
and print_app_expr tyvars some_thm vars ({ name = c, dom, range, annotate, ... }, ts) = |
44792
26b19918e670
adding minimalistic implementation for printing the type annotations
bulwahn
parents:
44789
diff
changeset
|
89 |
let |
48072
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
90 |
val ty = Library.foldr (fn (ty1, ty2) => Code_Thingol.fun_tyco `%% [ty1, ty2]) (dom, range) |
45009
99e1965f9c21
syntactic improvements and tuning names in the code generator due to Florian's code review
bulwahn
parents:
44940
diff
changeset
|
91 |
val printed_const = |
99e1965f9c21
syntactic improvements and tuning names in the code generator due to Florian's code review
bulwahn
parents:
44940
diff
changeset
|
92 |
if annotate then |
99e1965f9c21
syntactic improvements and tuning names in the code generator due to Florian's code review
bulwahn
parents:
44940
diff
changeset
|
93 |
brackets [(str o deresolve) c, str "::", print_typ tyvars NOBR ty] |
99e1965f9c21
syntactic improvements and tuning names in the code generator due to Florian's code review
bulwahn
parents:
44940
diff
changeset
|
94 |
else |
99e1965f9c21
syntactic improvements and tuning names in the code generator due to Florian's code review
bulwahn
parents:
44940
diff
changeset
|
95 |
(str o deresolve) c |
44792
26b19918e670
adding minimalistic implementation for printing the type annotations
bulwahn
parents:
44789
diff
changeset
|
96 |
in |
45009
99e1965f9c21
syntactic improvements and tuning names in the code generator due to Florian's code review
bulwahn
parents:
44940
diff
changeset
|
97 |
printed_const :: map (print_term tyvars some_thm vars BR) ts |
44792
26b19918e670
adding minimalistic implementation for printing the type annotations
bulwahn
parents:
44789
diff
changeset
|
98 |
end |
38923 | 99 |
and print_app tyvars = gen_print_app (print_app_expr tyvars) (print_term tyvars) const_syntax |
35228 | 100 |
and print_bind tyvars some_thm fxy p = gen_print_bind (print_term tyvars) some_thm fxy p |
48072
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
101 |
and print_case tyvars some_thm vars fxy { clauses = [], ... } = |
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
102 |
(brackify fxy o Pretty.breaks o map str) ["error", "\"empty case\""] |
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
103 |
| print_case tyvars some_thm vars fxy (case_expr as { clauses = [_], ... }) = |
28054 | 104 |
let |
48072
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
105 |
val (binds, body) = Code_Thingol.unfold_let (ICase case_expr); |
47609 | 106 |
fun print_match ((pat, _), t) vars = |
28054 | 107 |
vars |
35228 | 108 |
|> print_bind tyvars some_thm BR pat |
109 |
|>> (fn p => semicolon [p, str "=", print_term tyvars some_thm vars NOBR t]) |
|
33991 | 110 |
val (ps, vars') = fold_map print_match binds vars; |
31665 | 111 |
in brackify_block fxy (str "let {") |
112 |
ps |
|
35228 | 113 |
(concat [str "}", str "in", print_term tyvars some_thm vars' NOBR body]) |
28054 | 114 |
end |
48072
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
115 |
| print_case tyvars some_thm vars fxy { term = t, typ = ty, clauses = clauses as _ :: _, ... } = |
28054 | 116 |
let |
33991 | 117 |
fun print_select (pat, body) = |
28054 | 118 |
let |
35228 | 119 |
val (p, vars') = print_bind tyvars some_thm NOBR pat vars; |
120 |
in semicolon [p, str "->", print_term tyvars some_thm vars' NOBR body] end; |
|
36576 | 121 |
in Pretty.block_enclose |
122 |
(concat [str "(case", print_term tyvars some_thm vars NOBR t, str "of", str "{"], str "})") |
|
33991 | 123 |
(map print_select clauses) |
48072
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
124 |
end; |
37437 | 125 |
fun print_stmt (name, Code_Thingol.Fun (_, (((vs, ty), raw_eqs), _))) = |
28054 | 126 |
let |
32924
d2e9b2dab760
dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents:
32913
diff
changeset
|
127 |
val tyvars = intro_vars (map fst vs) reserved; |
34269 | 128 |
fun print_err n = |
33991 | 129 |
semicolon ( |
39207
0c3d19af759d
dropped ancient deresolve_base; plain_const_syntax also needs modification of instance statement
haftmann
parents:
39206
diff
changeset
|
130 |
(str o deresolve) name |
28054 | 131 |
:: map str (replicate n "_") |
132 |
@ str "=" |
|
133 |
:: str "error" |
|
33991 | 134 |
@@ (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
|
135 |
o Long_Name.base_name o Long_Name.qualifier) name |
34269 | 136 |
); |
35228 | 137 |
fun print_eqn ((ts, t), (some_thm, _)) = |
28054 | 138 |
let |
32913 | 139 |
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
|
140 |
val vars = reserved |
d2e9b2dab760
dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents:
32913
diff
changeset
|
141 |
|> intro_base_names |
38923 | 142 |
(is_none o const_syntax) deresolve consts |
32924
d2e9b2dab760
dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents:
32913
diff
changeset
|
143 |
|> intro_vars ((fold o Code_Thingol.fold_varnames) |
32925 | 144 |
(insert (op =)) ts []); |
28054 | 145 |
in |
146 |
semicolon ( |
|
39207
0c3d19af759d
dropped ancient deresolve_base; plain_const_syntax also needs modification of instance statement
haftmann
parents:
39206
diff
changeset
|
147 |
(str o deresolve) name |
35228 | 148 |
:: map (print_term tyvars some_thm vars BR) ts |
28054 | 149 |
@ str "=" |
35228 | 150 |
@@ print_term tyvars some_thm vars NOBR t |
28054 | 151 |
) |
152 |
end; |
|
153 |
in |
|
154 |
Pretty.chunks ( |
|
33991 | 155 |
semicolon [ |
39207
0c3d19af759d
dropped ancient deresolve_base; plain_const_syntax also needs modification of instance statement
haftmann
parents:
39206
diff
changeset
|
156 |
(str o suffix " ::" o deresolve) name, |
33991 | 157 |
print_typscheme tyvars (vs, ty) |
28054 | 158 |
] |
34269 | 159 |
:: (case filter (snd o snd) raw_eqs |
160 |
of [] => [print_err ((length o fst o Code_Thingol.unfold_fun) ty)] |
|
161 |
| eqs => map print_eqn eqs) |
|
28054 | 162 |
) |
163 |
end |
|
33991 | 164 |
| print_stmt (name, Code_Thingol.Datatype (_, (vs, []))) = |
28054 | 165 |
let |
48003
1d11af40b106
dropped sort constraints on datatype specifications
haftmann
parents:
47609
diff
changeset
|
166 |
val tyvars = intro_vars vs reserved; |
28054 | 167 |
in |
168 |
semicolon [ |
|
169 |
str "data", |
|
48003
1d11af40b106
dropped sort constraints on datatype specifications
haftmann
parents:
47609
diff
changeset
|
170 |
print_typdecl tyvars (deresolve name, vs) |
28054 | 171 |
] |
172 |
end |
|
37449 | 173 |
| print_stmt (name, Code_Thingol.Datatype (_, (vs, [((co, _), [ty])]))) = |
28054 | 174 |
let |
48003
1d11af40b106
dropped sort constraints on datatype specifications
haftmann
parents:
47609
diff
changeset
|
175 |
val tyvars = intro_vars vs reserved; |
28054 | 176 |
in |
177 |
semicolon ( |
|
178 |
str "newtype" |
|
48003
1d11af40b106
dropped sort constraints on datatype specifications
haftmann
parents:
47609
diff
changeset
|
179 |
:: print_typdecl tyvars (deresolve name, vs) |
28054 | 180 |
:: str "=" |
39207
0c3d19af759d
dropped ancient deresolve_base; plain_const_syntax also needs modification of instance statement
haftmann
parents:
39206
diff
changeset
|
181 |
:: (str o deresolve) co |
33991 | 182 |
:: print_typ tyvars BR ty |
28054 | 183 |
:: (if deriving_show name then [str "deriving (Read, Show)"] else []) |
184 |
) |
|
185 |
end |
|
33991 | 186 |
| print_stmt (name, Code_Thingol.Datatype (_, (vs, co :: cos))) = |
28054 | 187 |
let |
48003
1d11af40b106
dropped sort constraints on datatype specifications
haftmann
parents:
47609
diff
changeset
|
188 |
val tyvars = intro_vars vs reserved; |
37449 | 189 |
fun print_co ((co, _), tys) = |
28054 | 190 |
concat ( |
39207
0c3d19af759d
dropped ancient deresolve_base; plain_const_syntax also needs modification of instance statement
haftmann
parents:
39206
diff
changeset
|
191 |
(str o deresolve) co |
33991 | 192 |
:: map (print_typ tyvars BR) tys |
28054 | 193 |
) |
194 |
in |
|
195 |
semicolon ( |
|
196 |
str "data" |
|
48003
1d11af40b106
dropped sort constraints on datatype specifications
haftmann
parents:
47609
diff
changeset
|
197 |
:: print_typdecl tyvars (deresolve name, vs) |
28054 | 198 |
:: str "=" |
33991 | 199 |
:: print_co co |
200 |
:: map ((fn p => Pretty.block [str "| ", p]) o print_co) cos |
|
28054 | 201 |
@ (if deriving_show name then [str "deriving (Read, Show)"] else []) |
202 |
) |
|
203 |
end |
|
37447
ad3e04f289b6
transitive superclasses were also only a misunderstanding
haftmann
parents:
37446
diff
changeset
|
204 |
| print_stmt (name, Code_Thingol.Class (_, (v, (super_classes, classparams)))) = |
28054 | 205 |
let |
32924
d2e9b2dab760
dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents:
32913
diff
changeset
|
206 |
val tyvars = intro_vars [v] reserved; |
33991 | 207 |
fun print_classparam (classparam, ty) = |
28054 | 208 |
semicolon [ |
39207
0c3d19af759d
dropped ancient deresolve_base; plain_const_syntax also needs modification of instance statement
haftmann
parents:
39206
diff
changeset
|
209 |
(str o deresolve) classparam, |
28054 | 210 |
str "::", |
33991 | 211 |
print_typ tyvars NOBR ty |
28054 | 212 |
] |
213 |
in |
|
214 |
Pretty.block_enclose ( |
|
215 |
Pretty.block [ |
|
216 |
str "class ", |
|
37384
5aba26803073
more consistent naming aroud type classes and instances
haftmann
parents:
37242
diff
changeset
|
217 |
Pretty.block (print_typcontext tyvars [(v, map fst super_classes)]), |
39207
0c3d19af759d
dropped ancient deresolve_base; plain_const_syntax also needs modification of instance statement
haftmann
parents:
39206
diff
changeset
|
218 |
str (deresolve name ^ " " ^ lookup_var tyvars v), |
28054 | 219 |
str " where {" |
220 |
], |
|
221 |
str "};" |
|
33991 | 222 |
) (map print_classparam classparams) |
28054 | 223 |
end |
48072
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
224 |
| print_stmt (_, Code_Thingol.Classinst { class, tyco, vs, inst_params, ... }) = |
28054 | 225 |
let |
32924
d2e9b2dab760
dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents:
32913
diff
changeset
|
226 |
val tyvars = intro_vars (map fst vs) reserved; |
38923 | 227 |
fun requires_args classparam = case const_syntax classparam |
39207
0c3d19af759d
dropped ancient deresolve_base; plain_const_syntax also needs modification of instance statement
haftmann
parents:
39206
diff
changeset
|
228 |
of NONE => NONE |
0c3d19af759d
dropped ancient deresolve_base; plain_const_syntax also needs modification of instance statement
haftmann
parents:
39206
diff
changeset
|
229 |
| SOME (Code_Printer.Plain_const_syntax _) => SOME 0 |
0c3d19af759d
dropped ancient deresolve_base; plain_const_syntax also needs modification of instance statement
haftmann
parents:
39206
diff
changeset
|
230 |
| SOME (Code_Printer.Complex_const_syntax (k,_ )) => SOME k; |
37881 | 231 |
fun print_classparam_instance ((classparam, const), (thm, _)) = |
232 |
case requires_args classparam |
|
39207
0c3d19af759d
dropped ancient deresolve_base; plain_const_syntax also needs modification of instance statement
haftmann
parents:
39206
diff
changeset
|
233 |
of NONE => semicolon [ |
0c3d19af759d
dropped ancient deresolve_base; plain_const_syntax also needs modification of instance statement
haftmann
parents:
39206
diff
changeset
|
234 |
(str o Long_Name.base_name o deresolve) classparam, |
28687 | 235 |
str "=", |
37881 | 236 |
print_app tyvars (SOME thm) reserved NOBR (const, []) |
28687 | 237 |
] |
39207
0c3d19af759d
dropped ancient deresolve_base; plain_const_syntax also needs modification of instance statement
haftmann
parents:
39206
diff
changeset
|
238 |
| SOME k => |
37881 | 239 |
let |
48072
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
240 |
val { name = c, dom, range, ... } = const; |
37881 | 241 |
val (vs, rhs) = (apfst o map) fst |
242 |
(Code_Thingol.unfold_abs (Code_Thingol.eta_expand k (const, []))); |
|
38923 | 243 |
val s = if (is_some o const_syntax) c |
37881 | 244 |
then NONE else (SOME o Long_Name.base_name o deresolve) c; |
245 |
val vars = reserved |
|
246 |
|> intro_vars (map_filter I (s :: vs)); |
|
48072
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
247 |
val lhs = IConst { name = classparam, typargs = [], |
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
248 |
dicts = [], dom = dom, range = range, annotate = false } `$$ map IVar vs; |
45009
99e1965f9c21
syntactic improvements and tuning names in the code generator due to Florian's code review
bulwahn
parents:
44940
diff
changeset
|
249 |
(*dictionaries are not relevant at this late stage, |
99e1965f9c21
syntactic improvements and tuning names in the code generator due to Florian's code review
bulwahn
parents:
44940
diff
changeset
|
250 |
and these consts never need type annotations for disambiguation *) |
37881 | 251 |
in |
252 |
semicolon [ |
|
253 |
print_term tyvars (SOME thm) vars NOBR lhs, |
|
254 |
str "=", |
|
255 |
print_term tyvars (SOME thm) vars NOBR rhs |
|
256 |
] |
|
257 |
end; |
|
28054 | 258 |
in |
259 |
Pretty.block_enclose ( |
|
260 |
Pretty.block [ |
|
261 |
str "instance ", |
|
33991 | 262 |
Pretty.block (print_typcontext tyvars vs), |
28054 | 263 |
str (class_name class ^ " "), |
33991 | 264 |
print_typ tyvars BR (tyco `%% map (ITyVar o fst) vs), |
28054 | 265 |
str " where {" |
266 |
], |
|
267 |
str "};" |
|
48072
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
268 |
) (map print_classparam_instance inst_params) |
28054 | 269 |
end; |
33991 | 270 |
in print_stmt end; |
28054 | 271 |
|
39202 | 272 |
fun haskell_program_of_program labelled_name module_alias module_prefix reserved = |
273 |
let |
|
274 |
fun namify_fun upper base (nsp_fun, nsp_typ) = |
|
275 |
let |
|
43326
47cf4bc789aa
simplified Name.variant -- discontinued builtin fold_map;
wenzelm
parents:
42003
diff
changeset
|
276 |
val (base', nsp_fun') = |
47cf4bc789aa
simplified Name.variant -- discontinued builtin fold_map;
wenzelm
parents:
42003
diff
changeset
|
277 |
Name.variant (if upper then first_upper base else base) nsp_fun; |
39202 | 278 |
in (base', (nsp_fun', nsp_typ)) end; |
279 |
fun namify_typ base (nsp_fun, nsp_typ) = |
|
280 |
let |
|
43326
47cf4bc789aa
simplified Name.variant -- discontinued builtin fold_map;
wenzelm
parents:
42003
diff
changeset
|
281 |
val (base', nsp_typ') = Name.variant (first_upper base) nsp_typ; |
39202 | 282 |
in (base', (nsp_fun, nsp_typ')) end; |
283 |
fun namify_stmt (Code_Thingol.Fun (_, (_, SOME _))) = pair |
|
284 |
| namify_stmt (Code_Thingol.Fun _) = namify_fun false |
|
285 |
| namify_stmt (Code_Thingol.Datatype _) = namify_typ |
|
286 |
| namify_stmt (Code_Thingol.Datatypecons _) = namify_fun true |
|
287 |
| namify_stmt (Code_Thingol.Class _) = namify_typ |
|
288 |
| namify_stmt (Code_Thingol.Classrel _) = pair |
|
289 |
| namify_stmt (Code_Thingol.Classparam _) = namify_fun false |
|
290 |
| namify_stmt (Code_Thingol.Classinst _) = pair; |
|
291 |
fun select_stmt (Code_Thingol.Fun (_, (_, SOME _))) = false |
|
292 |
| select_stmt (Code_Thingol.Fun _) = true |
|
293 |
| select_stmt (Code_Thingol.Datatype _) = true |
|
294 |
| select_stmt (Code_Thingol.Datatypecons _) = false |
|
295 |
| select_stmt (Code_Thingol.Class _) = true |
|
296 |
| select_stmt (Code_Thingol.Classrel _) = false |
|
297 |
| select_stmt (Code_Thingol.Classparam _) = false |
|
298 |
| select_stmt (Code_Thingol.Classinst _) = true; |
|
299 |
in |
|
39206 | 300 |
Code_Namespace.flat_program labelled_name |
39202 | 301 |
{ module_alias = module_alias, module_prefix = module_prefix, |
302 |
reserved = reserved, empty_nsp = (reserved, reserved), namify_stmt = namify_stmt, |
|
303 |
modify_stmt = fn stmt => if select_stmt stmt then SOME stmt else NONE } |
|
304 |
end; |
|
305 |
||
48431
6efff142bb54
restrict unqualified imports from Haskell Prelude to a small set of fundamental operations
haftmann
parents:
48072
diff
changeset
|
306 |
val prelude_import_operators = [ |
6efff142bb54
restrict unqualified imports from Haskell Prelude to a small set of fundamental operations
haftmann
parents:
48072
diff
changeset
|
307 |
"==", "/=", "<", "<=", ">=", ">", "+", "-", "*", "/", "**", ">>=", ">>", "=<<", "&&", "||", "^", "^^", ".", "$", "$!", "++", "!!" |
6efff142bb54
restrict unqualified imports from Haskell Prelude to a small set of fundamental operations
haftmann
parents:
48072
diff
changeset
|
308 |
]; |
6efff142bb54
restrict unqualified imports from Haskell Prelude to a small set of fundamental operations
haftmann
parents:
48072
diff
changeset
|
309 |
|
6efff142bb54
restrict unqualified imports from Haskell Prelude to a small set of fundamental operations
haftmann
parents:
48072
diff
changeset
|
310 |
val prelude_import_unqualified = [ |
6efff142bb54
restrict unqualified imports from Haskell Prelude to a small set of fundamental operations
haftmann
parents:
48072
diff
changeset
|
311 |
"Eq", |
6efff142bb54
restrict unqualified imports from Haskell Prelude to a small set of fundamental operations
haftmann
parents:
48072
diff
changeset
|
312 |
"error", |
6efff142bb54
restrict unqualified imports from Haskell Prelude to a small set of fundamental operations
haftmann
parents:
48072
diff
changeset
|
313 |
"id", |
6efff142bb54
restrict unqualified imports from Haskell Prelude to a small set of fundamental operations
haftmann
parents:
48072
diff
changeset
|
314 |
"return", |
6efff142bb54
restrict unqualified imports from Haskell Prelude to a small set of fundamental operations
haftmann
parents:
48072
diff
changeset
|
315 |
"not", |
6efff142bb54
restrict unqualified imports from Haskell Prelude to a small set of fundamental operations
haftmann
parents:
48072
diff
changeset
|
316 |
"fst", "snd", |
6efff142bb54
restrict unqualified imports from Haskell Prelude to a small set of fundamental operations
haftmann
parents:
48072
diff
changeset
|
317 |
"map", "filter", "concat", "concatMap", "reverse", "zip", "null", "takeWhile", "dropWhile", "all", "any", |
6efff142bb54
restrict unqualified imports from Haskell Prelude to a small set of fundamental operations
haftmann
parents:
48072
diff
changeset
|
318 |
"Integer", "negate", "abs", "divMod", |
6efff142bb54
restrict unqualified imports from Haskell Prelude to a small set of fundamental operations
haftmann
parents:
48072
diff
changeset
|
319 |
"String" |
6efff142bb54
restrict unqualified imports from Haskell Prelude to a small set of fundamental operations
haftmann
parents:
48072
diff
changeset
|
320 |
]; |
6efff142bb54
restrict unqualified imports from Haskell Prelude to a small set of fundamental operations
haftmann
parents:
48072
diff
changeset
|
321 |
|
6efff142bb54
restrict unqualified imports from Haskell Prelude to a small set of fundamental operations
haftmann
parents:
48072
diff
changeset
|
322 |
val prelude_import_unqualified_constr = [ |
6efff142bb54
restrict unqualified imports from Haskell Prelude to a small set of fundamental operations
haftmann
parents:
48072
diff
changeset
|
323 |
("Bool", ["True", "False"]), |
6efff142bb54
restrict unqualified imports from Haskell Prelude to a small set of fundamental operations
haftmann
parents:
48072
diff
changeset
|
324 |
("Maybe", ["Nothing", "Just"]) |
6efff142bb54
restrict unqualified imports from Haskell Prelude to a small set of fundamental operations
haftmann
parents:
48072
diff
changeset
|
325 |
]; |
6efff142bb54
restrict unqualified imports from Haskell Prelude to a small set of fundamental operations
haftmann
parents:
48072
diff
changeset
|
326 |
|
39142 | 327 |
fun serialize_haskell module_prefix string_classes { labelled_name, reserved_syms, |
41343 | 328 |
includes, module_alias, class_syntax, tyco_syntax, const_syntax } program = |
28054 | 329 |
let |
39204 | 330 |
|
331 |
(* build program *) |
|
38926 | 332 |
val reserved = fold (insert (op =) o fst) includes reserved_syms; |
39204 | 333 |
val { deresolver, flat_program = haskell_program } = haskell_program_of_program |
334 |
labelled_name module_alias module_prefix (Name.make_context reserved) program; |
|
335 |
||
336 |
(* print statements *) |
|
28054 | 337 |
fun deriving_show tyco = |
338 |
let |
|
339 |
fun deriv _ "fun" = false |
|
34085 | 340 |
| deriv tycos tyco = not (tyco = Code_Thingol.fun_tyco) |
341 |
andalso (member (op =) tycos tyco |
|
342 |
orelse case try (Graph.get_node program) tyco |
|
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
343 |
of SOME (Code_Thingol.Datatype (_, (_, cs))) => forall (deriv' (tyco :: tycos)) |
28054 | 344 |
(maps snd cs) |
34085 | 345 |
| NONE => true) |
28054 | 346 |
and deriv' tycos (tyco `%% tys) = deriv tycos tyco |
347 |
andalso forall (deriv' tycos) tys |
|
348 |
| deriv' _ (ITyVar _) = true |
|
349 |
in deriv [] tyco end; |
|
47609 | 350 |
fun print_stmt deresolve = print_haskell_stmt |
39204 | 351 |
class_syntax tyco_syntax const_syntax (make_vars reserved) |
45009
99e1965f9c21
syntactic improvements and tuning names in the code generator due to Florian's code review
bulwahn
parents:
44940
diff
changeset
|
352 |
deresolve (if string_classes then deriving_show else K false); |
39204 | 353 |
|
354 |
(* print modules *) |
|
355 |
fun print_module_frame module_name ps = |
|
356 |
(module_name, Pretty.chunks2 ( |
|
39209 | 357 |
str ("module " ^ module_name ^ " where {") |
39204 | 358 |
:: ps |
359 |
@| str "}" |
|
360 |
)); |
|
48431
6efff142bb54
restrict unqualified imports from Haskell Prelude to a small set of fundamental operations
haftmann
parents:
48072
diff
changeset
|
361 |
fun print_qualified_import module_name = semicolon [str "import qualified", str module_name]; |
6efff142bb54
restrict unqualified imports from Haskell Prelude to a small set of fundamental operations
haftmann
parents:
48072
diff
changeset
|
362 |
val import_common_ps = |
6efff142bb54
restrict unqualified imports from Haskell Prelude to a small set of fundamental operations
haftmann
parents:
48072
diff
changeset
|
363 |
enclose "import Prelude (" ");" (commas (map str |
6efff142bb54
restrict unqualified imports from Haskell Prelude to a small set of fundamental operations
haftmann
parents:
48072
diff
changeset
|
364 |
(map (Library.enclose "(" ")") prelude_import_operators @ prelude_import_unqualified) |
6efff142bb54
restrict unqualified imports from Haskell Prelude to a small set of fundamental operations
haftmann
parents:
48072
diff
changeset
|
365 |
@ map (fn (tyco, constrs) => (enclose (tyco ^ "(") ")" o commas o map str) constrs) prelude_import_unqualified_constr)) |
6efff142bb54
restrict unqualified imports from Haskell Prelude to a small set of fundamental operations
haftmann
parents:
48072
diff
changeset
|
366 |
:: print_qualified_import "Prelude" |
6efff142bb54
restrict unqualified imports from Haskell Prelude to a small set of fundamental operations
haftmann
parents:
48072
diff
changeset
|
367 |
:: map (print_qualified_import o fst) includes; |
39204 | 368 |
fun print_module module_name (gr, imports) = |
28054 | 369 |
let |
48431
6efff142bb54
restrict unqualified imports from Haskell Prelude to a small set of fundamental operations
haftmann
parents:
48072
diff
changeset
|
370 |
val deresolve = deresolver module_name; |
39204 | 371 |
fun print_import module_name = (semicolon o map str) ["import qualified", module_name]; |
48431
6efff142bb54
restrict unqualified imports from Haskell Prelude to a small set of fundamental operations
haftmann
parents:
48072
diff
changeset
|
372 |
val import_ps = import_common_ps @ map (print_qualified_import o fst) imports; |
6efff142bb54
restrict unqualified imports from Haskell Prelude to a small set of fundamental operations
haftmann
parents:
48072
diff
changeset
|
373 |
fun print_stmt' name = case Graph.get_node gr name |
39204 | 374 |
of (_, NONE) => NONE |
375 |
| (_, SOME stmt) => SOME (markup_stmt name (print_stmt deresolve (name, stmt))); |
|
48431
6efff142bb54
restrict unqualified imports from Haskell Prelude to a small set of fundamental operations
haftmann
parents:
48072
diff
changeset
|
376 |
val body_ps = map_filter print_stmt' ((flat o rev o Graph.strong_conn) gr); |
39204 | 377 |
in |
378 |
print_module_frame module_name |
|
379 |
((if null import_ps then [] else [Pretty.chunks import_ps]) @ body_ps) |
|
380 |
end; |
|
381 |
||
382 |
(*serialization*) |
|
383 |
fun write_module width (SOME destination) (module_name, content) = |
|
38915 | 384 |
let |
42003
6e45dc518ebb
replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents:
41952
diff
changeset
|
385 |
val _ = File.check_dir destination; |
39204 | 386 |
val filepath = (Path.append destination o Path.ext "hs" o Path.explode o implode |
387 |
o separate "/" o Long_Name.explode) module_name; |
|
41246
e1da70df68c1
allocate intermediate directories in module hierarchy
haftmann
parents:
40804
diff
changeset
|
388 |
val _ = Isabelle_System.mkdirs (Path.dir filepath); |
39209 | 389 |
in |
390 |
(File.write filepath o format [] width o Pretty.chunks2) |
|
44926
de3ed037c9a5
create central list for language extensions used by the haskell code generator
noschinl
parents:
44852
diff
changeset
|
391 |
[str language_pragma, content] |
39209 | 392 |
end |
39056 | 393 |
| write_module width NONE (_, content) = writeln (format [] width content); |
28054 | 394 |
in |
38916 | 395 |
Code_Target.serialization |
38915 | 396 |
(fn width => fn destination => K () o map (write_module width destination)) |
40705 | 397 |
(fn present => fn width => rpair (try (deresolver "")) |
48568 | 398 |
o (map o apsnd) (format present width)) |
39204 | 399 |
(map (uncurry print_module_frame o apsnd single) includes |
400 |
@ map (fn module_name => print_module module_name (Graph.get_node haskell_program module_name)) |
|
401 |
((flat o rev o Graph.strong_conn) haskell_program)) |
|
28054 | 402 |
end; |
403 |
||
38966 | 404 |
val serializer : Code_Target.serializer = |
39206 | 405 |
Code_Target.parse_args (Scan.optional (Args.$$$ "root" -- Args.colon |-- Args.name) "" |
38966 | 406 |
-- Scan.optional (Args.$$$ "string_classes" >> K true) false |
407 |
>> (fn (module_prefix, string_classes) => |
|
408 |
serialize_haskell module_prefix string_classes)); |
|
409 |
||
28064 | 410 |
val literals = let |
411 |
fun char_haskell c = |
|
412 |
let |
|
413 |
val s = ML_Syntax.print_char c; |
|
414 |
in if s = "'" then "\\'" else s end; |
|
34944
970e1466028d
code literals: distinguish numeral classes by different entries
haftmann
parents:
34269
diff
changeset
|
415 |
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
|
416 |
else Library.enclose "(" ")" (signed_string_of_int k); |
28064 | 417 |
in Literals { |
34178
a78b8d5b91cb
take care for destructive print mode properly using dedicated pretty builders
haftmann
parents:
34085
diff
changeset
|
418 |
literal_char = Library.enclose "'" "'" o char_haskell, |
28064 | 419 |
literal_string = quote o translate_string char_haskell, |
34944
970e1466028d
code literals: distinguish numeral classes by different entries
haftmann
parents:
34269
diff
changeset
|
420 |
literal_numeral = numeral_haskell, |
970e1466028d
code literals: distinguish numeral classes by different entries
haftmann
parents:
34269
diff
changeset
|
421 |
literal_positive_numeral = numeral_haskell, |
37958
9728342bcd56
another refinement chapter in the neverending numeral story
haftmann
parents:
37881
diff
changeset
|
422 |
literal_alternative_numeral = numeral_haskell, |
34944
970e1466028d
code literals: distinguish numeral classes by different entries
haftmann
parents:
34269
diff
changeset
|
423 |
literal_naive_numeral = numeral_haskell, |
34178
a78b8d5b91cb
take care for destructive print mode properly using dedicated pretty builders
haftmann
parents:
34085
diff
changeset
|
424 |
literal_list = enum "," "[" "]", |
28064 | 425 |
infix_cons = (5, ":") |
426 |
} end; |
|
427 |
||
28054 | 428 |
|
429 |
(** optional monad syntax **) |
|
430 |
||
431 |
fun pretty_haskell_monad c_bind = |
|
432 |
let |
|
31874 | 433 |
fun dest_bind t1 t2 = case Code_Thingol.split_pat_abs t2 |
31889 | 434 |
of SOME ((pat, ty), t') => |
435 |
SOME ((SOME ((pat, ty), true), t1), t') |
|
28145 | 436 |
| NONE => NONE; |
48072
ace701efe203
prefer records with speaking labels over deeply nested tuples
haftmann
parents:
48003
diff
changeset
|
437 |
fun dest_monad c_bind_name (IConst { name = c, ... } `$ t1 `$ t2) = |
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
438 |
if c = c_bind_name then dest_bind t1 t2 |
28145 | 439 |
else NONE |
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
440 |
| dest_monad _ t = case Code_Thingol.split_let t |
28145 | 441 |
of SOME (((pat, ty), tbind), t') => |
31889 | 442 |
SOME ((SOME ((pat, ty), false), tbind), t') |
28145 | 443 |
| NONE => NONE; |
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28350
diff
changeset
|
444 |
fun implode_monad c_bind_name = Code_Thingol.unfoldr (dest_monad c_bind_name); |
33991 | 445 |
fun print_monad print_bind print_term (NONE, t) vars = |
446 |
(semicolon [print_term vars NOBR t], vars) |
|
447 |
| print_monad print_bind print_term (SOME ((bind, _), true), t) vars = vars |
|
448 |
|> print_bind NOBR bind |
|
449 |
|>> (fn p => semicolon [p, str "<-", print_term vars NOBR t]) |
|
450 |
| print_monad print_bind print_term (SOME ((bind, _), false), t) vars = vars |
|
451 |
|> print_bind NOBR bind |
|
37832 | 452 |
|>> (fn p => semicolon [str "let", str "{", p, str "=", print_term vars NOBR t, str "}"]); |
33991 | 453 |
fun pretty _ [c_bind'] print_term thm vars fxy [(t1, _), (t2, _)] = case dest_bind t1 t2 |
28145 | 454 |
of SOME (bind, t') => let |
31054 | 455 |
val (binds, t'') = implode_monad c_bind' t' |
33991 | 456 |
val (ps, vars') = fold_map (print_monad (gen_print_bind (K print_term) thm) print_term) |
457 |
(bind :: binds) vars; |
|
458 |
in |
|
37833
1381665d9550
more consistent spacing in generated monadic code
haftmann
parents:
37832
diff
changeset
|
459 |
(brackify fxy o single o enclose "do { " " }" o Pretty.breaks) |
33991 | 460 |
(ps @| print_term vars' NOBR t'') |
461 |
end |
|
28145 | 462 |
| 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
|
463 |
(print_term vars (INFX (1, L)) t1, str ">>=", print_term vars (INFX (1, X)) t2) |
31054 | 464 |
in (2, ([c_bind], pretty)) end; |
28054 | 465 |
|
28145 | 466 |
fun add_monad target' raw_c_bind thy = |
28054 | 467 |
let |
31156 | 468 |
val c_bind = Code.read_const thy raw_c_bind; |
28054 | 469 |
in if target = target' then |
470 |
thy |
|
38923 | 471 |
|> Code_Target.add_const_syntax target c_bind |
37881 | 472 |
(SOME (Code_Printer.complex_const_syntax (pretty_haskell_monad c_bind))) |
28054 | 473 |
else error "Only Haskell target allows for monad syntax" end; |
474 |
||
475 |
||
476 |
(** Isar setup **) |
|
477 |
||
478 |
val _ = |
|
46961
5c6955f487e5
outer syntax command definitions based on formal command_spec derived from theory header declarations;
wenzelm
parents:
45009
diff
changeset
|
479 |
Outer_Syntax.command @{command_spec "code_monad"} "define code syntax for monads" |
5c6955f487e5
outer syntax command definitions based on formal command_spec derived from theory header declarations;
wenzelm
parents:
45009
diff
changeset
|
480 |
(Parse.term_group -- Parse.name >> (fn (raw_bind, target) => |
5c6955f487e5
outer syntax command definitions based on formal command_spec derived from theory header declarations;
wenzelm
parents:
45009
diff
changeset
|
481 |
Toplevel.theory (add_monad target raw_bind))); |
28054 | 482 |
|
483 |
val setup = |
|
37821 | 484 |
Code_Target.add_target |
38966 | 485 |
(target, { serializer = serializer, literals = literals, |
41952
c7297638599b
cleanup of former settings GHC_PATH, EXEC_GHC, EXEC_OCAML, EXEC_SWIPL, EXEC_YAP -- discontinued implicit detection;
wenzelm
parents:
41940
diff
changeset
|
486 |
check = { env_var = "ISABELLE_GHC", make_destination = I, |
41940 | 487 |
make_command = fn module_name => |
44926
de3ed037c9a5
create central list for language extensions used by the haskell code generator
noschinl
parents:
44852
diff
changeset
|
488 |
"\"$ISABELLE_GHC\" " ^ language_params ^ " -odir build -hidir build -stubdir build -e \"\" " ^ |
41940 | 489 |
module_name ^ ".hs" } }) |
38923 | 490 |
#> Code_Target.add_tyco_syntax 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
|
491 |
brackify_infix (1, R) fxy ( |
33991 | 492 |
print_typ (INFX (1, X)) ty1, |
28054 | 493 |
str "->", |
33991 | 494 |
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
|
495 |
))) |
28054 | 496 |
#> fold (Code_Target.add_reserved target) [ |
497 |
"hiding", "deriving", "where", "case", "of", "infix", "infixl", "infixr", |
|
498 |
"import", "default", "forall", "let", "in", "class", "qualified", "data", |
|
499 |
"newtype", "instance", "if", "then", "else", "type", "as", "do", "module" |
|
500 |
] |
|
48431
6efff142bb54
restrict unqualified imports from Haskell Prelude to a small set of fundamental operations
haftmann
parents:
48072
diff
changeset
|
501 |
#> fold (Code_Target.add_reserved target) prelude_import_unqualified |
6efff142bb54
restrict unqualified imports from Haskell Prelude to a small set of fundamental operations
haftmann
parents:
48072
diff
changeset
|
502 |
#> fold (Code_Target.add_reserved target o fst) prelude_import_unqualified_constr |
6efff142bb54
restrict unqualified imports from Haskell Prelude to a small set of fundamental operations
haftmann
parents:
48072
diff
changeset
|
503 |
#> fold (fold (Code_Target.add_reserved target) o snd) prelude_import_unqualified_constr; |
28054 | 504 |
|
505 |
end; (*struct*) |