comparison src/monoize.sml @ 1682:ac141fbb313a

'ORDER BY RANDOM' (based on a patch from Ron de Bruijn)
author Adam Chlipala <adam@chlipala.net>
date Thu, 02 Feb 2012 11:40:10 -0500
parents 5b2c7b9f6017
children a7b70c7b3f1a
comparison
equal deleted inserted replaced
1681:e8a84494d2c0 1682:ac141fbb313a
14 * 14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
72 val (r, fs') = ListPair.foldr (fn ((_, t), (x, n, _), (r, fs')) => 72 val (r, fs') = ListPair.foldr (fn ((_, t), (x, n, _), (r, fs')) =>
73 ((x, n, SOME t) :: r, 73 ((x, n, SOME t) :: r,
74 SM.insert (fs', x, n))) ([], SM.empty) (r, fs) 74 SM.insert (fs', x, n))) ([], SM.empty) (r, fs)
75 in 75 in
76 pvars := RM.insert (!pvars, r', (n, fs)); 76 pvars := RM.insert (!pvars, r', (n, fs));
77 pvarDefs := (L'.DDatatype [("$poly" ^ Int.toString n, n, map (fn (x, n, t) => (x, n, SOME t)) fs)], loc) 77 pvarDefs := (L'.DDatatype [("$poly" ^ Int.toString n, n, map (fn (x, n, t) => (x, n, SOME t)) fs)], loc)
78 :: !pvarDefs; 78 :: !pvarDefs;
79 pvarOldDefs := (n, r) :: !pvarOldDefs; 79 pvarOldDefs := (n, r) :: !pvarOldDefs;
80 (n, fs) 80 (n, fs)
81 end 81 end
82 | SOME v => v 82 | SOME v => v
310 SOME r => (L'.TDatatype (n, r), loc) 310 SOME r => (L'.TDatatype (n, r), loc)
311 | NONE => 311 | NONE =>
312 let 312 let
313 val r = ref (L'.Default, []) 313 val r = ref (L'.Default, [])
314 val (_, xs, xncs) = Env.lookupDatatype env n 314 val (_, xs, xncs) = Env.lookupDatatype env n
315 315
316 val dtmap' = IM.insert (dtmap, n, r) 316 val dtmap' = IM.insert (dtmap, n, r)
317 317
318 val xncs = map (fn (x, n, to) => (x, n, Option.map (mt env dtmap') to)) xncs 318 val xncs = map (fn (x, n, to) => (x, n, Option.map (mt env dtmap') to)) xncs
319 in 319 in
320 case xs of 320 case xs of
321 [] =>(r := (ElabUtil.classifyDatatype xncs, xncs); 321 [] =>(r := (ElabUtil.classifyDatatype xncs, xncs);
322 (L'.TDatatype (n, r), loc)) 322 (L'.TDatatype (n, r), loc))
578 branches, 578 branches,
579 {disc = dom, 579 {disc = dom,
580 result = ran}), loc)), loc), 580 result = ran}), loc)), loc),
581 "")], loc), 581 "")], loc),
582 fm) 582 fm)
583 end 583 end
584 584
585 val (fm, n) = Fm.lookup fm fk i makeDecl 585 val (fm, n) = Fm.lookup fm fk i makeDecl
586 in 586 in
587 ((L'.EApp ((L'.ENamed n, loc), e), loc), fm) 587 ((L'.EApp ((L'.ENamed n, loc), e), loc), fm)
588 end 588 end
592 val (body, fm) = fooify fm ((L'.ERel 0, loc), t) 592 val (body, fm) = fooify fm ((L'.ERel 0, loc), t)
593 in 593 in
594 ((L'.ECase (e, 594 ((L'.ECase (e,
595 [((L'.PNone t, loc), 595 [((L'.PNone t, loc),
596 (L'.EPrim (Prim.String "None"), loc)), 596 (L'.EPrim (Prim.String "None"), loc)),
597 597
598 ((L'.PSome (t, (L'.PVar ("x", t), loc)), loc), 598 ((L'.PSome (t, (L'.PVar ("x", t), loc)), loc),
599 (L'.EStrcat ((L'.EPrim (Prim.String "Some/"), loc), 599 (L'.EStrcat ((L'.EPrim (Prim.String "Some/"), loc),
600 body), loc))], 600 body), loc))],
601 {disc = tAll, 601 {disc = tAll,
602 result = (L'.TFfi ("Basis", "string"), loc)}), loc), 602 result = (L'.TFfi ("Basis", "string"), loc)}), loc),
1184 val dom = ordTy t 1184 val dom = ordTy t
1185 in 1185 in
1186 ((L'.EAbs ("f", dom, dom, 1186 ((L'.EAbs ("f", dom, dom,
1187 (L'.ERel 0, loc)), loc), fm) 1187 (L'.ERel 0, loc)), loc), fm)
1188 end 1188 end
1189 1189
1190 | L.ECApp ((L.EFfi ("Basis", "show"), _), t) => 1190 | L.ECApp ((L.EFfi ("Basis", "show"), _), t) =>
1191 let 1191 let
1192 val t = monoType env t 1192 val t = monoType env t
1193 val s = (L'.TFfi ("Basis", "string"), loc) 1193 val s = (L'.TFfi ("Basis", "string"), loc)
1194 in 1194 in
2057 sc ""), 2057 sc ""),
2058 ((L'.PWild, loc), 2058 ((L'.PWild, loc),
2059 strcat [sc " WHERE ", gf "Where"])], 2059 strcat [sc " WHERE ", gf "Where"])],
2060 {disc = s, 2060 {disc = s,
2061 result = s}), loc), 2061 result = s}), loc),
2062 2062
2063 if List.all (fn (x, xts) => 2063 if List.all (fn (x, xts) =>
2064 case List.find (fn (x', _) => x' = x) grouped of 2064 case List.find (fn (x', _) => x' = x) grouped of
2065 NONE => List.null xts 2065 NONE => List.null xts
2066 | SOME (_, xts') => 2066 | SOME (_, xts') =>
2067 List.all (fn (x, _) => 2067 List.all (fn (x, _) =>
2192 | L.ECApp ((L.EFfi ("Basis", "sql_subset_all"), _), _) => 2192 | L.ECApp ((L.EFfi ("Basis", "sql_subset_all"), _), _) =>
2193 ((L'.ERecord [], loc), fm) 2193 ((L'.ERecord [], loc), fm)
2194 | L.ECApp ((L.ECApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_subset_concat"), 2194 | L.ECApp ((L.ECApp ((L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_subset_concat"),
2195 _), _), _), _), _), _), _), _) => 2195 _), _), _), _), _), _), _), _) =>
2196 let 2196 let
2197 val un = (L'.TRecord [], loc) 2197 val un = (L'.TRecord [], loc)
2198 in 2198 in
2199 ((L'.EAbs ("_", un, (L'.TFun (un, un), loc), 2199 ((L'.EAbs ("_", un, (L'.TFun (un, un), loc),
2200 (L'.EAbs ("_", un, un, 2200 (L'.EAbs ("_", un, un,
2201 (L'.ERecord [], loc)), loc)), loc), 2201 (L'.ERecord [], loc)), loc)), loc),
2202 fm) 2202 fm)
2404 fm) 2404 fm)
2405 end 2405 end
2406 2406
2407 | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_order_by_Nil"), _), _), _), _) => 2407 | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_order_by_Nil"), _), _), _), _) =>
2408 ((L'.EPrim (Prim.String ""), loc), fm) 2408 ((L'.EPrim (Prim.String ""), loc), fm)
2409 | L.ECApp ((L.ECApp ((L.EFfi ("Basis", "sql_order_by_random"), _), _), _), _) =>
2410 ((L'.EPrim (Prim.String (#randomFunction (Settings.currentDbms ()) ^ "()")), loc), fm)
2409 | L.ECApp ( 2411 | L.ECApp (
2410 (L.ECApp ( 2412 (L.ECApp (
2411 (L.ECApp ( 2413 (L.ECApp (
2412 (L.EFfi ("Basis", "sql_order_by_Cons"), _), 2414 (L.EFfi ("Basis", "sql_order_by_Cons"), _),
2413 _), _), 2415 _), _),
2753 (L'.EPrim (Prim.String "MIN"), loc)), loc)), loc), 2755 (L'.EPrim (Prim.String "MIN"), loc)), loc)), loc),
2754 fm) 2756 fm)
2755 2757
2756 | L.EFfi ("Basis", "sql_asc") => ((L'.EPrim (Prim.String ""), loc), fm) 2758 | L.EFfi ("Basis", "sql_asc") => ((L'.EPrim (Prim.String ""), loc), fm)
2757 | L.EFfi ("Basis", "sql_desc") => ((L'.EPrim (Prim.String " DESC"), loc), fm) 2759 | L.EFfi ("Basis", "sql_desc") => ((L'.EPrim (Prim.String " DESC"), loc), fm)
2758
2759 | L.ECApp ( 2760 | L.ECApp (
2760 (L.ECApp ( 2761 (L.ECApp (
2761 (L.ECApp ( 2762 (L.ECApp (
2762 (L.ECApp ( 2763 (L.ECApp (
2763 (L.EFfi ("Basis", "sql_nfunc"), _), 2764 (L.EFfi ("Basis", "sql_nfunc"), _),
2764 _), _), 2765 _), _),
2765 _), _), 2766 _), _),
2766 _), _), 2767 _), _),
2767 _) => 2768 _) =>
2768 let 2769 let
2769 val s = (L'.TFfi ("Basis", "string"), loc) 2770 val s = (L'.TFfi ("Basis", "string"), loc)
2770 fun sc s = (L'.EPrim (Prim.String s), loc) 2771 fun sc s = (L'.EPrim (Prim.String s), loc)
2771 in 2772 in
2891 ((L'.EAbs ("u", (L'.TRecord [], loc), (L'.TFun (s, s), loc), 2892 ((L'.EAbs ("u", (L'.TRecord [], loc), (L'.TFun (s, s), loc),
2892 (L'.EAbs ("x", s, s, 2893 (L'.EAbs ("x", s, s,
2893 (L'.ERel 0, loc)), loc)), loc), 2894 (L'.ERel 0, loc)), loc)), loc),
2894 fm) 2895 fm)
2895 end 2896 end
2896 2897
2897 | L.ECApp ( 2898 | L.ECApp (
2898 (L.ECApp ( 2899 (L.ECApp (
2899 (L.ECApp ( 2900 (L.ECApp (
2900 (L.ECApp ( 2901 (L.ECApp (
2901 (L.ECApp ( 2902 (L.ECApp (
3043 case attrs of 3044 case attrs of
3044 [] => (onload, onunload, acc) 3045 [] => (onload, onunload, acc)
3045 | ("Onload", e, _) :: rest => findOnload (rest, SOME e, onunload, acc) 3046 | ("Onload", e, _) :: rest => findOnload (rest, SOME e, onunload, acc)
3046 | ("Onunload", e, _) :: rest => findOnload (rest, onload, SOME e, acc) 3047 | ("Onunload", e, _) :: rest => findOnload (rest, onload, SOME e, acc)
3047 | x :: rest => findOnload (rest, onload, onunload, x :: acc) 3048 | x :: rest => findOnload (rest, onload, onunload, x :: acc)
3048 3049
3049 val (onload, onunload, attrs) = findOnload (attrs, NONE, NONE, []) 3050 val (onload, onunload, attrs) = findOnload (attrs, NONE, NONE, [])
3050 3051
3051 val (class, fm) = monoExp (env, st, fm) class 3052 val (class, fm) = monoExp (env, st, fm) class
3052 val (dynClass, fm) = monoExp (env, st, fm) dynClass 3053 val (dynClass, fm) = monoExp (env, st, fm) dynClass
3053 3054
3323 fun inTag tag = case targs of 3324 fun inTag tag = case targs of
3324 (L.CRecord (_, ctx), _) :: _ => 3325 (L.CRecord (_, ctx), _) :: _ =>
3325 List.exists (fn ((L.CName tag', _), _) => tag' = tag 3326 List.exists (fn ((L.CName tag', _), _) => tag' = tag
3326 | _ => false) ctx 3327 | _ => false) ctx
3327 | _ => false 3328 | _ => false
3328 3329
3329 val tag = if inTag "Tr" then 3330 val tag = if inTag "Tr" then
3330 "tr" 3331 "tr"
3331 else if inTag "Table" then 3332 else if inTag "Table" then
3332 "table" 3333 "table"
3333 else 3334 else
3341 (L'.EStrcat ((L'.EJavaScript (L'.Script, e), loc), 3342 (L'.EStrcat ((L'.EJavaScript (L'.Script, e), loc),
3342 (L'.EPrim (Prim.String ("))</script>")), loc)), loc)), loc), 3343 (L'.EPrim (Prim.String ("))</script>")), loc)), loc)), loc),
3343 fm) 3344 fm)
3344 | _ => raise Fail "Monoize: Bad dyn attributes" 3345 | _ => raise Fail "Monoize: Bad dyn attributes"
3345 end 3346 end
3346 3347
3347 | "submit" => normal ("input type=\"submit\"", NONE, NONE) 3348 | "submit" => normal ("input type=\"submit\"", NONE, NONE)
3348 | "image" => normal ("input type=\"image\"", NONE, NONE) 3349 | "image" => normal ("input type=\"image\"", NONE, NONE)
3349 | "button" => normal ("input type=\"submit\"", NONE, NONE) 3350 | "button" => normal ("input type=\"submit\"", NONE, NONE)
3350 | "hidden" => input "hidden" 3351 | "hidden" => input "hidden"
3351 3352
4310 case #1 d of 4311 case #1 d of
4311 L.DDatabase s => 4312 L.DDatabase s =>
4312 let 4313 let
4313 val (nExp, fm) = Fm.freshName fm 4314 val (nExp, fm) = Fm.freshName fm
4314 val (nIni, fm) = Fm.freshName fm 4315 val (nIni, fm) = Fm.freshName fm
4315 4316
4316 val dExp = L'.DVal ("expunger", 4317 val dExp = L'.DVal ("expunger",
4317 nExp, 4318 nExp,
4318 (L'.TFun (client, unit), loc), 4319 (L'.TFun (client, unit), loc),
4319 (L'.EAbs ("cli", client, unit, expunger ()), loc), 4320 (L'.EAbs ("cli", client, unit, expunger ()), loc),
4320 "expunger") 4321 "expunger")