annotate lib/basis.urs @ 253:7f6620853c36

Monoized a WHERE clause with a comparison
author Adam Chlipala <adamc@hcoop.net>
date Sun, 31 Aug 2008 14:27:01 -0400
parents 7e9bd70ad3ce
children e52243e20858
rev   line source
adamc@56 1 type int
adamc@56 2 type float
adamc@56 3 type string
adamc@91 4
adamc@119 5 type unit = {}
adamc@119 6
adamc@186 7 datatype bool = False | True
adamc@186 8
adamc@91 9
adamc@203 10 (** SQL *)
adamc@203 11
adamc@203 12 con sql_table :: {Type} -> Type
adamc@203 13
adamc@204 14 (*** Queries *)
adamc@204 15
adamc@233 16 con sql_query :: {{Type}} -> {Type} -> Type
adamc@233 17 con sql_query1 :: {{Type}} -> {{Type}} -> {Type} -> Type
adamc@234 18 con sql_exp :: {{Type}} -> {{Type}} -> {Type} -> Type -> Type
adamc@204 19
adamc@223 20 con sql_subset :: {{Type}} -> {{Type}} -> Type
adamc@223 21 val sql_subset : keep_drop :: {({Type} * {Type})}
adamc@223 22 -> sql_subset
adamc@223 23 (fold (fn nm => fn fields :: ({Type} * {Type}) => fn acc =>
adamc@223 24 [nm] ~ acc => fields.1 ~ fields.2 =>
adamc@223 25 [nm = fields.1 ++ fields.2] ++ acc) [] keep_drop)
adamc@223 26 (fold (fn nm => fn fields :: ({Type} * {Type}) => fn acc =>
adamc@223 27 [nm] ~ acc =>
adamc@223 28 [nm = fields.1] ++ acc) [] keep_drop)
adamc@223 29 val sql_subset_all : tables :: {{Type}}
adamc@223 30 -> sql_subset tables tables
adamc@223 31
adamc@229 32 val sql_query1 : tables ::: {{Type}}
adamc@224 33 -> grouped ::: {{Type}}
adamc@233 34 -> selectedFields ::: {{Type}}
adamc@233 35 -> selectedExps ::: {Type}
adamc@223 36 -> {From : $(fold (fn nm => fn fields :: {Type} => fn acc =>
adamc@223 37 [nm] ~ acc => [nm = sql_table fields] ++ acc) [] tables),
adamc@234 38 Where : sql_exp tables [] [] bool,
adamc@224 39 GroupBy : sql_subset tables grouped,
adamc@234 40 Having : sql_exp grouped tables [] bool,
adamc@233 41 SelectFields : sql_subset grouped selectedFields,
adamc@233 42 SelectExps : $(fold (fn nm => fn t :: Type => fn acc =>
adamc@234 43 [nm] ~ acc => [nm = sql_exp grouped tables [] t] ++ acc) [] selectedExps) }
adamc@233 44 -> sql_query1 tables selectedFields selectedExps
adamc@229 45
adamc@229 46 type sql_relop
adamc@229 47 val sql_union : sql_relop
adamc@229 48 val sql_intersect : sql_relop
adamc@229 49 val sql_except : sql_relop
adamc@229 50 val sql_relop : sql_relop
adamc@229 51 -> tables1 ::: {{Type}}
adamc@229 52 -> tables2 ::: {{Type}}
adamc@233 53 -> selectedFields ::: {{Type}}
adamc@233 54 -> selectedExps ::: {Type}
adamc@233 55 -> sql_query1 tables1 selectedFields selectedExps
adamc@233 56 -> sql_query1 tables2 selectedFields selectedExps
adamc@233 57 -> sql_query1 selectedFields selectedFields selectedExps
adamc@229 58
adamc@230 59 type sql_direction
adamc@230 60 val sql_asc : sql_direction
adamc@230 61 val sql_desc : sql_direction
adamc@230 62
adamc@234 63 con sql_order_by :: {{Type}} -> {Type} -> Type
adamc@234 64 val sql_order_by_Nil : tables ::: {{Type}} -> exps :: {Type} -> sql_order_by tables exps
adamc@234 65 val sql_order_by_Cons : tables ::: {{Type}} -> exps ::: {Type} -> t ::: Type
adamc@234 66 -> sql_exp tables [] exps t -> sql_order_by tables exps
adamc@234 67 -> sql_order_by tables exps
adamc@230 68
adamc@231 69 type sql_limit
adamc@231 70 val sql_no_limit : sql_limit
adamc@231 71 val sql_limit : int -> sql_limit
adamc@231 72
adamc@232 73 type sql_offset
adamc@232 74 val sql_no_offset : sql_offset
adamc@232 75 val sql_offset : int -> sql_offset
adamc@232 76
adamc@229 77 val sql_query : tables ::: {{Type}}
adamc@233 78 -> selectedFields ::: {{Type}}
adamc@233 79 -> selectedExps ::: {Type}
adamc@233 80 -> {Rows : sql_query1 tables selectedFields selectedExps,
adamc@234 81 OrderBy : sql_order_by tables selectedExps,
adamc@232 82 Limit : sql_limit,
adamc@232 83 Offset : sql_offset}
adamc@233 84 -> sql_query selectedFields selectedExps
adamc@204 85
adamc@225 86 val sql_field : otherTabs ::: {{Type}} -> otherFields ::: {Type} -> fieldType ::: Type -> agg ::: {{Type}}
adamc@234 87 -> exps ::: {Type}
adamc@221 88 -> tab :: Name -> field :: Name
adamc@234 89 -> sql_exp ([tab = [field = fieldType] ++ otherFields] ++ otherTabs) agg exps fieldType
adamc@234 90
adamc@234 91 val sql_exp : tabs ::: {{Type}} -> agg ::: {{Type}} -> t ::: Type -> rest ::: {Type} -> nm :: Name
adamc@234 92 -> sql_exp tabs agg ([nm = t] ++ rest) t
adamc@209 93
adamc@221 94 class sql_injectable
adamc@221 95 val sql_bool : sql_injectable bool
adamc@221 96 val sql_int : sql_injectable int
adamc@221 97 val sql_float : sql_injectable float
adamc@221 98 val sql_string : sql_injectable string
adamc@234 99 val sql_inject : tables ::: {{Type}} -> agg ::: {{Type}} -> exps ::: {Type} -> t ::: Type
adamc@234 100 -> sql_injectable t -> t -> sql_exp tables agg exps t
adamc@209 101
adamc@220 102 con sql_unary :: Type -> Type -> Type
adamc@220 103 val sql_not : sql_unary bool bool
adamc@234 104 val sql_unary : tables ::: {{Type}} -> agg ::: {{Type}} -> exps ::: {Type} -> arg ::: Type -> res ::: Type
adamc@234 105 -> sql_unary arg res -> sql_exp tables agg exps arg -> sql_exp tables agg exps res
adamc@220 106
adamc@220 107 con sql_binary :: Type -> Type -> Type -> Type
adamc@220 108 val sql_and : sql_binary bool bool bool
adamc@220 109 val sql_or : sql_binary bool bool bool
adamc@234 110 val sql_binary : tables ::: {{Type}} -> agg ::: {{Type}} -> exps ::: {Type}
adamc@234 111 -> arg1 ::: Type -> arg2 ::: Type -> res ::: Type
adamc@234 112 -> sql_binary arg1 arg2 res -> sql_exp tables agg exps arg1 -> sql_exp tables agg exps arg2
adamc@234 113 -> sql_exp tables agg exps res
adamc@220 114
adamc@219 115 type sql_comparison
adamc@219 116 val sql_eq : sql_comparison
adamc@219 117 val sql_ne : sql_comparison
adamc@219 118 val sql_lt : sql_comparison
adamc@219 119 val sql_le : sql_comparison
adamc@219 120 val sql_gt : sql_comparison
adamc@219 121 val sql_ge : sql_comparison
adamc@253 122 val sql_comparison : tables ::: {{Type}} -> agg ::: {{Type}} -> exps ::: {Type}
adamc@236 123 -> t ::: Type
adamc@253 124 -> sql_comparison
adamc@234 125 -> sql_exp tables agg exps t -> sql_exp tables agg exps t
adamc@234 126 -> sql_exp tables agg exps bool
adamc@203 127
adamc@235 128 val sql_count : tables ::: {{Type}} -> agg ::: {{Type}} -> exps ::: {Type}
adamc@235 129 -> unit -> sql_exp tables agg exps int
adamc@235 130
adamc@236 131 con sql_aggregate :: Type -> Type
adamc@236 132 val sql_aggregate : tables ::: {{Type}} -> agg ::: {{Type}} -> exps ::: {Type} -> t ::: Type
adamc@236 133 -> sql_aggregate t -> sql_exp agg agg exps t -> sql_exp tables agg exps t
adamc@236 134
adamc@236 135 class sql_summable
adamc@236 136 val sql_summable_int : sql_summable int
adamc@236 137 val sql_summable_float : sql_summable float
adamc@236 138 val sql_avg : t ::: Type -> sql_summable t -> sql_aggregate t
adamc@236 139 val sql_sum : t ::: Type -> sql_summable t -> sql_aggregate t
adamc@236 140
adamc@236 141 class sql_maxable
adamc@236 142 val sql_maxable_int : sql_maxable int
adamc@236 143 val sql_maxable_float : sql_maxable float
adamc@236 144 val sql_maxable_string : sql_maxable string
adamc@236 145 val sql_max : t ::: Type -> sql_maxable t -> sql_aggregate t
adamc@236 146 val sql_min : t ::: Type -> sql_maxable t -> sql_aggregate t
adamc@236 147
adamc@235 148
adamc@243 149 (*** Executing queries *)
adamc@243 150
adamc@243 151 con transaction :: Type -> Type
adamc@243 152 val return : t ::: Type
adamc@243 153 -> t -> transaction t
adamc@243 154 val bind : t1 ::: Type -> t2 ::: Type
adamc@243 155 -> transaction t1 -> (t1 -> transaction t2)
adamc@243 156 -> transaction t2
adamc@243 157
adamc@251 158 val query : tables ::: {{Type}} -> exps ::: {Type} -> tables ~ exps
adamc@252 159 -> state ::: Type
adamc@243 160 -> sql_query tables exps
adamc@251 161 -> ($(exps ++ fold (fn nm (fields :: {Type}) acc => [nm] ~ acc => [nm = $fields] ++ acc) [] tables)
adamc@243 162 -> state
adamc@243 163 -> transaction state)
adamc@243 164 -> state
adamc@243 165 -> transaction state
adamc@243 166
adamc@243 167
adamc@203 168 (** XML *)
adamc@203 169
adamc@139 170 con tag :: {Type} -> {Unit} -> {Unit} -> {Type} -> {Type} -> Type
adamc@91 171
adamc@91 172
adamc@139 173 con xml :: {Unit} -> {Type} -> {Type} -> Type
adamc@141 174 val cdata : ctx ::: {Unit} -> use ::: {Type} -> string -> xml ctx use []
adamc@104 175 val tag : attrsGiven ::: {Type} -> attrsAbsent ::: {Type} -> attrsGiven ~ attrsAbsent
adamc@139 176 -> ctxOuter ::: {Unit} -> ctxInner ::: {Unit}
adamc@139 177 -> useOuter ::: {Type} -> useInner ::: {Type} -> useOuter ~ useInner
adamc@139 178 -> bindOuter ::: {Type} -> bindInner ::: {Type} -> bindOuter ~ bindInner
adamc@104 179 -> $attrsGiven
adamc@139 180 -> tag (attrsGiven ++ attrsAbsent) ctxOuter ctxInner useOuter bindOuter
adamc@139 181 -> xml ctxInner useInner bindInner
adamc@139 182 -> xml ctxOuter (useOuter ++ useInner) (bindOuter ++ bindInner)
adamc@140 183 val join : ctx ::: {Unit}
adamc@139 184 -> use1 ::: {Type} -> bind1 ::: {Type} -> bind2 ::: {Type}
adamc@139 185 -> use1 ~ bind1 -> bind1 ~ bind2
adamc@140 186 -> xml ctx use1 bind1
adamc@140 187 -> xml ctx (use1 ++ bind1) bind2
adamc@140 188 -> xml ctx use1 (bind1 ++ bind2)
adamc@148 189 val useMore : ctx ::: {Unit} -> use1 ::: {Type} -> use2 ::: {Type} -> bind ::: {Type}
adamc@148 190 -> use1 ~ use2
adamc@148 191 -> xml ctx use1 bind
adamc@148 192 -> xml ctx (use1 ++ use2) bind
adamc@91 193
adamc@110 194 con xhtml = xml [Html]
adamc@139 195 con page = xhtml [] []
adamc@110 196
adamc@204 197 (*** HTML details *)
adamc@204 198
adamc@140 199 con html = [Html]
adamc@140 200 con head = [Head]
adamc@140 201 con body = [Body]
adamc@141 202 con lform = [Body, LForm]
adamc@93 203
adamc@140 204 val head : unit -> tag [] html head [] []
adamc@140 205 val title : unit -> tag [] head [] [] []
adamc@110 206
adamc@140 207 val body : unit -> tag [] html body [] []
adamc@140 208 con bodyTag = fn attrs :: {Type} => ctx ::: {Unit} -> [Body] ~ ctx -> unit
adamc@140 209 -> tag attrs ([Body] ++ ctx) ([Body] ++ ctx) [] []
adamc@141 210 con bodyTagStandalone = fn attrs :: {Type} => ctx ::: {Unit} -> [Body] ~ ctx -> unit
adamc@141 211 -> tag attrs ([Body] ++ ctx) [] [] []
adamc@141 212
adamc@141 213 val br : bodyTagStandalone []
adamc@119 214
adamc@140 215 val p : bodyTag []
adamc@140 216 val b : bodyTag []
adamc@140 217 val i : bodyTag []
adamc@140 218 val font : bodyTag [Size = int, Face = string]
adamc@140 219
adamc@140 220 val h1 : bodyTag []
adamc@140 221 val li : bodyTag []
adamc@140 222
adamc@140 223 val a : bodyTag [Link = page]
adamc@140 224
adamc@141 225 val lform : ctx ::: {Unit} -> [Body] ~ ctx -> bind ::: {Type}
adamc@141 226 -> xml lform [] bind
adamc@141 227 -> xml ([Body] ++ ctx) [] []
adamc@153 228 con lformTag = fn ty :: Type => fn inner :: {Unit} => fn attrs :: {Type} =>
adamc@141 229 ctx ::: {Unit} -> [LForm] ~ ctx
adamc@141 230 -> nm :: Name -> unit
adamc@153 231 -> tag attrs ([LForm] ++ ctx) inner [] [nm = ty]
adamc@153 232 val textbox : lformTag string [] []
adamc@155 233 val password : lformTag string [] []
adamc@153 234 val ltextarea : lformTag string [] []
adamc@153 235
adamc@190 236 val checkbox : lformTag bool [] []
adamc@190 237
adamc@153 238 con radio = [Body, Radio]
adamc@153 239 val radio : lformTag string radio []
adamc@153 240 val radioOption : unit -> tag [Value = string] radio [] [] []
adamc@142 241
adamc@154 242 con select = [Select]
adamc@154 243 val lselect : lformTag string select []
adamc@154 244 val loption : unit -> tag [Value = string] select [] [] []
adamc@154 245
adamc@142 246 val submit : ctx ::: {Unit} -> [LForm] ~ ctx
adamc@142 247 -> use ::: {Type} -> unit
adamc@142 248 -> tag [Action = $use -> page] ([LForm] ++ ctx) ([LForm] ++ ctx) use []