# HG changeset patch # User Adam Chlipala # Date 1220207221 14400 # Node ID 7f6620853c36bb9ff88f3d19f495bed3807f2c73 # Parent 7e9bd70ad3ce994c3a3daca8b8fb47992e9153ce Monoized a WHERE clause with a comparison diff -r 7e9bd70ad3ce -r 7f6620853c36 lib/basis.urs --- a/lib/basis.urs Sun Aug 31 13:58:47 2008 -0400 +++ b/lib/basis.urs Sun Aug 31 14:27:01 2008 -0400 @@ -119,9 +119,9 @@ val sql_le : sql_comparison val sql_gt : sql_comparison val sql_ge : sql_comparison -val sql_comparison : sql_comparison - -> tables ::: {{Type}} -> agg ::: {{Type}} -> exps ::: {Type} +val sql_comparison : tables ::: {{Type}} -> agg ::: {{Type}} -> exps ::: {Type} -> t ::: Type + -> sql_comparison -> sql_exp tables agg exps t -> sql_exp tables agg exps t -> sql_exp tables agg exps bool diff -r 7e9bd70ad3ce -r 7f6620853c36 src/mono_opt.sml --- a/src/mono_opt.sml Sun Aug 31 13:58:47 2008 -0400 +++ b/src/mono_opt.sml Sun Aug 31 14:27:01 2008 -0400 @@ -79,6 +79,12 @@ str ch else "%" ^ hexIt ch) + + +val sqlifyInt = attrifyInt +val sqlifyFloat = attrifyFloat + +fun sqlifyString s = "E'" ^ String.toString s ^ "'" fun exp e = case e of @@ -203,6 +209,21 @@ | EWrite (EFfiApp ("Basis", "urlifyBool", [e]), _) => EFfiApp ("Basis", "urlifyBool_w", [e]) + | EFfiApp ("Basis", "sqlifyInt", [(EPrim (Prim.Int n), _)]) => + EPrim (Prim.String (sqlifyInt n)) + | EFfiApp ("Basis", "sqlifyFloat", [(EPrim (Prim.Float n), _)]) => + EPrim (Prim.String (sqlifyFloat n)) + | EFfiApp ("Basis", "sqlifyBool", [b as (_, loc)]) => + optExp (ECase (b, + [((PCon (Enum, PConFfi {mod = "Basis", datatyp = "bool", con = "True", arg = NONE}, NONE), loc), + (EPrim (Prim.String "TRUE"), loc)), + ((PCon (Enum, PConFfi {mod = "Basis", datatyp = "bool", con = "False", arg = NONE}, NONE), loc), + (EPrim (Prim.String "FALSE"), loc))], + {disc = (TFfi ("Basis", "bool"), loc), + result = (TFfi ("Basis", "string"), loc)}), loc) + | EFfiApp ("Basis", "sqlifyString", [(EPrim (Prim.String n), _)]) => + EPrim (Prim.String (sqlifyString n)) + | EWrite (ECase (discE, pes, {disc, ...}), loc) => optExp (ECase (discE, map (fn (p, e) => (p, (EWrite e, loc))) pes, diff -r 7e9bd70ad3ce -r 7f6620853c36 src/mono_reduce.sml --- a/src/mono_reduce.sml Sun Aug 31 13:58:47 2008 -0400 +++ b/src/mono_reduce.sml Sun Aug 31 14:27:01 2008 -0400 @@ -181,8 +181,8 @@ (ELet (x1, t1, b1, liftExpInExp 1 b2), loc)), loc) in - Print.prefaces "ELet commute" [("e", MonoPrint.p_exp env (e, loc)), - ("e'", MonoPrint.p_exp env e')]; + (*Print.prefaces "ELet commute" [("e", MonoPrint.p_exp env (e, loc)), + ("e'", MonoPrint.p_exp env e')];*) #1 (reduceExp env e') end | EApp ((ELet (x, t, e, b), loc), e') => diff -r 7e9bd70ad3ce -r 7f6620853c36 src/monoize.sml --- a/src/monoize.sml Sun Aug 31 13:58:47 2008 -0400 +++ b/src/monoize.sml Sun Aug 31 14:27:01 2008 -0400 @@ -612,7 +612,9 @@ xts)) stables), sc " FROM ", strcatComma loc (map (fn (x, _) => strcat loc [(L'.EField (gf "From", x), loc), - sc (" AS " ^ x)]) tables) + sc (" AS " ^ x)]) tables), + sc " WHERE ", + gf "Where" ]), loc), fm) | _ => poly () @@ -635,6 +637,23 @@ (L'.ERel 0, loc)), loc), fm) end + | L.EFfi ("Basis", "sql_int") => + ((L'.EAbs ("x", (L'.TFfi ("Basis", "int"), loc), (L'.TFfi ("Basis", "string"), loc), + (L'.EFfiApp ("Basis", "sqlifyInt", [(L'.ERel 0, loc)]), loc)), loc), + fm) + | L.EFfi ("Basis", "sql_float") => + ((L'.EAbs ("x", (L'.TFfi ("Basis", "float"), loc), (L'.TFfi ("Basis", "string"), loc), + (L'.EFfiApp ("Basis", "sqlifyFloat", [(L'.ERel 0, loc)]), loc)), loc), + fm) + | L.EFfi ("Basis", "sql_bool") => + ((L'.EAbs ("x", (L'.TFfi ("Basis", "bool"), loc), (L'.TFfi ("Basis", "string"), loc), + (L'.EFfiApp ("Basis", "sqlifyBool", [(L'.ERel 0, loc)]), loc)), loc), + fm) + | L.EFfi ("Basis", "sql_string") => + ((L'.EAbs ("x", (L'.TFfi ("Basis", "string"), loc), (L'.TFfi ("Basis", "string"), loc), + (L'.EFfiApp ("Basis", "sqlifyString", [(L'.ERel 0, loc)]), loc)), loc), + fm) + | L.ECApp ((L.EFfi ("Basis", "sql_subset"), _), _) => ((L'.ERecord [], loc), fm) | L.ECApp ((L.EFfi ("Basis", "sql_subset_all"), _), _) => @@ -647,6 +666,59 @@ ((L'.EPrim (Prim.String ""), loc), fm) | L.EFfi ("Basis", "sql_no_offset") => ((L'.EPrim (Prim.String ""), loc), fm) + + | L.EFfi ("Basis", "sql_eq") => + ((L'.EPrim (Prim.String "="), loc), fm) + | L.EFfi ("Basis", "sql_ne") => + ((L'.EPrim (Prim.String "<>"), loc), fm) + | L.EFfi ("Basis", "sql_lt") => + ((L'.EPrim (Prim.String "<"), loc), fm) + | L.EFfi ("Basis", "sql_le") => + ((L'.EPrim (Prim.String "<="), loc), fm) + | L.EFfi ("Basis", "sql_gt") => + ((L'.EPrim (Prim.String ">"), loc), fm) + | L.EFfi ("Basis", "sql_ge") => + ((L'.EPrim (Prim.String ">="), loc), fm) + + | L.ECApp ( + (L.ECApp ( + (L.ECApp ( + (L.ECApp ( + (L.EFfi ("Basis", "sql_comparison"), _), + _), _), + _), _), + _), _), + _) => + let + val s = (L'.TFfi ("Basis", "string"), loc) + fun sc s = (L'.EPrim (Prim.String s), loc) + in + ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc), + (L'.EAbs ("e1", s, (L'.TFun (s, s), loc), + (L'.EAbs ("e2", s, s, + strcat loc [(L'.ERel 1, loc), + sc " ", + (L'.ERel 2, loc), + sc " ", + (L'.ERel 0, loc)]), loc)), loc)), loc), + fm) + end + + | L.ECApp ( + (L.ECApp ( + (L.ECApp ( + (L.ECApp ( + (L.ECApp ( + (L.ECApp ( + (L.ECApp ( + (L.EFfi ("Basis", "sql_field"), _), + _), _), + _), _), + _), _), + _), _), + _), _), + (L.CName tab, _)), _), + (L.CName field, _)) => ((L'.EPrim (Prim.String (tab ^ "." ^ field)), loc), fm) | L.EApp ( (L.ECApp ( diff -r 7e9bd70ad3ce -r 7f6620853c36 tests/query.ur --- a/tests/query.ur Sun Aug 31 13:58:47 2008 -0400 +++ b/tests/query.ur Sun Aug 31 14:27:01 2008 -0400 @@ -1,10 +1,12 @@ -table t1 : {A : int, B : string, C : float} +table t1 : {A : int, B : string, C : float, D : bool} table t2 : {A : float, D : int} datatype list a = Nil | Cons of a * list a -val q1 = (SELECT * FROM t1) -val r1 : transaction (list {A : int, B : string, C : float}) = +(*val q1 = (SELECT * FROM t1)*) +val q1 = (SELECT * FROM t1 WHERE t1.A = 0) + +val r1 : transaction (list {A : int, B : string, C : float, D : bool}) = query q1 (fn fs acc => return (Cons (fs.T1, acc))) Nil