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