diff options
| author | bringert <bringert@cs.chalmers.se> | 2005-11-30 20:27:01 +0000 |
|---|---|---|
| committer | bringert <bringert@cs.chalmers.se> | 2005-11-30 20:27:01 +0000 |
| commit | 7dfa1842859b408d0eadd4d79a5b1ce0267a13b2 (patch) | |
| tree | 983536942b3836c01033612fb358a619a3505bf0 /transfer/lib | |
| parent | d92a26fc9be92fb269888947a8b26aa12883065e (diff) | |
Added bind operators, do-notation, a cons operator and list sytnax.
Diffstat (limited to 'transfer/lib')
| -rw-r--r-- | transfer/lib/list.tr | 31 | ||||
| -rw-r--r-- | transfer/lib/maybe.tr | 24 | ||||
| -rw-r--r-- | transfer/lib/prelude.tr | 63 |
3 files changed, 62 insertions, 56 deletions
diff --git a/transfer/lib/list.tr b/transfer/lib/list.tr deleted file mode 100644 index da3bcc516..000000000 --- a/transfer/lib/list.tr +++ /dev/null @@ -1,31 +0,0 @@ -import prelude -import nat - -data List : (_:Type) -> Type where - Nil : (A:Type) -> List A - Cons : (A:Type) -> A -> List A -> List A - -size : (A:Type) -> List A -> Nat -size _ (Nil _) = Zero -size A (Cons _ x xs) = Succ (size A xs) - -map : (A:Type) -> (B:Type) -> (A -> B) -> List A -> List B -map _ B _ (Nil _) = Nil B -map A B f (Cons _ x xs) = Cons B (f x) (map A B f xs) - -append : (A:Type) -> (xs:List A) -> List A -> List A -append A xs ys = foldr A (List A) (Cons A) ys xs - -concat : (A : Type) -> List (List A) -> List A -concat A = foldr (List A) (List A) (append A) (Nil A) - -foldr : (A : Type) -> (B : Type) -> (A -> B -> B) -> B -> List A -> B -foldr _ _ _ x (Nil _) = x -foldr A B f x (Cons _ y ys) = f y (foldr A B f x ys) - --- Instances: - -monad_List : Monad List -monad_list = rec return = \A -> \x -> Cons A x (Nil A) - bind = \A -> \B -> \m -> \k -> concat B (map A B k m) - diff --git a/transfer/lib/maybe.tr b/transfer/lib/maybe.tr deleted file mode 100644 index 2b7beee9a..000000000 --- a/transfer/lib/maybe.tr +++ /dev/null @@ -1,24 +0,0 @@ -import prelude - -data Maybe : Type -> Type where - Nothing : (A : Type) -> Maybe A - Just : (A : Type) -> A -> Maybe A - -fromMaybe : (A : Type) -> A -> Maybe A -> A -fromMaybe _ x Nothing = x -fromMaybe _ _ (Just x) = x - -maybe : (A : Type) -> (B : Type) -> B -> (A -> B) -> Maybe A -> A -maybe _ _ x _ Nothing = x -maybe _ _ _ f (Just x) = f x - --- Instances: - -monad_Maybe : Monad Maybe -monad_Maybe = - rec return = Just - bind = \A -> \B -> \m -> \k -> - case m of - Nothing _ -> Nothing B - Just _ x -> k x - diff --git a/transfer/lib/prelude.tr b/transfer/lib/prelude.tr index fb5f724dd..7034fbae7 100644 --- a/transfer/lib/prelude.tr +++ b/transfer/lib/prelude.tr @@ -15,7 +15,7 @@ id _ x = x -- --- The Bool type +-- The List type -- data Bool : Type where @@ -26,6 +26,46 @@ not : Bool -> Bool not b = if b then False else True +data List : (_:Type) -> Type where + Nil : (A:Type) -> List A + Cons : (A:Type) -> A -> List A -> List A + +size : (A:Type) -> List A -> Nat +size _ (Nil _) = Zero +size A (Cons _ x xs) = Succ (size A xs) + +map : (A:Type) -> (B:Type) -> (A -> B) -> List A -> List B +map _ B _ (Nil _) = Nil B +map A B f (Cons _ x xs) = Cons B (f x) (map A B f xs) + +append : (A:Type) -> (xs:List A) -> List A -> List A +append A xs ys = foldr A (List A) (Cons A) ys xs + +concat : (A : Type) -> List (List A) -> List A +concat A = foldr (List A) (List A) (append A) (Nil A) + +foldr : (A : Type) -> (B : Type) -> (A -> B -> B) -> B -> List A -> B +foldr _ _ _ x (Nil _) = x +foldr A B f x (Cons _ y ys) = f y (foldr A B f x ys) + + +-- +-- The Maybe type +-- + +data Maybe : Type -> Type where + Nothing : (A : Type) -> Maybe A + Just : (A : Type) -> A -> Maybe A + +fromMaybe : (A : Type) -> A -> Maybe A -> A +fromMaybe _ x Nothing = x +fromMaybe _ _ (Just x) = x + +maybe : (A : Type) -> (B : Type) -> B -> (A -> B) -> Maybe A -> A +maybe _ _ x _ Nothing = x +maybe _ _ _ f (Just x) = f x + + -- -- The Num class -- @@ -327,3 +367,24 @@ return _ d = d.return bind : (M : Type -> Type) -> Monad M -> (A : Type) -> (B : Type) -> M A -> (A -> M B) -> M B bind _ d = d.bind + +-- Operators: + +{- + (x >>= y) => bind ? ? ? ? x y + (x >> y) => bind ? ? ? ? x (\_ -> y) +-} + +-- Instances: + +monad_List : Monad List +monad_list = rec return = \A -> \x -> Cons A x (Nil A) + bind = \A -> \B -> \m -> \k -> concat B (map A B k m) + +monad_Maybe : Monad Maybe +monad_Maybe = + rec return = Just + bind = \A -> \B -> \m -> \k -> + case m of + Nothing _ -> Nothing B + Just _ x -> k x |
