annotate lib/top.ur @ 416:679b2fbbd4d0

Counter demo
author Adam Chlipala <adamc@hcoop.net>
date Thu, 23 Oct 2008 11:59:48 -0400
parents df4cbd90a26e
children ad7e854a518c
rev   line source
adamc@356 1 con idT (t :: Type) = t
adamc@356 2 con record (t :: {Type}) = $t
adamc@356 3 con fstTT (t :: (Type * Type)) = t.1
adamc@356 4 con sndTT (t :: (Type * Type)) = t.2
adamc@329 5
adamc@356 6 con mapTT (f :: Type -> Type) = fold (fn nm t acc [[nm] ~ acc] =>
adamc@356 7 [nm = f t] ++ acc) []
adamc@325 8
adamc@411 9 con mapUT = fn f :: Type => fold (fn nm t acc [[nm] ~ acc] =>
adamc@411 10 [nm = f] ++ acc) []
adamc@411 11
adamc@356 12 con mapT2T (f :: (Type * Type) -> Type) = fold (fn nm t acc [[nm] ~ acc] =>
adamc@356 13 [nm = f t] ++ acc) []
adamc@339 14
adamc@339 15 con ex = fn tf :: (Type -> Type) =>
adamc@356 16 res ::: Type -> (choice :: Type -> tf choice -> res) -> res
adamc@339 17
adamc@339 18 fun ex (tf :: (Type -> Type)) (choice :: Type) (body : tf choice) : ex tf =
adamc@356 19 fn (res ::: Type) (f : choice :: Type -> tf choice -> res) =>
adamc@356 20 f [choice] body
adamc@339 21
adamc@356 22 fun compose (t1 ::: Type) (t2 ::: Type) (t3 ::: Type)
adamc@356 23 (f1 : t2 -> t3) (f2 : t1 -> t2) (x : t1) = f1 (f2 x)
adamc@325 24
adamc@356 25 fun txt (t ::: Type) (ctx ::: {Unit}) (use ::: {Type}) (sh : show t) (v : t) =
adamc@403 26 cdata (@show sh v)
adamc@328 27
adamc@411 28 fun foldUR (tf :: Type) (tr :: {Unit} -> Type)
adamc@411 29 (f : nm :: Name -> rest :: {Unit}
adamc@411 30 -> fn [[nm] ~ rest] =>
adamc@411 31 tf -> tr rest -> tr ([nm] ++ rest))
adamc@411 32 (i : tr []) =
adamc@411 33 fold [fn r :: {Unit} => $(mapUT tf r) -> tr r]
adamc@412 34 (fn (nm :: Name) (t :: Unit) (rest :: {Unit}) acc
adamc@412 35 [[nm] ~ rest] r =>
adamc@411 36 f [nm] [rest] r.nm (acc (r -- nm)))
adamc@411 37 (fn _ => i)
adamc@411 38
adamc@336 39 fun foldTR (tf :: Type -> Type) (tr :: {Type} -> Type)
adamc@356 40 (f : nm :: Name -> t :: Type -> rest :: {Type}
adamc@356 41 -> fn [[nm] ~ rest] =>
adamc@356 42 tf t -> tr rest -> tr ([nm = t] ++ rest))
adamc@356 43 (i : tr []) =
adamc@356 44 fold [fn r :: {Type} => $(mapTT tf r) -> tr r]
adamc@356 45 (fn (nm :: Name) (t :: Type) (rest :: {Type}) (acc : _ -> tr rest)
adamc@356 46 [[nm] ~ rest] r =>
adamc@356 47 f [nm] [t] [rest] r.nm (acc (r -- nm)))
adamc@356 48 (fn _ => i)
adamc@336 49
adamc@339 50 fun foldT2R (tf :: (Type * Type) -> Type) (tr :: {(Type * Type)} -> Type)
adamc@367 51 (f : nm :: Name -> t :: (Type * Type) -> rest :: {(Type * Type)}
adamc@367 52 -> fn [[nm] ~ rest] =>
adamc@367 53 tf t -> tr rest -> tr ([nm = t] ++ rest))
adamc@367 54 (i : tr []) =
adamc@356 55 fold [fn r :: {(Type * Type)} => $(mapT2T tf r) -> tr r]
adamc@356 56 (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)})
adamc@356 57 (acc : _ -> tr rest) [[nm] ~ rest] r =>
adamc@356 58 f [nm] [t] [rest] r.nm (acc (r -- nm)))
adamc@356 59 (fn _ => i)
adamc@339 60
adamc@330 61 fun foldTR2 (tf1 :: Type -> Type) (tf2 :: Type -> Type) (tr :: {Type} -> Type)
adamc@367 62 (f : nm :: Name -> t :: Type -> rest :: {Type}
adamc@367 63 -> fn [[nm] ~ rest] =>
adamc@367 64 tf1 t -> tf2 t -> tr rest -> tr ([nm = t] ++ rest))
adamc@367 65 (i : tr []) =
adamc@356 66 fold [fn r :: {Type} => $(mapTT tf1 r) -> $(mapTT tf2 r) -> tr r]
adamc@356 67 (fn (nm :: Name) (t :: Type) (rest :: {Type})
adamc@356 68 (acc : _ -> _ -> tr rest) [[nm] ~ rest] r1 r2 =>
adamc@356 69 f [nm] [t] [rest] r1.nm r2.nm (acc (r1 -- nm) (r2 -- nm)))
adamc@356 70 (fn _ _ => i)
adamc@332 71
adamc@356 72 fun foldT2R2 (tf1 :: (Type * Type) -> Type) (tf2 :: (Type * Type) -> Type)
adamc@356 73 (tr :: {(Type * Type)} -> Type)
adamc@356 74 (f : nm :: Name -> t :: (Type * Type) -> rest :: {(Type * Type)}
adamc@356 75 -> fn [[nm] ~ rest] =>
adamc@356 76 tf1 t -> tf2 t -> tr rest -> tr ([nm = t] ++ rest))
adamc@356 77 (i : tr []) =
adamc@356 78 fold [fn r :: {(Type * Type)} => $(mapT2T tf1 r) -> $(mapT2T tf2 r) -> tr r]
adamc@356 79 (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)})
adamc@356 80 (acc : _ -> _ -> tr rest) [[nm] ~ rest] r1 r2 =>
adamc@356 81 f [nm] [t] [rest] r1.nm r2.nm (acc (r1 -- nm) (r2 -- nm)))
adamc@356 82 (fn _ _ => i)
adamc@339 83
adamc@336 84 fun foldTRX (tf :: Type -> Type) (ctx :: {Unit})
adamc@356 85 (f : nm :: Name -> t :: Type -> rest :: {Type}
adamc@356 86 -> fn [[nm] ~ rest] =>
adamc@356 87 tf t -> xml ctx [] []) =
adamc@356 88 foldTR [tf] [fn _ => xml ctx [] []]
adamc@356 89 (fn (nm :: Name) (t :: Type) (rest :: {Type}) [[nm] ~ rest] r acc =>
adamc@356 90 <xml>{f [nm] [t] [rest] r}{acc}</xml>)
adamc@360 91 <xml/>
adamc@336 92
adamc@339 93 fun foldT2RX (tf :: (Type * Type) -> Type) (ctx :: {Unit})
adamc@356 94 (f : nm :: Name -> t :: (Type * Type) -> rest :: {(Type * Type)}
adamc@356 95 -> fn [[nm] ~ rest] =>
adamc@356 96 tf t -> xml ctx [] []) =
adamc@356 97 foldT2R [tf] [fn _ => xml ctx [] []]
adamc@356 98 (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)})
adamc@356 99 [[nm] ~ rest] r acc =>
adamc@356 100 <xml>{f [nm] [t] [rest] r}{acc}</xml>)
adamc@360 101 <xml/>
adamc@339 102
adamc@332 103 fun foldTRX2 (tf1 :: Type -> Type) (tf2 :: Type -> Type) (ctx :: {Unit})
adamc@356 104 (f : nm :: Name -> t :: Type -> rest :: {Type}
adamc@356 105 -> fn [[nm] ~ rest] =>
adamc@356 106 tf1 t -> tf2 t -> xml ctx [] []) =
adamc@356 107 foldTR2 [tf1] [tf2] [fn _ => xml ctx [] []]
adamc@356 108 (fn (nm :: Name) (t :: Type) (rest :: {Type}) [[nm] ~ rest]
adamc@356 109 r1 r2 acc =>
adamc@356 110 <xml>{f [nm] [t] [rest] r1 r2}{acc}</xml>)
adamc@360 111 <xml/>
adamc@334 112
adamc@356 113 fun foldT2RX2 (tf1 :: (Type * Type) -> Type) (tf2 :: (Type * Type) -> Type)
adamc@356 114 (ctx :: {Unit})
adamc@356 115 (f : nm :: Name -> t :: (Type * Type) -> rest :: {(Type * Type)}
adamc@356 116 -> fn [[nm] ~ rest] =>
adamc@356 117 tf1 t -> tf2 t -> xml ctx [] []) =
adamc@356 118 foldT2R2 [tf1] [tf2] [fn _ => xml ctx [] []]
adamc@356 119 (fn (nm :: Name) (t :: (Type * Type)) (rest :: {(Type * Type)})
adamc@356 120 [[nm] ~ rest] r1 r2 acc =>
adamc@356 121 <xml>{f [nm] [t] [rest] r1 r2}{acc}</xml>)
adamc@360 122 <xml/>
adamc@339 123
adamc@356 124 fun queryX (tables ::: {{Type}}) (exps ::: {Type}) (ctx ::: {Unit})
adamc@356 125 (q : sql_query tables exps) [tables ~ exps]
adamc@356 126 (f : $(exps ++ fold (fn nm (fields :: {Type}) acc [[nm] ~ acc] =>
adamc@356 127 [nm = $fields] ++ acc) [] tables)
adamc@356 128 -> xml ctx [] []) =
adamc@356 129 query q
adamc@356 130 (fn fs acc => return <xml>{acc}{f fs}</xml>)
adamc@360 131 <xml/>
adamc@341 132
adamc@356 133 fun oneOrNoRows (tables ::: {{Type}}) (exps ::: {Type})
adamc@356 134 (q : sql_query tables exps) [tables ~ exps] =
adamc@356 135 query q
adamc@356 136 (fn fs _ => return (Some fs))
adamc@356 137 None