mirror of
https://github.com/zoriya/applicative-getopt.git
synced 2026-06-05 11:29:31 +00:00
PeutEtre & Monad/Alternative for Parser
This commit is contained in:
+48
-5
@@ -6,7 +6,7 @@ instance Functor Parser where
|
|||||||
-- fmap :: (a -> b) -> f a -> f b
|
-- fmap :: (a -> b) -> f a -> f b
|
||||||
fmap f p = P $ \x -> case parse p x of
|
fmap f p = P $ \x -> case parse p x of
|
||||||
Nothing -> Nothing
|
Nothing -> Nothing
|
||||||
Just (y, lo) -> Just $ (f y, lo)
|
Just (y, lo) -> Just (f y, lo)
|
||||||
|
|
||||||
instance Applicative Parser where
|
instance Applicative Parser where
|
||||||
-- pure :: a -> f a
|
-- pure :: a -> f a
|
||||||
@@ -17,14 +17,20 @@ instance Applicative Parser where
|
|||||||
Nothing -> Nothing
|
Nothing -> Nothing
|
||||||
Just (y, lo) -> parse (fmap y p) lo
|
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
|
instance Alternative Parser where
|
||||||
-- empty :: f a
|
-- empty :: f a
|
||||||
empty = P (\x -> Nothing)
|
empty = P (\x -> Nothing)
|
||||||
|
|
||||||
-- (<|>) :: f a -> f a -> f a
|
-- (<|>) :: f a -> f a -> f a
|
||||||
-- (<|>) a b = P $ \x -> case parse a x of
|
(<|>) a b = P $ \x -> case parse a x of
|
||||||
-- Nothing -> b
|
Nothing -> parse b x
|
||||||
-- _ -> a
|
y -> y
|
||||||
|
|
||||||
data Configuration = Configuration {
|
data Configuration = Configuration {
|
||||||
rule :: Int,
|
rule :: Int,
|
||||||
@@ -40,4 +46,41 @@ defaultConfiguration = Configuration {
|
|||||||
Main.lines = Nothing,
|
Main.lines = Nothing,
|
||||||
window = Just 80,
|
window = Just 80,
|
||||||
move = Just 0
|
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
|
||||||
Reference in New Issue
Block a user