PeutEtre & Monad/Alternative for Parser

This commit is contained in:
Zoe Roux
2021-03-08 17:12:54 +01:00
parent 6964953cfe
commit 8b5df8d86b
+48 -5
View File
@@ -6,7 +6,7 @@ instance Functor Parser where
-- fmap :: (a -> b) -> f a -> f b
fmap f p = P $ \x -> case parse p x of
Nothing -> Nothing
Just (y, lo) -> Just $ (f y, lo)
Just (y, lo) -> Just (f y, lo)
instance Applicative Parser where
-- pure :: a -> f a
@@ -17,14 +17,20 @@ instance Applicative Parser where
Nothing -> Nothing
Just (y, lo) -> parse (fmap y p) lo
instance Monad Parser where
-- (>>=) :: forall a b. m a -> (a -> m b) -> m b
(>>=) p f = P $ \x -> case parse p x of
Nothing -> Nothing
Just (y, lo) -> parse (f y) lo
instance Alternative Parser where
-- empty :: f a
empty = P (\x -> Nothing)
-- (<|>) :: f a -> f a -> f a
-- (<|>) a b = P $ \x -> case parse a x of
-- Nothing -> b
-- _ -> a
(<|>) a b = P $ \x -> case parse a x of
Nothing -> parse b x
y -> y
data Configuration = Configuration {
rule :: Int,
@@ -40,4 +46,41 @@ defaultConfiguration = Configuration {
Main.lines = Nothing,
window = Just 80,
move = Just 0
}
}
--
--data PeutEtre a = Juste a | Rien
--
--instance Functor PeutEtre where
-- -- fmap :: (a -> b) -> f a -> f b
-- fmap _ Rien = Rien
-- fmap f (Juste x) = Juste $ f x
--
--instance Applicative PeutEtre where
-- -- pure :: a -> f a
-- pure x = Juste x
--
-- -- (<*>) :: f (a -> b) -> f a -> f b
-- (<*>) _ Rien = Rien
-- (<*>) Rien _ = Rien
-- (<*>) (Juste f) (Juste x) = Juste $ f x
--
--instance Semigroup PeutEtre where
-- -- (<>) :: a -> a -> a
-- (<>) Rien x = x
-- (<>) x _ = x
-- (<>) Rien Rien = Rien
--
--instance Monad PeutEtre where
-- -- (>>=) :: forall a b. m a -> (a -> m b) -> m b
-- (>>=) Rien _ = Rien
-- (>>=) (Juste x) f = f x
--
--instance Alternative PeutEtre where
-- -- empty :: f a
-- empty = Rien
--
-- -- (<|>) :: f a -> f a -> f a
-- (<|>) (Juste a) _ = Juste a
-- (<|>) _ (Juste a) = Juste a
-- (<|>) _ _ = Rien