annotate lib/basis.urs @ 559:5d494183ca89

Add SQL arithmetic operators
author Adam Chlipala <adamc@hcoop.net>
date Tue, 09 Dec 2008 14:41:19 -0500
parents 4154b4dc62c6
children 803b2f3bb86b
rev   line source
adamc@56 1 type int
adamc@56 2 type float
adamc@56 3 type string
adamc@436 4 type time
adamc@91 5
adamc@119 6 type unit = {}
adamc@119 7
adamc@186 8 datatype bool = False | True
adamc@186 9
adamc@288 10 datatype option t = None | Some of t
adamc@284 11
adamc@91 12
adamc@256 13 (** Basic type classes *)
adamc@256 14
adamc@256 15 class eq
adamc@256 16 val eq : t ::: Type -> eq t -> t -> t -> bool
adamc@257 17 val ne : t ::: Type -> eq t -> t -> t -> bool
adamc@256 18 val eq_int : eq int
adamc@394 19 val eq_float : eq float
adamc@256 20 val eq_string : eq string
adamc@256 21 val eq_bool : eq bool
adamc@437 22 val eq_time : eq time
adamc@422 23 val mkEq : t ::: Type -> (t -> t -> bool) -> eq t
adamc@256 24
adamc@389 25 class num
adamc@417 26 val zero : t ::: Type -> num t -> t
adamc@389 27 val neg : t ::: Type -> num t -> t -> t
adamc@389 28 val plus : t ::: Type -> num t -> t -> t -> t
adamc@389 29 val minus : t ::: Type -> num t -> t -> t -> t
adamc@389 30 val times : t ::: Type -> num t -> t -> t -> t
adamc@389 31 val div : t ::: Type -> num t -> t -> t -> t
adamc@389 32 val mod : t ::: Type -> num t -> t -> t -> t
adamc@389 33 val num_int : num int
adamc@390 34 val num_float : num float
adamc@389 35
adamc@391 36 class ord
adamc@391 37 val lt : t ::: Type -> ord t -> t -> t -> bool
adamc@391 38 val le : t ::: Type -> ord t -> t -> t -> bool
adamc@391 39 val gt : t ::: Type -> ord t -> t -> t -> bool
adamc@391 40 val ge : t ::: Type -> ord t -> t -> t -> bool
adamc@391 41 val ord_int : ord int
adamc@394 42 val ord_float : ord float
adamc@394 43 val ord_string : ord string
adamc@394 44 val ord_bool : ord bool
adamc@437 45 val ord_time : ord time
adamc@391 46
adamc@256 47
adamc@283 48 (** String operations *)
adamc@283 49
adamc@283 50 val strcat : string -> string -> string
adamc@283 51
adamc@286 52 class show
adamc@286 53 val show : t ::: Type -> show t -> t -> string
adamc@286 54 val show_int : show int
adamc@286 55 val show_float : show float
adamc@286 56 val show_string : show string
adamc@286 57 val show_bool : show bool
adamc@436 58 val show_time : show time
adamc@452 59 val mkShow : t ::: Type -> (t -> string) -> show t
adamc@286 60
adamc@290 61 class read
adamc@290 62 val read : t ::: Type -> read t -> string -> option t
adamc@292 63 val readError : t ::: Type -> read t -> string -> t
adamc@292 64 (* [readError] calls [error] if the input is malformed. *)
adamc@290 65 val read_int : read int
adamc@290 66 val read_float : read float
adamc@290 67 val read_string : read string
adamc@290 68 val read_bool : read bool
adamc@436 69 val read_time : read time
adamc@288 70
adamc@283 71
adamc@456 72 (** * Transactions *)
adamc@456 73
adamc@456 74 con transaction :: Type -> Type
adamc@456 75
adamc@456 76 val return : t ::: Type
adamc@456 77 -> t -> transaction t
adamc@456 78 val bind : t1 ::: Type -> t2 ::: Type
adamc@456 79 -> transaction t1 -> (t1 -> transaction t2)
adamc@456 80 -> transaction t2
adamc@456 81
adamc@456 82
adamc@456 83 (** HTTP operations *)
adamc@456 84
adamc@456 85 val requestHeader : string -> transaction (option string)
adamc@456 86
adamc@459 87 con http_cookie :: Type -> Type
adamc@459 88 val getCookie : t ::: Type -> http_cookie t -> transaction (option t)
adamc@459 89 val setCookie : t ::: Type -> http_cookie t -> t -> transaction unit
adamc@459 90
adamc@456 91
adamc@203 92 (** SQL *)
adamc@203 93
adamc@203 94 con sql_table :: {Type} -> Type
adamc@203 95
adamc@204 96 (*** Queries *)
adamc@204 97
adamc@233 98 con sql_query :: {{Type}} -> {Type} -> Type
adamc@233 99 con sql_query1 :: {{Type}} -> {{Type}} -> {Type} -> Type
adamc@234 100 con sql_exp :: {{Type}} -> {{Type}} -> {Type} -> Type -> Type
adamc@204 101
adamc@223 102 con sql_subset :: {{Type}} -> {{Type}} -> Type
adamc@223 103 val sql_subset : keep_drop :: {({Type} * {Type})}
adamc@354 104 -> sql_subset
adamc@354 105 (fold (fn nm (fields :: ({Type} * {Type}))
adamc@354 106 acc [[nm] ~ acc]
adamc@354 107 [fields.1 ~ fields.2] =>
adamc@354 108 [nm = fields.1 ++ fields.2]
adamc@354 109 ++ acc) [] keep_drop)
adamc@354 110 (fold (fn nm (fields :: ({Type} * {Type}))
adamc@354 111 acc [[nm] ~ acc] =>
adamc@354 112 [nm = fields.1] ++ acc)
adamc@354 113 [] keep_drop)
adamc@354 114 val sql_subset_all : tables :: {{Type}} -> sql_subset tables tables
adamc@223 115
adamc@229 116 val sql_query1 : tables ::: {{Type}}
adamc@354 117 -> grouped ::: {{Type}}
adamc@354 118 -> selectedFields ::: {{Type}}
adamc@354 119 -> selectedExps ::: {Type}
adamc@354 120 -> {From : $(fold (fn nm (fields :: {Type}) acc [[nm] ~ acc] =>
adamc@354 121 [nm = sql_table fields] ++ acc)
adamc@354 122 [] tables),
adamc@354 123 Where : sql_exp tables [] [] bool,
adamc@354 124 GroupBy : sql_subset tables grouped,
adamc@354 125 Having : sql_exp grouped tables [] bool,
adamc@354 126 SelectFields : sql_subset grouped selectedFields,
adamc@354 127 SelectExps : $(fold (fn nm (t :: Type) acc [[nm] ~ acc] =>
adamc@354 128 [nm = sql_exp grouped tables [] t]
adamc@354 129 ++ acc) [] selectedExps) }
adamc@354 130 -> sql_query1 tables selectedFields selectedExps
adamc@229 131
adamc@229 132 type sql_relop
adamc@229 133 val sql_union : sql_relop
adamc@229 134 val sql_intersect : sql_relop
adamc@229 135 val sql_except : sql_relop
adamc@260 136 val sql_relop : tables1 ::: {{Type}}
adamc@354 137 -> tables2 ::: {{Type}}
adamc@354 138 -> selectedFields ::: {{Type}}
adamc@354 139 -> selectedExps ::: {Type}
adamc@354 140 -> sql_relop
adamc@354 141 -> sql_query1 tables1 selectedFields selectedExps
adamc@354 142 -> sql_query1 tables2 selectedFields selectedExps
adamc@354 143 -> sql_query1 selectedFields selectedFields selectedExps
adamc@229 144
adamc@230 145 type sql_direction
adamc@230 146 val sql_asc : sql_direction
adamc@230 147 val sql_desc : sql_direction
adamc@230 148
adamc@234 149 con sql_order_by :: {{Type}} -> {Type} -> Type
adamc@234 150 val sql_order_by_Nil : tables ::: {{Type}} -> exps :: {Type} -> sql_order_by tables exps
adamc@234 151 val sql_order_by_Cons : tables ::: {{Type}} -> exps ::: {Type} -> t ::: Type
adamc@354 152 -> sql_exp tables [] exps t -> sql_direction
adamc@354 153 -> sql_order_by tables exps
adamc@354 154 -> sql_order_by tables exps
adamc@230 155
adamc@231 156 type sql_limit
adamc@231 157 val sql_no_limit : sql_limit
adamc@231 158 val sql_limit : int -> sql_limit
adamc@354 159
adamc@232 160 type sql_offset
adamc@232 161 val sql_no_offset : sql_offset
adamc@232 162 val sql_offset : int -> sql_offset
adamc@232 163
adamc@229 164 val sql_query : tables ::: {{Type}}
adamc@354 165 -> selectedFields ::: {{Type}}
adamc@354 166 -> selectedExps ::: {Type}
adamc@354 167 -> {Rows : sql_query1 tables selectedFields selectedExps,
adamc@354 168 OrderBy : sql_order_by tables selectedExps,
adamc@354 169 Limit : sql_limit,
adamc@354 170 Offset : sql_offset}
adamc@354 171 -> sql_query selectedFields selectedExps
adamc@204 172
adamc@354 173 val sql_field : otherTabs ::: {{Type}} -> otherFields ::: {Type}
adamc@354 174 -> fieldType ::: Type -> agg ::: {{Type}}
adamc@354 175 -> exps ::: {Type}
adamc@354 176 -> tab :: Name -> field :: Name
adamc@354 177 -> sql_exp
adamc@354 178 ([tab = [field = fieldType] ++ otherFields] ++ otherTabs)
adamc@354 179 agg exps fieldType
adamc@234 180
adamc@354 181 val sql_exp : tabs ::: {{Type}} -> agg ::: {{Type}} -> t ::: Type -> rest ::: {Type}
adamc@354 182 -> nm :: Name
adamc@354 183 -> sql_exp tabs agg ([nm = t] ++ rest) t
adamc@209 184
adamc@221 185 class sql_injectable
adamc@221 186 val sql_bool : sql_injectable bool
adamc@221 187 val sql_int : sql_injectable int
adamc@221 188 val sql_float : sql_injectable float
adamc@221 189 val sql_string : sql_injectable string
adamc@437 190 val sql_time : sql_injectable time
adamc@467 191 val sql_option_bool : sql_injectable (option bool)
adamc@467 192 val sql_option_int : sql_injectable (option int)
adamc@467 193 val sql_option_float : sql_injectable (option float)
adamc@467 194 val sql_option_string : sql_injectable (option string)
adamc@467 195 val sql_option_time : sql_injectable (option time)
adamc@354 196 val sql_inject : tables ::: {{Type}} -> agg ::: {{Type}} -> exps ::: {Type}
adamc@354 197 -> t ::: Type
adamc@354 198 -> sql_injectable t -> t -> sql_exp tables agg exps t
adamc@209 199
adamc@470 200 val sql_is_null : tables ::: {{Type}} -> agg ::: {{Type}} -> exps ::: {Type}
adamc@470 201 -> t ::: Type
adamc@470 202 -> sql_exp tables agg exps (option t)
adamc@470 203 -> sql_exp tables agg exps bool
adamc@470 204
adamc@559 205 class sql_arith
adamc@559 206 val sql_int_arith : sql_arith int
adamc@559 207 val sql_float_arith : sql_arith float
adamc@559 208
adamc@220 209 con sql_unary :: Type -> Type -> Type
adamc@220 210 val sql_not : sql_unary bool bool
adamc@354 211 val sql_unary : tables ::: {{Type}} -> agg ::: {{Type}} -> exps ::: {Type}
adamc@354 212 -> arg ::: Type -> res ::: Type
adamc@354 213 -> sql_unary arg res -> sql_exp tables agg exps arg
adamc@354 214 -> sql_exp tables agg exps res
adamc@220 215
adamc@559 216 val sql_neg : t ::: Type -> sql_arith t -> sql_unary t t
adamc@559 217
adamc@220 218 con sql_binary :: Type -> Type -> Type -> Type
adamc@220 219 val sql_and : sql_binary bool bool bool
adamc@220 220 val sql_or : sql_binary bool bool bool
adamc@234 221 val sql_binary : tables ::: {{Type}} -> agg ::: {{Type}} -> exps ::: {Type}
adamc@354 222 -> arg1 ::: Type -> arg2 ::: Type -> res ::: Type
adamc@354 223 -> sql_binary arg1 arg2 res -> sql_exp tables agg exps arg1
adamc@354 224 -> sql_exp tables agg exps arg2
adamc@354 225 -> sql_exp tables agg exps res
adamc@220 226
adamc@559 227 val sql_plus : t ::: Type -> sql_arith t -> sql_binary t t t
adamc@559 228 val sql_minus : t ::: Type -> sql_arith t -> sql_binary t t t
adamc@559 229 val sql_times : t ::: Type -> sql_arith t -> sql_binary t t t
adamc@559 230 val sql_div : t ::: Type -> sql_arith t -> sql_binary t t t
adamc@559 231 val sql_mod : sql_binary int int int
adamc@559 232
adamc@559 233 val sql_eq : t ::: Type -> sql_binary t t bool
adamc@559 234 val sql_ne : t ::: Type -> sql_binary t t bool
adamc@559 235 val sql_lt : t ::: Type -> sql_binary t t bool
adamc@559 236 val sql_le : t ::: Type -> sql_binary t t bool
adamc@559 237 val sql_gt : t ::: Type -> sql_binary t t bool
adamc@559 238 val sql_ge : t ::: Type -> sql_binary t t bool
adamc@203 239
adamc@235 240 val sql_count : tables ::: {{Type}} -> agg ::: {{Type}} -> exps ::: {Type}
adamc@544 241 -> sql_exp tables agg exps int
adamc@235 242
adamc@236 243 con sql_aggregate :: Type -> Type
adamc@354 244 val sql_aggregate : tables ::: {{Type}} -> agg ::: {{Type}} -> exps ::: {Type}
adamc@354 245 -> t ::: Type
adamc@354 246 -> sql_aggregate t -> sql_exp agg agg exps t
adamc@354 247 -> sql_exp tables agg exps t
adamc@236 248
adamc@236 249 class sql_summable
adamc@236 250 val sql_summable_int : sql_summable int
adamc@236 251 val sql_summable_float : sql_summable float
adamc@236 252 val sql_avg : t ::: Type -> sql_summable t -> sql_aggregate t
adamc@236 253 val sql_sum : t ::: Type -> sql_summable t -> sql_aggregate t
adamc@236 254
adamc@236 255 class sql_maxable
adamc@236 256 val sql_maxable_int : sql_maxable int
adamc@236 257 val sql_maxable_float : sql_maxable float
adamc@236 258 val sql_maxable_string : sql_maxable string
adamc@437 259 val sql_maxable_time : sql_maxable time
adamc@236 260 val sql_max : t ::: Type -> sql_maxable t -> sql_aggregate t
adamc@236 261 val sql_min : t ::: Type -> sql_maxable t -> sql_aggregate t
adamc@236 262
adamc@441 263 con sql_nfunc :: Type -> Type
adamc@441 264 val sql_nfunc : tables ::: {{Type}} -> agg ::: {{Type}} -> exps ::: {Type}
adamc@441 265 -> t ::: Type
adamc@441 266 -> sql_nfunc t -> sql_exp tables agg exps t
adamc@441 267 val sql_current_timestamp : sql_nfunc time
adamc@441 268
adamc@235 269
adamc@243 270 (*** Executing queries *)
adamc@243 271
adamc@354 272 val query : tables ::: {{Type}} -> exps ::: {Type}
adamc@354 273 -> fn [tables ~ exps] =>
adamc@354 274 state ::: Type
adamc@354 275 -> sql_query tables exps
adamc@356 276 -> ($(exps ++ fold (fn nm (fields :: {Type}) acc [[nm] ~ acc] =>
adamc@354 277 [nm = $fields] ++ acc) [] tables)
adamc@354 278 -> state
adamc@354 279 -> transaction state)
adamc@354 280 -> state
adamc@354 281 -> transaction state
adamc@243 282
adamc@243 283
adamc@299 284 (*** Database mutators *)
adamc@299 285
adamc@299 286 type dml
adamc@299 287 val dml : dml -> transaction unit
adamc@299 288
adamc@299 289 val insert : fields ::: {Type}
adamc@354 290 -> sql_table fields
adamc@354 291 -> $(fold (fn nm (t :: Type) acc [[nm] ~ acc] =>
adamc@354 292 [nm = sql_exp [] [] [] t] ++ acc)
adamc@354 293 [] fields)
adamc@354 294 -> dml
adamc@299 295
adamc@403 296 val update : unchanged ::: {Type} -> changed :: {Type} ->
adamc@354 297 fn [changed ~ unchanged] =>
adamc@354 298 $(fold (fn nm (t :: Type) acc [[nm] ~ acc] =>
adamc@354 299 [nm = sql_exp [T = changed ++ unchanged] [] [] t]
adamc@354 300 ++ acc)
adamc@354 301 [] changed)
adamc@354 302 -> sql_table (changed ++ unchanged)
adamc@354 303 -> sql_exp [T = changed ++ unchanged] [] [] bool
adamc@354 304 -> dml
adamc@299 305
adamc@299 306 val delete : fields ::: {Type}
adamc@354 307 -> sql_table fields
adamc@354 308 -> sql_exp [T = fields] [] [] bool
adamc@354 309 -> dml
adamc@299 310
adamc@338 311 (*** Sequences *)
adamc@338 312
adamc@338 313 type sql_sequence
adamc@338 314 val nextval : sql_sequence -> transaction int
adamc@338 315
adamc@299 316
adamc@203 317 (** XML *)
adamc@203 318
adamc@139 319 con tag :: {Type} -> {Unit} -> {Unit} -> {Type} -> {Type} -> Type
adamc@91 320
adamc@91 321
adamc@139 322 con xml :: {Unit} -> {Type} -> {Type} -> Type
adamc@141 323 val cdata : ctx ::: {Unit} -> use ::: {Type} -> string -> xml ctx use []
adamc@354 324 val tag : attrsGiven ::: {Type} -> attrsAbsent ::: {Type}
adamc@354 325 -> ctxOuter ::: {Unit} -> ctxInner ::: {Unit}
adamc@354 326 -> useOuter ::: {Type} -> useInner ::: {Type}
adamc@354 327 -> bindOuter ::: {Type} -> bindInner ::: {Type}
adamc@354 328 -> fn [attrsGiven ~ attrsAbsent]
adamc@354 329 [useOuter ~ useInner]
adamc@354 330 [bindOuter ~ bindInner] =>
adamc@354 331 $attrsGiven
adamc@354 332 -> tag (attrsGiven ++ attrsAbsent)
adamc@354 333 ctxOuter ctxInner useOuter bindOuter
adamc@354 334 -> xml ctxInner useInner bindInner
adamc@354 335 -> xml ctxOuter (useOuter ++ useInner) (bindOuter ++ bindInner)
adamc@140 336 val join : ctx ::: {Unit}
adamc@139 337 -> use1 ::: {Type} -> bind1 ::: {Type} -> bind2 ::: {Type}
adamc@354 338 -> fn [use1 ~ bind1] [bind1 ~ bind2] =>
adamc@354 339 xml ctx use1 bind1
adamc@354 340 -> xml ctx (use1 ++ bind1) bind2
adamc@354 341 -> xml ctx use1 (bind1 ++ bind2)
adamc@354 342 val useMore : ctx ::: {Unit} -> use1 ::: {Type} -> use2 ::: {Type}
adamc@354 343 -> bind ::: {Type}
adamc@354 344 -> fn [use1 ~ use2] =>
adamc@354 345 xml ctx use1 bind
adamc@354 346 -> xml ctx (use1 ++ use2) bind
adamc@91 347
adamc@110 348 con xhtml = xml [Html]
adamc@139 349 con page = xhtml [] []
adamc@325 350 con xbody = xml [Body] [] []
adamc@326 351 con xtr = xml [Body, Tr] [] []
adamc@361 352 con xform = xml [Body, Form] [] []
adamc@110 353
adamc@204 354 (*** HTML details *)
adamc@204 355
adamc@140 356 con html = [Html]
adamc@140 357 con head = [Head]
adamc@140 358 con body = [Body]
adamc@361 359 con form = [Body, Form]
adamc@332 360 con tabl = [Body, Table]
adamc@332 361 con tr = [Body, Tr]
adamc@93 362
adamc@140 363 val head : unit -> tag [] html head [] []
adamc@140 364 val title : unit -> tag [] head [] [] []
adamc@110 365
adamc@140 366 val body : unit -> tag [] html body [] []
adamc@354 367 con bodyTag = fn (attrs :: {Type}) =>
adamc@354 368 ctx ::: {Unit} ->
adamc@354 369 fn [[Body] ~ ctx] =>
adamc@354 370 unit -> tag attrs ([Body] ++ ctx) ([Body] ++ ctx) [] []
adamc@354 371 con bodyTagStandalone = fn (attrs :: {Type}) =>
adamc@354 372 ctx ::: {Unit}
adamc@354 373 -> fn [[Body] ~ ctx] =>
adamc@354 374 unit -> tag attrs ([Body] ++ ctx) [] [] []
adamc@141 375
adamc@141 376 val br : bodyTagStandalone []
adamc@119 377
adamc@140 378 val p : bodyTag []
adamc@140 379 val b : bodyTag []
adamc@140 380 val i : bodyTag []
adamc@407 381 val tt : bodyTag []
adamc@140 382 val font : bodyTag [Size = int, Face = string]
adamc@140 383
adamc@140 384 val h1 : bodyTag []
adamc@443 385 val h2 : bodyTag []
adamc@443 386 val h3 : bodyTag []
adamc@443 387 val h4 : bodyTag []
adamc@469 388
adamc@140 389 val li : bodyTag []
adamc@469 390 val ol : bodyTag []
adamc@469 391 val ul : bodyTag []
adamc@140 392
adamc@410 393 val hr : bodyTag []
adamc@410 394
adamc@280 395 val a : bodyTag [Link = transaction page]
adamc@140 396
adamc@361 397 val form : ctx ::: {Unit} -> bind ::: {Type}
adamc@354 398 -> fn [[Body] ~ ctx] =>
adamc@354 399 xml form [] bind
adamc@354 400 -> xml ([Body] ++ ctx) [] []
adamc@361 401 con formTag = fn (ty :: Type) (inner :: {Unit}) (attrs :: {Type}) =>
adamc@354 402 ctx ::: {Unit}
adamc@361 403 -> fn [[Form] ~ ctx] =>
adamc@354 404 nm :: Name -> unit
adamc@361 405 -> tag attrs ([Form] ++ ctx) inner [] [nm = ty]
adamc@444 406 val textbox : formTag string [] [Value = string, Size = int]
adamc@444 407 val password : formTag string [] [Value = string, Size = int]
adamc@444 408 val textarea : formTag string [] [Rows = int, Cols = int]
adamc@153 409
adamc@361 410 val checkbox : formTag bool [] [Checked = bool]
adamc@190 411
adamc@153 412 con radio = [Body, Radio]
adamc@361 413 val radio : formTag string radio []
adamc@153 414 val radioOption : unit -> tag [Value = string] radio [] [] []
adamc@142 415
adamc@154 416 con select = [Select]
adamc@361 417 val select : formTag string select []
adamc@422 418 val option : unit -> tag [Value = string, Selected = bool] select [] [] []
adamc@154 419
adamc@354 420 val submit : ctx ::: {Unit} -> use ::: {Type}
adamc@361 421 -> fn [[Form] ~ ctx] =>
adamc@354 422 unit
adamc@406 423 -> tag [Value = string, Action = $use -> transaction page]
adamc@361 424 ([Form] ++ ctx) ([Form] ++ ctx) use []
adamc@283 425
adamc@325 426 (*** Tables *)
adamc@325 427
adamc@406 428 val tabl : other ::: {Unit} -> fn [other ~ [Body, Table]] =>
adamc@406 429 unit -> tag [Border = int] ([Body] ++ other) ([Body, Table] ++ other) [] []
adamc@406 430 val tr : other ::: {Unit} -> fn [other ~ [Body, Table, Tr]] =>
adamc@406 431 unit -> tag [] ([Body, Table] ++ other) ([Body, Tr] ++ other) [] []
adamc@406 432 val th : other ::: {Unit} -> fn [other ~ [Body, Tr]] =>
adamc@406 433 unit -> tag [] ([Body, Tr] ++ other) ([Body] ++ other) [] []
adamc@406 434 val td : other ::: {Unit} -> fn [other ~ [Body, Tr]] =>
adamc@406 435 unit -> tag [] ([Body, Tr] ++ other) ([Body] ++ other) [] []
adamc@325 436
adamc@283 437
adamc@283 438 (** Aborting *)
adamc@283 439
adamc@283 440 val error : t ::: Type -> xml [Body] [] [] -> t