adamc@422: fun not b = if b then False else True
adamc@422:
adamc@356: con idT (t :: Type) = t
adamc@356: con record (t :: {Type}) = $t
adamc@356: con fstTT (t :: (Type * Type)) = t.1
adamc@356: con sndTT (t :: (Type * Type)) = t.2
adamc@445: con fstTTT (t :: (Type * Type * Type)) = t.1
adamc@445: con sndTTT (t :: (Type * Type * Type)) = t.2
adamc@445: con thdTTT (t :: (Type * Type * Type)) = t.3
adamc@329:
adamc@356: con mapTT (f :: Type -> Type) = fold (fn nm t acc [[nm] ~ acc] =>
adamc@356: [nm = f t] ++ acc) []
adamc@325:
adamc@411: con mapUT = fn f :: Type => fold (fn nm t acc [[nm] ~ acc] =>
adamc@411: [nm = f] ++ acc) []
adamc@411:
adamc@356: con mapT2T (f :: (Type * Type) -> Type) = fold (fn nm t acc [[nm] ~ acc] =>
adamc@356: [nm = f t] ++ acc) []
adamc@339:
adamc@445: con mapT3T (f :: (Type * Type * Type) -> Type) = fold (fn nm t acc [[nm] ~ acc] =>
adamc@445: [nm = f t] ++ acc) []
adamc@445:
adamc@339: con ex = fn tf :: (Type -> Type) =>
adamc@356: res ::: Type -> (choice :: Type -> tf choice -> res) -> res
adamc@339:
adamc@339: fun ex (tf :: (Type -> Type)) (choice :: Type) (body : tf choice) : ex tf =
adamc@356: fn (res ::: Type) (f : choice :: Type -> tf choice -> res) =>
adamc@356: f [choice] body
adamc@339:
adamc@356: fun compose (t1 ::: Type) (t2 ::: Type) (t3 ::: Type)
adamc@356: (f1 : t2 -> t3) (f2 : t1 -> t2) (x : t1) = f1 (f2 x)
adamc@325:
adamc@356: fun txt (t ::: Type) (ctx ::: {Unit}) (use ::: {Type}) (sh : show t) (v : t) =
adamc@403: cdata (@show sh v)
adamc@328:
adamc@411: fun foldUR (tf :: Type) (tr :: {Unit} -> Type)
adamc@411: (f : nm :: Name -> rest :: {Unit}
adamc@411: -> fn [[nm] ~ rest] =>
adamc@411: tf -> tr rest -> tr ([nm] ++ rest))
adamc@411: (i : tr []) =
adamc@411: fold [fn r :: {Unit} => $(mapUT tf r) -> tr r]
adamc@412: (fn (nm :: Name) (t :: Unit) (rest :: {Unit}) acc
adamc@412: [[nm] ~ rest] r =>
adamc@411: f [nm] [rest] r.nm (acc (r -- nm)))
adamc@411: (fn _ => i)
adamc@411:
adamc@418: fun foldUR2 (tf1 :: Type) (tf2 :: Type) (tr :: {Unit} -> Type)
adamc@418: (f : nm :: Name -> rest :: {Unit}
adamc@418: -> fn [[nm] ~ rest] =>
adamc@418: tf1 -> tf2 -> tr rest -> tr ([nm] ++ rest))
adamc@418: (i : tr []) =
adamc@418: fold [fn r :: {Unit} => $(mapUT tf1 r) -> $(mapUT tf2 r) -> tr r]
adamc@418: (fn (nm :: Name) (t :: Unit) (rest :: {Unit}) acc
adamc@418: [[nm] ~ rest] r1 r2 =>
adamc@418: f [nm] [rest] r1.nm r2.nm (acc (r1 -- nm) (r2 -- nm)))
adamc@418: (fn _ _ => i)
adamc@418:
adamc@418: fun foldURX2 (tf1 :: Type) (tf2 :: Type) (ctx :: {Unit})
adamc@418: (f : nm :: Name -> rest :: {Unit}
adamc@418: -> fn [[nm] ~ rest] =>
adamc@418: tf1 -> tf2 -> xml ctx [] []) =
adamc@418: foldUR2 [tf1] [tf2] [fn _ => xml ctx [] []]
adamc@418: (fn (nm :: Name) (rest :: {Unit}) [[nm] ~ rest] v1 v2 acc =>
adamc@418: {f [nm] [rest] v1 v2}{acc})
adamc@418:
adamc@418:
adamc@336: fun foldTR (tf :: Type -> Type) (tr :: {Type} -> Type)
adamc@356: (f : nm :: Name -> t :: Type -> rest :: {Type}
adamc@356: -> fn [[nm] ~ rest] =>
adamc@356: tf t -> tr rest -> tr ([nm = t] ++ rest))
adamc@356: (i : tr []) =
adamc@356: fold [fn r :: {Type} => $(mapTT tf r) -> tr r]
adamc@356: (fn (nm :: Name) (t :: Type) (rest :: {Type}) (acc : _ -> tr rest)
adamc@356: [[nm] ~ rest] r =>
adamc@356: f [nm] [t] [rest] r.nm (acc (r -- nm)))
adamc@356: (fn _ => i)
adamc@336:
adamc@339: fun foldT2R (tf :: (Type * Type) -> Type) (tr :: {(Type * Type)} -> Type)
adamc@367: (f : nm :: Name -> t :: (Type * Type) -> rest :: {(Type * Type)}
adamc@367: -> fn [[nm] ~ rest] =>
adamc@367: tf t -> tr rest -> tr ([nm = t] ++ rest))
adamc@367: (i : tr []) =
adamc@356: fold [fn r :: {(Type * Type)} => $(mapT2T tf r) -> tr r]
adamc@356: (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)})
adamc@356: (acc : _ -> tr rest) [[nm] ~ rest] r =>
adamc@356: f [nm] [t] [rest] r.nm (acc (r -- nm)))
adamc@356: (fn _ => i)
adamc@339:
adamc@445: fun foldT3R (tf :: (Type * Type * Type) -> Type) (tr :: {(Type * Type * Type)} -> Type)
adamc@445: (f : nm :: Name -> t :: (Type * Type * Type) -> rest :: {(Type * Type * Type)}
adamc@445: -> fn [[nm] ~ rest] =>
adamc@445: tf t -> tr rest -> tr ([nm = t] ++ rest))
adamc@445: (i : tr []) =
adamc@445: fold [fn r :: {(Type * Type * Type)} => $(mapT3T tf r) -> tr r]
adamc@445: (fn (nm :: Name) (t :: (Type * Type * Type)) (rest :: {(Type * Type * Type)})
adamc@445: (acc : _ -> tr rest) [[nm] ~ rest] r =>
adamc@445: f [nm] [t] [rest] r.nm (acc (r -- nm)))
adamc@445: (fn _ => i)
adamc@445:
adamc@330: fun foldTR2 (tf1 :: Type -> Type) (tf2 :: Type -> Type) (tr :: {Type} -> Type)
adamc@367: (f : nm :: Name -> t :: Type -> rest :: {Type}
adamc@367: -> fn [[nm] ~ rest] =>
adamc@367: tf1 t -> tf2 t -> tr rest -> tr ([nm = t] ++ rest))
adamc@367: (i : tr []) =
adamc@356: fold [fn r :: {Type} => $(mapTT tf1 r) -> $(mapTT tf2 r) -> tr r]
adamc@356: (fn (nm :: Name) (t :: Type) (rest :: {Type})
adamc@356: (acc : _ -> _ -> tr rest) [[nm] ~ rest] r1 r2 =>
adamc@356: f [nm] [t] [rest] r1.nm r2.nm (acc (r1 -- nm) (r2 -- nm)))
adamc@356: (fn _ _ => i)
adamc@332:
adamc@356: fun foldT2R2 (tf1 :: (Type * Type) -> Type) (tf2 :: (Type * Type) -> Type)
adamc@356: (tr :: {(Type * Type)} -> Type)
adamc@356: (f : nm :: Name -> t :: (Type * Type) -> rest :: {(Type * Type)}
adamc@356: -> fn [[nm] ~ rest] =>
adamc@356: tf1 t -> tf2 t -> tr rest -> tr ([nm = t] ++ rest))
adamc@356: (i : tr []) =
adamc@356: fold [fn r :: {(Type * Type)} => $(mapT2T tf1 r) -> $(mapT2T tf2 r) -> tr r]
adamc@356: (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)})
adamc@356: (acc : _ -> _ -> tr rest) [[nm] ~ rest] r1 r2 =>
adamc@356: f [nm] [t] [rest] r1.nm r2.nm (acc (r1 -- nm) (r2 -- nm)))
adamc@356: (fn _ _ => i)
adamc@339:
adamc@445: fun foldT3R2 (tf1 :: (Type * Type * Type) -> Type) (tf2 :: (Type * Type * Type) -> Type)
adamc@445: (tr :: {(Type * Type * Type)} -> Type)
adamc@445: (f : nm :: Name -> t :: (Type * Type * Type) -> rest :: {(Type * Type * Type)}
adamc@445: -> fn [[nm] ~ rest] =>
adamc@445: tf1 t -> tf2 t -> tr rest -> tr ([nm = t] ++ rest))
adamc@445: (i : tr []) =
adamc@445: fold [fn r :: {(Type * Type * Type)} => $(mapT3T tf1 r) -> $(mapT3T tf2 r) -> tr r]
adamc@445: (fn (nm :: Name) (t :: (Type * Type * Type)) (rest :: {(Type * Type * Type)})
adamc@445: (acc : _ -> _ -> tr rest) [[nm] ~ rest] r1 r2 =>
adamc@445: f [nm] [t] [rest] r1.nm r2.nm (acc (r1 -- nm) (r2 -- nm)))
adamc@445: (fn _ _ => i)
adamc@445:
adamc@336: fun foldTRX (tf :: Type -> Type) (ctx :: {Unit})
adamc@356: (f : nm :: Name -> t :: Type -> rest :: {Type}
adamc@356: -> fn [[nm] ~ rest] =>
adamc@356: tf t -> xml ctx [] []) =
adamc@356: foldTR [tf] [fn _ => xml ctx [] []]
adamc@356: (fn (nm :: Name) (t :: Type) (rest :: {Type}) [[nm] ~ rest] r acc =>
adamc@356: {f [nm] [t] [rest] r}{acc})
adamc@360:
adamc@336:
adamc@339: fun foldT2RX (tf :: (Type * Type) -> Type) (ctx :: {Unit})
adamc@356: (f : nm :: Name -> t :: (Type * Type) -> rest :: {(Type * Type)}
adamc@356: -> fn [[nm] ~ rest] =>
adamc@356: tf t -> xml ctx [] []) =
adamc@356: foldT2R [tf] [fn _ => xml ctx [] []]
adamc@356: (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)})
adamc@356: [[nm] ~ rest] r acc =>
adamc@356: {f [nm] [t] [rest] r}{acc})
adamc@360:
adamc@339:
adamc@445: fun foldT3RX (tf :: (Type * Type * Type) -> Type) (ctx :: {Unit})
adamc@445: (f : nm :: Name -> t :: (Type * Type * Type) -> rest :: {(Type * Type * Type)}
adamc@445: -> fn [[nm] ~ rest] =>
adamc@445: tf t -> xml ctx [] []) =
adamc@445: foldT3R [tf] [fn _ => xml ctx [] []]
adamc@445: (fn (nm :: Name) (t :: (Type * Type * Type)) (rest :: {(Type * Type * Type)})
adamc@445: [[nm] ~ rest] r acc =>
adamc@445: {f [nm] [t] [rest] r}{acc})
adamc@445:
adamc@445:
adamc@332: fun foldTRX2 (tf1 :: Type -> Type) (tf2 :: Type -> Type) (ctx :: {Unit})
adamc@356: (f : nm :: Name -> t :: Type -> rest :: {Type}
adamc@356: -> fn [[nm] ~ rest] =>
adamc@356: tf1 t -> tf2 t -> xml ctx [] []) =
adamc@356: foldTR2 [tf1] [tf2] [fn _ => xml ctx [] []]
adamc@356: (fn (nm :: Name) (t :: Type) (rest :: {Type}) [[nm] ~ rest]
adamc@356: r1 r2 acc =>
adamc@356: {f [nm] [t] [rest] r1 r2}{acc})
adamc@360:
adamc@334:
adamc@356: fun foldT2RX2 (tf1 :: (Type * Type) -> Type) (tf2 :: (Type * Type) -> Type)
adamc@356: (ctx :: {Unit})
adamc@356: (f : nm :: Name -> t :: (Type * Type) -> rest :: {(Type * Type)}
adamc@356: -> fn [[nm] ~ rest] =>
adamc@356: tf1 t -> tf2 t -> xml ctx [] []) =
adamc@356: foldT2R2 [tf1] [tf2] [fn _ => xml ctx [] []]
adamc@356: (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)})
adamc@356: [[nm] ~ rest] r1 r2 acc =>
adamc@356: {f [nm] [t] [rest] r1 r2}{acc})
adamc@360:
adamc@339:
adamc@445: fun foldT3RX2 (tf1 :: (Type * Type * Type) -> Type) (tf2 :: (Type * Type * Type) -> Type)
adamc@445: (ctx :: {Unit})
adamc@445: (f : nm :: Name -> t :: (Type * Type * Type) -> rest :: {(Type * Type * Type)}
adamc@445: -> fn [[nm] ~ rest] =>
adamc@445: tf1 t -> tf2 t -> xml ctx [] []) =
adamc@445: foldT3R2 [tf1] [tf2] [fn _ => xml ctx [] []]
adamc@445: (fn (nm :: Name) (t :: (Type * Type * Type)) (rest :: {(Type * Type * Type)})
adamc@445: [[nm] ~ rest] r1 r2 acc =>
adamc@445: {f [nm] [t] [rest] r1 r2}{acc})
adamc@445:
adamc@445:
adamc@356: fun queryX (tables ::: {{Type}}) (exps ::: {Type}) (ctx ::: {Unit})
adamc@356: (q : sql_query tables exps) [tables ~ exps]
adamc@356: (f : $(exps ++ fold (fn nm (fields :: {Type}) acc [[nm] ~ acc] =>
adamc@356: [nm = $fields] ++ acc) [] tables)
adamc@356: -> xml ctx [] []) =
adamc@356: query q
adamc@356: (fn fs acc => return {acc}{f fs})
adamc@360:
adamc@341:
adamc@469: fun queryX' (tables ::: {{Type}}) (exps ::: {Type}) (ctx ::: {Unit})
adamc@469: (q : sql_query tables exps) [tables ~ exps]
adamc@469: (f : $(exps ++ fold (fn nm (fields :: {Type}) acc [[nm] ~ acc] =>
adamc@469: [nm = $fields] ++ acc) [] tables)
adamc@469: -> transaction (xml ctx [] [])) =
adamc@469: query q
adamc@469: (fn fs acc =>
adamc@469: r <- f fs;
adamc@469: return {acc}{r})
adamc@469:
adamc@469:
adamc@356: fun oneOrNoRows (tables ::: {{Type}}) (exps ::: {Type})
adamc@356: (q : sql_query tables exps) [tables ~ exps] =
adamc@356: query q
adamc@356: (fn fs _ => return (Some fs))
adamc@356: None
adamc@440:
adamc@440: fun oneRow (tables ::: {{Type}}) (exps ::: {Type})
adamc@440: (q : sql_query tables exps) [tables ~ exps] =
adamc@440: o <- oneOrNoRows q;
adamc@440: return (case o of
adamc@440: None => error Query returned no rows
adamc@440: | Some r => r)
adamc@440:
adamc@470: fun eqNullable (tables ::: {{Type}}) (agg ::: {{Type}}) (exps ::: {Type})
adamc@470: (t ::: Type) (_ : sql_injectable (option t))
adamc@470: (e1 : sql_exp tables agg exps (option t))
adamc@470: (e2 : sql_exp tables agg exps (option t)) =
adamc@471: (SQL ({e1} IS NULL AND {e2} IS NULL) OR {e1} = {e2})
adamc@470:
adamc@470: fun eqNullable' (tables ::: {{Type}}) (agg ::: {{Type}}) (exps ::: {Type})
adamc@470: (t ::: Type) (inj : sql_injectable (option t))
adamc@470: (e1 : sql_exp tables agg exps (option t))
adamc@470: (e2 : option t) =
adamc@470: case e2 of
adamc@471: None => (SQL {e1} IS NULL)
adamc@470: | Some _ => sql_comparison sql_eq e1 (@sql_inject inj e2)