adamc@25: (* Copyright (c) 2008, Adam Chlipala adamc@25: * All rights reserved. adamc@25: * adamc@25: * Redistribution and use in source and binary forms, with or without adamc@25: * modification, are permitted provided that the following conditions are met: adamc@25: * adamc@25: * - Redistributions of source code must retain the above copyright notice, adamc@25: * this list of conditions and the following disclaimer. adamc@25: * - Redistributions in binary form must reproduce the above copyright notice, adamc@25: * this list of conditions and the following disclaimer in the documentation adamc@25: * and/or other materials provided with the distribution. adamc@25: * - The names of contributors may not be used to endorse or promote products adamc@25: * derived from this software without specific prior written permission. adamc@25: * adamc@25: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" adamc@25: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE adamc@25: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE adamc@25: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE adamc@25: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR adamc@25: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF adamc@25: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS adamc@25: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN adamc@25: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) adamc@25: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE adamc@25: * POSSIBILITY OF SUCH DAMAGE. adamc@25: *) adamc@25: adamc@25: structure Monoize :> MONOIZE = struct adamc@25: adamc@25: structure E = ErrorMsg adamc@25: structure Env = CoreEnv adamc@25: adamc@25: structure L = Core adamc@25: structure L' = Mono adamc@25: adamc@196: structure IM = IntBinaryMap adamc@196: adamc@385: val urlPrefix = ref "/" adamc@385: adamc@196: val dummyTyp = (L'.TDatatype (0, ref (L'.Enum, [])), E.dummySpan) adamc@25: adamc@252: structure U = MonoUtil adamc@252: adamc@252: val liftExpInExp = adamc@252: U.Exp.mapB {typ = fn t => t, adamc@252: exp = fn bound => fn e => adamc@252: case e of adamc@252: L'.ERel xn => adamc@252: if xn < bound then adamc@252: e adamc@252: else adamc@252: L'.ERel (xn + 1) adamc@252: | _ => e, adamc@252: bind = fn (bound, U.Exp.RelE _) => bound + 1 adamc@252: | (bound, _) => bound} adamc@252: adamc@25: fun monoName env (all as (c, loc)) = adamc@25: let adamc@25: fun poly () = adamc@25: (E.errorAt loc "Unsupported name constructor"; adamc@25: Print.eprefaces' [("Constructor", CorePrint.p_con env all)]; adamc@25: "") adamc@25: in adamc@25: case c of adamc@25: L.CName s => s adamc@25: | _ => poly () adamc@25: end adamc@25: adamc@292: fun readType' (t, loc) = (L'.TFun ((L'.TFfi ("Basis", "string"), loc), adamc@292: (L'.TOption t, loc)), loc) adamc@292: fun readErrType (t, loc) = (L'.TFun ((L'.TFfi ("Basis", "string"), loc), adamc@292: t), loc) adamc@292: fun readType (t, loc) = adamc@292: (L'.TRecord [("Read", readType' (t, loc)), adamc@292: ("ReadError", readErrType (t, loc))], adamc@292: loc) adamc@292: adamc@196: fun monoType env = adamc@25: let adamc@196: fun mt env dtmap (all as (c, loc)) = adamc@196: let adamc@196: fun poly () = adamc@196: (E.errorAt loc "Unsupported type constructor"; adamc@196: Print.eprefaces' [("Constructor", CorePrint.p_con env all)]; adamc@196: dummyTyp) adamc@196: in adamc@196: case c of adamc@196: L.TFun (c1, c2) => (L'.TFun (mt env dtmap c1, mt env dtmap c2), loc) adamc@196: | L.TCFun _ => poly () adamc@196: | L.TRecord (L.CRecord ((L.KType, _), xcs), _) => adamc@196: (L'.TRecord (map (fn (x, t) => (monoName env x, mt env dtmap t)) xcs), loc) adamc@196: | L.TRecord _ => poly () adamc@196: adamc@288: | L.CApp ((L.CFfi ("Basis", "option"), _), t) => adamc@288: (L'.TOption (mt env dtmap t), loc) adamc@288: adamc@387: | L.CApp ((L.CFfi ("Basis", "eq"), _), t) => adamc@387: let adamc@387: val t = mt env dtmap t adamc@387: in adamc@387: (L'.TFun (t, (L'.TFun (t, (L'.TFfi ("Basis", "bool"), loc)), loc)), loc) adamc@387: end adamc@389: | L.CApp ((L.CFfi ("Basis", "num"), _), t) => adamc@389: let adamc@389: val t = mt env dtmap t adamc@389: in adamc@417: (L'.TRecord [("Zero", t), adamc@417: ("Neg", (L'.TFun (t, t), loc)), adamc@389: ("Plus", (L'.TFun (t, (L'.TFun (t, t), loc)), loc)), adamc@389: ("Minus", (L'.TFun (t, (L'.TFun (t, t), loc)), loc)), adamc@389: ("Times", (L'.TFun (t, (L'.TFun (t, t), loc)), loc)), adamc@389: ("Div", (L'.TFun (t, (L'.TFun (t, t), loc)), loc)), adamc@389: ("Mod", (L'.TFun (t, (L'.TFun (t, t), loc)), loc))], adamc@389: loc) adamc@389: end adamc@391: | L.CApp ((L.CFfi ("Basis", "ord"), _), t) => adamc@391: let adamc@391: val t = mt env dtmap t adamc@391: in adamc@391: (L'.TRecord [("Lt", (L'.TFun (t, (L'.TFun (t, (L'.TFfi ("Basis", "bool"), loc)), loc)), loc)), adamc@391: ("Le", (L'.TFun (t, (L'.TFun (t, (L'.TFfi ("Basis", "bool"), loc)), loc)), loc))], adamc@391: loc) adamc@391: end adamc@286: | L.CApp ((L.CFfi ("Basis", "show"), _), t) => adamc@286: (L'.TFun (mt env dtmap t, (L'.TFfi ("Basis", "string"), loc)), loc) adamc@290: | L.CApp ((L.CFfi ("Basis", "read"), _), t) => adamc@292: readType (mt env dtmap t, loc) adamc@286: adamc@196: | L.CApp ((L.CApp ((L.CApp ((L.CFfi ("Basis", "xml"), _), _), _), _), _), _) => adamc@196: (L'.TFfi ("Basis", "string"), loc) adamc@196: | L.CApp ((L.CApp ((L.CFfi ("Basis", "xhtml"), _), _), _), _) => adamc@196: (L'.TFfi ("Basis", "string"), loc) adamc@196: adamc@251: | L.CApp ((L.CFfi ("Basis", "transaction"), _), t) => adamc@252: (L'.TFun ((L'.TRecord [], loc), mt env dtmap t), loc) adamc@565: | L.CApp ((L.CFfi ("Basis", "source"), _), t) => adamc@577: (L'.TSource, loc) adamc@568: | L.CApp ((L.CFfi ("Basis", "signal"), _), t) => adamc@568: (L'.TSignal (mt env dtmap t), loc) adamc@462: | L.CApp ((L.CFfi ("Basis", "http_cookie"), _), _) => adamc@462: (L'.TFfi ("Basis", "string"), loc) adamc@252: | L.CApp ((L.CFfi ("Basis", "sql_table"), _), _) => adamc@252: (L'.TFfi ("Basis", "string"), loc) adamc@338: | L.CFfi ("Basis", "sql_sequence") => adamc@338: (L'.TFfi ("Basis", "string"), loc) adamc@252: | L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_query"), _), _), _), _) => adamc@252: (L'.TFfi ("Basis", "string"), loc) adamc@252: | L.CApp ((L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_query1"), _), _), _), _), _), _) => adamc@252: (L'.TFfi ("Basis", "string"), loc) adamc@252: | L.CApp ((L.CApp ((L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_exp"), _), _), _), _), _), _), _), _) => adamc@252: (L'.TFfi ("Basis", "string"), loc) adamc@252: adamc@252: | L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_subset"), _), _), _), _) => adamc@252: (L'.TRecord [], loc) adamc@252: | L.CFfi ("Basis", "sql_relop") => adamc@252: (L'.TFfi ("Basis", "string"), loc) adamc@252: | L.CFfi ("Basis", "sql_direction") => adamc@252: (L'.TFfi ("Basis", "string"), loc) adamc@252: | L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_order_by"), _), _), _), _) => adamc@252: (L'.TFfi ("Basis", "string"), loc) adamc@252: | L.CFfi ("Basis", "sql_limit") => adamc@252: (L'.TFfi ("Basis", "string"), loc) adamc@252: | L.CFfi ("Basis", "sql_offset") => adamc@252: (L'.TFfi ("Basis", "string"), loc) adamc@252: adamc@252: | L.CApp ((L.CFfi ("Basis", "sql_injectable"), _), t) => adamc@252: (L'.TFun (mt env dtmap t, (L'.TFfi ("Basis", "string"), loc)), loc) adamc@252: | L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_unary"), _), _), _), _) => adamc@252: (L'.TFfi ("Basis", "string"), loc) adamc@252: | L.CApp ((L.CApp ((L.CApp ((L.CFfi ("Basis", "sql_binary"), _), _), _), _), _), _) => adamc@252: (L'.TFfi ("Basis", "string"), loc) adamc@252: | L.CApp ((L.CFfi ("Basis", "sql_aggregate"), _), t) => adamc@252: (L'.TFfi ("Basis", "string"), loc) adamc@252: | L.CApp ((L.CFfi ("Basis", "sql_summable"), _), _) => adamc@252: (L'.TRecord [], loc) adamc@252: | L.CApp ((L.CFfi ("Basis", "sql_maxable"), _), _) => adamc@252: (L'.TRecord [], loc) adamc@559: | L.CApp ((L.CFfi ("Basis", "sql_arith"), _), _) => adamc@559: (L'.TRecord [], loc) adamc@441: | L.CApp ((L.CFfi ("Basis", "sql_nfunc"), _), _) => adamc@441: (L'.TFfi ("Basis", "string"), loc) adamc@251: adamc@196: | L.CRel _ => poly () adamc@196: | L.CNamed n => adamc@196: (case IM.find (dtmap, n) of adamc@196: SOME r => (L'.TDatatype (n, r), loc) adamc@196: | NONE => adamc@196: let adamc@196: val r = ref (L'.Default, []) adamc@196: val (_, xs, xncs) = Env.lookupDatatype env n adamc@196: adamc@196: val dtmap' = IM.insert (dtmap, n, r) adamc@196: adamc@196: val xncs = map (fn (x, n, to) => (x, n, Option.map (mt env dtmap') to)) xncs adamc@196: in adamc@196: case xs of adamc@198: [] =>(r := (ElabUtil.classifyDatatype xncs, xncs); adamc@196: (L'.TDatatype (n, r), loc)) adamc@196: | _ => poly () adamc@196: end) adamc@196: | L.CFfi mx => (L'.TFfi mx, loc) adamc@196: | L.CApp _ => poly () adamc@196: | L.CAbs _ => poly () adamc@196: adamc@196: | L.CName _ => poly () adamc@196: adamc@196: | L.CRecord _ => poly () adamc@196: | L.CConcat _ => poly () adamc@196: | L.CFold _ => poly () adamc@196: | L.CUnit => poly () adamc@214: adamc@214: | L.CTuple _ => poly () adamc@214: | L.CProj _ => poly () adamc@196: end adamc@25: in adamc@196: mt env IM.empty adamc@25: end adamc@25: adamc@25: val dummyExp = (L'.EPrim (Prim.Int 0), E.dummySpan) adamc@25: adamc@179: structure IM = IntBinaryMap adamc@179: adamc@179: datatype foo_kind = adamc@179: Attr adamc@179: | Url adamc@179: adamc@179: fun fk2s fk = adamc@179: case fk of adamc@179: Attr => "attr" adamc@179: | Url => "url" adamc@179: adamc@179: structure Fm :> sig adamc@179: type t adamc@179: adamc@179: val empty : int -> t adamc@179: adamc@179: val lookup : t -> foo_kind -> int -> (int -> t -> L'.decl * t) -> t * int adamc@179: val enter : t -> t adamc@179: val decls : t -> L'.decl list adamc@179: end = struct adamc@179: adamc@179: structure M = BinaryMapFn(struct adamc@179: type ord_key = foo_kind adamc@179: fun compare x = adamc@179: case x of adamc@179: (Attr, Attr) => EQUAL adamc@179: | (Attr, _) => LESS adamc@179: | (_, Attr) => GREATER adamc@179: adamc@179: | (Url, Url) => EQUAL adamc@179: end) adamc@179: adamc@179: type t = { adamc@179: count : int, adamc@179: map : int IM.map M.map, adamc@179: decls : L'.decl list adamc@179: } adamc@179: adamc@179: fun empty count = { adamc@179: count = count, adamc@179: map = M.empty, adamc@179: decls = [] adamc@179: } adamc@179: adamc@179: fun enter ({count, map, ...} : t) = {count = count, map = map, decls = []} adamc@179: fun decls ({decls, ...} : t) = decls adamc@179: adamc@179: fun lookup (t as {count, map, decls}) k n thunk = adamc@120: let adamc@179: val im = Option.getOpt (M.find (map, k), IM.empty) adamc@179: in adamc@179: case IM.find (im, n) of adamc@179: NONE => adamc@179: let adamc@179: val n' = count adamc@179: val (d, {count, map, decls}) = thunk count {count = count + 1, adamc@179: map = M.insert (map, k, IM.insert (im, n, n')), adamc@179: decls = decls} adamc@179: in adamc@179: ({count = count, adamc@179: map = map, adamc@179: decls = d :: decls}, n') adamc@179: end adamc@179: | SOME n' => (t, n') adamc@179: end adamc@179: adamc@179: end adamc@185: adamc@185: adamc@185: fun capitalize s = adamc@185: if s = "" then adamc@185: s adamc@185: else adamc@185: str (Char.toUpper (String.sub (s, 0))) ^ String.extract (s, 1, NONE) adamc@179: adamc@179: fun fooifyExp fk env = adamc@179: let adamc@179: fun fooify fm (e, tAll as (t, loc)) = adamc@120: case #1 e of adamc@120: L'.EClosure (fnam, [(L'.ERecord [], _)]) => adamc@120: let adamc@120: val (_, _, _, s) = Env.lookupENamed env fnam adamc@120: in adamc@385: ((L'.EPrim (Prim.String (!urlPrefix ^ s)), loc), fm) adamc@120: end adamc@120: | L'.EClosure (fnam, args) => adamc@120: let adamc@120: val (_, ft, _, s) = Env.lookupENamed env fnam adamc@120: val ft = monoType env ft adamc@111: adamc@179: fun attrify (args, ft, e, fm) = adamc@120: case (args, ft) of adamc@179: ([], _) => (e, fm) adamc@120: | (arg :: args, (L'.TFun (t, ft), _)) => adamc@179: let adamc@179: val (arg', fm) = fooify fm (arg, t) adamc@179: in adamc@179: attrify (args, ft, adamc@179: (L'.EStrcat (e, adamc@179: (L'.EStrcat ((L'.EPrim (Prim.String "/"), loc), adamc@179: arg'), loc)), loc), adamc@179: fm) adamc@179: end adamc@120: | _ => (E.errorAt loc "Type mismatch encoding attribute"; adamc@179: (e, fm)) adamc@120: in adamc@385: attrify (args, ft, (L'.EPrim (Prim.String (!urlPrefix ^ s)), loc), fm) adamc@120: end adamc@120: | _ => adamc@120: case t of adamc@185: L'.TFfi (m, x) => ((L'.EFfiApp (m, fk2s fk ^ "ify" ^ capitalize x, [e]), loc), fm) adamc@200: adamc@179: | L'.TRecord [] => ((L'.EPrim (Prim.String ""), loc), fm) adamc@200: | L'.TRecord ((x, t) :: xts) => adamc@200: let adamc@200: val (se, fm) = fooify fm ((L'.EField (e, x), loc), t) adamc@200: in adamc@200: foldl (fn ((x, t), (se, fm)) => adamc@200: let adamc@200: val (se', fm) = fooify fm ((L'.EField (e, x), loc), t) adamc@200: in adamc@200: ((L'.EStrcat (se, adamc@200: (L'.EStrcat ((L'.EPrim (Prim.String "/"), loc), adamc@200: se'), loc)), loc), adamc@200: fm) adamc@200: end) (se, fm) xts adamc@200: end adamc@111: adamc@196: | L'.TDatatype (i, ref (dk, _)) => adamc@179: let adamc@179: fun makeDecl n fm = adamc@179: let adamc@193: val (x, _, xncs) = Env.lookupDatatype env i adamc@179: adamc@179: val (branches, fm) = adamc@179: ListUtil.foldlMap adamc@179: (fn ((x, n, to), fm) => adamc@179: case to of adamc@179: NONE => adamc@188: (((L'.PCon (dk, L'.PConVar n, NONE), loc), adamc@179: (L'.EPrim (Prim.String x), loc)), adamc@179: fm) adamc@179: | SOME t => adamc@179: let adamc@182: val t = monoType env t adamc@182: val (arg, fm) = fooify fm ((L'.ERel 0, loc), t) adamc@179: in adamc@188: (((L'.PCon (dk, L'.PConVar n, SOME (L'.PVar ("a", t), loc)), loc), adamc@179: (L'.EStrcat ((L'.EPrim (Prim.String (x ^ "/")), loc), adamc@179: arg), loc)), adamc@179: fm) adamc@179: end) adamc@179: fm xncs adamc@179: adamc@179: val dom = tAll adamc@179: val ran = (L'.TFfi ("Basis", "string"), loc) adamc@179: in adamc@179: ((L'.DValRec [(fk2s fk ^ "ify_" ^ x, adamc@179: n, adamc@179: (L'.TFun (dom, ran), loc), adamc@179: (L'.EAbs ("x", adamc@179: dom, adamc@179: ran, adamc@179: (L'.ECase ((L'.ERel 0, loc), adamc@179: branches, adamc@182: {disc = dom, adamc@182: result = ran}), loc)), loc), adamc@179: "")], loc), adamc@179: fm) adamc@179: end adamc@179: adamc@179: val (fm, n) = Fm.lookup fm fk i makeDecl adamc@179: in adamc@179: ((L'.EApp ((L'.ENamed n, loc), e), loc), fm) adamc@179: end adamc@164: adamc@471: | L'.TOption t => adamc@471: let adamc@471: val (body, fm) = fooify fm ((L'.ERel 0, loc), t) adamc@471: in adamc@471: ((L'.ECase (e, adamc@471: [((L'.PNone t, loc), adamc@471: (L'.EPrim (Prim.String "None"), loc)), adamc@471: adamc@471: ((L'.PSome (t, (L'.PVar ("x", t), loc)), loc), adamc@471: (L'.EStrcat ((L'.EPrim (Prim.String "Some/"), loc), adamc@471: body), loc))], adamc@471: {disc = tAll, adamc@471: result = (L'.TFfi ("Basis", "string"), loc)}), loc), adamc@471: fm) adamc@471: end adamc@471: adamc@490: | _ => (E.errorAt loc "Don't know how to encode attribute/URL type"; adamc@120: Print.eprefaces' [("Type", MonoPrint.p_typ MonoEnv.empty tAll)]; adamc@179: (dummyExp, fm)) adamc@120: in adamc@120: fooify adamc@120: end adamc@120: adamc@179: val attrifyExp = fooifyExp Attr adamc@179: val urlifyExp = fooifyExp Url adamc@105: adamc@143: datatype 'a failable_search = adamc@143: Found of 'a adamc@143: | NotFound adamc@143: | Error adamc@143: adamc@153: structure St :> sig adamc@153: type t adamc@153: adamc@153: val empty : t adamc@153: adamc@153: val radioGroup : t -> string option adamc@153: val setRadioGroup : t * string -> t adamc@153: end = struct adamc@153: adamc@153: type t = { adamc@153: radioGroup : string option adamc@153: } adamc@153: adamc@153: val empty = {radioGroup = NONE} adamc@153: adamc@153: fun radioGroup (t : t) = #radioGroup t adamc@153: adamc@153: fun setRadioGroup (t : t, x) = {radioGroup = SOME x} adamc@153: adamc@153: end adamc@153: adamc@186: fun monoPatCon env pc = adamc@178: case pc of adamc@178: L.PConVar n => L'.PConVar n adamc@188: | L.PConFfi {mod = m, datatyp, con, arg, ...} => L'.PConFfi {mod = m, datatyp = datatyp, con = con, adamc@188: arg = Option.map (monoType env) arg} adamc@178: adamc@193: val dummyPat = (L'.PPrim (Prim.Int 0), ErrorMsg.dummySpan) adamc@193: adamc@193: fun monoPat env (all as (p, loc)) = adamc@193: let adamc@193: fun poly () = adamc@193: (E.errorAt loc "Unsupported pattern"; adamc@193: Print.eprefaces' [("Pattern", CorePrint.p_pat env all)]; adamc@193: dummyPat) adamc@193: in adamc@193: case p of adamc@193: L.PWild => (L'.PWild, loc) adamc@193: | L.PVar (x, t) => (L'.PVar (x, monoType env t), loc) adamc@193: | L.PPrim p => (L'.PPrim p, loc) adamc@193: | L.PCon (dk, pc, [], po) => (L'.PCon (dk, monoPatCon env pc, Option.map (monoPat env) po), loc) adamc@288: | L.PCon (L.Option, _, [t], NONE) => (L'.PNone (monoType env t), loc) adamc@288: | L.PCon (L.Option, _, [t], SOME p) => (L'.PSome (monoType env t, monoPat env p), loc) adamc@193: | L.PCon _ => poly () adamc@193: | L.PRecord xps => (L'.PRecord (map (fn (x, p, t) => (x, monoPat env p, monoType env t)) xps), loc) adamc@193: end adamc@178: adamc@252: fun strcat loc es = adamc@252: case es of adamc@252: [] => (L'.EPrim (Prim.String ""), loc) adamc@252: | [e] => e adamc@252: | _ => adamc@252: let adamc@252: val e2 = List.last es adamc@252: val es = List.take (es, length es - 1) adamc@252: val e1 = List.last es adamc@252: val es = List.take (es, length es - 1) adamc@252: in adamc@252: foldr (fn (e, e') => (L'.EStrcat (e, e'), loc)) adamc@252: (L'.EStrcat (e1, e2), loc) es adamc@252: end adamc@252: adamc@252: fun strcatComma loc es = adamc@252: case es of adamc@252: [] => (L'.EPrim (Prim.String ""), loc) adamc@252: | [e] => e adamc@252: | _ => adamc@252: let adamc@252: val e1 = List.last es adamc@252: val es = List.take (es, length es - 1) adamc@252: in adamc@252: foldr (fn (e, e') => adamc@265: case (e, e') of adamc@265: ((L'.EPrim (Prim.String ""), _), _) => e' adamc@265: | (_, (L'.EPrim (Prim.String ""), _)) => e adamc@252: | _ => adamc@252: (L'.EStrcat (e, adamc@252: (L'.EStrcat ((L'.EPrim (Prim.String ", "), loc), e'), loc)), loc)) adamc@252: e1 es adamc@252: end adamc@252: adamc@252: fun strcatR loc e xs = strcatComma loc (map (fn (x, _) => (L'.EField (e, x), loc)) xs) adamc@252: adamc@179: fun monoExp (env, st, fm) (all as (e, loc)) = adamc@25: let adamc@598: val strcat = strcat loc adamc@598: val strcatComma = strcatComma loc adamc@598: fun str s = (L'.EPrim (Prim.String s), loc) adamc@598: adamc@25: fun poly () = adamc@25: (E.errorAt loc "Unsupported expression"; adamc@25: Print.eprefaces' [("Expression", CorePrint.p_exp env all)]; adamc@179: (dummyExp, fm)) adamc@389: adamc@389: fun numTy t = adamc@417: (L'.TRecord [("Zero", t), adamc@417: ("Neg", (L'.TFun (t, t), loc)), adamc@389: ("Plus", (L'.TFun (t, (L'.TFun (t, t), loc)), loc)), adamc@389: ("Minus", (L'.TFun (t, (L'.TFun (t, t), loc)), loc)), adamc@389: ("Times", (L'.TFun (t, (L'.TFun (t, t), loc)), loc)), adamc@389: ("Div", (L'.TFun (t, (L'.TFun (t, t), loc)), loc)), adamc@389: ("Mod", (L'.TFun (t, (L'.TFun (t, t), loc)), loc))], loc) adamc@417: fun numEx (t, zero, neg, plus, minus, times, dv, md) = adamc@417: ((L'.ERecord [("Zero", (L'.EPrim zero, loc), t), adamc@417: ("Neg", neg, (L'.TFun (t, t), loc)), adamc@389: ("Plus", plus, (L'.TFun (t, (L'.TFun (t, t), loc)), loc)), adamc@389: ("Minus", minus, (L'.TFun (t, (L'.TFun (t, t), loc)), loc)), adamc@389: ("Times", times, (L'.TFun (t, (L'.TFun (t, t), loc)), loc)), adamc@389: ("Div", dv, (L'.TFun (t, (L'.TFun (t, t), loc)), loc)), adamc@389: ("Mod", md, (L'.TFun (t, (L'.TFun (t, t), loc)), loc))], loc), fm) adamc@391: adamc@391: fun ordTy t = adamc@391: (L'.TRecord [("Lt", (L'.TFun (t, (L'.TFun (t, (L'.TFfi ("Basis", "bool"), loc)), loc)), loc)), adamc@391: ("Le", (L'.TFun (t, (L'.TFun (t, (L'.TFfi ("Basis", "bool"), loc)), loc)), loc))], loc) adamc@391: fun ordEx (t, lt, le) = adamc@391: ((L'.ERecord [("Lt", lt, (L'.TFun (t, (L'.TFun (t, (L'.TFfi ("Basis", "bool"), loc)), loc)), loc)), adamc@391: ("Le", le, (L'.TFun (t, (L'.TFun (t, (L'.TFfi ("Basis", "bool"), loc)), loc)), loc))], adamc@391: loc), fm) adamc@25: in adamc@25: case e of adamc@179: L.EPrim p => ((L'.EPrim p, loc), fm) adamc@179: | L.ERel n => ((L'.ERel n, loc), fm) adamc@179: | L.ENamed n => ((L'.ENamed n, loc), fm) adamc@193: | L.ECon (dk, pc, [], eo) => adamc@193: let adamc@179: val (eo, fm) = adamc@179: case eo of adamc@179: NONE => (NONE, fm) adamc@179: | SOME e => adamc@179: let adamc@179: val (e, fm) = monoExp (env, st, fm) e adamc@179: in adamc@179: (SOME e, fm) adamc@179: end adamc@179: in adamc@188: ((L'.ECon (dk, monoPatCon env pc, eo), loc), fm) adamc@193: end adamc@297: | L.ECon (L.Option, _, [t], NONE) => adamc@297: ((L'.ENone (monoType env t), loc), fm) adamc@297: | L.ECon (L.Option, _, [t], SOME e) => adamc@297: let adamc@297: val (e, fm) = monoExp (env, st, fm) e adamc@297: in adamc@297: ((L'.ESome (monoType env t, e), loc), fm) adamc@297: end adamc@193: | L.ECon _ => poly () adamc@94: adamc@387: | L.ECApp ((L.EFfi ("Basis", "eq"), _), t) => adamc@387: let adamc@387: val t = monoType env t adamc@387: val b = (L'.TFfi ("Basis", "bool"), loc) adamc@387: val dom = (L'.TFun (t, (L'.TFun (t, b), loc)), loc) adamc@387: in adamc@387: ((L'.EAbs ("f", dom, dom, adamc@387: (L'.ERel 0, loc)), loc), fm) adamc@387: end adamc@387: | L.ECApp ((L.EFfi ("Basis", "ne"), _), t) => adamc@387: let adamc@387: val t = monoType env t adamc@387: val b = (L'.TFfi ("Basis", "bool"), loc) adamc@387: val dom = (L'.TFun (t, (L'.TFun (t, b), loc)), loc) adamc@387: in adamc@387: ((L'.EAbs ("f", dom, dom, adamc@387: (L'.EAbs ("x", t, (L'.TFun (t, b), loc), adamc@387: (L'.EAbs ("y", t, b, adamc@387: (L'.EUnop ("!", (L'.EApp ((L'.EApp ((L'.ERel 2, loc), adamc@387: (L'.ERel 1, loc)), loc), adamc@387: (L'.ERel 0, loc)), loc)), loc)), adamc@387: loc)), adamc@387: loc)), adamc@387: loc), fm) adamc@387: end adamc@387: | L.EFfi ("Basis", "eq_int") => adamc@387: ((L'.EAbs ("x", (L'.TFfi ("Basis", "int"), loc), adamc@387: (L'.TFun ((L'.TFfi ("Basis", "int"), loc), (L'.TFfi ("Basis", "bool"), loc)), loc), adamc@387: (L'.EAbs ("y", (L'.TFfi ("Basis", "int"), loc), adamc@387: (L'.TFfi ("Basis", "bool"), loc), adamc@387: (L'.EBinop ("==", (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc), adamc@387: fm) adamc@394: | L.EFfi ("Basis", "eq_float") => adamc@394: ((L'.EAbs ("x", (L'.TFfi ("Basis", "float"), loc), adamc@394: (L'.TFun ((L'.TFfi ("Basis", "float"), loc), (L'.TFfi ("Basis", "bool"), loc)), loc), adamc@394: (L'.EAbs ("y", (L'.TFfi ("Basis", "float"), loc), adamc@394: (L'.TFfi ("Basis", "bool"), loc), adamc@394: (L'.EBinop ("==", (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc), adamc@394: fm) adamc@388: | L.EFfi ("Basis", "eq_bool") => adamc@388: ((L'.EAbs ("x", (L'.TFfi ("Basis", "bool"), loc), adamc@388: (L'.TFun ((L'.TFfi ("Basis", "bool"), loc), (L'.TFfi ("Basis", "bool"), loc)), loc), adamc@388: (L'.EAbs ("y", (L'.TFfi ("Basis", "bool"), loc), adamc@388: (L'.TFfi ("Basis", "bool"), loc), adamc@388: (L'.EBinop ("==", (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc), adamc@388: fm) adamc@388: | L.EFfi ("Basis", "eq_string") => adamc@388: ((L'.EAbs ("x", (L'.TFfi ("Basis", "string"), loc), adamc@388: (L'.TFun ((L'.TFfi ("Basis", "string"), loc), (L'.TFfi ("Basis", "bool"), loc)), loc), adamc@388: (L'.EAbs ("y", (L'.TFfi ("Basis", "string"), loc), adamc@388: (L'.TFfi ("Basis", "bool"), loc), adamc@388: (L'.EBinop ("!strcmp", (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc), adamc@388: fm) adamc@437: | L.EFfi ("Basis", "eq_time") => adamc@437: ((L'.EAbs ("x", (L'.TFfi ("Basis", "time"), loc), adamc@437: (L'.TFun ((L'.TFfi ("Basis", "time"), loc), (L'.TFfi ("Basis", "bool"), loc)), loc), adamc@437: (L'.EAbs ("y", (L'.TFfi ("Basis", "time"), loc), adamc@437: (L'.TFfi ("Basis", "bool"), loc), adamc@437: (L'.EBinop ("==", (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc), adamc@437: fm) adamc@422: | L.ECApp ((L.EFfi ("Basis", "mkEq"), _), t) => adamc@422: let adamc@422: val t = monoType env t adamc@422: val b = (L'.TFfi ("Basis", "bool"), loc) adamc@422: val dom = (L'.TFun (t, (L'.TFun (t, b), loc)), loc) adamc@422: in adamc@422: ((L'.EAbs ("f", dom, dom, adamc@422: (L'.ERel 0, loc)), loc), fm) adamc@422: end adamc@387: adamc@417: | L.ECApp ((L.EFfi ("Basis", "zero"), _), t) => adamc@417: let adamc@417: val t = monoType env t adamc@417: in adamc@417: ((L'.EAbs ("r", numTy t, t, adamc@417: (L'.EField ((L'.ERel 0, loc), "Zero"), loc)), loc), fm) adamc@417: end adamc@389: | L.ECApp ((L.EFfi ("Basis", "neg"), _), t) => adamc@389: let adamc@389: val t = monoType env t adamc@389: in adamc@389: ((L'.EAbs ("r", numTy t, (L'.TFun (t, t), loc), adamc@389: (L'.EField ((L'.ERel 0, loc), "Neg"), loc)), loc), fm) adamc@389: end adamc@389: | L.ECApp ((L.EFfi ("Basis", "plus"), _), t) => adamc@389: let adamc@389: val t = monoType env t adamc@389: in adamc@389: ((L'.EAbs ("r", numTy t, (L'.TFun (t, (L'.TFun (t, t), loc)), loc), adamc@389: (L'.EField ((L'.ERel 0, loc), "Plus"), loc)), loc), fm) adamc@389: end adamc@389: | L.ECApp ((L.EFfi ("Basis", "minus"), _), t) => adamc@389: let adamc@389: val t = monoType env t adamc@389: in adamc@389: ((L'.EAbs ("r", numTy t, (L'.TFun (t, (L'.TFun (t, t), loc)), loc), adamc@389: (L'.EField ((L'.ERel 0, loc), "Minus"), loc)), loc), fm) adamc@389: end adamc@389: | L.ECApp ((L.EFfi ("Basis", "times"), _), t) => adamc@389: let adamc@389: val t = monoType env t adamc@389: in adamc@389: ((L'.EAbs ("r", numTy t, (L'.TFun (t, (L'.TFun (t, t), loc)), loc), adamc@389: (L'.EField ((L'.ERel 0, loc), "Times"), loc)), loc), fm) adamc@389: end adamc@389: | L.ECApp ((L.EFfi ("Basis", "div"), _), t) => adamc@389: let adamc@389: val t = monoType env t adamc@389: in adamc@389: ((L'.EAbs ("r", numTy t, (L'.TFun (t, (L'.TFun (t, t), loc)), loc), adamc@389: (L'.EField ((L'.ERel 0, loc), "Div"), loc)), loc), fm) adamc@389: end adamc@389: | L.ECApp ((L.EFfi ("Basis", "mod"), _), t) => adamc@389: let adamc@389: val t = monoType env t adamc@389: in adamc@389: ((L'.EAbs ("r", numTy t, (L'.TFun (t, (L'.TFun (t, t), loc)), loc), adamc@389: (L'.EField ((L'.ERel 0, loc), "Mod"), loc)), loc), fm) adamc@389: end adamc@389: | L.EFfi ("Basis", "num_int") => adamc@389: let adamc@389: fun intBin s = adamc@389: (L'.EAbs ("x", (L'.TFfi ("Basis", "int"), loc), adamc@389: (L'.TFun ((L'.TFfi ("Basis", "int"), loc), (L'.TFfi ("Basis", "int"), loc)), loc), adamc@389: (L'.EAbs ("y", (L'.TFfi ("Basis", "int"), loc), adamc@389: (L'.TFfi ("Basis", "int"), loc), adamc@389: (L'.EBinop (s, (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc) adamc@389: in adamc@389: numEx ((L'.TFfi ("Basis", "int"), loc), adamc@417: Prim.Int (Int64.fromInt 0), adamc@389: (L'.EAbs ("x", (L'.TFfi ("Basis", "int"), loc), adamc@389: (L'.TFfi ("Basis", "int"), loc), adamc@389: (L'.EUnop ("-", (L'.ERel 0, loc)), loc)), loc), adamc@389: intBin "+", adamc@389: intBin "-", adamc@389: intBin "*", adamc@389: intBin "/", adamc@389: intBin "%") adamc@389: end adamc@390: | L.EFfi ("Basis", "num_float") => adamc@390: let adamc@390: fun floatBin s = adamc@390: (L'.EAbs ("x", (L'.TFfi ("Basis", "float"), loc), adamc@390: (L'.TFun ((L'.TFfi ("Basis", "float"), loc), (L'.TFfi ("Basis", "float"), loc)), loc), adamc@390: (L'.EAbs ("y", (L'.TFfi ("Basis", "float"), loc), adamc@390: (L'.TFfi ("Basis", "float"), loc), adamc@390: (L'.EBinop (s, (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc) adamc@390: in adamc@390: numEx ((L'.TFfi ("Basis", "float"), loc), adamc@417: Prim.Float 0.0, adamc@390: (L'.EAbs ("x", (L'.TFfi ("Basis", "float"), loc), adamc@390: (L'.TFfi ("Basis", "float"), loc), adamc@390: (L'.EUnop ("-", (L'.ERel 0, loc)), loc)), loc), adamc@390: floatBin "+", adamc@390: floatBin "-", adamc@390: floatBin "*", adamc@390: floatBin "/", adamc@390: floatBin "fmod") adamc@390: end adamc@391: adamc@391: | L.ECApp ((L.EFfi ("Basis", "lt"), _), t) => adamc@391: let adamc@391: val t = monoType env t adamc@391: in adamc@391: ((L'.EAbs ("r", ordTy t, (L'.TFun (t, (L'.TFun (t, (L'.TFfi ("Basis", "bool"), loc)), loc)), loc), adamc@391: (L'.EField ((L'.ERel 0, loc), "Lt"), loc)), loc), fm) adamc@391: end adamc@391: | L.ECApp ((L.EFfi ("Basis", "le"), _), t) => adamc@391: let adamc@391: val t = monoType env t adamc@391: in adamc@391: ((L'.EAbs ("r", ordTy t, (L'.TFun (t, (L'.TFun (t, (L'.TFfi ("Basis", "bool"), loc)), loc)), loc), adamc@391: (L'.EField ((L'.ERel 0, loc), "Le"), loc)), loc), fm) adamc@391: end adamc@392: | L.ECApp ((L.EFfi ("Basis", "gt"), _), t) => adamc@392: let adamc@392: val t = monoType env t adamc@392: val b = (L'.TFfi ("Basis", "bool"), loc) adamc@392: in adamc@392: ((L'.EAbs ("f", ordTy t, (L'.TFun (t, (L'.TFun (t, b), loc)), loc), adamc@392: (L'.EAbs ("x", t, (L'.TFun (t, b), loc), adamc@392: (L'.EAbs ("y", t, b, adamc@392: (L'.EUnop ("!", adamc@392: (L'.EApp ((L'.EApp ((L'.EField ((L'.ERel 2, loc), adamc@392: "Le"), loc), adamc@392: (L'.ERel 1, loc)), loc), adamc@392: (L'.ERel 0, loc)), loc)), loc)), loc)), adamc@392: loc)), adamc@392: loc), fm) adamc@392: end adamc@392: | L.ECApp ((L.EFfi ("Basis", "ge"), _), t) => adamc@392: let adamc@392: val t = monoType env t adamc@392: val b = (L'.TFfi ("Basis", "bool"), loc) adamc@392: in adamc@392: ((L'.EAbs ("f", ordTy t, (L'.TFun (t, (L'.TFun (t, b), loc)), loc), adamc@392: (L'.EAbs ("x", t, (L'.TFun (t, b), loc), adamc@392: (L'.EAbs ("y", t, b, adamc@392: (L'.EUnop ("!", adamc@392: (L'.EApp ((L'.EApp ((L'.EField ((L'.ERel 2, loc), adamc@392: "Lt"), loc), adamc@392: (L'.ERel 1, loc)), loc), adamc@392: (L'.ERel 0, loc)), loc)), loc)), loc)), adamc@392: loc)), adamc@392: loc), fm) adamc@392: end adamc@391: | L.EFfi ("Basis", "ord_int") => adamc@391: let adamc@391: fun intBin s = adamc@391: (L'.EAbs ("x", (L'.TFfi ("Basis", "int"), loc), adamc@391: (L'.TFun ((L'.TFfi ("Basis", "int"), loc), (L'.TFfi ("Basis", "bool"), loc)), loc), adamc@391: (L'.EAbs ("y", (L'.TFfi ("Basis", "int"), loc), adamc@391: (L'.TFfi ("Basis", "bool"), loc), adamc@391: (L'.EBinop (s, (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc) adamc@391: in adamc@391: ordEx ((L'.TFfi ("Basis", "int"), loc), adamc@391: intBin "<", adamc@391: intBin "<=") adamc@391: end adamc@394: | L.EFfi ("Basis", "ord_float") => adamc@394: let adamc@394: fun floatBin s = adamc@394: (L'.EAbs ("x", (L'.TFfi ("Basis", "float"), loc), adamc@394: (L'.TFun ((L'.TFfi ("Basis", "float"), loc), (L'.TFfi ("Basis", "bool"), loc)), loc), adamc@394: (L'.EAbs ("y", (L'.TFfi ("Basis", "float"), loc), adamc@394: (L'.TFfi ("Basis", "bool"), loc), adamc@394: (L'.EBinop (s, (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc) adamc@394: in adamc@394: ordEx ((L'.TFfi ("Basis", "float"), loc), adamc@394: floatBin "<", adamc@394: floatBin "<=") adamc@394: end adamc@394: | L.EFfi ("Basis", "ord_bool") => adamc@394: let adamc@394: fun boolBin s = adamc@394: (L'.EAbs ("x", (L'.TFfi ("Basis", "bool"), loc), adamc@394: (L'.TFun ((L'.TFfi ("Basis", "bool"), loc), (L'.TFfi ("Basis", "bool"), loc)), loc), adamc@394: (L'.EAbs ("y", (L'.TFfi ("Basis", "bool"), loc), adamc@394: (L'.TFfi ("Basis", "bool"), loc), adamc@394: (L'.EBinop (s, (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc) adamc@394: in adamc@394: ordEx ((L'.TFfi ("Basis", "bool"), loc), adamc@394: boolBin "<", adamc@394: boolBin "<=") adamc@394: end adamc@395: | L.EFfi ("Basis", "ord_string") => adamc@395: let adamc@395: fun boolBin s = adamc@395: (L'.EAbs ("x", (L'.TFfi ("Basis", "string"), loc), adamc@395: (L'.TFun ((L'.TFfi ("Basis", "string"), loc), (L'.TFfi ("Basis", "bool"), loc)), loc), adamc@395: (L'.EAbs ("y", (L'.TFfi ("Basis", "string"), loc), adamc@395: (L'.TFfi ("Basis", "bool"), loc), adamc@395: (L'.EBinop (s, adamc@395: (L'.EBinop ("strcmp", adamc@395: (L'.ERel 1, loc), adamc@395: (L'.ERel 0, loc)), loc), adamc@395: (L'.EPrim (Prim.Int (Int64.fromInt 0)), loc)), loc)), loc)), loc) adamc@395: in adamc@395: ordEx ((L'.TFfi ("Basis", "string"), loc), adamc@395: boolBin "<", adamc@395: boolBin "<=") adamc@395: end adamc@437: | L.EFfi ("Basis", "ord_time") => adamc@437: let adamc@437: fun boolBin s = adamc@437: (L'.EAbs ("x", (L'.TFfi ("Basis", "time"), loc), adamc@437: (L'.TFun ((L'.TFfi ("Basis", "time"), loc), (L'.TFfi ("Basis", "bool"), loc)), loc), adamc@437: (L'.EAbs ("y", (L'.TFfi ("Basis", "time"), loc), adamc@437: (L'.TFfi ("Basis", "bool"), loc), adamc@437: (L'.EBinop (s, (L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc) adamc@437: in adamc@437: ordEx ((L'.TFfi ("Basis", "time"), loc), adamc@437: boolBin "<", adamc@437: boolBin "<=") adamc@437: end adamc@389: adamc@286: | L.ECApp ((L.EFfi ("Basis", "show"), _), t) => adamc@286: let adamc@286: val t = monoType env t adamc@286: val s = (L'.TFfi ("Basis", "string"), loc) adamc@286: in adamc@286: ((L'.EAbs ("f", (L'.TFun (t, s), loc), (L'.TFun (t, s), loc), adamc@286: (L'.ERel 0, loc)), loc), fm) adamc@286: end adamc@286: | L.EFfi ("Basis", "show_int") => adamc@286: ((L'.EFfi ("Basis", "intToString"), loc), fm) adamc@286: | L.EFfi ("Basis", "show_float") => adamc@286: ((L'.EFfi ("Basis", "floatToString"), loc), fm) adamc@286: | L.EFfi ("Basis", "show_string") => adamc@286: let adamc@286: val s = (L'.TFfi ("Basis", "string"), loc) adamc@286: in adamc@286: ((L'.EAbs ("s", s, s, (L'.ERel 0, loc)), loc), fm) adamc@286: end adamc@286: | L.EFfi ("Basis", "show_bool") => adamc@286: ((L'.EFfi ("Basis", "boolToString"), loc), fm) adamc@436: | L.EFfi ("Basis", "show_time") => adamc@436: ((L'.EFfi ("Basis", "timeToString"), loc), fm) adamc@452: | L.ECApp ((L.EFfi ("Basis", "mkShow"), _), t) => adamc@452: let adamc@452: val t = monoType env t adamc@452: val b = (L'.TFfi ("Basis", "string"), loc) adamc@452: val dom = (L'.TFun (t, b), loc) adamc@452: in adamc@452: ((L'.EAbs ("f", dom, dom, adamc@452: (L'.ERel 0, loc)), loc), fm) adamc@452: end adamc@286: adamc@290: | L.ECApp ((L.EFfi ("Basis", "read"), _), t) => adamc@290: let adamc@290: val t = monoType env t adamc@290: val s = (L'.TFfi ("Basis", "string"), loc) adamc@290: in adamc@292: ((L'.EAbs ("f", readType (t, loc), readType' (t, loc), adamc@292: (L'.EField ((L'.ERel 0, loc), "Read"), loc)), loc), fm) adamc@292: end adamc@292: | L.ECApp ((L.EFfi ("Basis", "readError"), _), t) => adamc@292: let adamc@292: val t = monoType env t adamc@292: val s = (L'.TFfi ("Basis", "string"), loc) adamc@292: in adamc@292: ((L'.EAbs ("f", readType (t, loc), readErrType (t, loc), adamc@292: (L'.EField ((L'.ERel 0, loc), "ReadError"), loc)), loc), fm) adamc@290: end adamc@290: | L.EFfi ("Basis", "read_int") => adamc@292: let adamc@292: val t = (L'.TFfi ("Basis", "int"), loc) adamc@292: in adamc@292: ((L'.ERecord [("Read", (L'.EFfi ("Basis", "stringToInt"), loc), readType' (t, loc)), adamc@292: ("ReadError", (L'.EFfi ("Basis", "stringToInt_error"), loc), readErrType (t, loc))], adamc@292: loc), adamc@292: fm) adamc@292: end adamc@290: | L.EFfi ("Basis", "read_float") => adamc@292: let adamc@292: val t = (L'.TFfi ("Basis", "float"), loc) adamc@292: in adamc@292: ((L'.ERecord [("Read", (L'.EFfi ("Basis", "stringToFloat"), loc), readType' (t, loc)), adamc@292: ("ReadError", (L'.EFfi ("Basis", "stringToFloat_error"), loc), readErrType (t, loc))], adamc@292: loc), adamc@292: fm) adamc@292: end adamc@290: | L.EFfi ("Basis", "read_string") => adamc@290: let adamc@290: val s = (L'.TFfi ("Basis", "string"), loc) adamc@290: in adamc@292: ((L'.ERecord [("Read", (L'.EAbs ("s", s, (L'.TOption s, loc), adamc@292: (L'.ESome (s, (L'.ERel 0, loc)), loc)), loc), readType' (s, loc)), adamc@292: ("ReadError", (L'.EAbs ("s", s, s, (L'.ERel 0, loc)), loc), readErrType (s, loc))], loc), adamc@292: fm) adamc@290: end adamc@290: | L.EFfi ("Basis", "read_bool") => adamc@292: let adamc@292: val t = (L'.TFfi ("Basis", "bool"), loc) adamc@292: in adamc@292: ((L'.ERecord [("Read", (L'.EFfi ("Basis", "stringToBool"), loc), readType' (t, loc)), adamc@292: ("ReadError", (L'.EFfi ("Basis", "stringToBool_error"), loc), readErrType (t, loc))], adamc@292: loc), adamc@292: fm) adamc@292: end adamc@436: | L.EFfi ("Basis", "read_time") => adamc@436: let adamc@436: val t = (L'.TFfi ("Basis", "time"), loc) adamc@436: in adamc@436: ((L'.ERecord [("Read", (L'.EFfi ("Basis", "stringToTime"), loc), readType' (t, loc)), adamc@436: ("ReadError", (L'.EFfi ("Basis", "stringToTime_error"), loc), readErrType (t, loc))], adamc@436: loc), adamc@436: fm) adamc@436: end adamc@290: adamc@564: | L.EApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "return"), _), _), _), t), _), adamc@564: (L.EFfi ("Basis", "transaction_monad"), _)) => adamc@252: let adamc@252: val t = monoType env t adamc@252: in adamc@252: ((L'.EAbs ("x", t, adamc@252: (L'.TFun ((L'.TRecord [], loc), t), loc), adamc@252: (L'.EAbs ("_", (L'.TRecord [], loc), t, adamc@252: (L'.ERel 1, loc)), loc)), loc), fm) adamc@252: end adamc@564: | L.EApp ((L.ECApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "bind"), _), _), _), t1), _), t2), _), adamc@564: (L.EFfi ("Basis", "transaction_monad"), _)) => adamc@251: let adamc@251: val t1 = monoType env t1 adamc@251: val t2 = monoType env t2 adamc@251: val un = (L'.TRecord [], loc) adamc@252: val mt1 = (L'.TFun (un, t1), loc) adamc@252: val mt2 = (L'.TFun (un, t2), loc) adamc@251: in adamc@572: ((L'.EAbs ("m1", mt1, (L'.TFun ((L'.TFun (t1, mt2), loc), (L'.TFun (un, un), loc)), loc), adamc@572: (L'.EAbs ("m2", (L'.TFun (t1, mt2), loc), (L'.TFun (un, un), loc), adamc@252: (L'.EAbs ("_", un, un, adamc@252: (L'.ELet ("r", t1, (L'.EApp ((L'.ERel 2, loc), adamc@252: (L'.ERecord [], loc)), loc), adamc@252: (L'.EApp ( adamc@252: (L'.EApp ((L'.ERel 2, loc), (L'.ERel 0, loc)), loc), adamc@252: (L'.ERecord [], loc)), adamc@252: loc)), loc)), loc)), loc)), loc), adamc@251: fm) adamc@251: end adamc@251: adamc@565: | L.ECApp ((L.EFfi ("Basis", "source"), _), t) => adamc@565: let adamc@565: val t = monoType env t adamc@565: in adamc@577: ((L'.EAbs ("x", t, (L'.TFun ((L'.TRecord [], loc), (L'.TSource, loc)), loc), adamc@577: (L'.EAbs ("_", (L'.TRecord [], loc), (L'.TSource, loc), adamc@577: (L'.EFfiApp ("Basis", "new_client_source", adamc@590: [(L'.EJavaScript (L'.Source t, (L'.ERel 1, loc), NONE), loc)]), adamc@578: loc)), loc)), adamc@565: loc), adamc@565: fm) adamc@565: end adamc@575: | L.ECApp ((L.EFfi ("Basis", "set"), _), t) => adamc@575: let adamc@575: val t = monoType env t adamc@575: in adamc@577: ((L'.EAbs ("src", (L'.TSource, loc), adamc@575: (L'.TFun (t, (L'.TFun ((L'.TRecord [], loc), (L'.TRecord [], loc)), loc)), loc), adamc@575: (L'.EAbs ("v", t, (L'.TFun ((L'.TRecord [], loc), (L'.TRecord [], loc)), loc), adamc@575: (L'.EAbs ("_", (L'.TRecord [], loc), (L'.TRecord [], loc), adamc@575: (L'.EFfiApp ("Basis", "set_client_source", adamc@577: [(L'.ERel 2, loc), adamc@590: (L'.EJavaScript (L'.Source t, adamc@590: (L'.ERel 1, loc), NONE), loc)]), adamc@575: loc)), loc)), loc)), loc), adamc@575: fm) adamc@575: end adamc@601: | L.ECApp ((L.EFfi ("Basis", "get"), _), t) => adamc@601: let adamc@601: val t = monoType env t adamc@601: in adamc@601: ((L'.EAbs ("src", (L'.TSource, loc), adamc@601: (L'.TFun ((L'.TRecord [], loc), t), loc), adamc@601: (L'.EAbs ("_", (L'.TRecord [], loc), t, adamc@601: (L'.EFfiApp ("Basis", "get_client_source", adamc@601: [(L'.ERel 1, loc)]), adamc@601: loc)), loc)), loc), adamc@601: fm) adamc@601: end adamc@565: adamc@568: | L.EApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "return"), _), _), _), t), _), adamc@568: (L.EFfi ("Basis", "signal_monad"), _)) => adamc@568: let adamc@568: val t = monoType env t adamc@568: in adamc@568: ((L'.EAbs ("x", t, (L'.TSignal t, loc), adamc@568: (L'.ESignalReturn (L'.ERel 0, loc), loc)), loc), adamc@568: fm) adamc@568: end adamc@572: | L.EApp ((L.ECApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "bind"), _), _), _), t1), _), t2), _), adamc@572: (L.EFfi ("Basis", "signal_monad"), _)) => adamc@572: let adamc@572: val t1 = monoType env t1 adamc@572: val t2 = monoType env t2 adamc@572: val un = (L'.TRecord [], loc) adamc@572: val mt1 = (L'.TSignal t1, loc) adamc@572: val mt2 = (L'.TSignal t2, loc) adamc@572: in adamc@572: ((L'.EAbs ("m1", mt1, (L'.TFun ((L'.TFun (t1, mt2), loc), mt2), loc), adamc@572: (L'.EAbs ("m2", (L'.TFun (t1, mt2), loc), mt2, adamc@572: (L'.ESignalBind ((L'.ERel 1, loc), (L'.ERel 0, loc)), loc)), loc)), loc), adamc@572: fm) adamc@572: end adamc@574: | L.ECApp ((L.EFfi ("Basis", "signal"), _), t) => adamc@574: let adamc@574: val t = monoType env t adamc@574: in adamc@574: ((L'.EAbs ("x", (L'.TFfi ("Basis", "int"), loc), (L'.TSignal t, loc), adamc@574: (L'.ESignalSource (L'.ERel 0, loc), loc)), loc), adamc@574: fm) adamc@574: end adamc@568: adamc@462: | L.ECApp ((L.EFfi ("Basis", "getCookie"), _), t) => adamc@462: let adamc@462: val s = (L'.TFfi ("Basis", "string"), loc) adamc@462: val un = (L'.TRecord [], loc) adamc@462: val t = monoType env t adamc@462: in adamc@462: ((L'.EAbs ("c", s, (L'.TFun (un, s), loc), adamc@462: (L'.EAbs ("_", un, s, adamc@463: (L'.EUnurlify ((L'.EFfiApp ("Basis", "get_cookie", [(L'.ERel 1, loc)]), loc), adamc@463: t), adamc@463: loc)), loc)), loc), adamc@462: fm) adamc@462: end adamc@462: adamc@462: | L.ECApp ((L.EFfi ("Basis", "setCookie"), _), t) => adamc@462: let adamc@462: val s = (L'.TFfi ("Basis", "string"), loc) adamc@462: val un = (L'.TRecord [], loc) adamc@462: val t = monoType env t adamc@462: val (e, fm) = urlifyExp env fm ((L'.ERel 1, loc), t) adamc@462: in adamc@462: ((L'.EAbs ("c", s, (L'.TFun (t, (L'.TFun (un, un), loc)), loc), adamc@462: (L'.EAbs ("v", t, (L'.TFun (un, un), loc), adamc@462: (L'.EAbs ("_", un, un, adamc@466: (L'.EFfiApp ("Basis", "set_cookie", [(L'.EPrim (Prim.String (!urlPrefix)), adamc@466: loc), adamc@466: (L'.ERel 2, loc), adamc@466: e]), loc)), adamc@462: loc)), loc)), loc), adamc@462: fm) adamc@462: end adamc@462: adamc@307: | L.EFfiApp ("Basis", "dml", [e]) => adamc@307: let adamc@307: val (e, fm) = monoExp (env, st, fm) e adamc@307: in adamc@468: ((L'.EDml e, loc), adamc@307: fm) adamc@307: end adamc@308: adamc@307: | L.ECApp ((L.EFfi ("Basis", "insert"), _), fields) => adamc@307: (case monoType env (L.TRecord fields, loc) of adamc@307: (L'.TRecord fields, _) => adamc@307: let adamc@307: val s = (L'.TFfi ("Basis", "string"), loc) adamc@307: val fields = map (fn (x, _) => (x, s)) fields adamc@307: val rt = (L'.TRecord fields, loc) adamc@307: fun sc s = (L'.EPrim (Prim.String s), loc) adamc@307: in adamc@307: ((L'.EAbs ("tab", s, (L'.TFun (rt, s), loc), adamc@307: (L'.EAbs ("fs", rt, s, adamc@598: strcat [sc "INSERT INTO ", adamc@598: (L'.ERel 1, loc), adamc@598: sc " (", adamc@598: strcatComma (map (fn (x, _) => sc ("uw_" ^ x)) fields), adamc@598: sc ") VALUES (", adamc@598: strcatComma (map (fn (x, _) => adamc@598: (L'.EField ((L'.ERel 0, loc), adamc@598: x), loc)) fields), adamc@598: sc ")"]), loc)), loc), adamc@307: fm) adamc@307: end adamc@307: | _ => poly ()) adamc@307: adamc@403: | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "update"), _), _), _), changed) => adamc@308: (case monoType env (L.TRecord changed, loc) of adamc@308: (L'.TRecord changed, _) => adamc@308: let adamc@308: val s = (L'.TFfi ("Basis", "string"), loc) adamc@308: val changed = map (fn (x, _) => (x, s)) changed adamc@308: val rt = (L'.TRecord changed, loc) adamc@308: fun sc s = (L'.EPrim (Prim.String s), loc) adamc@308: in adamc@308: ((L'.EAbs ("fs", rt, (L'.TFun (s, (L'.TFun (s, s), loc)), loc), adamc@308: (L'.EAbs ("tab", s, (L'.TFun (s, s), loc), adamc@308: (L'.EAbs ("e", s, s, adamc@598: strcat [sc "UPDATE ", adamc@598: (L'.ERel 1, loc), adamc@598: sc " AS T SET ", adamc@598: strcatComma (map (fn (x, _) => adamc@598: strcat [sc ("uw_" ^ x adamc@598: ^ " = "), adamc@598: (L'.EField adamc@598: ((L'.ERel 2, adamc@598: loc), adamc@598: x), loc)]) adamc@598: changed), adamc@598: sc " WHERE ", adamc@598: (L'.ERel 0, loc)]), loc)), loc)), loc), adamc@308: fm) adamc@308: end adamc@308: | _ => poly ()) adamc@308: adamc@309: | L.ECApp ((L.EFfi ("Basis", "delete"), _), _) => adamc@309: let adamc@309: val s = (L'.TFfi ("Basis", "string"), loc) adamc@309: fun sc s = (L'.EPrim (Prim.String s), loc) adamc@309: in adamc@309: ((L'.EAbs ("tab", s, (L'.TFun (s, s), loc), adamc@309: (L'.EAbs ("e", s, s, adamc@598: strcat [sc "DELETE FROM ", adamc@598: (L'.ERel 1, loc), adamc@598: sc " AS T WHERE ", adamc@598: (L'.ERel 0, loc)]), loc)), loc), adamc@309: fm) adamc@309: end adamc@309: adamc@252: | L.ECApp ( adamc@252: (L.ECApp ( adamc@252: (L.ECApp ((L.EFfi ("Basis", "query"), _), (L.CRecord (_, tables), _)), _), adamc@252: exps), _), adamc@252: state) => adamc@252: (case monoType env (L.TRecord exps, loc) of adamc@252: (L'.TRecord exps, _) => adamc@252: let adamc@252: val tables = map (fn ((L.CName x, _), xts) => adamc@252: (case monoType env (L.TRecord xts, loc) of adamc@252: (L'.TRecord xts, _) => SOME (x, xts) adamc@252: | _ => NONE) adamc@252: | _ => NONE) tables adamc@252: in adamc@252: if List.exists (fn x => x = NONE) tables then adamc@252: poly () adamc@252: else adamc@252: let adamc@252: val tables = List.mapPartial (fn x => x) tables adamc@252: val state = monoType env state adamc@252: val s = (L'.TFfi ("Basis", "string"), loc) adamc@252: val un = (L'.TRecord [], loc) adamc@252: adamc@252: val rt = exps @ map (fn (x, xts) => (x, (L'.TRecord xts, loc))) tables adamc@252: val ft = (L'.TFun ((L'.TRecord rt, loc), adamc@252: (L'.TFun (state, adamc@252: (L'.TFun (un, state), loc)), adamc@252: loc)), loc) adamc@252: adamc@267: val body' = (L'.EApp ( adamc@267: (L'.EApp ( adamc@267: (L'.EApp ((L'.ERel 4, loc), adamc@267: (L'.ERel 1, loc)), loc), adamc@267: (L'.ERel 0, loc)), loc), adamc@267: (L'.ERecord [], loc)), loc) adamc@252: adamc@252: val body = (L'.EQuery {exps = exps, adamc@252: tables = tables, adamc@252: state = state, adamc@252: query = (L'.ERel 3, loc), adamc@252: body = body', adamc@252: initial = (L'.ERel 1, loc)}, adamc@252: loc) adamc@252: in adamc@252: ((L'.EAbs ("q", s, (L'.TFun (ft, (L'.TFun (state, (L'.TFun (un, state), loc)), loc)), loc), adamc@252: (L'.EAbs ("f", ft, (L'.TFun (state, (L'.TFun (un, state), loc)), loc), adamc@252: (L'.EAbs ("i", state, (L'.TFun (un, state), loc), adamc@252: (L'.EAbs ("_", un, state, adamc@252: body), loc)), loc)), loc)), loc), fm) adamc@252: end adamc@252: end adamc@252: | _ => poly ()) adamc@252: adamc@252: | L.ECApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_query"), _), _), _), _), _), _) => adamc@252: let adamc@252: fun sc s = (L'.EPrim (Prim.String s), loc) adamc@252: val s = (L'.TFfi ("Basis", "string"), loc) adamc@252: fun gf s = (L'.EField ((L'.ERel 0, loc), s), loc) adamc@252: in adamc@252: ((L'.EAbs ("r", adamc@252: (L'.TRecord [("Rows", s), ("OrderBy", s), ("Limit", s), ("Offset", s)], loc), adamc@252: s, adamc@598: strcat [gf "Rows", adamc@598: (L'.ECase (gf "OrderBy", adamc@598: [((L'.PPrim (Prim.String ""), loc), sc ""), adamc@598: ((L'.PWild, loc), adamc@598: strcat [sc " ORDER BY ", adamc@598: gf "OrderBy"])], adamc@598: {disc = s, result = s}), loc), adamc@598: gf "Limit", adamc@598: gf "Offset"]), loc), fm) adamc@252: end adamc@252: adamc@252: | L.ECApp ( adamc@252: (L.ECApp ( adamc@252: (L.ECApp ( adamc@252: (L.ECApp ( adamc@252: (L.EFfi ("Basis", "sql_query1"), _), adamc@252: (L.CRecord (_, tables), _)), _), adamc@252: (L.CRecord (_, grouped), _)), _), adamc@252: (L.CRecord (_, stables), _)), _), adamc@252: sexps) => adamc@252: let adamc@252: fun sc s = (L'.EPrim (Prim.String s), loc) adamc@252: val s = (L'.TFfi ("Basis", "string"), loc) adamc@252: val un = (L'.TRecord [], loc) adamc@252: fun gf s = (L'.EField ((L'.ERel 0, loc), s), loc) adamc@252: adamc@252: fun doTables tables = adamc@252: let adamc@252: val tables = map (fn ((L.CName x, _), xts) => adamc@252: (case monoType env (L.TRecord xts, loc) of adamc@252: (L'.TRecord xts, _) => SOME (x, xts) adamc@252: | _ => NONE) adamc@252: | _ => NONE) tables adamc@252: in adamc@252: if List.exists (fn x => x = NONE) tables then adamc@252: NONE adamc@252: else adamc@260: let adamc@260: val tables = List.mapPartial (fn x => x) tables adamc@260: val tables = ListMergeSort.sort adamc@260: (fn ((x, _), (y, _)) => String.compare (x, y) = GREATER) adamc@260: tables adamc@260: val tables = map (fn (x, xts) => adamc@260: (x, ListMergeSort.sort adamc@260: (fn ((x, _), (y, _)) => String.compare (x, y) = GREATER) adamc@260: xts)) tables adamc@260: in adamc@260: SOME tables adamc@260: end adamc@252: end adamc@252: in adamc@252: case (doTables tables, doTables grouped, doTables stables, monoType env (L.TRecord sexps, loc)) of adamc@252: (SOME tables, SOME grouped, SOME stables, (L'.TRecord sexps, _)) => adamc@441: let adamc@441: val sexps = ListMergeSort.sort adamc@441: (fn ((x, _), (y, _)) => String.compare (x, y) = GREATER) sexps adamc@441: in adamc@441: ((L'.EAbs ("r", adamc@441: (L'.TRecord [("From", (L'.TRecord (map (fn (x, _) => (x, s)) tables), loc)), adamc@441: ("Where", s), adamc@441: ("GroupBy", un), adamc@441: ("Having", s), adamc@441: ("SelectFields", un), adamc@441: ("SelectExps", (L'.TRecord (map (fn (x, _) => (x, s)) sexps), loc))], adamc@441: loc), adamc@441: s, adamc@598: strcat [sc "SELECT ", adamc@598: strcatComma (map (fn (x, t) => adamc@598: strcat [ adamc@598: (L'.EField (gf "SelectExps", x), loc), adamc@598: sc (" AS _" ^ x) adamc@598: ]) sexps adamc@598: @ map (fn (x, xts) => adamc@598: strcatComma adamc@598: (map (fn (x', _) => adamc@598: sc (x ^ ".uw_" ^ x')) adamc@598: xts)) stables), adamc@598: sc " FROM ", adamc@598: strcatComma (map (fn (x, _) => strcat [(L'.EField (gf "From", x), loc), adamc@598: sc (" AS " ^ x)]) tables), adamc@598: (L'.ECase (gf "Where", adamc@598: [((L'.PPrim (Prim.String "TRUE"), loc), adamc@598: sc ""), adamc@598: ((L'.PWild, loc), adamc@598: strcat [sc " WHERE ", gf "Where"])], adamc@598: {disc = s, adamc@598: result = s}), loc), adamc@598: adamc@598: if List.all (fn (x, xts) => adamc@598: case List.find (fn (x', _) => x' = x) grouped of adamc@598: NONE => List.null xts adamc@598: | SOME (_, xts') => adamc@598: List.all (fn (x, _) => adamc@598: List.exists (fn (x', _) => x' = x) adamc@598: xts') xts) tables then adamc@598: sc "" adamc@598: else adamc@598: strcat [ adamc@598: sc " GROUP BY ", adamc@598: strcatComma (map (fn (x, xts) => adamc@598: strcatComma adamc@598: (map (fn (x', _) => adamc@598: sc (x ^ ".uw_" ^ x')) adamc@598: xts)) grouped) adamc@598: ], adamc@259: adamc@598: (L'.ECase (gf "Having", adamc@598: [((L'.PPrim (Prim.String "TRUE"), loc), adamc@598: sc ""), adamc@598: ((L'.PWild, loc), adamc@598: strcat [sc " HAVING ", gf "Having"])], adamc@598: {disc = s, adamc@598: result = s}), loc) adamc@441: ]), loc), adamc@441: fm) adamc@441: end adamc@252: | _ => poly () adamc@252: end adamc@252: adamc@252: | L.ECApp ( adamc@252: (L.ECApp ( adamc@252: (L.ECApp ( adamc@252: (L.ECApp ( adamc@252: (L.EFfi ("Basis", "sql_inject"), _), adamc@252: _), _), adamc@252: _), _), adamc@252: _), _), adamc@252: t) => adamc@252: let adamc@252: val t = monoType env t adamc@252: val s = (L'.TFfi ("Basis", "string"), loc) adamc@252: in adamc@252: ((L'.EAbs ("f", (L'.TFun (t, s), loc), (L'.TFun (t, s), loc), adamc@252: (L'.ERel 0, loc)), loc), fm) adamc@252: end adamc@252: adamc@253: | L.EFfi ("Basis", "sql_int") => adamc@253: ((L'.EAbs ("x", (L'.TFfi ("Basis", "int"), loc), (L'.TFfi ("Basis", "string"), loc), adamc@253: (L'.EFfiApp ("Basis", "sqlifyInt", [(L'.ERel 0, loc)]), loc)), loc), adamc@253: fm) adamc@253: | L.EFfi ("Basis", "sql_float") => adamc@253: ((L'.EAbs ("x", (L'.TFfi ("Basis", "float"), loc), (L'.TFfi ("Basis", "string"), loc), adamc@253: (L'.EFfiApp ("Basis", "sqlifyFloat", [(L'.ERel 0, loc)]), loc)), loc), adamc@253: fm) adamc@253: | L.EFfi ("Basis", "sql_bool") => adamc@253: ((L'.EAbs ("x", (L'.TFfi ("Basis", "bool"), loc), (L'.TFfi ("Basis", "string"), loc), adamc@253: (L'.EFfiApp ("Basis", "sqlifyBool", [(L'.ERel 0, loc)]), loc)), loc), adamc@253: fm) adamc@253: | L.EFfi ("Basis", "sql_string") => adamc@253: ((L'.EAbs ("x", (L'.TFfi ("Basis", "string"), loc), (L'.TFfi ("Basis", "string"), loc), adamc@253: (L'.EFfiApp ("Basis", "sqlifyString", [(L'.ERel 0, loc)]), loc)), loc), adamc@253: fm) adamc@439: | L.EFfi ("Basis", "sql_time") => adamc@439: ((L'.EAbs ("x", (L'.TFfi ("Basis", "time"), loc), (L'.TFfi ("Basis", "string"), loc), adamc@439: (L'.EFfiApp ("Basis", "sqlifyTime", [(L'.ERel 0, loc)]), loc)), loc), adamc@439: fm) adamc@467: | L.EFfi ("Basis", "sql_option_int") => adamc@467: ((L'.EAbs ("x", (L'.TOption (L'.TFfi ("Basis", "int"), loc), loc), (L'.TFfi ("Basis", "string"), loc), adamc@467: (L'.EFfiApp ("Basis", "sqlifyIntN", [(L'.ERel 0, loc)]), loc)), loc), adamc@467: fm) adamc@467: | L.EFfi ("Basis", "sql_option_float") => adamc@467: ((L'.EAbs ("x", (L'.TOption (L'.TFfi ("Basis", "float"), loc), loc), (L'.TFfi ("Basis", "string"), loc), adamc@467: (L'.EFfiApp ("Basis", "sqlifyFloatN", [(L'.ERel 0, loc)]), loc)), loc), adamc@467: fm) adamc@467: | L.EFfi ("Basis", "sql_option_bool") => adamc@467: ((L'.EAbs ("x", (L'.TOption (L'.TFfi ("Basis", "bool"), loc), loc), (L'.TFfi ("Basis", "string"), loc), adamc@467: (L'.EFfiApp ("Basis", "sqlifyBoolN", [(L'.ERel 0, loc)]), loc)), loc), adamc@467: fm) adamc@467: | L.EFfi ("Basis", "sql_option_string") => adamc@467: ((L'.EAbs ("x", (L'.TOption (L'.TFfi ("Basis", "string"), loc), loc), (L'.TFfi ("Basis", "string"), loc), adamc@467: (L'.EFfiApp ("Basis", "sqlifyStringN", [(L'.ERel 0, loc)]), loc)), loc), adamc@467: fm) adamc@467: | L.EFfi ("Basis", "sql_option_time") => adamc@467: ((L'.EAbs ("x", (L'.TOption (L'.TFfi ("Basis", "time"), loc), loc), (L'.TFfi ("Basis", "string"), loc), adamc@467: (L'.EFfiApp ("Basis", "sqlifyTimeN", [(L'.ERel 0, loc)]), loc)), loc), adamc@467: fm) adamc@253: adamc@252: | L.ECApp ((L.EFfi ("Basis", "sql_subset"), _), _) => adamc@252: ((L'.ERecord [], loc), fm) adamc@252: | L.ECApp ((L.EFfi ("Basis", "sql_subset_all"), _), _) => adamc@252: ((L'.ERecord [], loc), fm) adamc@252: adamc@252: | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_order_by_Nil"), _), _), _), _) => adamc@252: ((L'.EPrim (Prim.String ""), loc), fm) adamc@261: | L.ECApp ( adamc@261: (L.ECApp ( adamc@261: (L.ECApp ( adamc@261: (L.EFfi ("Basis", "sql_order_by_Cons"), _), adamc@261: _), _), adamc@261: _), _), adamc@261: _) => adamc@261: let adamc@261: val s = (L'.TFfi ("Basis", "string"), loc) adamc@261: fun sc s = (L'.EPrim (Prim.String s), loc) adamc@261: in adamc@268: ((L'.EAbs ("e1", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc), adamc@268: (L'.EAbs ("d", s, (L'.TFun (s, s), loc), adamc@268: (L'.EAbs ("e2", s, s, adamc@268: (L'.ECase ((L'.ERel 0, loc), adamc@268: [((L'.PPrim (Prim.String ""), loc), adamc@598: strcat [(L'.ERel 2, loc), adamc@598: (L'.ERel 1, loc)]), adamc@268: ((L'.PWild, loc), adamc@598: strcat [(L'.ERel 2, loc), adamc@598: (L'.ERel 1, loc), adamc@598: sc ", ", adamc@598: (L'.ERel 0, loc)])], adamc@268: {disc = s, result = s}), loc)), loc)), loc)), loc), adamc@261: fm) adamc@261: end adamc@252: adamc@252: | L.EFfi ("Basis", "sql_no_limit") => adamc@252: ((L'.EPrim (Prim.String ""), loc), fm) adamc@262: | L.EFfiApp ("Basis", "sql_limit", [e]) => adamc@262: let adamc@262: val (e, fm) = monoExp (env, st, fm) e adamc@262: in adamc@598: (strcat [ adamc@262: (L'.EPrim (Prim.String " LIMIT "), loc), adamc@262: (L'.EFfiApp ("Basis", "sqlifyInt", [e]), loc) adamc@262: ], adamc@262: fm) adamc@262: end adamc@262: adamc@252: | L.EFfi ("Basis", "sql_no_offset") => adamc@252: ((L'.EPrim (Prim.String ""), loc), fm) adamc@263: | L.EFfiApp ("Basis", "sql_offset", [e]) => adamc@263: let adamc@263: val (e, fm) = monoExp (env, st, fm) e adamc@263: in adamc@598: (strcat [ adamc@263: (L'.EPrim (Prim.String " OFFSET "), loc), adamc@263: (L'.EFfiApp ("Basis", "sqlifyInt", [e]), loc) adamc@263: ], adamc@263: fm) adamc@263: end adamc@253: adamc@559: | L.ECApp ((L.EFfi ("Basis", "sql_eq"), _), _) => adamc@253: ((L'.EPrim (Prim.String "="), loc), fm) adamc@559: | L.ECApp ((L.EFfi ("Basis", "sql_ne"), _), _) => adamc@253: ((L'.EPrim (Prim.String "<>"), loc), fm) adamc@559: | L.ECApp ((L.EFfi ("Basis", "sql_lt"), _), _) => adamc@253: ((L'.EPrim (Prim.String "<"), loc), fm) adamc@559: | L.ECApp ((L.EFfi ("Basis", "sql_le"), _), _) => adamc@253: ((L'.EPrim (Prim.String "<="), loc), fm) adamc@559: | L.ECApp ((L.EFfi ("Basis", "sql_gt"), _), _) => adamc@253: ((L'.EPrim (Prim.String ">"), loc), fm) adamc@559: | L.ECApp ((L.EFfi ("Basis", "sql_ge"), _), _) => adamc@253: ((L'.EPrim (Prim.String ">="), loc), fm) adamc@253: adamc@559: | L.ECApp ((L.EFfi ("Basis", "sql_plus"), _), _) => adamc@559: ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc), adamc@559: (L'.EPrim (Prim.String "+"), loc)), loc), fm) adamc@559: | L.ECApp ((L.EFfi ("Basis", "sql_minus"), _), _) => adamc@559: ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc), adamc@559: (L'.EPrim (Prim.String "-"), loc)), loc), fm) adamc@559: | L.ECApp ((L.EFfi ("Basis", "sql_times"), _), _) => adamc@559: ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc), adamc@559: (L'.EPrim (Prim.String "*"), loc)), loc), fm) adamc@559: | L.ECApp ((L.EFfi ("Basis", "sql_div"), _), _) => adamc@559: ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc), adamc@559: (L'.EPrim (Prim.String "/"), loc)), loc), fm) adamc@559: | L.EFfi ("Basis", "sql_mod") => adamc@559: ((L'.EPrim (Prim.String "%"), loc), fm) adamc@559: adamc@253: | L.ECApp ( adamc@253: (L.ECApp ( adamc@253: (L.ECApp ( adamc@253: (L.ECApp ( adamc@254: (L.ECApp ( adamc@264: (L.EFfi ("Basis", "sql_unary"), _), adamc@264: _), _), adamc@264: _), _), adamc@264: _), _), adamc@264: _), _), adamc@264: _) => adamc@264: let adamc@264: val s = (L'.TFfi ("Basis", "string"), loc) adamc@264: fun sc s = (L'.EPrim (Prim.String s), loc) adamc@264: in adamc@264: ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc), adamc@264: (L'.EAbs ("e1", s, (L'.TFun (s, s), loc), adamc@598: strcat [sc "(", adamc@598: (L'.ERel 1, loc), adamc@598: sc " ", adamc@598: (L'.ERel 0, loc), adamc@598: sc ")"]), loc)), loc), adamc@264: fm) adamc@264: end adamc@264: | L.EFfi ("Basis", "sql_not") => ((L'.EPrim (Prim.String "NOT"), loc), fm) adamc@559: | L.ECApp ((L.EFfi ("Basis", "sql_neg"), _), _) => adamc@559: ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc), adamc@559: (L'.EPrim (Prim.String "-"), loc)), loc), fm) adamc@264: adamc@264: | L.ECApp ( adamc@264: (L.ECApp ( adamc@264: (L.ECApp ( adamc@264: (L.ECApp ( adamc@264: (L.ECApp ( adamc@254: (L.ECApp ( adamc@254: (L.EFfi ("Basis", "sql_binary"), _), adamc@254: _), _), adamc@254: _), _), adamc@254: _), _), adamc@254: _), _), adamc@254: _), _), adamc@254: _) => adamc@254: let adamc@254: val s = (L'.TFfi ("Basis", "string"), loc) adamc@254: fun sc s = (L'.EPrim (Prim.String s), loc) adamc@254: in adamc@254: ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc), adamc@254: (L'.EAbs ("e1", s, (L'.TFun (s, s), loc), adamc@254: (L'.EAbs ("e2", s, s, adamc@598: strcat [sc "(", adamc@598: (L'.ERel 1, loc), adamc@598: sc " ", adamc@598: (L'.ERel 2, loc), adamc@598: sc " ", adamc@598: (L'.ERel 0, loc), adamc@598: sc ")"]), loc)), loc)), loc), adamc@254: fm) adamc@254: end adamc@254: | L.EFfi ("Basis", "sql_and") => ((L'.EPrim (Prim.String "AND"), loc), fm) adamc@254: | L.EFfi ("Basis", "sql_or") => ((L'.EPrim (Prim.String "OR"), loc), fm) adamc@254: adamc@254: | L.ECApp ( adamc@254: (L.ECApp ( adamc@254: (L.ECApp ( adamc@254: (L.ECApp ( adamc@253: (L.ECApp ( adamc@253: (L.ECApp ( adamc@253: (L.ECApp ( adamc@253: (L.EFfi ("Basis", "sql_field"), _), adamc@253: _), _), adamc@253: _), _), adamc@253: _), _), adamc@253: _), _), adamc@253: _), _), adamc@253: (L.CName tab, _)), _), adamc@311: (L.CName field, _)) => ((L'.EPrim (Prim.String (tab ^ ".uw_" ^ field)), loc), fm) adamc@260: adamc@260: | L.ECApp ( adamc@260: (L.ECApp ( adamc@260: (L.ECApp ( adamc@260: (L.ECApp ( adamc@261: (L.ECApp ( adamc@261: (L.EFfi ("Basis", "sql_exp"), _), adamc@261: _), _), adamc@261: _), _), adamc@261: _), _), adamc@261: _), _), adamc@261: (L.CName nm, _)) => ((L'.EPrim (Prim.String ("_" ^ nm)), loc), fm) adamc@261: adamc@261: | L.ECApp ( adamc@261: (L.ECApp ( adamc@261: (L.ECApp ( adamc@261: (L.ECApp ( adamc@260: (L.EFfi ("Basis", "sql_relop"), _), adamc@260: _), _), adamc@260: _), _), adamc@260: _), _), adamc@260: _) => adamc@260: let adamc@260: val s = (L'.TFfi ("Basis", "string"), loc) adamc@260: fun sc s = (L'.EPrim (Prim.String s), loc) adamc@260: in adamc@260: ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc), adamc@260: (L'.EAbs ("e1", s, (L'.TFun (s, s), loc), adamc@260: (L'.EAbs ("e2", s, s, adamc@598: strcat [sc "((", adamc@598: (L'.ERel 1, loc), adamc@598: sc ") ", adamc@598: (L'.ERel 2, loc), adamc@598: sc " (", adamc@598: (L'.ERel 0, loc), adamc@598: sc "))"]), loc)), loc)), loc), adamc@260: fm) adamc@260: end adamc@260: adamc@260: | L.EFfi ("Basis", "sql_union") => ((L'.EPrim (Prim.String "UNION"), loc), fm) adamc@260: | L.EFfi ("Basis", "sql_intersect") => ((L'.EPrim (Prim.String "INTERSECT"), loc), fm) adamc@260: | L.EFfi ("Basis", "sql_except") => ((L'.EPrim (Prim.String "EXCEPT"), loc), fm) adamc@260: adamc@265: | L.ECApp ( adamc@265: (L.ECApp ( adamc@265: (L.ECApp ( adamc@265: (L.EFfi ("Basis", "sql_count"), _), adamc@265: _), _), adamc@265: _), _), adamc@544: _) => ((L'.EPrim (Prim.String "COUNT(*)"), loc), adamc@265: fm) adamc@266: adamc@266: | L.ECApp ( adamc@266: (L.ECApp ( adamc@266: (L.ECApp ( adamc@266: (L.ECApp ( adamc@266: (L.EFfi ("Basis", "sql_aggregate"), _), adamc@266: _), _), adamc@266: _), _), adamc@266: _), _), adamc@266: _) => adamc@266: let adamc@266: val s = (L'.TFfi ("Basis", "string"), loc) adamc@266: fun sc s = (L'.EPrim (Prim.String s), loc) adamc@266: in adamc@266: ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc), adamc@266: (L'.EAbs ("e1", s, (L'.TFun (s, s), loc), adamc@598: strcat [(L'.ERel 1, loc), adamc@598: sc "(", adamc@598: (L'.ERel 0, loc), adamc@598: sc ")"]), loc)), loc), adamc@266: fm) adamc@266: end adamc@266: adamc@266: | L.EFfi ("Basis", "sql_summable_int") => ((L'.ERecord [], loc), fm) adamc@266: | L.EFfi ("Basis", "sql_summable_float") => ((L'.ERecord [], loc), fm) adamc@266: | L.ECApp ((L.EFfi ("Basis", "sql_avg"), _), _) => adamc@266: ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc), adamc@266: (L'.EPrim (Prim.String "AVG"), loc)), loc), adamc@266: fm) adamc@266: | L.ECApp ((L.EFfi ("Basis", "sql_sum"), _), _) => adamc@266: ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc), adamc@266: (L'.EPrim (Prim.String "SUM"), loc)), loc), adamc@266: fm) adamc@266: adamc@559: | L.EFfi ("Basis", "sql_arith_int") => ((L'.ERecord [], loc), fm) adamc@559: | L.EFfi ("Basis", "sql_arith_float") => ((L'.ERecord [], loc), fm) adamc@559: adamc@266: | L.EFfi ("Basis", "sql_maxable_int") => ((L'.ERecord [], loc), fm) adamc@266: | L.EFfi ("Basis", "sql_maxable_float") => ((L'.ERecord [], loc), fm) adamc@266: | L.EFfi ("Basis", "sql_maxable_string") => ((L'.ERecord [], loc), fm) adamc@266: | L.ECApp ((L.EFfi ("Basis", "sql_max"), _), _) => adamc@266: ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc), adamc@266: (L'.EPrim (Prim.String "MAX"), loc)), loc), adamc@266: fm) adamc@266: | L.ECApp ((L.EFfi ("Basis", "sql_min"), _), _) => adamc@266: ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TFfi ("Basis", "string"), loc), adamc@266: (L'.EPrim (Prim.String "MIN"), loc)), loc), adamc@266: fm) adamc@266: adamc@268: | L.EFfi ("Basis", "sql_asc") => ((L'.EPrim (Prim.String ""), loc), fm) adamc@268: | L.EFfi ("Basis", "sql_desc") => ((L'.EPrim (Prim.String " DESC"), loc), fm) adamc@268: adamc@441: | L.ECApp ( adamc@441: (L.ECApp ( adamc@441: (L.ECApp ( adamc@441: (L.ECApp ( adamc@441: (L.EFfi ("Basis", "sql_nfunc"), _), adamc@441: _), _), adamc@441: _), _), adamc@441: _), _), adamc@441: _) => adamc@441: let adamc@441: val s = (L'.TFfi ("Basis", "string"), loc) adamc@441: fun sc s = (L'.EPrim (Prim.String s), loc) adamc@441: in adamc@441: ((L'.EAbs ("s", s, s, (L'.ERel 0, loc)), loc), adamc@441: fm) adamc@441: end adamc@441: | L.EFfi ("Basis", "sql_current_timestamp") => ((L'.EPrim (Prim.String "CURRENT_TIMESTAMP"), loc), fm) adamc@441: adamc@470: | (L.ECApp ( adamc@470: (L.ECApp ( adamc@470: (L.ECApp ( adamc@470: (L.ECApp ( adamc@470: (L.EFfi ("Basis", "sql_is_null"), _), _), adamc@470: _), _), adamc@470: _), _), adamc@470: _), _)) => adamc@470: let adamc@470: val s = (L'.TFfi ("Basis", "string"), loc) adamc@470: fun sc s = (L'.EPrim (Prim.String s), loc) adamc@470: in adamc@470: ((L'.EAbs ("s", s, s, adamc@598: strcat [sc "(", adamc@598: (L'.ERel 0, loc), adamc@598: sc " IS NULL)"]), loc), adamc@470: fm) adamc@470: end adamc@470: adamc@338: | L.EFfiApp ("Basis", "nextval", [e]) => adamc@338: let adamc@338: val (e, fm) = monoExp (env, st, fm) e adamc@338: in adamc@465: ((L'.ENextval e, loc), fm) adamc@338: end adamc@338: adamc@139: | L.EApp ( adamc@139: (L.ECApp ( adamc@141: (L.ECApp ((L.EFfi ("Basis", "cdata"), _), _), _), adamc@139: _), _), adamc@179: se) => adamc@179: let adamc@179: val (se, fm) = monoExp (env, st, fm) se adamc@179: in adamc@179: ((L'.EFfiApp ("Basis", "htmlifyString", [se]), loc), fm) adamc@179: end adamc@179: adamc@95: | L.EApp ( adamc@95: (L.EApp ( adamc@95: (L.ECApp ( adamc@95: (L.ECApp ( adamc@95: (L.ECApp ( adamc@139: (L.ECApp ( adamc@140: (L.EFfi ("Basis", "join"), adamc@139: _), _), _), adamc@139: _), _), adamc@95: _), _), adamc@95: _), _), adamc@95: xml1), _), adamc@179: xml2) => adamc@179: let adamc@179: val (xml1, fm) = monoExp (env, st, fm) xml1 adamc@179: val (xml2, fm) = monoExp (env, st, fm) xml2 adamc@179: in adamc@179: ((L'.EStrcat (xml1, xml2), loc), fm) adamc@179: end adamc@95: adamc@95: | L.EApp ( adamc@95: (L.EApp ( adamc@104: (L.EApp ( adamc@95: (L.ECApp ( adamc@104: (L.ECApp ( adamc@104: (L.ECApp ( adamc@104: (L.ECApp ( adamc@139: (L.ECApp ( adamc@139: (L.ECApp ( adamc@139: (L.ECApp ( adamc@139: (L.ECApp ( adamc@139: (L.EFfi ("Basis", "tag"), adamc@139: _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), adamc@104: attrs), _), adamc@95: tag), _), adamc@95: xml) => adamc@95: let adamc@140: fun getTag' (e, _) = adamc@140: case e of adamc@143: L.EFfi ("Basis", tag) => (tag, []) adamc@143: | L.ECApp (e, t) => let adamc@143: val (tag, ts) = getTag' e adamc@143: in adamc@143: (tag, ts @ [t]) adamc@143: end adamc@140: | _ => (E.errorAt loc "Non-constant XML tag"; adamc@140: Print.eprefaces' [("Expression", CorePrint.p_exp env tag)]; adamc@143: ("", [])) adamc@140: adamc@95: fun getTag (e, _) = adamc@95: case e of adamc@143: L.EFfiApp ("Basis", tag, [(L.ERecord [], _)]) => (tag, []) adamc@140: | L.EApp (e, (L.ERecord [], _)) => getTag' e adamc@95: | _ => (E.errorAt loc "Non-constant XML tag"; adamc@95: Print.eprefaces' [("Expression", CorePrint.p_exp env tag)]; adamc@143: ("", [])) adamc@95: adamc@143: val (tag, targs) = getTag tag adamc@95: adamc@179: val (attrs, fm) = monoExp (env, st, fm) attrs adamc@598: val attrs = case #1 attrs of adamc@598: L'.ERecord xes => xes adamc@598: | _ => raise Fail "Non-record attributes!" adamc@104: adamc@606: fun lowercaseFirst "" = "" adamc@606: | lowercaseFirst s = String.str (Char.toLower (String.sub (s, 0))) adamc@606: ^ String.extract (s, 1, NONE) adamc@606: adamc@143: fun tagStart tag = adamc@598: let adamc@598: val s = (L'.EPrim (Prim.String (String.concat ["<", tag])), loc) adamc@598: in adamc@598: foldl (fn (("Action", _, _), acc) => acc adamc@598: | (("Source", _, _), acc) => acc adamc@598: | ((x, e, t), (s, fm)) => adamc@598: case t of adamc@598: (L'.TFfi ("Basis", "bool"), _) => adamc@598: let adamc@598: val s' = " " ^ lowercaseFirst x adamc@598: in adamc@598: ((L'.ECase (e, adamc@598: [((L'.PCon (L'.Enum, adamc@598: L'.PConFfi {mod = "Basis", adamc@598: datatyp = "bool", adamc@598: con = "True", adamc@598: arg = NONE}, adamc@598: NONE), loc), adamc@598: (L'.EStrcat (s, adamc@598: (L'.EPrim (Prim.String s'), loc)), loc)), adamc@598: ((L'.PCon (L'.Enum, adamc@598: L'.PConFfi {mod = "Basis", adamc@598: datatyp = "bool", adamc@598: con = "False", adamc@598: arg = NONE}, adamc@598: NONE), loc), adamc@598: s)], adamc@598: {disc = (L'.TFfi ("Basis", "bool"), loc), adamc@598: result = (L'.TFfi ("Basis", "string"), loc)}), loc), adamc@598: fm) adamc@598: end adamc@598: | (L'.TFun _, _) => adamc@598: let adamc@598: val s' = " " ^ lowercaseFirst x ^ "='" adamc@598: in adamc@598: ((L'.EStrcat (s, adamc@598: (L'.EStrcat ( adamc@598: (L'.EPrim (Prim.String s'), loc), adamc@598: (L'.EStrcat ( adamc@598: (L'.EJavaScript (L'.Attribute, e, NONE), loc), adamc@598: (L'.EPrim (Prim.String "'"), loc)), loc)), adamc@598: loc)), loc), adamc@598: fm) adamc@598: end adamc@598: | _ => adamc@598: let adamc@598: val fooify = adamc@598: case x of adamc@598: "Href" => urlifyExp adamc@598: | "Link" => urlifyExp adamc@598: | _ => attrifyExp adamc@120: adamc@598: val xp = " " ^ lowercaseFirst x ^ "=\"" adamc@179: adamc@598: val (e, fm) = fooify env fm (e, t) adamc@598: in adamc@598: ((L'.EStrcat (s, adamc@598: (L'.EStrcat ((L'.EPrim (Prim.String xp), loc), adamc@598: (L'.EStrcat (e, adamc@598: (L'.EPrim (Prim.String "\""), adamc@598: loc)), adamc@598: loc)), adamc@598: loc)), loc), adamc@598: fm) adamc@598: end) adamc@598: (s, fm) attrs adamc@598: end adamc@104: adamc@143: fun input typ = adamc@143: case targs of adamc@155: [_, (L.CName name, _)] => adamc@179: let adamc@179: val (ts, fm) = tagStart "input" adamc@179: in adamc@179: ((L'.EStrcat (ts, adamc@179: (L'.EPrim (Prim.String (" type=\"" ^ typ ^ "\" name=\"" ^ name ^ "\"/>")), adamc@179: loc)), loc), fm) adamc@179: end adamc@143: | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs); adamc@153: raise Fail "No name passed to input tag") adamc@104: adamc@565: fun normal (tag, extra, extraInner) = adamc@143: let adamc@179: val (tagStart, fm) = tagStart tag adamc@152: val tagStart = case extra of adamc@152: NONE => tagStart adamc@152: | SOME extra => (L'.EStrcat (tagStart, extra), loc) adamc@152: adamc@143: fun normal () = adamc@179: let adamc@179: val (xml, fm) = monoExp (env, st, fm) xml adamc@565: val xml = case extraInner of adamc@565: NONE => xml adamc@565: | SOME ei => (L'.EStrcat (ei, xml), loc) adamc@179: in adamc@179: ((L'.EStrcat ((L'.EStrcat (tagStart, (L'.EPrim (Prim.String ">"), loc)), loc), adamc@179: (L'.EStrcat (xml, adamc@179: (L'.EPrim (Prim.String (String.concat [""])), adamc@179: loc)), loc)), adamc@179: loc), adamc@179: fm) adamc@179: end adamc@143: in adamc@143: case xml of adamc@143: (L.EApp ((L.ECApp ( adamc@143: (L.ECApp ((L.EFfi ("Basis", "cdata"), _), adamc@143: _), _), adamc@143: _), _), adamc@143: (L.EPrim (Prim.String s), _)), _) => adamc@143: if CharVector.all Char.isSpace s then adamc@179: ((L'.EStrcat (tagStart, (L'.EPrim (Prim.String "/>"), loc)), loc), fm) adamc@143: else adamc@143: normal () adamc@143: | _ => normal () adamc@143: end adamc@606: adamc@606: fun setAttrs jexp = adamc@606: let adamc@606: val s = (L'.EPrim (Prim.String (String.concat ["<", tag])), loc) adamc@606: adamc@606: val assgns = List.mapPartial adamc@606: (fn ("Source", _, _) => NONE adamc@606: | (x, e, _) => adamc@606: SOME (strcat [str ("d." ^ lowercaseFirst x ^ "="), adamc@606: (L'.EJavaScript (L'.Script, e, NONE), loc), adamc@606: str ";"])) adamc@606: attrs adamc@606: in adamc@606: case assgns of adamc@606: [] => jexp adamc@606: | _ => strcat (str "var d=" adamc@606: :: jexp adamc@606: :: str ";" adamc@606: :: assgns) adamc@606: end adamc@152: in adamc@152: case tag of adamc@565: "body" => normal ("body", NONE, adamc@569: SOME (L'.EStrcat ((L'.EPrim (Prim.String ""), loc), adamc@569: (L'.EFfiApp ("Basis", "get_script", [(L'.ERecord [], loc)]), adamc@569: loc)), loc)) adamc@568: adamc@568: | "dyn" => adamc@598: (case attrs of adamc@598: [("Signal", (L'.EApp ((L'.EAbs (_, _, _, (L'.ESignalReturn (L'.ERel 0, _), _)), _), adamc@598: e), _), _)] => (e, fm) adamc@598: | [("Signal", e, _)] => adamc@568: ((L'.EStrcat adamc@603: ((L'.EPrim (Prim.String ""), loc)), loc)), loc), adamc@568: fm) adamc@568: | _ => raise Fail "Monoize: Bad dyn attributes") adamc@565: adamc@565: | "submit" => normal ("input type=\"submit\"", NONE, NONE) adamc@601: | "button" => normal ("input type=\"submit\"", NONE, NONE) adamc@152: adamc@152: | "textbox" => adamc@152: (case targs of adamc@152: [_, (L.CName name, _)] => adamc@598: (case List.find (fn ("Source", _, _) => true | _ => false) attrs of adamc@598: NONE => adamc@598: let adamc@598: val (ts, fm) = tagStart "input" adamc@598: in adamc@598: ((L'.EStrcat (ts, adamc@598: (L'.EPrim (Prim.String (" name=\"" ^ name ^ "\"/>")), adamc@598: loc)), loc), fm) adamc@598: end adamc@598: | SOME (_, src, _) => adamc@603: (strcat [str ""], adamc@598: fm)) adamc@152: | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs); adamc@598: raise Fail "No name passed to textbox tag")) adamc@155: | "password" => input "password" adamc@361: | "textarea" => adamc@152: (case targs of adamc@152: [_, (L.CName name, _)] => adamc@179: let adamc@179: val (ts, fm) = tagStart "textarea" adamc@179: val (xml, fm) = monoExp (env, st, fm) xml adamc@179: in adamc@179: ((L'.EStrcat ((L'.EStrcat (ts, adamc@179: (L'.EPrim (Prim.String (" name=\"" ^ name ^ "\">")), loc)), loc), adamc@179: (L'.EStrcat (xml, adamc@179: (L'.EPrim (Prim.String ""), adamc@179: loc)), loc)), adamc@179: loc), fm) adamc@179: end adamc@152: | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs); adamc@153: raise Fail "No name passed to ltextarea tag")) adamc@153: adamc@190: | "checkbox" => input "checkbox" adamc@190: adamc@153: | "radio" => adamc@153: (case targs of adamc@153: [_, (L.CName name, _)] => adamc@179: monoExp (env, St.setRadioGroup (st, name), fm) xml adamc@153: | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs); adamc@153: raise Fail "No name passed to radio tag")) adamc@153: | "radioOption" => adamc@153: (case St.radioGroup st of adamc@153: NONE => raise Fail "No name for radioGroup" adamc@153: | SOME name => adamc@153: normal ("input", adamc@565: SOME (L'.EPrim (Prim.String (" type=\"radio\" name=\"" ^ name ^ "\"")), loc), adamc@565: NONE)) adamc@152: adamc@361: | "select" => adamc@154: (case targs of adamc@154: [_, (L.CName name, _)] => adamc@179: let adamc@179: val (ts, fm) = tagStart "select" adamc@179: val (xml, fm) = monoExp (env, st, fm) xml adamc@179: in adamc@179: ((L'.EStrcat ((L'.EStrcat (ts, adamc@598: (L'.EPrim (Prim.String (" name=\"" ^ name ^ "\">")), adamc@598: loc)), loc), adamc@179: (L'.EStrcat (xml, adamc@179: (L'.EPrim (Prim.String ""), adamc@179: loc)), loc)), adamc@179: loc), adamc@179: fm) adamc@179: end adamc@154: | _ => (Print.prefaces "Targs" (map (fn t => ("T", CorePrint.p_con env t)) targs); adamc@154: raise Fail "No name passed to lselect tag")) adamc@154: adamc@601: | "ctextbox" => adamc@601: (case List.find (fn ("Source", _, _) => true | _ => false) attrs of adamc@601: NONE => adamc@601: let adamc@601: val (ts, fm) = tagStart "input" adamc@601: in adamc@601: ((L'.EStrcat (ts, adamc@601: (L'.EPrim (Prim.String "/>"), loc)), adamc@601: loc), fm) adamc@601: end adamc@601: | SOME (_, src, _) => adamc@606: let adamc@606: val sc = strcat [str "inp(\"input\",", adamc@606: (L'.EJavaScript (L'.Script, src, NONE), loc), adamc@606: str ")"] adamc@606: val sc = setAttrs sc adamc@606: in adamc@606: (strcat [str ""], adamc@606: fm) adamc@606: end) adamc@154: adamc@565: | "tabl" => normal ("table", NONE, NONE) adamc@565: | _ => normal (tag, NONE, NONE) adamc@95: end adamc@94: adamc@141: | L.EApp ((L.ECApp ( adamc@361: (L.ECApp ((L.EFfi ("Basis", "form"), _), _), _), adamc@141: _), _), adamc@141: xml) => adamc@143: let adamc@143: fun findSubmit (e, _) = adamc@143: case e of adamc@143: L.EApp ( adamc@143: (L.EApp ( adamc@143: (L.ECApp ( adamc@143: (L.ECApp ( adamc@143: (L.ECApp ( adamc@143: (L.ECApp ( adamc@143: (L.EFfi ("Basis", "join"), adamc@143: _), _), _), adamc@143: _), _), adamc@143: _), _), adamc@143: _), _), adamc@143: xml1), _), adamc@143: xml2) => (case findSubmit xml1 of adamc@143: Error => Error adamc@143: | NotFound => findSubmit xml2 adamc@143: | Found e => adamc@143: case findSubmit xml2 of adamc@143: NotFound => Found e adamc@143: | _ => Error) adamc@143: | L.EApp ( adamc@143: (L.EApp ( adamc@143: (L.EApp ( adamc@143: (L.ECApp ( adamc@143: (L.ECApp ( adamc@143: (L.ECApp ( adamc@143: (L.ECApp ( adamc@143: (L.ECApp ( adamc@143: (L.ECApp ( adamc@143: (L.ECApp ( adamc@143: (L.ECApp ( adamc@143: (L.EFfi ("Basis", "tag"), adamc@143: _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), _), adamc@143: attrs), _), adamc@143: _), _), adamc@143: xml) => adamc@143: (case #1 attrs of adamc@143: L.ERecord xes => adamc@143: (case ListUtil.search (fn ((L.CName "Action", _), e, t) => SOME (e, t) adamc@143: | _ => NONE) xes of adamc@143: NONE => findSubmit xml adamc@143: | SOME et => adamc@143: case findSubmit xml of adamc@143: NotFound => Found et adamc@143: | _ => Error) adamc@143: | _ => findSubmit xml) adamc@143: | _ => NotFound adamc@143: adamc@598: val (action, fm) = case findSubmit xml of adamc@598: NotFound => ((L'.EPrim (Prim.String ""), loc), fm) adamc@143: | Error => raise Fail "Not ready for multi-submit lforms yet" adamc@598: | Found (action, actionT) => adamc@598: let adamc@598: val actionT = monoType env actionT adamc@598: val (action, fm) = monoExp (env, st, fm) action adamc@598: val (action, fm) = urlifyExp env fm (action, actionT) adamc@598: in adamc@598: ((L'.EStrcat ((L'.EPrim (Prim.String " action=\""), loc), adamc@598: (L'.EStrcat (action, adamc@598: (L'.EPrim (Prim.String "\""), loc)), loc)), loc), adamc@598: fm) adamc@598: end adamc@598: adamc@179: val (xml, fm) = monoExp (env, st, fm) xml adamc@143: in adamc@598: ((L'.EStrcat ((L'.EStrcat ((L'.EPrim (Prim.String ""), loc)), loc)), loc), adamc@179: (L'.EStrcat (xml, adamc@179: (L'.EPrim (Prim.String ""), loc)), loc)), loc), adamc@179: fm) adamc@143: end adamc@141: adamc@148: | L.EApp ((L.ECApp ( adamc@148: (L.ECApp ( adamc@148: (L.ECApp ( adamc@148: (L.ECApp ( adamc@148: (L.EFfi ("Basis", "useMore"), _), _), _), adamc@148: _), _), adamc@148: _), _), adamc@148: _), _), adamc@179: xml) => monoExp (env, st, fm) xml adamc@148: adamc@283: | L.ECApp ((L.EFfi ("Basis", "error"), _), t) => adamc@283: let adamc@283: val t = monoType env t adamc@283: in adamc@283: ((L'.EAbs ("s", (L'.TFfi ("Basis", "string"), loc), t, adamc@283: (L'.EError ((L'.ERel 0, loc), t), loc)), loc), adamc@283: fm) adamc@283: end adamc@283: adamc@179: | L.EApp (e1, e2) => adamc@179: let adamc@179: val (e1, fm) = monoExp (env, st, fm) e1 adamc@179: val (e2, fm) = monoExp (env, st, fm) e2 adamc@179: in adamc@179: ((L'.EApp (e1, e2), loc), fm) adamc@179: end adamc@26: | L.EAbs (x, dom, ran, e) => adamc@179: let adamc@179: val (e, fm) = monoExp (Env.pushERel env x dom, st, fm) e adamc@179: in adamc@179: ((L'.EAbs (x, monoType env dom, monoType env ran, e), loc), fm) adamc@179: end adamc@25: | L.ECApp _ => poly () adamc@25: | L.ECAbs _ => poly () adamc@25: adamc@252: | L.EFfi mx => ((L'.EFfi mx, loc), fm) adamc@252: | L.EFfiApp (m, x, es) => adamc@252: let adamc@252: val (es, fm) = ListUtil.foldlMap (fn (e, fm) => monoExp (env, st, fm) e) fm es adamc@252: in adamc@252: ((L'.EFfiApp (m, x, es), loc), fm) adamc@252: end adamc@252: adamc@179: | L.ERecord xes => adamc@179: let adamc@179: val (xes, fm) = ListUtil.foldlMap adamc@179: (fn ((x, e, t), fm) => adamc@179: let adamc@179: val (e, fm) = monoExp (env, st, fm) e adamc@179: in adamc@179: ((monoName env x, adamc@179: e, adamc@179: monoType env t), fm) adamc@179: end) fm xes adamc@179: in adamc@179: ((L'.ERecord xes, loc), fm) adamc@179: end adamc@179: | L.EField (e, x, _) => adamc@179: let adamc@179: val (e, fm) = monoExp (env, st, fm) e adamc@179: in adamc@179: ((L'.EField (e, monoName env x), loc), fm) adamc@179: end adamc@445: | L.EConcat _ => poly () adamc@149: | L.ECut _ => poly () adamc@493: | L.ECutMulti _ => poly () adamc@73: | L.EFold _ => poly () adamc@177: adamc@182: | L.ECase (e, pes, {disc, result}) => adamc@179: let adamc@179: val (e, fm) = monoExp (env, st, fm) e adamc@179: val (pes, fm) = ListUtil.foldlMap adamc@179: (fn ((p, e), fm) => adamc@179: let adamc@179: val (e, fm) = monoExp (env, st, fm) e adamc@179: in adamc@182: ((monoPat env p, e), fm) adamc@179: end) fm pes adamc@179: in adamc@182: ((L'.ECase (e, pes, {disc = monoType env disc, result = monoType env result}), loc), fm) adamc@179: end adamc@177: adamc@179: | L.EWrite e => adamc@179: let adamc@179: val (e, fm) = monoExp (env, st, fm) e adamc@179: in adamc@252: ((L'.EAbs ("_", (L'.TRecord [], loc), (L'.TRecord [], loc), adamc@252: (L'.EWrite (liftExpInExp 0 e), loc)), loc), fm) adamc@179: end adamc@110: adamc@179: | L.EClosure (n, es) => adamc@179: let adamc@179: val (es, fm) = ListUtil.foldlMap (fn (e, fm) => adamc@179: monoExp (env, st, fm) e) adamc@179: fm es adamc@179: in adamc@179: ((L'.EClosure (n, es), loc), fm) adamc@179: end adamc@450: adamc@450: | L.ELet (x, t, e1, e2) => adamc@450: let adamc@450: val t' = monoType env t adamc@450: val (e1, fm) = monoExp (env, st, fm) e1 adamc@450: val (e2, fm) = monoExp (Env.pushERel env x t, st, fm) e2 adamc@450: in adamc@450: ((L'.ELet (x, t', e1, e2), loc), fm) adamc@450: end adamc@607: adamc@609: | L.EServerCall (n, es, ek, t) => adamc@608: let adamc@609: val t = monoType env t adamc@614: val (_, ft, _, name) = Env.lookupENamed env n adamc@608: val (es, fm) = ListUtil.foldlMap (fn (e, fm) => monoExp (env, st, fm) e) fm es adamc@614: adamc@614: fun encodeArgs (es, ft, acc, fm) = adamc@614: case (es, ft) of adamc@614: ([], _) => (rev acc, fm) adamc@614: | (e :: es, (L.TFun (dom, ran), _)) => adamc@614: let adamc@614: val (e, fm) = urlifyExp env fm (e, monoType env dom) adamc@614: in adamc@614: encodeArgs (es, ran, e adamc@614: :: (L'.EPrim (Prim.String "/"), loc) adamc@614: :: acc, fm) adamc@614: end adamc@614: | _ => raise Fail "Monoize: Not enough arguments visible in RPC function type" adamc@614: adamc@614: val (call, fm) = encodeArgs (es, ft, [], fm) adamc@614: val call = foldl (fn (e, call) => (L'.EStrcat (call, e), loc)) adamc@614: (L'.EPrim (Prim.String name), loc) call adamc@614: adamc@608: val (ek, fm) = monoExp (env, st, fm) ek adamc@609: adamc@609: val ekf = (L'.EAbs ("f", adamc@609: (L'.TFun (t, adamc@609: (L'.TFun ((L'.TRecord [], loc), adamc@609: (L'.TRecord [], loc)), loc)), loc), adamc@609: (L'.TFun (t, adamc@609: (L'.TRecord [], loc)), loc), adamc@609: (L'.EAbs ("x", adamc@609: t, adamc@609: (L'.TRecord [], loc), adamc@609: (L'.EApp ((L'.EApp ((L'.ERel 1, loc), adamc@609: (L'.ERel 0, loc)), loc), adamc@609: (L'.ERecord [], loc)), loc)), loc)), loc) adamc@609: val ek = (L'.EApp (ekf, ek), loc) adamc@608: in adamc@614: ((L'.EServerCall (call, ek, t), loc), fm) adamc@608: end adamc@25: end adamc@25: adamc@179: fun monoDecl (env, fm) (all as (d, loc)) = adamc@25: let adamc@25: fun poly () = adamc@25: (E.errorAt loc "Unsupported declaration"; adamc@25: Print.eprefaces' [("Declaration", CorePrint.p_decl env all)]; adamc@25: NONE) adamc@25: in adamc@25: case d of adamc@25: L.DCon _ => NONE adamc@193: | L.DDatatype (x, n, [], xncs) => adamc@193: let adamc@196: val env' = Env.declBinds env all adamc@196: val d = (L'.DDatatype (x, n, map (fn (x, n, to) => (x, n, Option.map (monoType env') to)) xncs), loc) adamc@164: in adamc@273: SOME (env', fm, [d]) adamc@193: end adamc@193: | L.DDatatype _ => poly () adamc@179: | L.DVal (x, n, t, e, s) => adamc@179: let adamc@179: val (e, fm) = monoExp (env, St.empty, fm) e adamc@179: in adamc@179: SOME (Env.pushENamed env x n t NONE s, adamc@179: fm, adamc@273: [(L'.DVal (x, n, monoType env t, e, s), loc)]) adamc@179: end adamc@128: | L.DValRec vis => adamc@128: let adamc@128: val env = foldl (fn ((x, n, t, e, s), env) => Env.pushENamed env x n t NONE s) env vis adamc@179: adamc@179: val (vis, fm) = ListUtil.foldlMap adamc@179: (fn ((x, n, t, e, s), fm) => adamc@179: let adamc@179: val (e, fm) = monoExp (env, St.empty, fm) e adamc@179: in adamc@179: ((x, n, monoType env t, e, s), fm) adamc@179: end) adamc@179: fm vis adamc@128: in adamc@128: SOME (env, adamc@179: fm, adamc@273: [(L'.DValRec vis, loc)]) adamc@128: end adamc@144: | L.DExport (ek, n) => adamc@115: let adamc@120: val (_, t, _, s) = Env.lookupENamed env n adamc@120: adamc@609: fun unwind (t, args) = adamc@609: case #1 t of adamc@609: L.TFun (dom, ran) => unwind (ran, dom :: args) adamc@314: | L.CApp ((L.CFfi ("Basis", "transaction"), _), t) => adamc@609: unwind (t, (L.TRecord (L.CRecord ((L.KType, loc), []), loc), loc) :: args) adamc@609: | _ => (rev args, t) adamc@120: adamc@609: val (ts, ran) = unwind (t, []) adamc@609: val ts = map (monoType env) ts adamc@609: val ran = monoType env ran adamc@115: in adamc@609: SOME (env, fm, [(L'.DExport (ek, s, n, ts, ran), loc)]) adamc@115: end adamc@273: | L.DTable (x, n, (L.CRecord (_, xts), _), s) => adamc@251: let adamc@251: val t = (L.CFfi ("Basis", "string"), loc) adamc@251: val t' = (L'.TFfi ("Basis", "string"), loc) adamc@311: val s = "uw_" ^ s adamc@251: val e = (L'.EPrim (Prim.String s), loc) adamc@273: adamc@273: val xts = map (fn (x, t) => (monoName env x, monoType env t)) xts adamc@251: in adamc@251: SOME (Env.pushENamed env x n t NONE s, adamc@251: fm, adamc@273: [(L'.DTable (s, xts), loc), adamc@273: (L'.DVal (x, n, t', e, s), loc)]) adamc@251: end adamc@273: | L.DTable _ => poly () adamc@338: | L.DSequence (x, n, s) => adamc@338: let adamc@338: val t = (L.CFfi ("Basis", "string"), loc) adamc@338: val t' = (L'.TFfi ("Basis", "string"), loc) adamc@338: val s = "uw_" ^ s adamc@338: val e = (L'.EPrim (Prim.String s), loc) adamc@338: in adamc@338: SOME (Env.pushENamed env x n t NONE s, adamc@338: fm, adamc@338: [(L'.DSequence s, loc), adamc@338: (L'.DVal (x, n, t', e, s), loc)]) adamc@338: end adamc@273: | L.DDatabase s => SOME (env, fm, [(L'.DDatabase s, loc)]) adamc@462: | L.DCookie (x, n, t, s) => adamc@462: let adamc@462: val t = (L.CFfi ("Basis", "string"), loc) adamc@462: val t' = (L'.TFfi ("Basis", "string"), loc) adamc@462: val e = (L'.EPrim (Prim.String s), loc) adamc@462: in adamc@462: SOME (Env.pushENamed env x n t NONE s, adamc@462: fm, adamc@462: [(L'.DVal (x, n, t', e, s), loc)]) adamc@462: end adamc@25: end adamc@25: adamc@25: fun monoize env ds = adamc@25: let adamc@385: val p = !urlPrefix adamc@385: val () = adamc@385: if p = "" then adamc@385: urlPrefix := "/" adamc@385: else if String.sub (p, size p - 1) <> #"/" then adamc@385: urlPrefix := p ^ "/" adamc@385: else adamc@385: () adamc@385: adamc@179: val (_, _, ds) = List.foldl (fn (d, (env, fm, ds)) => adamc@179: case monoDecl (env, fm) d of adamc@179: NONE => (env, fm, ds) adamc@273: | SOME (env, fm, ds') => adamc@179: (env, adamc@179: Fm.enter fm, adamc@273: ds' @ Fm.decls fm @ ds)) adamc@179: (env, Fm.empty (CoreUtil.File.maxName ds + 1), []) ds adamc@25: in adamc@25: rev ds adamc@25: end adamc@25: adamc@25: end