haftmann@24219: (* Title: Tools/code/code_target.ML haftmann@24219: ID: $Id$ haftmann@24219: Author: Florian Haftmann, TU Muenchen haftmann@24219: haftmann@24219: Serializer from intermediate language ("Thin-gol") haftmann@24219: to target languages (like SML or Haskell). haftmann@24219: *) haftmann@24219: haftmann@24219: signature CODE_TARGET = haftmann@24219: sig haftmann@24219: include BASIC_CODE_THINGOL; haftmann@24219: haftmann@24219: val add_syntax_class: string -> class haftmann@24423: -> (string * (string * string) list) option -> theory -> theory; haftmann@24219: val add_syntax_inst: string -> string * class -> bool -> theory -> theory; haftmann@24219: val add_syntax_tycoP: string -> string -> OuterParse.token list haftmann@24219: -> (theory -> theory) * OuterParse.token list; haftmann@24219: val add_syntax_constP: string -> string -> OuterParse.token list haftmann@24219: -> (theory -> theory) * OuterParse.token list; haftmann@24219: haftmann@24219: val add_undefined: string -> string -> string -> theory -> theory; haftmann@24219: val add_pretty_list: string -> string -> string -> theory -> theory; haftmann@24219: val add_pretty_list_string: string -> string -> string haftmann@24219: -> string -> string list -> theory -> theory; haftmann@24219: val add_pretty_char: string -> string -> string list -> theory -> theory haftmann@24423: val add_pretty_numeral: string -> bool -> string -> string -> string -> string haftmann@24219: -> string -> string -> theory -> theory; haftmann@24219: val add_pretty_ml_string: string -> string -> string list -> string haftmann@24219: -> string -> string -> theory -> theory; haftmann@24219: val add_pretty_imperative_monad_bind: string -> string -> theory -> theory; haftmann@24219: haftmann@24219: type serializer; haftmann@24219: val add_serializer: string * serializer -> theory -> theory; haftmann@24219: val get_serializer: theory -> string -> bool -> string option -> string option -> Args.T list haftmann@24621: -> (theory -> string -> string) -> (string -> bool) -> string list option haftmann@24621: -> CodeThingol.code -> unit; haftmann@24219: val assert_serializer: theory -> string -> string; haftmann@24219: haftmann@24219: val eval_verbose: bool ref; haftmann@24621: val eval_invoke: theory -> (theory -> string -> string) -> (string -> bool) haftmann@24621: -> (string * (unit -> 'a) option ref) -> CodeThingol.code haftmann@24621: -> CodeThingol.iterm * CodeThingol.itype -> string list -> 'a; haftmann@24219: val code_width: int ref; haftmann@24219: haftmann@24219: val setup: theory -> theory; haftmann@24219: end; haftmann@24219: haftmann@24219: structure CodeTarget : CODE_TARGET = haftmann@24219: struct haftmann@24219: haftmann@24219: open BasicCodeThingol; haftmann@24219: haftmann@24219: (** basics **) haftmann@24219: haftmann@24219: infixr 5 @@; haftmann@24219: infixr 5 @|; haftmann@24219: fun x @@ y = [x, y]; haftmann@24219: fun xs @| y = xs @ [y]; wenzelm@24634: val str = PrintMode.setmp [] Pretty.str; haftmann@24219: val concat = Pretty.block o Pretty.breaks; haftmann@24219: val brackets = Pretty.enclose "(" ")" o Pretty.breaks; haftmann@24219: fun semicolon ps = Pretty.block [concat ps, str ";"]; haftmann@24219: haftmann@24219: haftmann@24219: (** syntax **) haftmann@24219: haftmann@24219: datatype lrx = L | R | X; haftmann@24219: haftmann@24219: datatype fixity = haftmann@24219: BR haftmann@24219: | NOBR haftmann@24219: | INFX of (int * lrx); haftmann@24219: haftmann@24219: val APP = INFX (~1, L); haftmann@24219: haftmann@24219: fun eval_lrx L L = false haftmann@24219: | eval_lrx R R = false haftmann@24219: | eval_lrx _ _ = true; haftmann@24219: haftmann@24219: fun eval_fxy NOBR NOBR = false haftmann@24219: | eval_fxy BR NOBR = false haftmann@24219: | eval_fxy NOBR BR = false haftmann@24219: | eval_fxy (INFX (pr, lr)) (INFX (pr_ctxt, lr_ctxt)) = haftmann@24219: pr < pr_ctxt haftmann@24219: orelse pr = pr_ctxt haftmann@24219: andalso eval_lrx lr lr_ctxt haftmann@24219: orelse pr_ctxt = ~1 haftmann@24219: | eval_fxy _ (INFX _) = false haftmann@24219: | eval_fxy (INFX _) NOBR = false haftmann@24219: | eval_fxy _ _ = true; haftmann@24219: haftmann@24219: fun gen_brackify _ [p] = p haftmann@24219: | gen_brackify true (ps as _::_) = Pretty.enclose "(" ")" ps haftmann@24219: | gen_brackify false (ps as _::_) = Pretty.block ps; haftmann@24219: haftmann@24219: fun brackify fxy_ctxt ps = haftmann@24219: gen_brackify (eval_fxy BR fxy_ctxt) (Pretty.breaks ps); haftmann@24219: haftmann@24219: fun brackify_infix infx fxy_ctxt ps = haftmann@24219: gen_brackify (eval_fxy (INFX infx) fxy_ctxt) (Pretty.breaks ps); haftmann@24219: haftmann@24219: type class_syntax = string * (string -> string option); haftmann@24219: type typ_syntax = int * ((fixity -> itype -> Pretty.T) haftmann@24219: -> fixity -> itype list -> Pretty.T); haftmann@24219: type term_syntax = int * ((CodeName.var_ctxt -> fixity -> iterm -> Pretty.T) haftmann@24219: -> CodeName.var_ctxt -> fixity -> (iterm * itype) list -> Pretty.T); haftmann@24219: haftmann@24219: haftmann@24219: (* user-defined syntax *) haftmann@24219: haftmann@24219: datatype 'a mixfix = haftmann@24219: Arg of fixity haftmann@24219: | Pretty of Pretty.T; haftmann@24219: haftmann@24219: fun mk_mixfix prep_arg (fixity_this, mfx) = haftmann@24219: let haftmann@24219: fun is_arg (Arg _) = true haftmann@24219: | is_arg _ = false; haftmann@24219: val i = (length o filter is_arg) mfx; haftmann@24219: fun fillin _ [] [] = haftmann@24219: [] haftmann@24219: | fillin pr (Arg fxy :: mfx) (a :: args) = haftmann@24219: (pr fxy o prep_arg) a :: fillin pr mfx args haftmann@24219: | fillin pr (Pretty p :: mfx) args = haftmann@24219: p :: fillin pr mfx args haftmann@24219: | fillin _ [] _ = haftmann@24219: error ("Inconsistent mixfix: too many arguments") haftmann@24219: | fillin _ _ [] = haftmann@24219: error ("Inconsistent mixfix: too less arguments"); haftmann@24219: in haftmann@24219: (i, fn pr => fn fixity_ctxt => fn args => haftmann@24219: gen_brackify (eval_fxy fixity_this fixity_ctxt) (fillin pr mfx args)) haftmann@24219: end; haftmann@24219: haftmann@24219: fun parse_infix prep_arg (x, i) s = haftmann@24219: let haftmann@24219: val l = case x of L => INFX (i, L) | _ => INFX (i, X); haftmann@24219: val r = case x of R => INFX (i, R) | _ => INFX (i, X); haftmann@24219: in haftmann@24219: mk_mixfix prep_arg (INFX (i, x), [Arg l, (Pretty o Pretty.brk) 1, (Pretty o str) s, (Pretty o Pretty.brk) 1, Arg r]) haftmann@24219: end; haftmann@24219: haftmann@24219: fun parse_mixfix prep_arg s = haftmann@24219: let haftmann@24219: val sym_any = Scan.one Symbol.is_regular; haftmann@24219: val parse = Scan.optional ($$ "!" >> K true) false -- Scan.repeat ( haftmann@24219: ($$ "(" -- $$ "_" -- $$ ")" >> K (Arg NOBR)) haftmann@24219: || ($$ "_" >> K (Arg BR)) haftmann@24219: || ($$ "/" |-- Scan.repeat ($$ " ") >> (Pretty o Pretty.brk o length)) haftmann@24219: || (Scan.repeat1 haftmann@24219: ( $$ "'" |-- sym_any haftmann@24219: || Scan.unless ($$ "_" || $$ "/" || $$ "(" |-- $$ "_" |-- $$ ")") haftmann@24219: sym_any) >> (Pretty o str o implode))); haftmann@24219: in case Scan.finite Symbol.stopper parse (Symbol.explode s) haftmann@24219: of ((_, p as [_]), []) => mk_mixfix prep_arg (NOBR, p) haftmann@24219: | ((b, p as _ :: _ :: _), []) => mk_mixfix prep_arg (if b then NOBR else BR, p) haftmann@24219: | _ => Scan.!! (the_default ("malformed mixfix annotation: " ^ quote s) o snd) Scan.fail () haftmann@24219: end; haftmann@24219: haftmann@24219: fun parse_args f args = haftmann@24219: case Scan.read Args.stopper f args haftmann@24219: of SOME x => x haftmann@24219: | NONE => error "Bad serializer arguments"; haftmann@24219: haftmann@24219: haftmann@24219: (* generic serializer combinators *) haftmann@24219: haftmann@24219: fun gen_pr_app pr_app' pr_term const_syntax labelled_name is_cons haftmann@24219: lhs vars fxy (app as ((c, (_, tys)), ts)) = haftmann@24219: case const_syntax c haftmann@24219: of NONE => if lhs andalso not (is_cons c) then haftmann@24219: error ("non-constructor on left hand side of equation: " ^ labelled_name c) haftmann@24219: else brackify fxy (pr_app' lhs vars app) haftmann@24219: | SOME (i, pr) => haftmann@24219: let haftmann@24219: val k = if i < 0 then length tys else i; haftmann@24219: fun pr' fxy ts = pr (pr_term lhs) vars fxy (ts ~~ curry Library.take k tys); haftmann@24219: in if k = length ts haftmann@24219: then pr' fxy ts haftmann@24219: else if k < length ts haftmann@24219: then case chop k ts of (ts1, ts2) => haftmann@24219: brackify fxy (pr' APP ts1 :: map (pr_term lhs vars BR) ts2) haftmann@24219: else pr_term lhs vars fxy (CodeThingol.eta_expand app k) haftmann@24219: end; haftmann@24219: haftmann@24219: fun gen_pr_bind pr_bind' pr_term fxy ((v, pat), ty) vars = haftmann@24219: let haftmann@24219: val vs = case pat haftmann@24219: of SOME pat => CodeThingol.fold_varnames (insert (op =)) pat [] haftmann@24219: | NONE => []; haftmann@24219: val vars' = CodeName.intro_vars (the_list v) vars; haftmann@24219: val vars'' = CodeName.intro_vars vs vars'; haftmann@24219: val v' = Option.map (CodeName.lookup_var vars') v; haftmann@24284: val pat' = Option.map (pr_term true vars'' fxy) pat; haftmann@24219: in (pr_bind' ((v', pat'), ty), vars'') end; haftmann@24219: haftmann@24219: haftmann@24219: (* list, char, string, numeral and monad abstract syntax transformations *) haftmann@24219: haftmann@24219: fun implode_list c_nil c_cons t = haftmann@24219: let haftmann@24219: fun dest_cons (IConst (c, _) `$ t1 `$ t2) = haftmann@24219: if c = c_cons haftmann@24219: then SOME (t1, t2) haftmann@24219: else NONE haftmann@24219: | dest_cons _ = NONE; haftmann@24219: val (ts, t') = CodeThingol.unfoldr dest_cons t; haftmann@24219: in case t' haftmann@24219: of IConst (c, _) => if c = c_nil then SOME ts else NONE haftmann@24219: | _ => NONE haftmann@24219: end; haftmann@24219: haftmann@24219: fun decode_char c_nibbles (IConst (c1, _), IConst (c2, _)) = haftmann@24219: let haftmann@24219: fun idx c = find_index (curry (op =) c) c_nibbles; haftmann@24219: fun decode ~1 _ = NONE haftmann@24219: | decode _ ~1 = NONE haftmann@24219: | decode n m = SOME (chr (n * 16 + m)); haftmann@24219: in decode (idx c1) (idx c2) end haftmann@24219: | decode_char _ _ = NONE; haftmann@24219: haftmann@24219: fun implode_string c_char c_nibbles mk_char mk_string ts = haftmann@24219: let haftmann@24219: fun implode_char (IConst (c, _) `$ t1 `$ t2) = haftmann@24219: if c = c_char then decode_char c_nibbles (t1, t2) else NONE haftmann@24219: | implode_char _ = NONE; haftmann@24219: val ts' = map implode_char ts; haftmann@24219: in if forall is_some ts' haftmann@24219: then (SOME o str o mk_string o implode o map_filter I) ts' haftmann@24219: else NONE haftmann@24219: end; haftmann@24219: haftmann@24219: fun implode_numeral c_bit0 c_bit1 c_pls c_min c_bit = haftmann@24219: let haftmann@24219: fun dest_bit (IConst (c, _)) = if c = c_bit0 then SOME 0 haftmann@24219: else if c = c_bit1 then SOME 1 haftmann@24219: else NONE haftmann@24219: | dest_bit _ = NONE; wenzelm@24630: fun dest_numeral (IConst (c, _)) = if c = c_pls then SOME 0 wenzelm@24630: else if c = c_min then SOME ~1 haftmann@24219: else NONE haftmann@24219: | dest_numeral (IConst (c, _) `$ t1 `$ t2) = haftmann@24219: if c = c_bit then case (dest_numeral t1, dest_bit t2) wenzelm@24630: of (SOME n, SOME b) => SOME (2 * n + b) haftmann@24219: | _ => NONE haftmann@24219: else NONE haftmann@24219: | dest_numeral _ = NONE; haftmann@24219: in dest_numeral end; haftmann@24219: haftmann@24219: fun implode_monad c_mbind c_kbind t = haftmann@24219: let haftmann@24219: fun dest_monad (IConst (c, _) `$ t1 `$ t2) = haftmann@24219: if c = c_mbind haftmann@24219: then case CodeThingol.split_abs t2 haftmann@24219: of SOME (((v, pat), ty), t') => SOME ((SOME (((SOME v, pat), ty), true), t1), t') haftmann@24219: | NONE => NONE haftmann@24219: else if c = c_kbind haftmann@24219: then SOME ((NONE, t1), t2) haftmann@24219: else NONE haftmann@24219: | dest_monad t = case CodeThingol.split_let t haftmann@24219: of SOME (((pat, ty), tbind), t') => SOME ((SOME (((NONE, SOME pat), ty), false), tbind), t') haftmann@24219: | NONE => NONE; haftmann@24219: in CodeThingol.unfoldr dest_monad t end; haftmann@24219: haftmann@24219: haftmann@24219: (** name auxiliary **) haftmann@24219: haftmann@24219: val first_upper = implode o nth_map 0 Symbol.to_ascii_upper o explode; haftmann@24219: val first_lower = implode o nth_map 0 Symbol.to_ascii_lower o explode; haftmann@24219: haftmann@24219: val dest_name = haftmann@24219: apfst NameSpace.implode o split_last o fst o split_last o NameSpace.explode; haftmann@24219: haftmann@24219: fun mk_modl_name_tab init_names prefix module_alias code = haftmann@24219: let haftmann@24219: fun nsp_map f = NameSpace.explode #> map f #> NameSpace.implode; haftmann@24219: fun mk_alias name = haftmann@24219: case module_alias name haftmann@24219: of SOME name' => name' haftmann@24219: | NONE => nsp_map (fn name => (the_single o fst) haftmann@24219: (Name.variants [name] init_names)) name; haftmann@24219: fun mk_prefix name = haftmann@24219: case prefix haftmann@24219: of SOME prefix => NameSpace.append prefix name haftmann@24219: | NONE => name; haftmann@24219: val tab = haftmann@24219: Symtab.empty haftmann@24219: |> Graph.fold ((fn name => Symtab.default (name, (mk_alias #> mk_prefix) name)) haftmann@24219: o fst o dest_name o fst) haftmann@24219: code haftmann@24219: in fn name => (the o Symtab.lookup tab) name end; haftmann@24219: haftmann@24219: haftmann@24219: haftmann@24219: (** SML/OCaml serializer **) haftmann@24219: haftmann@24219: datatype ml_def = haftmann@24381: MLFuns of (string * (typscheme * (iterm list * iterm) list)) list haftmann@24219: | MLDatas of (string * ((vname * sort) list * (string * itype list) list)) list haftmann@24219: | MLClass of string * ((class * string) list * (vname * (string * itype) list)) haftmann@24219: | MLClassinst of string * ((class * (string * (vname * sort) list)) haftmann@24219: * ((class * (string * (string * dict list list))) list haftmann@24591: * (string * const) list)); haftmann@24219: haftmann@24621: fun pr_sml allows_exception tyco_syntax const_syntax labelled_name init_syms deresolv is_cons ml_def = haftmann@24219: let haftmann@24219: val pr_label_classrel = translate_string (fn "." => "__" | c => c) o NameSpace.qualifier; haftmann@24219: val pr_label_classop = NameSpace.base o NameSpace.qualifier; haftmann@24219: fun pr_dicts fxy ds = haftmann@24219: let haftmann@24219: fun pr_dictvar (v, (_, 1)) = first_upper v ^ "_" haftmann@24219: | pr_dictvar (v, (i, _)) = first_upper v ^ string_of_int (i+1) ^ "_"; haftmann@24219: fun pr_proj [] p = haftmann@24219: p haftmann@24219: | pr_proj [p'] p = haftmann@24219: brackets [p', p] haftmann@24219: | pr_proj (ps as _ :: _) p = haftmann@24219: brackets [Pretty.enum " o" "(" ")" ps, p]; haftmann@24219: fun pr_dictc fxy (DictConst (inst, dss)) = haftmann@24219: brackify fxy ((str o deresolv) inst :: map (pr_dicts BR) dss) haftmann@24219: | pr_dictc fxy (DictVar (classrels, v)) = haftmann@24219: pr_proj (map (str o deresolv) classrels) ((str o pr_dictvar) v) haftmann@24219: in case ds haftmann@24219: of [] => str "()" haftmann@24219: | [d] => pr_dictc fxy d haftmann@24219: | _ :: _ => (Pretty.list "(" ")" o map (pr_dictc NOBR)) ds haftmann@24219: end; haftmann@24219: fun pr_tyvars vs = haftmann@24219: vs haftmann@24219: |> map (fn (v, sort) => map_index (fn (i, _) => DictVar ([], (v, (i, length sort)))) sort) haftmann@24219: |> map (pr_dicts BR); haftmann@24219: fun pr_tycoexpr fxy (tyco, tys) = haftmann@24219: let haftmann@24219: val tyco' = (str o deresolv) tyco haftmann@24219: in case map (pr_typ BR) tys haftmann@24219: of [] => tyco' haftmann@24219: | [p] => Pretty.block [p, Pretty.brk 1, tyco'] haftmann@24219: | (ps as _::_) => Pretty.block [Pretty.list "(" ")" ps, Pretty.brk 1, tyco'] haftmann@24219: end haftmann@24219: and pr_typ fxy (tyco `%% tys) = haftmann@24219: (case tyco_syntax tyco haftmann@24219: of NONE => pr_tycoexpr fxy (tyco, tys) haftmann@24219: | SOME (i, pr) => haftmann@24219: if not (i = length tys) haftmann@24219: then error ("Number of argument mismatch in customary serialization: " haftmann@24219: ^ (string_of_int o length) tys ^ " given, " haftmann@24219: ^ string_of_int i ^ " expected") haftmann@24219: else pr pr_typ fxy tys) haftmann@24219: | pr_typ fxy (ITyVar v) = haftmann@24219: str ("'" ^ v); haftmann@24219: fun pr_term lhs vars fxy (IConst c) = haftmann@24219: pr_app lhs vars fxy (c, []) haftmann@24219: | pr_term lhs vars fxy (IVar v) = haftmann@24219: str (CodeName.lookup_var vars v) haftmann@24219: | pr_term lhs vars fxy (t as t1 `$ t2) = haftmann@24219: (case CodeThingol.unfold_const_app t haftmann@24219: of SOME c_ts => pr_app lhs vars fxy c_ts haftmann@24219: | NONE => haftmann@24219: brackify fxy [pr_term lhs vars NOBR t1, pr_term lhs vars BR t2]) haftmann@24219: | pr_term lhs vars fxy (t as _ `|-> _) = haftmann@24219: let haftmann@24219: val (binds, t') = CodeThingol.unfold_abs t; haftmann@24219: fun pr ((v, pat), ty) = haftmann@24219: pr_bind NOBR ((SOME v, pat), ty) haftmann@24219: #>> (fn p => concat [str "fn", p, str "=>"]); haftmann@24219: val (ps, vars') = fold_map pr binds vars; haftmann@24219: in brackets (ps @ [pr_term lhs vars' NOBR t']) end haftmann@24219: | pr_term lhs vars fxy (ICase (cases as (_, t0))) = (case CodeThingol.unfold_const_app t0 haftmann@24219: of SOME (c_ts as ((c, _), _)) => if is_none (const_syntax c) haftmann@24219: then pr_case vars fxy cases haftmann@24219: else pr_app lhs vars fxy c_ts haftmann@24219: | NONE => pr_case vars fxy cases) haftmann@24219: and pr_app' lhs vars (app as ((c, (iss, tys)), ts)) = haftmann@24219: if is_cons c then let haftmann@24219: val k = length tys haftmann@24219: in if k < 2 then haftmann@24219: (str o deresolv) c :: map (pr_term lhs vars BR) ts haftmann@24219: else if k = length ts then haftmann@24219: [(str o deresolv) c, Pretty.enum "," "(" ")" (map (pr_term lhs vars NOBR) ts)] haftmann@24219: else [pr_term lhs vars BR (CodeThingol.eta_expand app k)] end else haftmann@24219: (str o deresolv) c haftmann@24219: :: (map (pr_dicts BR) o filter_out null) iss @ map (pr_term lhs vars BR) ts haftmann@24219: and pr_app lhs vars = gen_pr_app pr_app' pr_term const_syntax labelled_name is_cons lhs vars haftmann@24219: and pr_bind' ((NONE, NONE), _) = str "_" haftmann@24219: | pr_bind' ((SOME v, NONE), _) = str v haftmann@24219: | pr_bind' ((NONE, SOME p), _) = p haftmann@24219: | pr_bind' ((SOME v, SOME p), _) = concat [str v, str "as", p] haftmann@24284: and pr_bind fxy = gen_pr_bind pr_bind' pr_term fxy haftmann@24219: and pr_case vars fxy (cases as ((_, [_]), _)) = haftmann@24219: let haftmann@24219: val (binds, t') = CodeThingol.unfold_let (ICase cases); haftmann@24219: fun pr ((pat, ty), t) vars = haftmann@24219: vars haftmann@24219: |> pr_bind NOBR ((NONE, SOME pat), ty) haftmann@24219: |>> (fn p => semicolon [str "val", p, str "=", pr_term false vars NOBR t]) haftmann@24219: val (ps, vars') = fold_map pr binds vars; haftmann@24219: in haftmann@24219: Pretty.chunks [ haftmann@24219: [str ("let"), Pretty.fbrk, Pretty.chunks ps] |> Pretty.block, haftmann@24219: [str ("in"), Pretty.fbrk, pr_term false vars' NOBR t'] |> Pretty.block, haftmann@24219: str ("end") haftmann@24219: ] haftmann@24219: end haftmann@24219: | pr_case vars fxy (((td, ty), b::bs), _) = haftmann@24219: let haftmann@24219: fun pr delim (pat, t) = haftmann@24219: let haftmann@24219: val (p, vars') = pr_bind NOBR ((NONE, SOME pat), ty) vars; haftmann@24219: in haftmann@24219: concat [str delim, p, str "=>", pr_term false vars' NOBR t] haftmann@24219: end; haftmann@24219: in haftmann@24219: (Pretty.enclose "(" ")" o single o brackify fxy) ( haftmann@24219: str "case" haftmann@24219: :: pr_term false vars NOBR td haftmann@24219: :: pr "of" b haftmann@24219: :: map (pr "|") bs haftmann@24219: ) haftmann@24219: end haftmann@24219: | pr_case vars fxy ((_, []), _) = str "raise Fail \"empty case\"" haftmann@24219: fun pr_def (MLFuns (funns as (funn :: funns'))) = haftmann@24219: let haftmann@24219: val definer = haftmann@24219: let haftmann@24219: fun mk [] [] = "val" haftmann@24219: | mk (_::_) _ = "fun" haftmann@24219: | mk [] vs = if (null o filter_out (null o snd)) vs then "val" else "fun"; haftmann@24381: fun chk (_, ((vs, _), (ts, _) :: _)) NONE = SOME (mk ts vs) haftmann@24381: | chk (_, ((vs, _), (ts, _) :: _)) (SOME defi) = haftmann@24219: if defi = mk ts vs then SOME defi haftmann@24219: else error ("Mixing simultaneous vals and funs not implemented: " haftmann@24219: ^ commas (map (labelled_name o fst) funns)); haftmann@24219: in the (fold chk funns NONE) end; haftmann@24381: fun pr_funn definer (name, ((raw_vs, ty), eqs as eq :: eqs')) = haftmann@24219: let haftmann@24219: val vs = filter_out (null o snd) raw_vs; haftmann@24219: val shift = if null eqs' then I else haftmann@24219: map (Pretty.block o single o Pretty.block o single); haftmann@24219: fun pr_eq definer (ts, t) = haftmann@24219: let haftmann@24219: val consts = map_filter haftmann@24219: (fn c => if (is_some o const_syntax) c haftmann@24219: then NONE else (SOME o NameSpace.base o deresolv) c) haftmann@24219: ((fold o CodeThingol.fold_constnames) (insert (op =)) (t :: ts) []); haftmann@24219: val vars = init_syms haftmann@24219: |> CodeName.intro_vars consts haftmann@24219: |> CodeName.intro_vars ((fold o CodeThingol.fold_unbound_varnames) haftmann@24219: (insert (op =)) ts []); haftmann@24219: in haftmann@24219: concat ( haftmann@24219: [str definer, (str o deresolv) name] haftmann@24219: @ (if null ts andalso null vs haftmann@24219: then [str ":", pr_typ NOBR ty] haftmann@24219: else haftmann@24219: pr_tyvars vs haftmann@24219: @ map (pr_term true vars BR) ts) haftmann@24219: @ [str "=", pr_term false vars NOBR t] haftmann@24219: ) haftmann@24219: end haftmann@24219: in haftmann@24219: (Pretty.block o Pretty.fbreaks o shift) ( haftmann@24219: pr_eq definer eq haftmann@24219: :: map (pr_eq "|") eqs' haftmann@24219: ) haftmann@24219: end; haftmann@24219: val (ps, p) = split_last (pr_funn definer funn :: map (pr_funn "and") funns'); haftmann@24219: in Pretty.chunks (ps @ [Pretty.block ([p, str ";"])]) end haftmann@24219: | pr_def (MLDatas (datas as (data :: datas'))) = haftmann@24219: let haftmann@24219: fun pr_co (co, []) = haftmann@24219: str (deresolv co) haftmann@24219: | pr_co (co, tys) = haftmann@24219: concat [ haftmann@24219: str (deresolv co), haftmann@24219: str "of", haftmann@24219: Pretty.enum " *" "" "" (map (pr_typ (INFX (2, X))) tys) haftmann@24219: ]; haftmann@24219: fun pr_data definer (tyco, (vs, [])) = haftmann@24219: concat ( haftmann@24219: str definer haftmann@24219: :: pr_tycoexpr NOBR (tyco, map (ITyVar o fst) vs) haftmann@24219: :: str "=" haftmann@24219: @@ str "EMPTY__" haftmann@24219: ) haftmann@24219: | pr_data definer (tyco, (vs, cos)) = haftmann@24219: concat ( haftmann@24219: str definer haftmann@24219: :: pr_tycoexpr NOBR (tyco, map (ITyVar o fst) vs) haftmann@24219: :: str "=" haftmann@24219: :: separate (str "|") (map pr_co cos) haftmann@24219: ); haftmann@24219: val (ps, p) = split_last (pr_data "datatype" data :: map (pr_data "and") datas'); haftmann@24219: in Pretty.chunks (ps @ [Pretty.block ([p, str ";"])]) end haftmann@24219: | pr_def (MLClass (class, (superclasses, (v, classops)))) = haftmann@24219: let haftmann@24219: val w = first_upper v ^ "_"; haftmann@24219: fun pr_superclass_field (class, classrel) = haftmann@24219: (concat o map str) [ haftmann@24219: pr_label_classrel classrel, ":", "'" ^ v, deresolv class haftmann@24219: ]; haftmann@24219: fun pr_classop_field (classop, ty) = haftmann@24219: concat [ haftmann@24219: (str o pr_label_classop) classop, str ":", pr_typ NOBR ty haftmann@24219: ]; haftmann@24219: fun pr_classop_proj (classop, _) = haftmann@24219: semicolon [ haftmann@24219: str "fun", haftmann@24219: (str o deresolv) classop, haftmann@24219: Pretty.enclose "(" ")" [str (w ^ ":'" ^ v ^ " " ^ deresolv class)], haftmann@24219: str "=", haftmann@24219: str ("#" ^ pr_label_classop classop), haftmann@24219: str w haftmann@24219: ]; haftmann@24219: fun pr_superclass_proj (_, classrel) = haftmann@24219: semicolon [ haftmann@24219: str "fun", haftmann@24219: (str o deresolv) classrel, haftmann@24219: Pretty.enclose "(" ")" [str (w ^ ":'" ^ v ^ " " ^ deresolv class)], haftmann@24219: str "=", haftmann@24219: str ("#" ^ pr_label_classrel classrel), haftmann@24219: str w haftmann@24219: ]; haftmann@24219: in haftmann@24219: Pretty.chunks ( haftmann@24219: concat [ haftmann@24219: str ("type '" ^ v), haftmann@24219: (str o deresolv) class, haftmann@24219: str "=", haftmann@24219: Pretty.enum "," "{" "};" ( haftmann@24219: map pr_superclass_field superclasses @ map pr_classop_field classops haftmann@24219: ) haftmann@24219: ] haftmann@24219: :: map pr_superclass_proj superclasses haftmann@24219: @ map pr_classop_proj classops haftmann@24219: ) haftmann@24219: end haftmann@24219: | pr_def (MLClassinst (inst, ((class, (tyco, arity)), (superarities, classop_defs)))) = haftmann@24219: let haftmann@24219: fun pr_superclass (_, (classrel, dss)) = haftmann@24219: concat [ haftmann@24219: (str o pr_label_classrel) classrel, haftmann@24219: str "=", haftmann@24219: pr_dicts NOBR [DictConst dss] haftmann@24219: ]; haftmann@24591: fun pr_classop (classop, c_inst) = haftmann@24591: concat [ haftmann@24591: (str o pr_label_classop) classop, haftmann@24591: str "=", haftmann@24591: pr_app false init_syms NOBR (c_inst, []) haftmann@24591: ]; haftmann@24219: in haftmann@24219: semicolon ([ haftmann@24219: str (if null arity then "val" else "fun"), haftmann@24219: (str o deresolv) inst ] @ haftmann@24219: pr_tyvars arity @ [ haftmann@24219: str "=", haftmann@24219: Pretty.enum "," "{" "}" (map pr_superclass superarities @ map pr_classop classop_defs), haftmann@24219: str ":", haftmann@24219: pr_tycoexpr NOBR (class, [tyco `%% map (ITyVar o fst) arity]) haftmann@24219: ]) haftmann@24219: end; haftmann@24219: in pr_def ml_def end; haftmann@24219: haftmann@24219: fun pr_sml_modl name content = haftmann@24219: Pretty.chunks ([ haftmann@24219: str ("structure " ^ name ^ " = "), haftmann@24219: str "struct", haftmann@24219: str "" haftmann@24219: ] @ content @ [ haftmann@24219: str "", haftmann@24219: str ("end; (*struct " ^ name ^ "*)") haftmann@24219: ]); haftmann@24219: haftmann@24621: fun pr_ocaml allows_exception tyco_syntax const_syntax labelled_name haftmann@24621: init_syms deresolv is_cons ml_def = haftmann@24219: let haftmann@24219: fun pr_dicts fxy ds = haftmann@24219: let haftmann@24219: fun pr_dictvar (v, (_, 1)) = "_" ^ first_upper v haftmann@24219: | pr_dictvar (v, (i, _)) = "_" ^ first_upper v ^ string_of_int (i+1); haftmann@24219: fun pr_proj ps p = haftmann@24219: fold_rev (fn p2 => fn p1 => Pretty.block [p1, str ".", str p2]) ps p haftmann@24219: fun pr_dictc fxy (DictConst (inst, dss)) = haftmann@24219: brackify fxy ((str o deresolv) inst :: map (pr_dicts BR) dss) haftmann@24219: | pr_dictc fxy (DictVar (classrels, v)) = haftmann@24219: pr_proj (map deresolv classrels) ((str o pr_dictvar) v) haftmann@24219: in case ds haftmann@24219: of [] => str "()" haftmann@24219: | [d] => pr_dictc fxy d haftmann@24219: | _ :: _ => (Pretty.list "(" ")" o map (pr_dictc NOBR)) ds haftmann@24219: end; haftmann@24219: fun pr_tyvars vs = haftmann@24219: vs haftmann@24219: |> map (fn (v, sort) => map_index (fn (i, _) => DictVar ([], (v, (i, length sort)))) sort) haftmann@24219: |> map (pr_dicts BR); haftmann@24219: fun pr_tycoexpr fxy (tyco, tys) = haftmann@24219: let haftmann@24219: val tyco' = (str o deresolv) tyco haftmann@24219: in case map (pr_typ BR) tys haftmann@24219: of [] => tyco' haftmann@24219: | [p] => Pretty.block [p, Pretty.brk 1, tyco'] haftmann@24219: | (ps as _::_) => Pretty.block [Pretty.list "(" ")" ps, Pretty.brk 1, tyco'] haftmann@24219: end haftmann@24219: and pr_typ fxy (tyco `%% tys) = haftmann@24219: (case tyco_syntax tyco haftmann@24219: of NONE => pr_tycoexpr fxy (tyco, tys) haftmann@24219: | SOME (i, pr) => haftmann@24219: if not (i = length tys) haftmann@24219: then error ("Number of argument mismatch in customary serialization: " haftmann@24219: ^ (string_of_int o length) tys ^ " given, " haftmann@24219: ^ string_of_int i ^ " expected") haftmann@24219: else pr pr_typ fxy tys) haftmann@24219: | pr_typ fxy (ITyVar v) = haftmann@24219: str ("'" ^ v); haftmann@24219: fun pr_term lhs vars fxy (IConst c) = haftmann@24219: pr_app lhs vars fxy (c, []) haftmann@24219: | pr_term lhs vars fxy (IVar v) = haftmann@24219: str (CodeName.lookup_var vars v) haftmann@24219: | pr_term lhs vars fxy (t as t1 `$ t2) = haftmann@24219: (case CodeThingol.unfold_const_app t haftmann@24219: of SOME c_ts => pr_app lhs vars fxy c_ts haftmann@24219: | NONE => haftmann@24219: brackify fxy [pr_term lhs vars NOBR t1, pr_term lhs vars BR t2]) haftmann@24219: | pr_term lhs vars fxy (t as _ `|-> _) = haftmann@24219: let haftmann@24219: val (binds, t') = CodeThingol.unfold_abs t; haftmann@24219: fun pr ((v, pat), ty) = pr_bind BR ((SOME v, pat), ty); haftmann@24219: val (ps, vars') = fold_map pr binds vars; haftmann@24219: in brackets (str "fun" :: ps @ str "->" @@ pr_term lhs vars' NOBR t') end haftmann@24219: | pr_term lhs vars fxy (ICase (cases as (_, t0))) = (case CodeThingol.unfold_const_app t0 haftmann@24219: of SOME (c_ts as ((c, _), _)) => if is_none (const_syntax c) haftmann@24219: then pr_case vars fxy cases haftmann@24219: else pr_app lhs vars fxy c_ts haftmann@24219: | NONE => pr_case vars fxy cases) haftmann@24219: and pr_app' lhs vars (app as ((c, (iss, tys)), ts)) = haftmann@24219: if is_cons c then haftmann@24219: if length tys = length ts haftmann@24219: then case ts haftmann@24219: of [] => [(str o deresolv) c] haftmann@24219: | [t] => [(str o deresolv) c, pr_term lhs vars BR t] haftmann@24219: | _ => [(str o deresolv) c, Pretty.enum "," "(" ")" (map (pr_term lhs vars NOBR) ts)] haftmann@24219: else [pr_term lhs vars BR (CodeThingol.eta_expand app (length tys))] haftmann@24219: else (str o deresolv) c haftmann@24219: :: ((map (pr_dicts BR) o filter_out null) iss @ map (pr_term lhs vars BR) ts) haftmann@24219: and pr_app lhs vars = gen_pr_app pr_app' pr_term const_syntax labelled_name is_cons lhs vars haftmann@24219: and pr_bind' ((NONE, NONE), _) = str "_" haftmann@24219: | pr_bind' ((SOME v, NONE), _) = str v haftmann@24219: | pr_bind' ((NONE, SOME p), _) = p haftmann@24219: | pr_bind' ((SOME v, SOME p), _) = brackets [p, str "as", str v] haftmann@24284: and pr_bind fxy = gen_pr_bind pr_bind' pr_term fxy haftmann@24219: and pr_case vars fxy (cases as ((_, [_]), _)) = haftmann@24219: let haftmann@24219: val (binds, t') = CodeThingol.unfold_let (ICase cases); haftmann@24219: fun pr ((pat, ty), t) vars = haftmann@24219: vars haftmann@24219: |> pr_bind NOBR ((NONE, SOME pat), ty) haftmann@24219: |>> (fn p => concat [str "let", p, str "=", pr_term false vars NOBR t, str "in"]) haftmann@24219: val (ps, vars') = fold_map pr binds vars; haftmann@24219: in Pretty.chunks (ps @| pr_term false vars' NOBR t') end haftmann@24219: | pr_case vars fxy (((td, ty), b::bs), _) = haftmann@24219: let haftmann@24219: fun pr delim (pat, t) = haftmann@24219: let haftmann@24219: val (p, vars') = pr_bind NOBR ((NONE, SOME pat), ty) vars; haftmann@24219: in concat [str delim, p, str "->", pr_term false vars' NOBR t] end; haftmann@24219: in haftmann@24219: (Pretty.enclose "(" ")" o single o brackify fxy) ( haftmann@24219: str "match" haftmann@24219: :: pr_term false vars NOBR td haftmann@24219: :: pr "with" b haftmann@24219: :: map (pr "|") bs haftmann@24219: ) haftmann@24219: end haftmann@24219: | pr_case vars fxy ((_, []), _) = str "failwith \"empty case\""; haftmann@24219: fun pr_def (MLFuns (funns as funn :: funns')) = haftmann@24219: let haftmann@24219: fun fish_parm _ (w as SOME _) = w haftmann@24219: | fish_parm (IVar v) NONE = SOME v haftmann@24219: | fish_parm _ NONE = NONE; haftmann@24219: fun fillup_parm _ (_, SOME v) = v haftmann@24219: | fillup_parm x (i, NONE) = x ^ string_of_int i; haftmann@24219: fun fish_parms vars eqs = haftmann@24219: let haftmann@24294: val fished1 = fold (map2 fish_parm) eqs (replicate (length (hd eqs)) NONE); haftmann@24294: val x = Name.variant (map_filter I fished1) "x"; haftmann@24294: val fished2 = map_index (fillup_parm x) fished1; haftmann@24294: val (fished3, _) = Name.variants fished2 Name.context; haftmann@24294: val vars' = CodeName.intro_vars fished3 vars; haftmann@24294: in map (CodeName.lookup_var vars') fished3 end; haftmann@24219: fun pr_eq (ts, t) = haftmann@24219: let haftmann@24219: val consts = map_filter haftmann@24219: (fn c => if (is_some o const_syntax) c haftmann@24219: then NONE else (SOME o NameSpace.base o deresolv) c) haftmann@24219: ((fold o CodeThingol.fold_constnames) (insert (op =)) (t :: ts) []); haftmann@24219: val vars = init_syms haftmann@24219: |> CodeName.intro_vars consts haftmann@24219: |> CodeName.intro_vars ((fold o CodeThingol.fold_unbound_varnames) haftmann@24219: (insert (op =)) ts []); haftmann@24219: in concat [ haftmann@24219: (Pretty.block o Pretty.commas) (map (pr_term true vars NOBR) ts), haftmann@24219: str "->", haftmann@24219: pr_term false vars NOBR t haftmann@24219: ] end; haftmann@24219: fun pr_eqs [(ts, t)] = haftmann@24219: let haftmann@24219: val consts = map_filter haftmann@24219: (fn c => if (is_some o const_syntax) c haftmann@24219: then NONE else (SOME o NameSpace.base o deresolv) c) haftmann@24219: ((fold o CodeThingol.fold_constnames) (insert (op =)) (t :: ts) []); haftmann@24219: val vars = init_syms haftmann@24219: |> CodeName.intro_vars consts haftmann@24219: |> CodeName.intro_vars ((fold o CodeThingol.fold_unbound_varnames) haftmann@24219: (insert (op =)) ts []); haftmann@24219: in haftmann@24219: concat ( haftmann@24219: map (pr_term true vars BR) ts haftmann@24219: @ str "=" haftmann@24219: @@ pr_term false vars NOBR t haftmann@24219: ) haftmann@24219: end haftmann@24219: | pr_eqs (eqs as (eq as ([_], _)) :: eqs') = haftmann@24219: Pretty.block ( haftmann@24219: str "=" haftmann@24219: :: Pretty.brk 1 haftmann@24219: :: str "function" haftmann@24219: :: Pretty.brk 1 haftmann@24219: :: pr_eq eq haftmann@24219: :: maps (append [Pretty.fbrk, str "|", Pretty.brk 1] o single o pr_eq) eqs' haftmann@24219: ) haftmann@24219: | pr_eqs (eqs as eq :: eqs') = haftmann@24219: let haftmann@24219: val consts = map_filter haftmann@24219: (fn c => if (is_some o const_syntax) c haftmann@24219: then NONE else (SOME o NameSpace.base o deresolv) c) haftmann@24219: ((fold o CodeThingol.fold_constnames) (insert (op =)) (map snd eqs) []); haftmann@24219: val vars = init_syms haftmann@24219: |> CodeName.intro_vars consts; haftmann@24219: val dummy_parms = (map str o fish_parms vars o map fst) eqs; haftmann@24219: in haftmann@24219: Pretty.block ( haftmann@24219: Pretty.breaks dummy_parms haftmann@24219: @ Pretty.brk 1 haftmann@24219: :: str "=" haftmann@24219: :: Pretty.brk 1 haftmann@24219: :: str "match" haftmann@24219: :: Pretty.brk 1 haftmann@24219: :: (Pretty.block o Pretty.commas) dummy_parms haftmann@24219: :: Pretty.brk 1 haftmann@24219: :: str "with" haftmann@24219: :: Pretty.brk 1 haftmann@24219: :: pr_eq eq haftmann@24219: :: maps (append [Pretty.fbrk, str "|", Pretty.brk 1] o single o pr_eq) eqs' haftmann@24219: ) haftmann@24219: end; haftmann@24381: fun pr_funn definer (name, ((vs, ty), eqs)) = haftmann@24219: concat ( haftmann@24219: str definer haftmann@24219: :: (str o deresolv) name haftmann@24219: :: pr_tyvars (filter_out (null o snd) vs) haftmann@24219: @| pr_eqs eqs haftmann@24219: ); haftmann@24219: val (ps, p) = split_last (pr_funn "let rec" funn :: map (pr_funn "and") funns'); haftmann@24219: in Pretty.chunks (ps @ [Pretty.block ([p, str ";;"])]) end haftmann@24219: | pr_def (MLDatas (datas as (data :: datas'))) = haftmann@24219: let haftmann@24219: fun pr_co (co, []) = haftmann@24219: str (deresolv co) haftmann@24219: | pr_co (co, tys) = haftmann@24219: concat [ haftmann@24219: str (deresolv co), haftmann@24219: str "of", haftmann@24219: Pretty.enum " *" "" "" (map (pr_typ (INFX (2, X))) tys) haftmann@24219: ]; haftmann@24219: fun pr_data definer (tyco, (vs, [])) = haftmann@24219: concat ( haftmann@24219: str definer haftmann@24219: :: pr_tycoexpr NOBR (tyco, map (ITyVar o fst) vs) haftmann@24219: :: str "=" haftmann@24219: @@ str "EMPTY_" haftmann@24219: ) haftmann@24219: | pr_data definer (tyco, (vs, cos)) = haftmann@24219: concat ( haftmann@24219: str definer haftmann@24219: :: pr_tycoexpr NOBR (tyco, map (ITyVar o fst) vs) haftmann@24219: :: str "=" haftmann@24219: :: separate (str "|") (map pr_co cos) haftmann@24219: ); haftmann@24219: val (ps, p) = split_last (pr_data "type" data :: map (pr_data "and") datas'); haftmann@24219: in Pretty.chunks (ps @ [Pretty.block ([p, str ";;"])]) end haftmann@24219: | pr_def (MLClass (class, (superclasses, (v, classops)))) = haftmann@24219: let haftmann@24219: val w = "_" ^ first_upper v; haftmann@24219: fun pr_superclass_field (class, classrel) = haftmann@24219: (concat o map str) [ haftmann@24219: deresolv classrel, ":", "'" ^ v, deresolv class haftmann@24219: ]; haftmann@24219: fun pr_classop_field (classop, ty) = haftmann@24219: concat [ haftmann@24219: (str o deresolv) classop, str ":", pr_typ NOBR ty haftmann@24219: ]; haftmann@24219: fun pr_classop_proj (classop, _) = haftmann@24219: concat [ haftmann@24219: str "let", haftmann@24219: (str o deresolv) classop, haftmann@24219: str w, haftmann@24219: str "=", haftmann@24219: str (w ^ "." ^ deresolv classop ^ ";;") haftmann@24219: ]; haftmann@24219: in Pretty.chunks ( haftmann@24219: concat [ haftmann@24219: str ("type '" ^ v), haftmann@24219: (str o deresolv) class, haftmann@24219: str "=", haftmann@24219: Pretty.enum ";" "{" "};;" ( haftmann@24219: map pr_superclass_field superclasses @ map pr_classop_field classops haftmann@24219: ) haftmann@24219: ] haftmann@24219: :: map pr_classop_proj classops haftmann@24219: ) end haftmann@24219: | pr_def (MLClassinst (inst, ((class, (tyco, arity)), (superarities, classop_defs)))) = haftmann@24219: let haftmann@24219: fun pr_superclass (_, (classrel, dss)) = haftmann@24219: concat [ haftmann@24219: (str o deresolv) classrel, haftmann@24219: str "=", haftmann@24219: pr_dicts NOBR [DictConst dss] haftmann@24219: ]; haftmann@24591: fun pr_classop_def (classop, c_inst) = haftmann@24591: concat [ haftmann@24591: (str o deresolv) classop, haftmann@24591: str "=", haftmann@24591: pr_app false init_syms NOBR (c_inst, []) haftmann@24591: ]; haftmann@24219: in haftmann@24219: concat ( haftmann@24219: str "let" haftmann@24219: :: (str o deresolv) inst haftmann@24219: :: pr_tyvars arity haftmann@24219: @ str "=" haftmann@24219: @@ (Pretty.enclose "(" ");;" o Pretty.breaks) [ haftmann@24219: Pretty.enum ";" "{" "}" (map pr_superclass superarities @ map pr_classop_def classop_defs), haftmann@24219: str ":", haftmann@24219: pr_tycoexpr NOBR (class, [tyco `%% map (ITyVar o fst) arity]) haftmann@24219: ] haftmann@24219: ) haftmann@24219: end; haftmann@24219: in pr_def ml_def end; haftmann@24219: haftmann@24219: fun pr_ocaml_modl name content = haftmann@24219: Pretty.chunks ([ haftmann@24219: str ("module " ^ name ^ " = "), haftmann@24219: str "struct", haftmann@24219: str "" haftmann@24219: ] @ content @ [ haftmann@24219: str "", haftmann@24219: str ("end;; (*struct " ^ name ^ "*)") haftmann@24219: ]); haftmann@24219: haftmann@24219: val code_width = ref 80; haftmann@24219: fun code_output p = Pretty.setmp_margin (!code_width) Pretty.output p ^ "\n"; haftmann@24219: haftmann@24219: fun seri_ml pr_def pr_modl module output labelled_name reserved_syms raw_module_alias module_prolog haftmann@24621: allows_exception (_ : string -> class_syntax option) tyco_syntax const_syntax code = haftmann@24219: let haftmann@24219: val module_alias = if is_some module then K module else raw_module_alias; haftmann@24219: val is_cons = CodeThingol.is_cons code; haftmann@24219: datatype node = haftmann@24219: Def of string * ml_def option haftmann@24219: | Module of string * ((Name.context * Name.context) * node Graph.T); haftmann@24219: val init_names = Name.make_context reserved_syms; haftmann@24219: val init_module = ((init_names, init_names), Graph.empty); haftmann@24219: fun map_node [] f = f haftmann@24219: | map_node (m::ms) f = haftmann@24219: Graph.default_node (m, Module (m, init_module)) haftmann@24219: #> Graph.map_node m (fn (Module (dmodlname, (nsp, nodes))) => Module (dmodlname, (nsp, map_node ms f nodes))); haftmann@24219: fun map_nsp_yield [] f (nsp, nodes) = haftmann@24219: let haftmann@24219: val (x, nsp') = f nsp haftmann@24219: in (x, (nsp', nodes)) end haftmann@24219: | map_nsp_yield (m::ms) f (nsp, nodes) = haftmann@24219: let haftmann@24219: val (x, nodes') = haftmann@24219: nodes haftmann@24219: |> Graph.default_node (m, Module (m, init_module)) haftmann@24219: |> Graph.map_node_yield m (fn Module (dmodlname, nsp_nodes) => haftmann@24219: let haftmann@24219: val (x, nsp_nodes') = map_nsp_yield ms f nsp_nodes haftmann@24219: in (x, Module (dmodlname, nsp_nodes')) end) haftmann@24219: in (x, (nsp, nodes')) end; haftmann@24219: val init_syms = CodeName.make_vars reserved_syms; haftmann@24219: val name_modl = mk_modl_name_tab init_names NONE module_alias code; haftmann@24219: fun name_def upper name nsp = haftmann@24219: let haftmann@24219: val (_, base) = dest_name name; haftmann@24219: val base' = if upper then first_upper base else base; haftmann@24219: val ([base''], nsp') = Name.variants [base'] nsp; haftmann@24219: in (base'', nsp') end; haftmann@24219: fun map_nsp_fun f (nsp_fun, nsp_typ) = haftmann@24219: let haftmann@24219: val (x, nsp_fun') = f nsp_fun haftmann@24219: in (x, (nsp_fun', nsp_typ)) end; haftmann@24219: fun map_nsp_typ f (nsp_fun, nsp_typ) = haftmann@24219: let haftmann@24219: val (x, nsp_typ') = f nsp_typ haftmann@24219: in (x, (nsp_fun, nsp_typ')) end; haftmann@24219: fun mk_funs defs = haftmann@24219: fold_map haftmann@24219: (fn (name, CodeThingol.Fun info) => haftmann@24591: map_nsp_fun (name_def false name) >> (fn base => (base, (name, (apsnd o map) fst info))) haftmann@24219: | (name, def) => error ("Function block containing illegal definition: " ^ labelled_name name) haftmann@24219: ) defs haftmann@24219: >> (split_list #> apsnd MLFuns); haftmann@24219: fun mk_datatype defs = haftmann@24219: fold_map haftmann@24219: (fn (name, CodeThingol.Datatype info) => haftmann@24219: map_nsp_typ (name_def false name) >> (fn base => (base, SOME (name, info))) haftmann@24219: | (name, CodeThingol.Datatypecons _) => haftmann@24219: map_nsp_fun (name_def true name) >> (fn base => (base, NONE)) haftmann@24219: | (name, def) => error ("Datatype block containing illegal definition: " ^ labelled_name name) haftmann@24219: ) defs haftmann@24219: >> (split_list #> apsnd (map_filter I haftmann@24219: #> (fn [] => error ("Datatype block without data definition: " ^ (commas o map (labelled_name o fst)) defs) haftmann@24219: | infos => MLDatas infos))); haftmann@24219: fun mk_class defs = haftmann@24219: fold_map haftmann@24219: (fn (name, CodeThingol.Class info) => haftmann@24219: map_nsp_typ (name_def false name) >> (fn base => (base, SOME (name, info))) haftmann@24219: | (name, CodeThingol.Classrel _) => haftmann@24219: map_nsp_fun (name_def false name) >> (fn base => (base, NONE)) haftmann@24219: | (name, CodeThingol.Classop _) => haftmann@24219: map_nsp_fun (name_def false name) >> (fn base => (base, NONE)) haftmann@24219: | (name, def) => error ("Class block containing illegal definition: " ^ labelled_name name) haftmann@24219: ) defs haftmann@24219: >> (split_list #> apsnd (map_filter I haftmann@24219: #> (fn [] => error ("Class block without class definition: " ^ (commas o map (labelled_name o fst)) defs) haftmann@24219: | [info] => MLClass info))); haftmann@24219: fun mk_inst [(name, CodeThingol.Classinst info)] = haftmann@24219: map_nsp_fun (name_def false name) haftmann@24591: >> (fn base => ([base], MLClassinst (name, (apsnd o apsnd o map) fst info))); haftmann@24219: fun add_group mk defs nsp_nodes = haftmann@24219: let haftmann@24219: val names as (name :: names') = map fst defs; haftmann@24219: val deps = haftmann@24219: [] haftmann@24219: |> fold (fold (insert (op =)) o Graph.imm_succs code) names haftmann@24219: |> subtract (op =) names; haftmann@24219: val (modls, _) = (split_list o map dest_name) names; haftmann@24219: val modl' = (the_single o distinct (op =) o map name_modl) modls haftmann@24219: handle Empty => haftmann@24219: error ("Illegal mutual dependencies: " ^ commas (map labelled_name names)); haftmann@24219: val modl_explode = NameSpace.explode modl'; haftmann@24219: fun add_dep name name'' = haftmann@24219: let haftmann@24219: val modl'' = (name_modl o fst o dest_name) name''; haftmann@24219: in if modl' = modl'' then haftmann@24219: map_node modl_explode haftmann@24219: (Graph.add_edge (name, name'')) haftmann@24219: else let haftmann@24219: val (common, (diff1::_, diff2::_)) = chop_prefix (op =) haftmann@24219: (modl_explode, NameSpace.explode modl''); haftmann@24219: in haftmann@24219: map_node common haftmann@24219: (fn gr => Graph.add_edge_acyclic (diff1, diff2) gr haftmann@24219: handle Graph.CYCLES _ => error ("Dependency " haftmann@24219: ^ quote name ^ " -> " ^ quote name'' haftmann@24219: ^ " would result in module dependency cycle")) haftmann@24219: end end; haftmann@24219: in haftmann@24219: nsp_nodes haftmann@24219: |> map_nsp_yield modl_explode (mk defs) haftmann@24219: |-> (fn (base' :: bases', def') => haftmann@24219: apsnd (map_node modl_explode (Graph.new_node (name, (Def (base', SOME def'))) haftmann@24219: #> fold2 (fn name' => fn base' => Graph.new_node (name', (Def (base', NONE)))) names' bases'))) haftmann@24219: |> apsnd (fold (fn name => fold (add_dep name) deps) names) haftmann@24219: |> apsnd (fold (map_node modl_explode o Graph.add_edge) (product names names)) haftmann@24219: end; haftmann@24219: fun group_defs [(_, CodeThingol.Bot)] = haftmann@24219: I haftmann@24219: | group_defs ((defs as (_, CodeThingol.Fun _)::_)) = haftmann@24219: add_group mk_funs defs haftmann@24219: | group_defs ((defs as (_, CodeThingol.Datatypecons _)::_)) = haftmann@24219: add_group mk_datatype defs haftmann@24219: | group_defs ((defs as (_, CodeThingol.Datatype _)::_)) = haftmann@24219: add_group mk_datatype defs haftmann@24219: | group_defs ((defs as (_, CodeThingol.Class _)::_)) = haftmann@24219: add_group mk_class defs haftmann@24219: | group_defs ((defs as (_, CodeThingol.Classrel _)::_)) = haftmann@24219: add_group mk_class defs haftmann@24219: | group_defs ((defs as (_, CodeThingol.Classop _)::_)) = haftmann@24219: add_group mk_class defs haftmann@24219: | group_defs ((defs as [(_, CodeThingol.Classinst _)])) = haftmann@24219: add_group mk_inst defs haftmann@24219: | group_defs defs = error ("Illegal mutual dependencies: " ^ haftmann@24219: (commas o map (labelled_name o fst)) defs) haftmann@24219: val (_, nodes) = haftmann@24219: init_module haftmann@24219: |> fold group_defs (map (AList.make (Graph.get_node code)) haftmann@24219: (rev (Graph.strong_conn code))) haftmann@24219: fun deresolver prefix name = haftmann@24219: let haftmann@24219: val modl = (fst o dest_name) name; haftmann@24219: val modl' = (NameSpace.explode o name_modl) modl; haftmann@24219: val (_, (_, remainder)) = chop_prefix (op =) (prefix, modl'); haftmann@24219: val defname' = haftmann@24219: nodes haftmann@24219: |> fold (fn m => fn g => case Graph.get_node g m haftmann@24219: of Module (_, (_, g)) => g) modl' haftmann@24219: |> (fn g => case Graph.get_node g name of Def (defname, _) => defname); haftmann@24219: in haftmann@24219: NameSpace.implode (remainder @ [defname']) haftmann@24219: end handle Graph.UNDEF _ => haftmann@24219: error ("Unknown definition name: " ^ labelled_name name); haftmann@24219: fun the_prolog modlname = case module_prolog modlname haftmann@24219: of NONE => [] haftmann@24219: | SOME p => [p, str ""]; haftmann@24219: fun pr_node prefix (Def (_, NONE)) = haftmann@24219: NONE haftmann@24219: | pr_node prefix (Def (_, SOME def)) = haftmann@24621: SOME (pr_def allows_exception tyco_syntax const_syntax labelled_name init_syms haftmann@24219: (deresolver prefix) is_cons def) haftmann@24219: | pr_node prefix (Module (dmodlname, (_, nodes))) = haftmann@24219: SOME (pr_modl dmodlname (the_prolog (NameSpace.implode (prefix @ [dmodlname])) haftmann@24219: @ separate (str "") ((map_filter (pr_node (prefix @ [dmodlname]) o Graph.get_node nodes) haftmann@24219: o rev o flat o Graph.strong_conn) nodes))); haftmann@24219: val p = Pretty.chunks (the_prolog "" @ separate (str "") ((map_filter haftmann@24219: (pr_node [] o Graph.get_node nodes) o rev o flat o Graph.strong_conn) nodes)) haftmann@24219: in output p end; haftmann@24219: haftmann@24219: val eval_verbose = ref false; haftmann@24219: haftmann@24219: fun isar_seri_sml module file = haftmann@24219: let haftmann@24219: val output = case file haftmann@24219: of NONE => use_text "generated code" Output.ml_output (!eval_verbose) o code_output wenzelm@24634: | SOME "-" => PrintMode.setmp [] writeln o code_output haftmann@24219: | SOME file => File.write (Path.explode file) o code_output; haftmann@24219: in haftmann@24219: parse_args (Scan.succeed ()) haftmann@24219: #> (fn () => seri_ml pr_sml pr_sml_modl module output) haftmann@24219: end; haftmann@24219: haftmann@24219: fun isar_seri_ocaml module file = haftmann@24219: let haftmann@24219: val output = case file haftmann@24219: of NONE => error "OCaml: no internal compilation" wenzelm@24634: | SOME "-" => PrintMode.setmp [] writeln o code_output haftmann@24219: | SOME file => File.write (Path.explode file) o code_output; haftmann@24219: fun output_file file = File.write (Path.explode file) o code_output; wenzelm@24634: val output_diag = PrintMode.setmp [] writeln o code_output; haftmann@24219: in haftmann@24219: parse_args (Scan.succeed ()) haftmann@24219: #> (fn () => seri_ml pr_ocaml pr_ocaml_modl module output) haftmann@24219: end; haftmann@24219: haftmann@24219: haftmann@24219: (** Haskell serializer **) haftmann@24219: haftmann@24219: local haftmann@24219: haftmann@24219: fun pr_bind' ((NONE, NONE), _) = str "_" haftmann@24219: | pr_bind' ((SOME v, NONE), _) = str v haftmann@24219: | pr_bind' ((NONE, SOME p), _) = p haftmann@24219: | pr_bind' ((SOME v, SOME p), _) = brackets [str v, str "@", p] haftmann@24219: haftmann@24219: val pr_bind_haskell = gen_pr_bind pr_bind'; haftmann@24219: haftmann@24219: in haftmann@24219: haftmann@24621: fun pr_haskell allows_exception class_syntax tyco_syntax const_syntax labelled_name haftmann@24621: init_syms deresolv_here deresolv is_cons deriving_show def = haftmann@24219: let haftmann@24219: fun class_name class = case class_syntax class haftmann@24219: of NONE => deresolv class haftmann@24219: | SOME (class, _) => class; haftmann@24219: fun classop_name class classop = case class_syntax class haftmann@24219: of NONE => deresolv_here classop haftmann@24219: | SOME (_, classop_syntax) => case classop_syntax classop haftmann@24219: of NONE => (snd o dest_name) classop haftmann@24219: | SOME classop => classop haftmann@24219: fun pr_typparms tyvars vs = haftmann@24219: case maps (fn (v, sort) => map (pair v) sort) vs haftmann@24219: of [] => str "" haftmann@24219: | xs => Pretty.block [ haftmann@24219: Pretty.enum "," "(" ")" ( haftmann@24219: map (fn (v, class) => str haftmann@24219: (class_name class ^ " " ^ CodeName.lookup_var tyvars v)) xs haftmann@24219: ), haftmann@24219: str " => " haftmann@24219: ]; haftmann@24219: fun pr_tycoexpr tyvars fxy (tyco, tys) = haftmann@24219: brackify fxy (str tyco :: map (pr_typ tyvars BR) tys) haftmann@24219: and pr_typ tyvars fxy (tycoexpr as tyco `%% tys) = haftmann@24219: (case tyco_syntax tyco haftmann@24219: of NONE => haftmann@24219: pr_tycoexpr tyvars fxy (deresolv tyco, tys) haftmann@24219: | SOME (i, pr) => haftmann@24219: if not (i = length tys) haftmann@24219: then error ("Number of argument mismatch in customary serialization: " haftmann@24219: ^ (string_of_int o length) tys ^ " given, " haftmann@24219: ^ string_of_int i ^ " expected") haftmann@24219: else pr (pr_typ tyvars) fxy tys) haftmann@24219: | pr_typ tyvars fxy (ITyVar v) = haftmann@24219: (str o CodeName.lookup_var tyvars) v; haftmann@24219: fun pr_typscheme_expr tyvars (vs, tycoexpr) = haftmann@24219: Pretty.block (pr_typparms tyvars vs @@ pr_tycoexpr tyvars NOBR tycoexpr); haftmann@24219: fun pr_typscheme tyvars (vs, ty) = haftmann@24219: Pretty.block (pr_typparms tyvars vs @@ pr_typ tyvars NOBR ty); haftmann@24219: fun pr_term lhs vars fxy (IConst c) = haftmann@24219: pr_app lhs vars fxy (c, []) haftmann@24219: | pr_term lhs vars fxy (t as (t1 `$ t2)) = haftmann@24219: (case CodeThingol.unfold_const_app t haftmann@24219: of SOME app => pr_app lhs vars fxy app haftmann@24219: | _ => haftmann@24219: brackify fxy [ haftmann@24219: pr_term lhs vars NOBR t1, haftmann@24219: pr_term lhs vars BR t2 haftmann@24219: ]) haftmann@24219: | pr_term lhs vars fxy (IVar v) = haftmann@24219: (str o CodeName.lookup_var vars) v haftmann@24219: | pr_term lhs vars fxy (t as _ `|-> _) = haftmann@24219: let haftmann@24219: val (binds, t') = CodeThingol.unfold_abs t; haftmann@24219: fun pr ((v, pat), ty) = pr_bind BR ((SOME v, pat), ty); haftmann@24219: val (ps, vars') = fold_map pr binds vars; haftmann@24219: in brackets (str "\\" :: ps @ str "->" @@ pr_term lhs vars' NOBR t') end haftmann@24219: | pr_term lhs vars fxy (ICase (cases as (_, t0))) = (case CodeThingol.unfold_const_app t0 haftmann@24219: of SOME (c_ts as ((c, _), _)) => if is_none (const_syntax c) haftmann@24219: then pr_case vars fxy cases haftmann@24219: else pr_app lhs vars fxy c_ts haftmann@24219: | NONE => pr_case vars fxy cases) haftmann@24219: and pr_app' lhs vars ((c, _), ts) = haftmann@24219: (str o deresolv) c :: map (pr_term lhs vars BR) ts haftmann@24219: and pr_app lhs vars = gen_pr_app pr_app' pr_term const_syntax labelled_name is_cons lhs vars haftmann@24284: and pr_bind fxy = pr_bind_haskell pr_term fxy haftmann@24219: and pr_case vars fxy (cases as ((_, [_]), _)) = haftmann@24219: let haftmann@24219: val (binds, t) = CodeThingol.unfold_let (ICase cases); haftmann@24219: fun pr ((pat, ty), t) vars = haftmann@24219: vars haftmann@24219: |> pr_bind BR ((NONE, SOME pat), ty) haftmann@24219: |>> (fn p => semicolon [p, str "=", pr_term false vars NOBR t]) haftmann@24219: val (ps, vars') = fold_map pr binds vars; haftmann@24219: in haftmann@24219: Pretty.block_enclose ( haftmann@24219: str "let {", haftmann@24219: concat [str "}", str "in", pr_term false vars' NOBR t] haftmann@24219: ) ps haftmann@24219: end haftmann@24219: | pr_case vars fxy (((td, ty), bs as _ :: _), _) = haftmann@24219: let haftmann@24219: fun pr (pat, t) = haftmann@24219: let haftmann@24219: val (p, vars') = pr_bind NOBR ((NONE, SOME pat), ty) vars; haftmann@24219: in semicolon [p, str "->", pr_term false vars' NOBR t] end; haftmann@24219: in haftmann@24219: Pretty.block_enclose ( haftmann@24219: concat [str "(case", pr_term false vars NOBR td, str "of", str "{"], haftmann@24219: str "})" haftmann@24219: ) (map pr bs) haftmann@24219: end haftmann@24219: | pr_case vars fxy ((_, []), _) = str "error \"empty case\""; haftmann@24381: fun pr_def (name, CodeThingol.Fun ((vs, ty), eqs)) = haftmann@24219: let haftmann@24219: val tyvars = CodeName.intro_vars (map fst vs) init_syms; haftmann@24591: fun pr_eq ((ts, t), _) = haftmann@24219: let haftmann@24219: val consts = map_filter haftmann@24219: (fn c => if (is_some o const_syntax) c haftmann@24219: then NONE else (SOME o NameSpace.base o deresolv) c) haftmann@24219: ((fold o CodeThingol.fold_constnames) (insert (op =)) (t :: ts) []); haftmann@24219: val vars = init_syms haftmann@24219: |> CodeName.intro_vars consts haftmann@24219: |> CodeName.intro_vars ((fold o CodeThingol.fold_unbound_varnames) haftmann@24219: (insert (op =)) ts []); haftmann@24219: in haftmann@24219: semicolon ( haftmann@24219: (str o deresolv_here) name haftmann@24219: :: map (pr_term true vars BR) ts haftmann@24219: @ str "=" haftmann@24219: @@ pr_term false vars NOBR t haftmann@24219: ) haftmann@24219: end; haftmann@24219: in haftmann@24219: Pretty.chunks ( haftmann@24219: Pretty.block [ haftmann@24219: (str o suffix " ::" o deresolv_here) name, haftmann@24219: Pretty.brk 1, haftmann@24219: pr_typscheme tyvars (vs, ty), haftmann@24219: str ";" haftmann@24219: ] haftmann@24219: :: map pr_eq eqs haftmann@24219: ) haftmann@24219: end haftmann@24219: | pr_def (name, CodeThingol.Datatype (vs, [])) = haftmann@24219: let haftmann@24219: val tyvars = CodeName.intro_vars (map fst vs) init_syms; haftmann@24219: in haftmann@24219: semicolon [ haftmann@24219: str "data", haftmann@24219: pr_typscheme_expr tyvars (vs, (deresolv_here name, map (ITyVar o fst) vs)) haftmann@24219: ] haftmann@24219: end haftmann@24219: | pr_def (name, CodeThingol.Datatype (vs, [(co, [ty])])) = haftmann@24219: let haftmann@24219: val tyvars = CodeName.intro_vars (map fst vs) init_syms; haftmann@24219: in haftmann@24219: semicolon ( haftmann@24219: str "newtype" haftmann@24219: :: pr_typscheme_expr tyvars (vs, (deresolv_here name, map (ITyVar o fst) vs)) haftmann@24219: :: str "=" haftmann@24219: :: (str o deresolv_here) co haftmann@24219: :: pr_typ tyvars BR ty haftmann@24219: :: (if deriving_show name then [str "deriving (Read, Show)"] else []) haftmann@24219: ) haftmann@24219: end haftmann@24219: | pr_def (name, CodeThingol.Datatype (vs, co :: cos)) = haftmann@24219: let haftmann@24219: val tyvars = CodeName.intro_vars (map fst vs) init_syms; haftmann@24219: fun pr_co (co, tys) = haftmann@24219: concat ( haftmann@24219: (str o deresolv_here) co haftmann@24219: :: map (pr_typ tyvars BR) tys haftmann@24219: ) haftmann@24219: in haftmann@24219: semicolon ( haftmann@24219: str "data" haftmann@24219: :: pr_typscheme_expr tyvars (vs, (deresolv_here name, map (ITyVar o fst) vs)) haftmann@24219: :: str "=" haftmann@24219: :: pr_co co haftmann@24219: :: map ((fn p => Pretty.block [str "| ", p]) o pr_co) cos haftmann@24219: @ (if deriving_show name then [str "deriving (Read, Show)"] else []) haftmann@24219: ) haftmann@24219: end haftmann@24219: | pr_def (name, CodeThingol.Class (superclasss, (v, classops))) = haftmann@24219: let haftmann@24219: val tyvars = CodeName.intro_vars [v] init_syms; haftmann@24219: fun pr_classop (classop, ty) = haftmann@24219: semicolon [ haftmann@24219: (str o classop_name name) classop, haftmann@24219: str "::", haftmann@24219: pr_typ tyvars NOBR ty haftmann@24219: ] haftmann@24219: in haftmann@24219: Pretty.block_enclose ( haftmann@24219: Pretty.block [ haftmann@24219: str "class ", haftmann@24219: pr_typparms tyvars [(v, map fst superclasss)], haftmann@24219: str (deresolv_here name ^ " " ^ CodeName.lookup_var tyvars v), haftmann@24219: str " where {" haftmann@24219: ], haftmann@24219: str "};" haftmann@24219: ) (map pr_classop classops) haftmann@24219: end haftmann@24219: | pr_def (_, CodeThingol.Classinst ((class, (tyco, vs)), (_, classop_defs))) = haftmann@24219: let haftmann@24219: val tyvars = CodeName.intro_vars (map fst vs) init_syms; haftmann@24591: fun pr_instdef ((classop, c_inst), _) = haftmann@24591: semicolon [ haftmann@24591: (str o classop_name class) classop, haftmann@24591: str "=", haftmann@24591: pr_app false init_syms NOBR (c_inst, []) haftmann@24591: ]; haftmann@24219: in haftmann@24219: Pretty.block_enclose ( haftmann@24219: Pretty.block [ haftmann@24219: str "instance ", haftmann@24219: pr_typparms tyvars vs, haftmann@24219: str (class_name class ^ " "), haftmann@24219: pr_typ tyvars BR (tyco `%% map (ITyVar o fst) vs), haftmann@24219: str " where {" haftmann@24219: ], haftmann@24219: str "};" haftmann@24219: ) (map pr_instdef classop_defs) haftmann@24219: end; haftmann@24219: in pr_def def end; haftmann@24219: haftmann@24219: fun pretty_haskell_monad c_mbind c_kbind = haftmann@24219: let haftmann@24219: fun pretty pr vars fxy [(t, _)] = haftmann@24219: let haftmann@24284: val pr_bind = pr_bind_haskell (K pr); haftmann@24219: fun pr_mbind (NONE, t) vars = haftmann@24219: (semicolon [pr vars NOBR t], vars) haftmann@24219: | pr_mbind (SOME (bind, true), t) vars = vars haftmann@24219: |> pr_bind NOBR bind haftmann@24219: |>> (fn p => semicolon [p, str "<-", pr vars NOBR t]) haftmann@24219: | pr_mbind (SOME (bind, false), t) vars = vars haftmann@24219: |> pr_bind NOBR bind haftmann@24219: |>> (fn p => semicolon [str "let", p, str "=", pr vars NOBR t]); haftmann@24219: val (binds, t) = implode_monad c_mbind c_kbind t; haftmann@24219: val (ps, vars') = fold_map pr_mbind binds vars; haftmann@24219: fun brack p = if eval_fxy BR fxy then Pretty.block [str "(", p, str ")"] else p; haftmann@24219: in (brack o Pretty.block_enclose (str "do {", str "}")) (ps @| pr vars' NOBR t) end; haftmann@24219: in (1, pretty) end; haftmann@24219: haftmann@24219: end; (*local*) haftmann@24219: haftmann@24219: fun seri_haskell module_prefix module destination string_classes labelled_name haftmann@24621: reserved_syms raw_module_alias module_prolog haftmann@24621: allows_exception class_syntax tyco_syntax const_syntax code = haftmann@24219: let haftmann@24219: val _ = Option.map File.check destination; haftmann@24219: val is_cons = CodeThingol.is_cons code; haftmann@24219: val module_alias = if is_some module then K module else raw_module_alias; haftmann@24219: val init_names = Name.make_context reserved_syms; haftmann@24219: val name_modl = mk_modl_name_tab init_names module_prefix module_alias code; haftmann@24219: fun add_def (name, (def, deps)) = haftmann@24219: let haftmann@24219: val (modl, base) = dest_name name; haftmann@24219: fun name_def base = Name.variants [base] #>> the_single; haftmann@24219: fun add_fun upper (nsp_fun, nsp_typ) = haftmann@24219: let haftmann@24219: val (base', nsp_fun') = name_def (if upper then first_upper base else base) nsp_fun haftmann@24219: in (base', (nsp_fun', nsp_typ)) end; haftmann@24219: fun add_typ (nsp_fun, nsp_typ) = haftmann@24219: let haftmann@24219: val (base', nsp_typ') = name_def (first_upper base) nsp_typ haftmann@24219: in (base', (nsp_fun, nsp_typ')) end; haftmann@24219: val add_name = haftmann@24219: case def haftmann@24219: of CodeThingol.Bot => pair base haftmann@24219: | CodeThingol.Fun _ => add_fun false haftmann@24219: | CodeThingol.Datatype _ => add_typ haftmann@24219: | CodeThingol.Datatypecons _ => add_fun true haftmann@24219: | CodeThingol.Class _ => add_typ haftmann@24219: | CodeThingol.Classrel _ => pair base haftmann@24219: | CodeThingol.Classop _ => add_fun false haftmann@24219: | CodeThingol.Classinst _ => pair base; haftmann@24219: val modlname' = name_modl modl; haftmann@24219: fun add_def base' = haftmann@24219: case def haftmann@24219: of CodeThingol.Bot => I haftmann@24219: | CodeThingol.Datatypecons _ => haftmann@24219: cons (name, ((NameSpace.append modlname' base', base'), NONE)) haftmann@24219: | CodeThingol.Classrel _ => I haftmann@24219: | CodeThingol.Classop _ => haftmann@24219: cons (name, ((NameSpace.append modlname' base', base'), NONE)) haftmann@24219: | _ => cons (name, ((NameSpace.append modlname' base', base'), SOME def)); haftmann@24219: in haftmann@24219: Symtab.map_default (modlname', ([], ([], (init_names, init_names)))) haftmann@24219: (apfst (fold (insert (op = : string * string -> bool)) deps)) haftmann@24219: #> `(fn code => add_name ((snd o snd o the o Symtab.lookup code) modlname')) haftmann@24219: #-> (fn (base', names) => haftmann@24219: (Symtab.map_entry modlname' o apsnd) (fn (defs, _) => haftmann@24219: (add_def base' defs, names))) haftmann@24219: end; haftmann@24219: val code' = haftmann@24219: fold add_def (AList.make (fn name => (Graph.get_node code name, Graph.imm_succs code name)) haftmann@24219: (Graph.strong_conn code |> flat)) Symtab.empty; haftmann@24219: val init_syms = CodeName.make_vars reserved_syms; haftmann@24219: fun deresolv name = haftmann@24219: (fst o fst o the o AList.lookup (op =) ((fst o snd o the haftmann@24219: o Symtab.lookup code') ((name_modl o fst o dest_name) name))) name haftmann@24219: handle Option => error ("Unknown definition name: " ^ labelled_name name); haftmann@24219: fun deresolv_here name = haftmann@24219: (snd o fst o the o AList.lookup (op =) ((fst o snd o the haftmann@24219: o Symtab.lookup code') ((name_modl o fst o dest_name) name))) name haftmann@24219: handle Option => error ("Unknown definition name: " ^ labelled_name name); haftmann@24219: fun deriving_show tyco = haftmann@24219: let haftmann@24219: fun deriv _ "fun" = false haftmann@24219: | deriv tycos tyco = member (op =) tycos tyco orelse haftmann@24219: case the_default CodeThingol.Bot (try (Graph.get_node code) tyco) haftmann@24219: of CodeThingol.Bot => true haftmann@24219: | CodeThingol.Datatype (_, cs) => forall (deriv' (tyco :: tycos)) haftmann@24219: (maps snd cs) haftmann@24219: and deriv' tycos (tyco `%% tys) = deriv tycos tyco haftmann@24219: andalso forall (deriv' tycos) tys haftmann@24219: | deriv' _ (ITyVar _) = true haftmann@24219: in deriv [] tyco end; haftmann@24621: fun seri_def qualified = pr_haskell allows_exception class_syntax tyco_syntax haftmann@24621: const_syntax labelled_name init_syms haftmann@24219: deresolv_here (if qualified then deresolv else deresolv_here) is_cons haftmann@24219: (if string_classes then deriving_show else K false); haftmann@24219: fun write_module (SOME destination) modlname = haftmann@24219: let haftmann@24219: val filename = case modlname haftmann@24219: of "" => Path.explode "Main.hs" haftmann@24219: | _ => (Path.ext "hs" o Path.explode o implode o separate "/" o NameSpace.explode) modlname; haftmann@24219: val pathname = Path.append destination filename; haftmann@24219: val _ = File.mkdir (Path.dir pathname); haftmann@24219: in File.write pathname end wenzelm@24634: | write_module NONE _ = PrintMode.setmp [] writeln; haftmann@24219: fun seri_module (modlname', (imports, (defs, _))) = haftmann@24219: let haftmann@24219: val imports' = haftmann@24219: imports haftmann@24219: |> map (name_modl o fst o dest_name) haftmann@24219: |> distinct (op =) haftmann@24219: |> remove (op =) modlname'; haftmann@24219: val qualified = haftmann@24423: imports @ map fst defs haftmann@24219: |> map_filter (try deresolv) haftmann@24219: |> map NameSpace.base haftmann@24219: |> has_duplicates (op =); haftmann@24219: val mk_import = str o (if qualified haftmann@24219: then prefix "import qualified " haftmann@24219: else prefix "import ") o suffix ";"; haftmann@24219: in haftmann@24219: Pretty.chunks ( haftmann@24219: str ("module " ^ modlname' ^ " where {") haftmann@24219: :: str "" haftmann@24219: :: map mk_import imports' haftmann@24219: @ str "" haftmann@24219: :: separate (str "") ((case module_prolog modlname' haftmann@24219: of SOME prolog => [prolog] haftmann@24219: | NONE => []) haftmann@24219: @ map_filter haftmann@24219: (fn (name, (_, SOME def)) => SOME (seri_def qualified (name, def)) haftmann@24219: | (_, (_, NONE)) => NONE) defs) haftmann@24219: @ str "" haftmann@24219: @@ str "}" haftmann@24219: ) haftmann@24219: |> code_output haftmann@24219: |> write_module destination modlname' haftmann@24219: end; haftmann@24219: in Symtab.fold (fn modl => fn () => seri_module modl) code' () end; haftmann@24219: haftmann@24219: fun isar_seri_haskell module file = haftmann@24219: let haftmann@24219: val destination = case file haftmann@24219: of NONE => error ("Haskell: no internal compilation") haftmann@24219: | SOME "-" => NONE haftmann@24219: | SOME file => SOME (Path.explode file) haftmann@24219: in haftmann@24219: parse_args (Scan.option (Args.$$$ "root" -- Args.colon |-- Args.name) haftmann@24219: -- Scan.optional (Args.$$$ "string_classes" >> K true) false haftmann@24219: >> (fn (module_prefix, string_classes) => haftmann@24219: seri_haskell module_prefix module destination string_classes)) haftmann@24219: end; haftmann@24219: haftmann@24219: haftmann@24219: (** diagnosis serializer **) haftmann@24219: haftmann@24621: fun seri_diagnosis labelled_name _ _ _ _ _ _ _ code = haftmann@24219: let haftmann@24219: val init_names = CodeName.make_vars []; haftmann@24219: fun pr_fun "fun" = SOME (2, fn pr_typ => fn fxy => fn [ty1, ty2] => haftmann@24219: brackify_infix (1, R) fxy [ haftmann@24219: pr_typ (INFX (1, X)) ty1, haftmann@24219: str "->", haftmann@24219: pr_typ (INFX (1, R)) ty2 haftmann@24219: ]) haftmann@24219: | pr_fun _ = NONE haftmann@24621: val pr = pr_haskell (K true) (K NONE) pr_fun (K NONE) labelled_name init_names I I (K false) (K false); haftmann@24219: in haftmann@24219: [] haftmann@24219: |> Graph.fold (fn (name, (def, _)) => case try pr (name, def) of SOME p => cons p | NONE => I) code haftmann@24219: |> Pretty.chunks2 haftmann@24219: |> code_output wenzelm@24634: |> PrintMode.setmp [] writeln haftmann@24219: end; haftmann@24219: haftmann@24219: haftmann@24219: haftmann@24219: (** theory data **) haftmann@24219: haftmann@24219: datatype syntax_expr = SyntaxExpr of { haftmann@24219: class: (string * (string -> string option)) Symtab.table, haftmann@24219: inst: unit Symtab.table, haftmann@24219: tyco: typ_syntax Symtab.table, haftmann@24219: const: term_syntax Symtab.table haftmann@24219: }; haftmann@24219: haftmann@24219: fun mk_syntax_expr ((class, inst), (tyco, const)) = haftmann@24219: SyntaxExpr { class = class, inst = inst, tyco = tyco, const = const }; haftmann@24219: fun map_syntax_expr f (SyntaxExpr { class, inst, tyco, const }) = haftmann@24219: mk_syntax_expr (f ((class, inst), (tyco, const))); haftmann@24219: fun merge_syntax_expr (SyntaxExpr { class = class1, inst = inst1, tyco = tyco1, const = const1 }, haftmann@24219: SyntaxExpr { class = class2, inst = inst2, tyco = tyco2, const = const2 }) = haftmann@24219: mk_syntax_expr ( haftmann@24219: (Symtab.join (K snd) (class1, class2), haftmann@24219: Symtab.join (K snd) (inst1, inst2)), haftmann@24219: (Symtab.join (K snd) (tyco1, tyco2), haftmann@24219: Symtab.join (K snd) (const1, const2)) haftmann@24219: ); haftmann@24219: haftmann@24219: datatype syntax_modl = SyntaxModl of { haftmann@24219: alias: string Symtab.table, haftmann@24219: prolog: Pretty.T Symtab.table haftmann@24219: }; haftmann@24219: haftmann@24219: fun mk_syntax_modl (alias, prolog) = haftmann@24219: SyntaxModl { alias = alias, prolog = prolog }; haftmann@24219: fun map_syntax_modl f (SyntaxModl { alias, prolog }) = haftmann@24219: mk_syntax_modl (f (alias, prolog)); haftmann@24219: fun merge_syntax_modl (SyntaxModl { alias = alias1, prolog = prolog1 }, haftmann@24219: SyntaxModl { alias = alias2, prolog = prolog2 }) = haftmann@24219: mk_syntax_modl ( haftmann@24219: Symtab.join (K snd) (alias1, alias2), haftmann@24219: Symtab.join (K snd) (prolog1, prolog2) haftmann@24219: ); haftmann@24219: haftmann@24219: type serializer = haftmann@24219: string option haftmann@24219: -> string option haftmann@24219: -> Args.T list haftmann@24219: -> (string -> string) haftmann@24219: -> string list haftmann@24219: -> (string -> string option) haftmann@24219: -> (string -> Pretty.T option) haftmann@24621: -> (string -> bool) haftmann@24219: -> (string -> class_syntax option) haftmann@24219: -> (string -> typ_syntax option) haftmann@24219: -> (string -> term_syntax option) haftmann@24219: -> CodeThingol.code -> unit; haftmann@24219: haftmann@24219: datatype target = Target of { haftmann@24219: serial: serial, haftmann@24219: serializer: serializer, haftmann@24219: syntax_expr: syntax_expr, haftmann@24219: syntax_modl: syntax_modl, haftmann@24219: reserved: string list haftmann@24219: }; haftmann@24219: haftmann@24219: fun mk_target (serial, ((serializer, reserved), (syntax_expr, syntax_modl))) = haftmann@24219: Target { serial = serial, reserved = reserved, serializer = serializer, syntax_expr = syntax_expr, syntax_modl = syntax_modl }; haftmann@24219: fun map_target f ( Target { serial, serializer, reserved, syntax_expr, syntax_modl } ) = haftmann@24219: mk_target (f (serial, ((serializer, reserved), (syntax_expr, syntax_modl)))); haftmann@24219: fun merge_target target (Target { serial = serial1, serializer = serializer, reserved = reserved1, haftmann@24219: syntax_expr = syntax_expr1, syntax_modl = syntax_modl1 }, haftmann@24219: Target { serial = serial2, serializer = _, reserved = reserved2, haftmann@24219: syntax_expr = syntax_expr2, syntax_modl = syntax_modl2 }) = haftmann@24219: if serial1 = serial2 then haftmann@24219: mk_target (serial1, ((serializer, merge (op =) (reserved1, reserved2)), haftmann@24219: (merge_syntax_expr (syntax_expr1, syntax_expr2), haftmann@24219: merge_syntax_modl (syntax_modl1, syntax_modl2)) haftmann@24219: )) haftmann@24219: else haftmann@24219: error ("Incompatible serializers: " ^ quote target); haftmann@24219: haftmann@24219: structure CodeTargetData = TheoryDataFun haftmann@24219: ( haftmann@24219: type T = target Symtab.table; haftmann@24219: val empty = Symtab.empty; haftmann@24219: val copy = I; haftmann@24219: val extend = I; haftmann@24219: fun merge _ = Symtab.join merge_target; haftmann@24219: ); haftmann@24219: haftmann@24219: fun the_serializer (Target { serializer, ... }) = serializer; haftmann@24219: fun the_reserved (Target { reserved, ... }) = reserved; haftmann@24219: fun the_syntax_expr (Target { syntax_expr = SyntaxExpr x, ... }) = x; haftmann@24219: fun the_syntax_modl (Target { syntax_modl = SyntaxModl x, ... }) = x; haftmann@24219: haftmann@24219: fun assert_serializer thy target = haftmann@24219: case Symtab.lookup (CodeTargetData.get thy) target haftmann@24219: of SOME data => target haftmann@24219: | NONE => error ("Unknown code target language: " ^ quote target); haftmann@24219: haftmann@24219: fun add_serializer (target, seri) thy = haftmann@24219: let haftmann@24219: val _ = case Symtab.lookup (CodeTargetData.get thy) target haftmann@24219: of SOME _ => warning ("overwriting existing serializer " ^ quote target) haftmann@24219: | NONE => (); haftmann@24219: in haftmann@24219: thy haftmann@24219: |> (CodeTargetData.map oo Symtab.map_default) haftmann@24219: (target, mk_target (serial (), ((seri, []), haftmann@24219: (mk_syntax_expr ((Symtab.empty, Symtab.empty), (Symtab.empty, Symtab.empty)), haftmann@24219: mk_syntax_modl (Symtab.empty, Symtab.empty))))) haftmann@24219: (map_target (fn (serial, ((_, keywords), syntax)) => (serial, ((seri, keywords), syntax)))) haftmann@24219: end; haftmann@24219: haftmann@24219: fun map_seri_data target f thy = haftmann@24219: let haftmann@24219: val _ = assert_serializer thy target; haftmann@24219: in haftmann@24219: thy haftmann@24219: |> (CodeTargetData.map o Symtab.map_entry target o map_target) f haftmann@24219: end; haftmann@24219: haftmann@24219: val target_SML = "SML"; haftmann@24219: val target_OCaml = "OCaml"; haftmann@24219: val target_Haskell = "Haskell"; haftmann@24219: val target_diag = "diag"; haftmann@24219: haftmann@24621: fun get_serializer thy target permissive module file args haftmann@24621: labelled_name allows_exception = fn cs => haftmann@24219: let haftmann@24219: val data = case Symtab.lookup (CodeTargetData.get thy) target haftmann@24219: of SOME data => data haftmann@24219: | NONE => error ("Unknown code target language: " ^ quote target); haftmann@24219: val seri = the_serializer data; haftmann@24219: val reserved = the_reserved data; haftmann@24219: val { alias, prolog } = the_syntax_modl data; haftmann@24219: val { class, inst, tyco, const } = the_syntax_expr data; haftmann@24219: val project = if target = target_diag then I haftmann@24219: else CodeThingol.project_code permissive haftmann@24219: (Symtab.keys class @ Symtab.keys inst @ Symtab.keys tyco @ Symtab.keys const) cs; haftmann@24219: fun check_empty_funs code = case CodeThingol.empty_funs code haftmann@24219: of [] => code haftmann@24219: | names => error ("No defining equations for " ^ commas (map (labelled_name thy) names)); haftmann@24219: in haftmann@24219: project haftmann@24219: #> check_empty_funs haftmann@24219: #> seri module file args (labelled_name thy) reserved (Symtab.lookup alias) (Symtab.lookup prolog) haftmann@24621: allows_exception (Symtab.lookup class) (Symtab.lookup tyco) (Symtab.lookup const) haftmann@24219: end; haftmann@24219: haftmann@24621: fun eval_invoke thy labelled_name allows_exception (ref_name, reff) code (t, ty) args = haftmann@24219: let haftmann@24219: val val_name = "Isabelle_Eval.EVAL.EVAL"; haftmann@24219: val val_name' = "Isabelle_Eval.EVAL"; haftmann@24219: val val_name'_args = space_implode " " (val_name' :: map (enclose "(" ")") args); haftmann@24621: val seri = get_serializer thy "SML" false (SOME "Isabelle_Eval") NONE [] haftmann@24621: labelled_name allows_exception; haftmann@24219: fun eval code = ( haftmann@24219: reff := NONE; haftmann@24219: seri (SOME [val_name]) code; haftmann@24219: use_text "generated code for evaluation" Output.ml_output (!eval_verbose) haftmann@24591: ("val _ = (" ^ ref_name ^ " := SOME (fn () => " ^ val_name'_args ^ "))"); haftmann@24219: case !reff haftmann@24219: of NONE => error ("Could not retrieve value of ML reference " ^ quote ref_name haftmann@24219: ^ " (reference probably has been shadowed)") haftmann@24591: | SOME f => f () haftmann@24219: ); haftmann@24219: in haftmann@24219: code haftmann@24284: |> CodeThingol.add_eval_def (val_name, (t, ty)) haftmann@24219: |> eval haftmann@24219: end; haftmann@24219: haftmann@24219: haftmann@24219: haftmann@24219: (** optional pretty serialization **) haftmann@24219: haftmann@24219: local haftmann@24219: haftmann@24219: val pretty : (string * { haftmann@24219: pretty_char: string -> string, haftmann@24219: pretty_string: string -> string, wenzelm@24630: pretty_numeral: bool -> int -> string, haftmann@24219: pretty_list: Pretty.T list -> Pretty.T, haftmann@24219: infix_cons: int * string haftmann@24219: }) list = [ haftmann@24219: ("SML", { pretty_char = prefix "#" o quote o ML_Syntax.print_char, haftmann@24219: pretty_string = ML_Syntax.print_string, haftmann@24219: pretty_numeral = fn unbounded => fn k => wenzelm@24630: if unbounded then "(" ^ string_of_int k ^ " : int)" wenzelm@24630: else string_of_int k, haftmann@24219: pretty_list = Pretty.enum "," "[" "]", haftmann@24219: infix_cons = (7, "::")}), haftmann@24219: ("OCaml", { pretty_char = fn c => enclose "'" "'" haftmann@24219: (let val i = ord c haftmann@24219: in if i < 32 orelse i = 39 orelse i = 92 haftmann@24219: then prefix "\\" (string_of_int i) haftmann@24219: else c haftmann@24219: end), haftmann@24219: pretty_string = (fn _ => error "OCaml: no pretty strings"), wenzelm@24630: pretty_numeral = fn unbounded => fn k => if k >= 0 then haftmann@24219: if unbounded then wenzelm@24630: "(Big_int.big_int_of_int " ^ string_of_int k ^ ")" wenzelm@24630: else string_of_int k haftmann@24219: else haftmann@24219: if unbounded then haftmann@24219: "(Big_int.big_int_of_int " ^ (enclose "(" ")" o prefix "-" wenzelm@24630: o string_of_int o op ~) k ^ ")" wenzelm@24630: else (enclose "(" ")" o prefix "-" o string_of_int o op ~) k, haftmann@24219: pretty_list = Pretty.enum ";" "[" "]", haftmann@24219: infix_cons = (6, "::")}), haftmann@24219: ("Haskell", { pretty_char = fn c => enclose "'" "'" haftmann@24219: (let val i = ord c haftmann@24219: in if i < 32 orelse i = 39 orelse i = 92 haftmann@24219: then Library.prefix "\\" (string_of_int i) haftmann@24219: else c haftmann@24219: end), haftmann@24219: pretty_string = ML_Syntax.print_string, wenzelm@24630: pretty_numeral = fn unbounded => fn k => if k >= 0 then string_of_int k wenzelm@24630: else enclose "(" ")" (signed_string_of_int k), haftmann@24219: pretty_list = Pretty.enum "," "[" "]", haftmann@24219: infix_cons = (5, ":")}) haftmann@24219: ]; haftmann@24219: haftmann@24219: in haftmann@24219: haftmann@24219: fun pr_pretty target = case AList.lookup (op =) pretty target haftmann@24219: of SOME x => x haftmann@24219: | NONE => error ("Unknown code target language: " ^ quote target); haftmann@24219: haftmann@24219: fun default_list (target_fxy, target_cons) pr fxy t1 t2 = haftmann@24219: brackify_infix (target_fxy, R) fxy [ haftmann@24219: pr (INFX (target_fxy, X)) t1, haftmann@24219: str target_cons, haftmann@24219: pr (INFX (target_fxy, R)) t2 haftmann@24219: ]; haftmann@24219: haftmann@24219: fun pretty_list c_nil c_cons target = haftmann@24219: let haftmann@24219: val pretty_ops = pr_pretty target; haftmann@24219: val mk_list = #pretty_list pretty_ops; haftmann@24219: fun pretty pr vars fxy [(t1, _), (t2, _)] = haftmann@24219: case Option.map (cons t1) (implode_list c_nil c_cons t2) haftmann@24219: of SOME ts => mk_list (map (pr vars NOBR) ts) haftmann@24219: | NONE => default_list (#infix_cons pretty_ops) (pr vars) fxy t1 t2; haftmann@24219: in (2, pretty) end; haftmann@24219: haftmann@24219: fun pretty_list_string c_nil c_cons c_char c_nibbles target = haftmann@24219: let haftmann@24219: val pretty_ops = pr_pretty target; haftmann@24219: val mk_list = #pretty_list pretty_ops; haftmann@24219: val mk_char = #pretty_char pretty_ops; haftmann@24219: val mk_string = #pretty_string pretty_ops; haftmann@24219: fun pretty pr vars fxy [(t1, _), (t2, _)] = haftmann@24219: case Option.map (cons t1) (implode_list c_nil c_cons t2) haftmann@24219: of SOME ts => case implode_string c_char c_nibbles mk_char mk_string ts haftmann@24219: of SOME p => p haftmann@24219: | NONE => mk_list (map (pr vars NOBR) ts) haftmann@24219: | NONE => default_list (#infix_cons pretty_ops) (pr vars) fxy t1 t2; haftmann@24219: in (2, pretty) end; haftmann@24219: haftmann@24219: fun pretty_char c_char c_nibbles target = haftmann@24219: let haftmann@24219: val mk_char = #pretty_char (pr_pretty target); haftmann@24219: fun pretty _ _ _ [(t1, _), (t2, _)] = haftmann@24219: case decode_char c_nibbles (t1, t2) haftmann@24219: of SOME c => (str o mk_char) c haftmann@24219: | NONE => error "Illegal character expression"; haftmann@24219: in (2, pretty) end; haftmann@24219: haftmann@24219: fun pretty_numeral unbounded c_bit0 c_bit1 c_pls c_min c_bit target = haftmann@24219: let haftmann@24219: val mk_numeral = #pretty_numeral (pr_pretty target); haftmann@24219: fun pretty _ _ _ [(t, _)] = haftmann@24219: case implode_numeral c_bit0 c_bit1 c_pls c_min c_bit t haftmann@24219: of SOME k => (str o mk_numeral unbounded) k haftmann@24219: | NONE => error "Illegal numeral expression"; haftmann@24219: in (1, pretty) end; haftmann@24219: haftmann@24219: fun pretty_ml_string c_char c_nibbles c_nil c_cons target = haftmann@24219: let haftmann@24219: val pretty_ops = pr_pretty target; haftmann@24219: val mk_char = #pretty_char pretty_ops; haftmann@24219: val mk_string = #pretty_string pretty_ops; haftmann@24219: fun pretty pr vars fxy [(t, _)] = haftmann@24219: case implode_list c_nil c_cons t haftmann@24219: of SOME ts => (case implode_string c_char c_nibbles mk_char mk_string ts haftmann@24219: of SOME p => p haftmann@24219: | NONE => error "Illegal ml_string expression") haftmann@24219: | NONE => error "Illegal ml_string expression"; haftmann@24219: in (1, pretty) end; haftmann@24219: haftmann@24219: val pretty_imperative_monad_bind = haftmann@24219: let haftmann@24219: fun pretty (pr : CodeName.var_ctxt -> fixity -> iterm -> Pretty.T) haftmann@24219: vars fxy [(t1, _), ((v, ty) `|-> t2, _)] = haftmann@24219: pr vars fxy (ICase (((t1, ty), ([(IVar v, t2)])), IVar "")) haftmann@24219: | pretty pr vars fxy [(t1, _), (t2, ty2)] = haftmann@24219: let haftmann@24219: (*this code suffers from the lack of a proper concept for bindings*) haftmann@24219: val vs = CodeThingol.fold_varnames cons t2 []; haftmann@24219: val v = Name.variant vs "x"; haftmann@24219: val vars' = CodeName.intro_vars [v] vars; haftmann@24219: val var = IVar v; haftmann@24219: val ty = (hd o fst o CodeThingol.unfold_fun) ty2; haftmann@24219: in pr vars' fxy (ICase (((t1, ty), ([(var, t2 `$ var)])), IVar "")) end; haftmann@24219: in (2, pretty) end; haftmann@24219: haftmann@24219: end; (*local*) haftmann@24219: haftmann@24219: (** ML and Isar interface **) haftmann@24219: haftmann@24219: local haftmann@24219: haftmann@24219: fun map_syntax_exprs target = haftmann@24219: map_seri_data target o apsnd o apsnd o apfst o map_syntax_expr; haftmann@24219: fun map_syntax_modls target = haftmann@24219: map_seri_data target o apsnd o apsnd o apsnd o map_syntax_modl; haftmann@24219: fun map_reserveds target = haftmann@24219: map_seri_data target o apsnd o apfst o apsnd; haftmann@24219: haftmann@24219: fun gen_add_syntax_class prep_class prep_const target raw_class raw_syn thy = haftmann@24219: let haftmann@24219: val cls = prep_class thy raw_class; haftmann@24219: val class = CodeName.class thy cls; haftmann@24423: fun mk_classop c = case AxClass.class_of_param thy c haftmann@24423: of SOME class' => if cls = class' then CodeName.const thy c haftmann@24219: else error ("Not a class operation for class " ^ quote class ^ ": " ^ quote c) haftmann@24219: | NONE => error ("Not a class operation: " ^ quote c); haftmann@24219: fun mk_syntax_ops raw_ops = AList.lookup (op =) haftmann@24219: ((map o apfst) (mk_classop o prep_const thy) raw_ops); haftmann@24219: in case raw_syn haftmann@24219: of SOME (syntax, raw_ops) => haftmann@24219: thy haftmann@24219: |> (map_syntax_exprs target o apfst o apfst) haftmann@24219: (Symtab.update (class, (syntax, mk_syntax_ops raw_ops))) haftmann@24219: | NONE => haftmann@24219: thy haftmann@24219: |> (map_syntax_exprs target o apfst o apfst) haftmann@24219: (Symtab.delete_safe class) haftmann@24219: end; haftmann@24219: haftmann@24219: fun gen_add_syntax_inst prep_class prep_tyco target (raw_tyco, raw_class) add_del thy = haftmann@24219: let haftmann@24219: val inst = CodeName.instance thy (prep_class thy raw_class, prep_tyco thy raw_tyco); haftmann@24219: in if add_del then haftmann@24219: thy haftmann@24219: |> (map_syntax_exprs target o apfst o apsnd) haftmann@24219: (Symtab.update (inst, ())) haftmann@24219: else haftmann@24219: thy haftmann@24219: |> (map_syntax_exprs target o apfst o apsnd) haftmann@24219: (Symtab.delete_safe inst) haftmann@24219: end; haftmann@24219: haftmann@24219: fun gen_add_syntax_tyco prep_tyco target raw_tyco raw_syn thy = haftmann@24219: let haftmann@24219: val tyco = prep_tyco thy raw_tyco; haftmann@24219: val tyco' = if tyco = "fun" then "fun" else CodeName.tyco thy tyco; haftmann@24219: fun check_args (syntax as (n, _)) = if n <> Sign.arity_number thy tyco haftmann@24219: then error ("Number of arguments mismatch in syntax for type constructor " ^ quote tyco) haftmann@24219: else syntax haftmann@24219: in case raw_syn haftmann@24219: of SOME syntax => haftmann@24219: thy haftmann@24219: |> (map_syntax_exprs target o apsnd o apfst) haftmann@24219: (Symtab.update (tyco', check_args syntax)) haftmann@24219: | NONE => haftmann@24219: thy haftmann@24219: |> (map_syntax_exprs target o apsnd o apfst) haftmann@24219: (Symtab.delete_safe tyco') haftmann@24219: end; haftmann@24219: haftmann@24219: fun gen_add_syntax_const prep_const target raw_c raw_syn thy = haftmann@24219: let haftmann@24219: val c = prep_const thy raw_c; haftmann@24219: val c' = CodeName.const thy c; haftmann@24423: fun check_args (syntax as (n, _)) = if n > CodeUnit.no_args thy c haftmann@24423: then error ("Too many arguments in syntax for constant " ^ quote c) haftmann@24219: else syntax; haftmann@24219: in case raw_syn haftmann@24219: of SOME syntax => haftmann@24219: thy haftmann@24219: |> (map_syntax_exprs target o apsnd o apsnd) haftmann@24219: (Symtab.update (c', check_args syntax)) haftmann@24219: | NONE => haftmann@24219: thy haftmann@24219: |> (map_syntax_exprs target o apsnd o apsnd) haftmann@24219: (Symtab.delete_safe c') haftmann@24219: end; haftmann@24219: haftmann@24219: fun cert_class thy class = haftmann@24219: let haftmann@24219: val _ = AxClass.get_definition thy class; haftmann@24219: in class end; haftmann@24219: haftmann@24219: fun read_class thy raw_class = haftmann@24219: let haftmann@24219: val class = Sign.intern_class thy raw_class; haftmann@24219: val _ = AxClass.get_definition thy class; haftmann@24219: in class end; haftmann@24219: haftmann@24219: fun cert_tyco thy tyco = haftmann@24219: let haftmann@24219: val _ = if Sign.declared_tyname thy tyco then () haftmann@24219: else error ("No such type constructor: " ^ quote tyco); haftmann@24219: in tyco end; haftmann@24219: haftmann@24219: fun read_tyco thy raw_tyco = haftmann@24219: let haftmann@24219: val tyco = Sign.intern_type thy raw_tyco; haftmann@24219: val _ = if Sign.declared_tyname thy tyco then () haftmann@24219: else error ("No such type constructor: " ^ quote raw_tyco); haftmann@24219: in tyco end; haftmann@24219: haftmann@24219: fun no_bindings x = (Option.map o apsnd) haftmann@24219: (fn pretty => fn pr => fn vars => pretty (pr vars)) x; haftmann@24219: haftmann@24219: fun gen_add_haskell_monad prep_const c_run c_mbind c_kbind thy = haftmann@24219: let haftmann@24219: val c_run' = prep_const thy c_run; haftmann@24219: val c_mbind' = prep_const thy c_mbind; haftmann@24219: val c_mbind'' = CodeName.const thy c_mbind'; haftmann@24219: val c_kbind' = prep_const thy c_kbind; haftmann@24219: val c_kbind'' = CodeName.const thy c_kbind'; haftmann@24219: val pr = pretty_haskell_monad c_mbind'' c_kbind'' haftmann@24219: in haftmann@24219: thy haftmann@24219: |> gen_add_syntax_const (K I) target_Haskell c_run' (SOME pr) haftmann@24219: |> gen_add_syntax_const (K I) target_Haskell c_mbind' haftmann@24219: (no_bindings (SOME (parse_infix fst (L, 1) ">>="))) haftmann@24219: |> gen_add_syntax_const (K I) target_Haskell c_kbind' haftmann@24219: (no_bindings (SOME (parse_infix fst (L, 1) ">>"))) haftmann@24219: end; haftmann@24219: haftmann@24219: fun add_reserved target = haftmann@24219: let haftmann@24219: fun add sym syms = if member (op =) syms sym haftmann@24219: then error ("Reserved symbol " ^ quote sym ^ " already declared") haftmann@24219: else insert (op =) sym syms haftmann@24219: in map_reserveds target o add end; haftmann@24219: haftmann@24219: fun add_modl_alias target = haftmann@24219: map_syntax_modls target o apfst o Symtab.update o apsnd CodeName.check_modulename; haftmann@24219: haftmann@24219: fun add_modl_prolog target = haftmann@24219: map_syntax_modls target o apsnd o haftmann@24219: (fn (modl, NONE) => Symtab.delete modl | (modl, SOME prolog) => haftmann@24219: Symtab.update (modl, Pretty.str prolog)); haftmann@24219: haftmann@24219: fun zip_list (x::xs) f g = haftmann@24219: f haftmann@24219: #-> (fn y => haftmann@24219: fold_map (fn x => g |-- f >> pair x) xs haftmann@24219: #-> (fn xys => pair ((x, y) :: xys))); haftmann@24219: haftmann@24219: structure P = OuterParse haftmann@24219: and K = OuterKeyword haftmann@24219: haftmann@24219: fun parse_multi_syntax parse_thing parse_syntax = haftmann@24219: P.and_list1 parse_thing haftmann@24219: #-> (fn things => Scan.repeat1 (P.$$$ "(" |-- P.name -- haftmann@24219: (zip_list things parse_syntax (P.$$$ "and")) --| P.$$$ ")")); haftmann@24219: haftmann@24219: val (infixK, infixlK, infixrK) = ("infix", "infixl", "infixr"); haftmann@24219: haftmann@24219: fun parse_syntax prep_arg xs = haftmann@24219: Scan.option (( haftmann@24219: ((P.$$$ infixK >> K X) haftmann@24219: || (P.$$$ infixlK >> K L) haftmann@24219: || (P.$$$ infixrK >> K R)) haftmann@24219: -- P.nat >> parse_infix prep_arg haftmann@24219: || Scan.succeed (parse_mixfix prep_arg)) haftmann@24219: -- P.string haftmann@24219: >> (fn (parse, s) => parse s)) xs; haftmann@24219: haftmann@24219: val (code_classK, code_instanceK, code_typeK, code_constK, code_monadK, haftmann@24219: code_reservedK, code_modulenameK, code_moduleprologK) = haftmann@24219: ("code_class", "code_instance", "code_type", "code_const", "code_monad", haftmann@24219: "code_reserved", "code_modulename", "code_moduleprolog"); haftmann@24219: haftmann@24219: in haftmann@24219: haftmann@24219: val parse_syntax = parse_syntax; haftmann@24219: haftmann@24219: val add_syntax_class = gen_add_syntax_class cert_class (K I); haftmann@24219: val add_syntax_inst = gen_add_syntax_inst cert_class cert_tyco; haftmann@24219: val add_syntax_tyco = gen_add_syntax_tyco cert_tyco; haftmann@24219: val add_syntax_const = gen_add_syntax_const (K I); haftmann@24219: haftmann@24219: val add_syntax_class_cmd = gen_add_syntax_class read_class CodeUnit.read_const; haftmann@24219: val add_syntax_inst_cmd = gen_add_syntax_inst read_class read_tyco; haftmann@24219: val add_syntax_tyco_cmd = gen_add_syntax_tyco read_tyco; haftmann@24219: val add_syntax_const_cmd = gen_add_syntax_const CodeUnit.read_const; haftmann@24219: haftmann@24219: fun add_syntax_tycoP target tyco = parse_syntax I >> add_syntax_tyco_cmd target tyco; haftmann@24219: fun add_syntax_constP target c = parse_syntax fst >> (add_syntax_const_cmd target c o no_bindings); haftmann@24219: haftmann@24219: fun add_undefined target undef target_undefined thy = haftmann@24219: let haftmann@24219: fun pr _ _ _ _ = str target_undefined; haftmann@24219: in haftmann@24219: thy haftmann@24423: |> add_syntax_const target undef (SOME (~1, pr)) haftmann@24219: end; haftmann@24219: haftmann@24219: fun add_pretty_list target nill cons thy = haftmann@24219: let haftmann@24423: val nil' = CodeName.const thy nill; haftmann@24423: val cons' = CodeName.const thy cons; haftmann@24423: val pr = pretty_list nil' cons' target; haftmann@24219: in haftmann@24219: thy haftmann@24423: |> add_syntax_const target cons (SOME pr) haftmann@24219: end; haftmann@24219: haftmann@24219: fun add_pretty_list_string target nill cons charr nibbles thy = haftmann@24219: let haftmann@24423: val nil' = CodeName.const thy nill; haftmann@24423: val cons' = CodeName.const thy cons; haftmann@24423: val charr' = CodeName.const thy charr; haftmann@24423: val nibbles' = map (CodeName.const thy) nibbles; haftmann@24423: val pr = pretty_list_string nil' cons' charr' nibbles' target; haftmann@24219: in haftmann@24219: thy haftmann@24423: |> add_syntax_const target cons (SOME pr) haftmann@24219: end; haftmann@24219: haftmann@24219: fun add_pretty_char target charr nibbles thy = haftmann@24219: let haftmann@24423: val charr' = CodeName.const thy charr; haftmann@24423: val nibbles' = map (CodeName.const thy) nibbles; haftmann@24423: val pr = pretty_char charr' nibbles' target; haftmann@24219: in haftmann@24219: thy haftmann@24423: |> add_syntax_const target charr (SOME pr) haftmann@24219: end; haftmann@24219: haftmann@24219: fun add_pretty_numeral target unbounded number_of b0 b1 pls min bit thy = haftmann@24219: let haftmann@24423: val b0' = CodeName.const thy b0; haftmann@24423: val b1' = CodeName.const thy b1; haftmann@24423: val pls' = CodeName.const thy pls; haftmann@24423: val min' = CodeName.const thy min; haftmann@24423: val bit' = CodeName.const thy bit; haftmann@24423: val pr = pretty_numeral unbounded b0' b1' pls' min' bit' target; haftmann@24219: in haftmann@24219: thy haftmann@24423: |> add_syntax_const target number_of (SOME pr) haftmann@24219: end; haftmann@24219: haftmann@24219: fun add_pretty_ml_string target charr nibbles nill cons str thy = haftmann@24219: let haftmann@24423: val charr' = CodeName.const thy charr; haftmann@24423: val nibbles' = map (CodeName.const thy) nibbles; haftmann@24423: val nil' = CodeName.const thy nill; haftmann@24423: val cons' = CodeName.const thy cons; haftmann@24423: val pr = pretty_ml_string charr' nibbles' nil' cons' target; haftmann@24219: in haftmann@24219: thy haftmann@24423: |> add_syntax_const target str (SOME pr) haftmann@24219: end; haftmann@24219: haftmann@24219: fun add_pretty_imperative_monad_bind target bind thy = haftmann@24219: let haftmann@24219: val pr = pretty_imperative_monad_bind haftmann@24219: in haftmann@24219: thy haftmann@24423: |> add_syntax_const target bind (SOME pr) haftmann@24219: end; haftmann@24219: haftmann@24219: val add_haskell_monad = gen_add_haskell_monad CodeUnit.read_const; haftmann@24219: haftmann@24219: val code_classP = haftmann@24219: OuterSyntax.command code_classK "define code syntax for class" K.thy_decl ( haftmann@24219: parse_multi_syntax P.xname haftmann@24219: (Scan.option (P.string -- Scan.optional (P.$$$ "where" |-- Scan.repeat1 haftmann@24219: (P.term --| (P.$$$ "\\" || P.$$$ "==") -- P.string)) [])) haftmann@24219: >> (Toplevel.theory oo fold) (fn (target, syns) => haftmann@24219: fold (fn (raw_class, syn) => add_syntax_class_cmd target raw_class syn) syns) haftmann@24219: ); haftmann@24219: haftmann@24219: val code_instanceP = haftmann@24219: OuterSyntax.command code_instanceK "define code syntax for instance" K.thy_decl ( haftmann@24219: parse_multi_syntax (P.xname --| P.$$$ "::" -- P.xname) haftmann@24219: ((P.minus >> K true) || Scan.succeed false) haftmann@24219: >> (Toplevel.theory oo fold) (fn (target, syns) => haftmann@24219: fold (fn (raw_inst, add_del) => add_syntax_inst_cmd target raw_inst add_del) syns) haftmann@24219: ); haftmann@24219: haftmann@24219: val code_typeP = haftmann@24219: OuterSyntax.command code_typeK "define code syntax for type constructor" K.thy_decl ( haftmann@24219: parse_multi_syntax P.xname (parse_syntax I) haftmann@24219: >> (Toplevel.theory oo fold) (fn (target, syns) => haftmann@24219: fold (fn (raw_tyco, syn) => add_syntax_tyco_cmd target raw_tyco syn) syns) haftmann@24219: ); haftmann@24219: haftmann@24219: val code_constP = haftmann@24219: OuterSyntax.command code_constK "define code syntax for constant" K.thy_decl ( haftmann@24219: parse_multi_syntax P.term (parse_syntax fst) haftmann@24219: >> (Toplevel.theory oo fold) (fn (target, syns) => haftmann@24219: fold (fn (raw_const, syn) => add_syntax_const_cmd target raw_const (no_bindings syn)) syns) haftmann@24219: ); haftmann@24219: haftmann@24219: val code_monadP = haftmann@24219: OuterSyntax.command code_monadK "define code syntax for Haskell monads" K.thy_decl ( haftmann@24219: P.term -- P.term -- P.term haftmann@24219: >> (fn ((raw_run, raw_mbind), raw_kbind) => Toplevel.theory haftmann@24219: (add_haskell_monad raw_run raw_mbind raw_kbind)) haftmann@24219: ); haftmann@24219: haftmann@24219: val code_reservedP = haftmann@24219: OuterSyntax.command code_reservedK "declare words as reserved for target language" K.thy_decl ( haftmann@24219: P.name -- Scan.repeat1 P.name haftmann@24219: >> (fn (target, reserveds) => (Toplevel.theory o fold (add_reserved target)) reserveds) haftmann@24219: ) haftmann@24219: haftmann@24219: val code_modulenameP = haftmann@24219: OuterSyntax.command code_modulenameK "alias module to other name" K.thy_decl ( haftmann@24219: P.name -- Scan.repeat1 (P.name -- P.name) haftmann@24219: >> (fn (target, modlnames) => (Toplevel.theory o fold (add_modl_alias target)) modlnames) haftmann@24219: ) haftmann@24219: haftmann@24219: val code_moduleprologP = haftmann@24219: OuterSyntax.command code_moduleprologK "add prolog to module" K.thy_decl ( haftmann@24219: P.name -- Scan.repeat1 (P.name -- (P.text >> (fn "-" => NONE | s => SOME s))) haftmann@24219: >> (fn (target, prologs) => (Toplevel.theory o fold (add_modl_prolog target)) prologs) haftmann@24219: ) haftmann@24219: haftmann@24219: val _ = OuterSyntax.add_keywords [infixK, infixlK, infixrK]; haftmann@24219: haftmann@24219: val _ = OuterSyntax.add_parsers [code_classP, code_instanceP, code_typeP, code_constP, haftmann@24219: code_reservedP, code_modulenameP, code_moduleprologP, code_monadP]; haftmann@24219: haftmann@24219: haftmann@24219: (*including serializer defaults*) haftmann@24219: val setup = haftmann@24219: add_serializer (target_SML, isar_seri_sml) haftmann@24219: #> add_serializer (target_OCaml, isar_seri_ocaml) haftmann@24219: #> add_serializer (target_Haskell, isar_seri_haskell) haftmann@24219: #> add_serializer (target_diag, fn _ => fn _ => fn _ => seri_diagnosis) haftmann@24219: #> add_syntax_tyco "SML" "fun" (SOME (2, fn pr_typ => fn fxy => fn [ty1, ty2] => haftmann@24219: (gen_brackify (case fxy of NOBR => false | _ => eval_fxy (INFX (1, R)) fxy) o Pretty.breaks) [ haftmann@24219: pr_typ (INFX (1, X)) ty1, haftmann@24219: str "->", haftmann@24219: pr_typ (INFX (1, R)) ty2 haftmann@24219: ])) haftmann@24219: #> add_syntax_tyco "OCaml" "fun" (SOME (2, fn pr_typ => fn fxy => fn [ty1, ty2] => haftmann@24219: (gen_brackify (case fxy of NOBR => false | _ => eval_fxy (INFX (1, R)) fxy) o Pretty.breaks) [ haftmann@24219: pr_typ (INFX (1, X)) ty1, haftmann@24219: str "->", haftmann@24219: pr_typ (INFX (1, R)) ty2 haftmann@24219: ])) haftmann@24219: #> add_syntax_tyco "Haskell" "fun" (SOME (2, fn pr_typ => fn fxy => fn [ty1, ty2] => haftmann@24219: brackify_infix (1, R) fxy [ haftmann@24219: pr_typ (INFX (1, X)) ty1, haftmann@24219: str "->", haftmann@24219: pr_typ (INFX (1, R)) ty2 haftmann@24219: ])) haftmann@24219: #> fold (add_reserved "SML") ML_Syntax.reserved_names haftmann@24219: #> fold (add_reserved "SML") haftmann@24219: ["o" (*dictionary projections use it already*), "Fail", "div", "mod" (*standard infixes*)] haftmann@24219: #> fold (add_reserved "OCaml") [ haftmann@24219: "and", "as", "assert", "begin", "class", haftmann@24219: "constraint", "do", "done", "downto", "else", "end", "exception", haftmann@24219: "external", "false", "for", "fun", "function", "functor", "if", haftmann@24219: "in", "include", "inherit", "initializer", "lazy", "let", "match", "method", haftmann@24219: "module", "mutable", "new", "object", "of", "open", "or", "private", "rec", haftmann@24219: "sig", "struct", "then", "to", "true", "try", "type", "val", haftmann@24219: "virtual", "when", "while", "with" haftmann@24219: ] haftmann@24219: #> fold (add_reserved "OCaml") ["failwith", "mod"] haftmann@24219: #> fold (add_reserved "Haskell") [ haftmann@24219: "hiding", "deriving", "where", "case", "of", "infix", "infixl", "infixr", haftmann@24219: "import", "default", "forall", "let", "in", "class", "qualified", "data", haftmann@24219: "newtype", "instance", "if", "then", "else", "type", "as", "do", "module" haftmann@24219: ] haftmann@24219: #> fold (add_reserved "Haskell") [ haftmann@24219: "Prelude", "Main", "Bool", "Maybe", "Either", "Ordering", "Char", "String", "Int", haftmann@24219: "Integer", "Float", "Double", "Rational", "IO", "Eq", "Ord", "Enum", "Bounded", haftmann@24219: "Num", "Real", "Integral", "Fractional", "Floating", "RealFloat", "Monad", "Functor", haftmann@24219: "AlreadyExists", "ArithException", "ArrayException", "AssertionFailed", "AsyncException", haftmann@24219: "BlockedOnDeadMVar", "Deadlock", "Denormal", "DivideByZero", "DotNetException", "DynException", haftmann@24219: "Dynamic", "EOF", "EQ", "EmptyRec", "ErrorCall", "ExitException", "ExitFailure", haftmann@24219: "ExitSuccess", "False", "GT", "HeapOverflow", haftmann@24219: "IOError", "IOException", "IllegalOperation", haftmann@24219: "IndexOutOfBounds", "Just", "Key", "LT", "Left", "LossOfPrecision", "NoMethodError", haftmann@24219: "NoSuchThing", "NonTermination", "Nothing", "Obj", "OtherError", "Overflow", haftmann@24219: "PatternMatchFail", "PermissionDenied", "ProtocolError", "RecConError", "RecSelError", haftmann@24219: "RecUpdError", "ResourceBusy", "ResourceExhausted", "Right", "StackOverflow", haftmann@24219: "ThreadKilled", "True", "TyCon", "TypeRep", "UndefinedElement", "Underflow", haftmann@24219: "UnsupportedOperation", "UserError", "abs", "absReal", "acos", "acosh", "all", haftmann@24219: "and", "any", "appendFile", "asTypeOf", "asciiTab", "asin", "asinh", "atan", haftmann@24219: "atan2", "atanh", "basicIORun", "blockIO", "boundedEnumFrom", "boundedEnumFromThen", haftmann@24219: "boundedEnumFromThenTo", "boundedEnumFromTo", "boundedPred", "boundedSucc", "break", haftmann@24219: "catch", "catchException", "ceiling", "compare", "concat", "concatMap", "const", haftmann@24219: "cos", "cosh", "curry", "cycle", "decodeFloat", "denominator", "div", "divMod", haftmann@24219: "doubleToRatio", "doubleToRational", "drop", "dropWhile", "either", "elem", haftmann@24219: "emptyRec", "encodeFloat", "enumFrom", "enumFromThen", "enumFromThenTo", haftmann@24219: "enumFromTo", "error", "even", "exp", "exponent", "fail", "filter", "flip", haftmann@24219: "floatDigits", "floatProperFraction", "floatRadix", "floatRange", "floatToRational", haftmann@24219: "floor", "fmap", "foldl", "foldl'", "foldl1", "foldr", "foldr1", "fromDouble", haftmann@24219: "fromEnum", "fromEnum_0", "fromInt", "fromInteger", "fromIntegral", "fromObj", haftmann@24219: "fromRational", "fst", "gcd", "getChar", "getContents", "getLine", "head", haftmann@24219: "id", "inRange", "index", "init", "intToRatio", "interact", "ioError", "isAlpha", haftmann@24219: "isAlphaNum", "isDenormalized", "isDigit", "isHexDigit", "isIEEE", "isInfinite", haftmann@24219: "isLower", "isNaN", "isNegativeZero", "isOctDigit", "isSpace", "isUpper", "iterate", "iterate'", haftmann@24219: "last", "lcm", "length", "lex", "lexDigits", "lexLitChar", "lexmatch", "lines", "log", haftmann@24219: "logBase", "lookup", "loop", "map", "mapM", "mapM_", "max", "maxBound", "maximum", haftmann@24219: "maybe", "min", "minBound", "minimum", "mod", "negate", "nonnull", "not", "notElem", haftmann@24219: "null", "numerator", "numericEnumFrom", "numericEnumFromThen", "numericEnumFromThenTo", haftmann@24219: "numericEnumFromTo", "odd", "or", "otherwise", "pi", "pred", haftmann@24219: "print", "product", "properFraction", "protectEsc", "putChar", "putStr", "putStrLn", haftmann@24219: "quot", "quotRem", "range", "rangeSize", "rationalToDouble", "rationalToFloat", haftmann@24219: "rationalToRealFloat", "read", "readDec", "readField", "readFieldName", "readFile", haftmann@24219: "readFloat", "readHex", "readIO", "readInt", "readList", "readLitChar", "readLn", haftmann@24219: "readOct", "readParen", "readSigned", "reads", "readsPrec", "realFloatToRational", haftmann@24219: "realToFrac", "recip", "reduce", "rem", "repeat", "replicate", "return", "reverse", haftmann@24219: "round", "scaleFloat", "scanl", "scanl1", "scanr", "scanr1", "seq", "sequence", haftmann@24219: "sequence_", "show", "showChar", "showException", "showField", "showList", haftmann@24219: "showLitChar", "showParen", "showString", "shows", "showsPrec", "significand", haftmann@24219: "signum", "signumReal", "sin", "sinh", "snd", "span", "splitAt", "sqrt", "subtract", haftmann@24219: "succ", "sum", "tail", "take", "takeWhile", "takeWhile1", "tan", "tanh", "threadToIOResult", haftmann@24219: "throw", "toEnum", "toInt", "toInteger", "toObj", "toRational", "truncate", "uncurry", haftmann@24219: "undefined", "unlines", "unsafeCoerce", "unsafeIndex", "unsafeRangeSize", "until", "unwords", haftmann@24219: "unzip", "unzip3", "userError", "words", "writeFile", "zip", "zip3", "zipWith", "zipWith3" haftmann@24219: ] (*due to weird handling of ':', we can't do anything else than to import *all* prelude symbols*); haftmann@24219: haftmann@24219: end; (*local*) haftmann@24219: haftmann@24219: end; (*struct*)