adamc@623: (** Row folding *) adamc@623: adamc@631: con folder :: K --> {K} -> Type adamc@623: adamc@653: val fold : K --> tf :: ({K} -> Type) adamc@653: -> (nm :: Name -> v :: K -> r :: {K} -> [[nm] ~ r] => adamc@653: tf r -> tf ([nm = v] ++ r)) adamc@653: -> tf [] adamc@1093: -> r ::: {K} -> folder r -> tf r adamc@653: adamc@627: structure Folder : sig adamc@627: val nil : K --> folder (([]) :: {K}) adamc@627: val cons : K --> r ::: {K} -> nm :: Name -> v :: K adamc@629: -> [[nm] ~ r] => folder r -> folder ([nm = v] ++ r) adamc@628: val concat : K --> r1 ::: {K} -> r2 ::: {K} adamc@629: -> [r1 ~ r2] => folder r1 -> folder r2 -> folder (r1 ++ r2) adamc@630: val mp : K1 --> K2 --> f ::: (K1 -> K2) -> r ::: {K1} adamc@630: -> folder r -> folder (map f r) adamc@627: end adamc@627: adamc@623: adamc@422: val not : bool -> bool adamc@422: greenrd@1480: (* Type-level identity function *) adam@1649: con ident = K ==> fn t :: K => t greenrd@1480: greenrd@1480: (* Type-level function which yields the value-level record greenrd@1480: described by the given type-level record *) adamc@329: con record = fn t :: {Type} => $t greenrd@1480: adamc@637: con fst = K1 ==> K2 ==> fn t :: (K1 * K2) => t.1 adamc@637: con snd = K1 ==> K2 ==> fn t :: (K1 * K2) => t.2 adamc@637: con fst3 = K1 ==> K2 ==> K3 ==> fn t :: (K1 * K2 * K3) => t.1 adamc@637: con snd3 = K1 ==> K2 ==> K3 ==> fn t :: (K1 * K2 * K3) => t.2 adamc@637: con thd3 = K1 ==> K2 ==> K3 ==> fn t :: (K1 * K2 * K3) => t.3 adamc@329: greenrd@1480: (* Convert a record of n Units into a type-level record where greenrd@1480: each field has the same value (which describes a uniformly greenrd@1480: typed record) *) adamc@643: con mapU = K ==> fn f :: K => map (fn _ :: Unit => f) adamc@445: greenrd@1480: (* Existential type former *) adam@1434: con ex :: K --> (K -> Type) -> Type adamc@339: greenrd@1480: (* Introduction of existential type *) adam@1434: val ex_intro : K --> tf :: (K -> Type) -> choice :: K -> tf choice -> ex tf greenrd@1480: greenrd@1480: (* Eliminator for existential type *) adam@1434: val ex_elim : K --> tf ::: (K -> Type) -> ex tf -> res ::: Type -> (choice :: K -> tf choice -> res) -> res adamc@339: greenrd@1480: (* Composition of ordinary (value-level) functions *) adamc@325: val compose : t1 ::: Type -> t2 ::: Type -> t3 ::: Type adamc@355: -> (t2 -> t3) -> (t1 -> t2) -> (t1 -> t3) adamc@325: adamc@777: val show_option : t ::: Type -> show t -> show (option t) adamc@777: val read_option : t ::: Type -> read t -> read (option t) adamc@777: adamc@720: val txt : t ::: Type -> ctx ::: {Unit} -> use ::: {Type} -> show t -> t adamc@720: -> xml ctx use [] adamc@329: greenrd@1480: (* Given a polymorphic type (tf) and a means of constructing greenrd@1480: "default" values of tf applied to arbitrary arguments, greenrd@1480: constructs records consisting of those "default" values *) adamc@993: val map0 : K --> tf :: (K -> Type) adamc@993: -> (t :: K -> tf t) adamc@1093: -> r ::: {K} -> folder r -> $(map tf r) greenrd@1480: greenrd@1480: (* Given two polymorphic types (tf1 and tf2) and a means of greenrd@1480: converting from tf1 t to tf2 t for arbitrary t, greenrd@1480: converts records of tf1's to records of tf2's *) adamc@898: val mp : K --> tf1 :: (K -> Type) -> tf2 :: (K -> Type) adamc@898: -> (t ::: K -> tf1 t -> tf2 t) adamc@1093: -> r ::: {K} -> folder r -> $(map tf1 r) -> $(map tf2 r) greenrd@1480: greenrd@1480: (* Two-argument conversion form of mp *) adamc@937: val map2 : K --> tf1 :: (K -> Type) -> tf2 :: (K -> Type) -> tf :: (K -> Type) adamc@937: -> (t ::: K -> tf1 t -> tf2 t -> tf t) adamc@1093: -> r ::: {K} -> folder r -> $(map tf1 r) -> $(map tf2 r) -> $(map tf r) greenrd@1480: greenrd@1480: (* Three-argument conversion form of mp *) adamc@937: val map3 : K --> tf1 :: (K -> Type) -> tf2 :: (K -> Type) -> tf3 :: (K -> Type) -> tf :: (K -> Type) adamc@937: -> (t ::: K -> tf1 t -> tf2 t -> tf3 t -> tf t) adamc@1093: -> r ::: {K} -> folder r -> $(map tf1 r) -> $(map tf2 r) -> $(map tf3 r) -> $(map tf r) adamc@898: greenrd@1480: (* Fold along a uniformly (homogenously) typed record *) adamc@411: val foldUR : tf :: Type -> tr :: ({Unit} -> Type) adamc@411: -> (nm :: Name -> rest :: {Unit} adamc@629: -> [[nm] ~ rest] => adamc@411: tf -> tr rest -> tr ([nm] ++ rest)) adamc@1093: -> tr [] -> r ::: {Unit} -> folder r -> $(mapU tf r) -> tr r adamc@411: greenrd@1480: (* Fold (generalized safe zip) along two equal-length uniformly-typed records *) adamc@418: val foldUR2 : tf1 :: Type -> tf2 :: Type -> tr :: ({Unit} -> Type) adamc@418: -> (nm :: Name -> rest :: {Unit} adamc@629: -> [[nm] ~ rest] => adamc@418: tf1 -> tf2 -> tr rest -> tr ([nm] ++ rest)) adamc@1093: -> tr [] -> r ::: {Unit} -> folder r -> $(mapU tf1 r) -> $(mapU tf2 r) -> tr r adamc@418: greenrd@1480: (* Fold along a heterogenously-typed record *) adamc@623: val foldR : K --> tf :: (K -> Type) -> tr :: ({K} -> Type) adamc@623: -> (nm :: Name -> t :: K -> rest :: {K} adamc@629: -> [[nm] ~ rest] => adamc@355: tf t -> tr rest -> tr ([nm = t] ++ rest)) adamc@1093: -> tr [] -> r ::: {K} -> folder r -> $(map tf r) -> tr r adamc@336: greenrd@1480: (* Fold (generalized safe zip) along two heterogenously-typed records *) adamc@623: val foldR2 : K --> tf1 :: (K -> Type) -> tf2 :: (K -> Type) adamc@623: -> tr :: ({K} -> Type) adamc@623: -> (nm :: Name -> t :: K -> rest :: {K} adamc@629: -> [[nm] ~ rest] => adamc@623: tf1 t -> tf2 t -> tr rest -> tr ([nm = t] ++ rest)) adamc@623: -> tr [] adamc@1093: -> r ::: {K} -> folder r -> $(map tf1 r) -> $(map tf2 r) -> tr r adamc@623: greenrd@1480: (* Fold (generalized safe zip) along three heterogenously-typed records *) adamc@910: val foldR3 : K --> tf1 :: (K -> Type) -> tf2 :: (K -> Type) -> tf3 :: (K -> Type) adamc@910: -> tr :: ({K} -> Type) adamc@910: -> (nm :: Name -> t :: K -> rest :: {K} adamc@910: -> [[nm] ~ rest] => adamc@910: tf1 t -> tf2 t -> tf3 t -> tr rest -> tr ([nm = t] ++ rest)) adamc@910: -> tr [] adamc@1093: -> r ::: {K} -> folder r -> $(map tf1 r) -> $(map tf2 r) -> $(map tf3 r) -> tr r adamc@910: greenrd@1480: (* Generate some XML by mapping over a uniformly-typed record *) adamc@1172: val mapUX : tf :: Type -> ctx :: {Unit} adamc@1172: -> (nm :: Name -> rest :: {Unit} -> [[nm] ~ rest] => adamc@1172: tf -> xml ctx [] []) adamc@1172: -> r ::: {Unit} -> folder r -> $(mapU tf r) -> xml ctx [] [] adamc@623: greenrd@1480: (* Generate some XML by mapping over a heterogenously-typed record *) adamc@1172: val mapX : K --> tf :: (K -> Type) -> ctx :: {Unit} adamc@1172: -> (nm :: Name -> t :: K -> rest :: {K} adamc@1172: -> [[nm] ~ rest] => adamc@1172: tf t -> xml ctx [] []) adamc@1172: -> r ::: {K} -> folder r -> $(map tf r) -> xml ctx [] [] adamc@445: adamc@1173: val mapUX2 : tf1 :: Type -> tf2 :: Type -> ctx :: {Unit} adamc@1173: -> (nm :: Name -> rest :: {Unit} adamc@1173: -> [[nm] ~ rest] => adamc@1173: tf1 -> tf2 -> xml ctx [] []) adamc@1173: -> r ::: {Unit} -> folder r adamc@1173: -> $(mapU tf1 r) -> $(mapU tf2 r) -> xml ctx [] [] adamc@1173: adamc@1172: val mapX2 : K --> tf1 :: (K -> Type) -> tf2 :: (K -> Type) -> ctx :: {Unit} adamc@1172: -> (nm :: Name -> t :: K -> rest :: {K} adamc@1172: -> [[nm] ~ rest] => adamc@1172: tf1 t -> tf2 t -> xml ctx [] []) adamc@1172: -> r ::: {K} -> folder r adamc@1172: -> $(map tf1 r) -> $(map tf2 r) -> xml ctx [] [] adamc@1172: adamc@1172: val mapX3 : K --> tf1 :: (K -> Type) -> tf2 :: (K -> Type) -> tf3 :: (K -> Type) -> ctx :: {Unit} adamc@1172: -> (nm :: Name -> t :: K -> rest :: {K} adamc@1172: -> [[nm] ~ rest] => adamc@1172: tf1 t -> tf2 t -> tf3 t -> xml ctx [] []) adamc@1172: -> r ::: {K} -> folder r adamc@1172: -> $(map tf1 r) -> $(map tf2 r) -> $(map tf3 r) -> xml ctx [] [] adamc@910: adam@2011: (* Note that the next two functions return elements in the _reverse_ of the natural order! adam@2011: * Such a choice interacts well with the time complexity of standard list operations. adam@2011: * It's easy to regain the natural order by inverting a query's 'ORDER BY' condition. *) adam@2011: adamc@1081: val queryL : tables ::: {{Type}} -> exps ::: {Type} adamc@1081: -> [tables ~ exps] => adam@1394: sql_query [] [] tables exps adamc@1081: -> transaction (list $(exps ++ map (fn fields :: {Type} => $fields) tables)) adamc@1081: adam@1321: val queryL1 : t ::: Name -> fs ::: {Type} adam@1394: -> sql_query [] [] [t = fs] [] adam@1321: -> transaction (list $fs) adam@1321: adamc@1177: val query1 : t ::: Name -> fs ::: {Type} -> state ::: Type adam@1394: -> sql_query [] [] [t = fs] [] adamc@1177: -> ($fs -> state -> transaction state) adamc@1177: -> state adamc@1177: -> transaction state adamc@1177: adamc@1177: val query1' : t ::: Name -> fs ::: {Type} -> state ::: Type adam@1394: -> sql_query [] [] [t = fs] [] adamc@1177: -> ($fs -> state -> state) adamc@1177: -> state adamc@1177: -> transaction state adamc@1177: adamc@682: val queryI : tables ::: {{Type}} -> exps ::: {Type} adamc@682: -> [tables ~ exps] => adam@1394: sql_query [] [] tables exps adamc@682: -> ($(exps ++ map (fn fields :: {Type} => $fields) tables) adamc@682: -> transaction unit) adamc@682: -> transaction unit adamc@682: adam@1363: val queryI1 : nm ::: Name -> fs ::: {Type} adam@1394: -> sql_query [] [] [nm = fs] [] adam@1363: -> ($fs -> transaction unit) adam@1363: -> transaction unit adam@1363: adamc@1004: val queryX : tables ::: {{Type}} -> exps ::: {Type} -> ctx ::: {Unit} -> inp ::: {Type} adamc@629: -> [tables ~ exps] => adam@1394: sql_query [] [] tables exps adamc@632: -> ($(exps ++ map (fn fields :: {Type} => $fields) tables) adamc@1004: -> xml ctx inp []) adamc@1004: -> transaction (xml ctx inp []) adamc@341: adam@1405: val queryXI : tables ::: {{Type}} -> exps ::: {Type} -> ctx ::: {Unit} -> inp ::: {Type} adam@1405: -> [tables ~ exps] => adam@1405: sql_query [] [] tables exps adam@1405: -> (int -> $(exps ++ map (fn fields :: {Type} => $fields) tables) adam@1405: -> xml ctx inp []) adam@1405: -> transaction (xml ctx inp []) adam@1405: adamc@1076: val queryX1 : nm ::: Name -> fs ::: {Type} -> ctx ::: {Unit} -> inp ::: {Type} adam@1394: -> sql_query [] [] [nm = fs] [] adamc@1076: -> ($fs -> xml ctx inp []) adamc@1076: -> transaction (xml ctx inp []) adamc@1076: adam@1405: val queryX1I : nm ::: Name -> fs ::: {Type} -> ctx ::: {Unit} -> inp ::: {Type} adam@1405: -> sql_query [] [] [nm = fs] [] adam@1405: -> (int -> $fs -> xml ctx inp []) adam@1405: -> transaction (xml ctx inp []) adam@1405: adamc@1032: val queryX' : tables ::: {{Type}} -> exps ::: {Type} -> ctx ::: {Unit} -> inp ::: {Type} adamc@629: -> [tables ~ exps] => adam@1394: sql_query [] [] tables exps adamc@632: -> ($(exps ++ map (fn fields :: {Type} => $fields) tables) adamc@1032: -> transaction (xml ctx inp [])) adamc@1032: -> transaction (xml ctx inp []) adamc@1110: val queryX1' : nm ::: Name -> fs ::: {Type} -> ctx ::: {Unit} -> inp ::: {Type} adam@1394: -> sql_query [] [] [nm = fs] [] adamc@1110: -> ($fs -> transaction (xml ctx inp [])) adamc@1110: -> transaction (xml ctx inp []) adamc@1110: val queryXE' : exps ::: {Type} -> ctx ::: {Unit} -> inp ::: {Type} adam@1394: -> sql_query [] [] [] exps adamc@1110: -> ($exps -> transaction (xml ctx inp [])) adamc@1110: -> transaction (xml ctx inp []) adamc@1110: adamc@1072: val hasRows : tables ::: {{Type}} -> exps ::: {Type} adamc@1072: -> [tables ~ exps] => adam@1394: sql_query [] [] tables exps adamc@1072: -> transaction bool adamc@1072: adamc@355: val oneOrNoRows : tables ::: {{Type}} -> exps ::: {Type} adamc@629: -> [tables ~ exps] => adam@1394: sql_query [] [] tables exps adamc@629: -> transaction adamc@629: (option adamc@629: $(exps adamc@629: ++ map (fn fields :: {Type} => $fields) tables)) adamc@440: adamc@1003: val oneOrNoRows1 : nm ::: Name -> fs ::: {Type} adam@1394: -> sql_query [] [] [nm = fs] [] adamc@1003: -> transaction (option $fs) adamc@1003: adamc@1076: val oneOrNoRowsE1 : tabs ::: {Unit} -> nm ::: Name -> t ::: Type adamc@1076: -> [tabs ~ [nm]] => adam@1394: sql_query [] [] (mapU [] tabs) [nm = t] adamc@1006: -> transaction (option t) adamc@1006: adamc@440: val oneRow : tables ::: {{Type}} -> exps ::: {Type} adamc@629: -> [tables ~ exps] => adam@1394: sql_query [] [] tables exps adamc@629: -> transaction adamc@629: $(exps adamc@629: ++ map (fn fields :: {Type} => $fields) tables) adamc@1003: adamc@1076: val oneRow1 : nm ::: Name -> fs ::: {Type} adam@1394: -> sql_query [] [] [nm = fs] [] adamc@1076: -> transaction $fs adamc@1076: adamc@1064: val oneRowE1 : tabs ::: {Unit} -> nm ::: Name -> t ::: Type adamc@1064: -> [tabs ~ [nm]] => adam@1394: sql_query [] [] (mapU [] tabs) [nm = t] adamc@1003: -> transaction t adamc@1003: adamc@1074: val nonempty : fs ::: {Type} -> us ::: {{Unit}} -> sql_table fs us adamc@1074: -> transaction bool adamc@1074: adamc@470: val eqNullable : tables ::: {{Type}} -> agg ::: {{Type}} -> exps ::: {Type} adam@1778: -> t ::: Type -> sql_injectable (option t) adam@1778: -> sql_exp tables agg exps (option t) adam@1778: -> sql_exp tables agg exps (option t) adam@1778: -> sql_exp tables agg exps bool adamc@470: adamc@470: val eqNullable' : tables ::: {{Type}} -> agg ::: {{Type}} -> exps ::: {Type} adam@1778: -> t ::: Type -> sql_injectable (option t) adam@1778: -> sql_exp tables agg exps (option t) adamc@470: -> option t adam@1778: -> sql_exp tables agg exps bool adam@1360: adam@1360: val mkRead' : t ::: Type -> (string -> option t) -> string -> read t adam@1787: adam@1787: val postFields : postBody -> list (string * string)