mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-17 16:59:34 -06:00
Added monad isntances for List and Maybe.
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import prelude
|
||||
import nat
|
||||
|
||||
data List : (_:Type) -> Type where
|
||||
@@ -13,5 +14,18 @@ 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 _ (Nil _) ys = ys
|
||||
append A (Cons _ x xs) ys = Cons A x (append A xs ys)
|
||||
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)
|
||||
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import prelude
|
||||
|
||||
data Maybe : Type -> Type where
|
||||
Nothing : (A : Type) -> Maybe A
|
||||
Just : (A : Type) -> A -> Maybe A
|
||||
@@ -8,4 +10,15 @@ 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
|
||||
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
|
||||
|
||||
|
||||
@@ -327,4 +327,3 @@ 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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user