Commit 31e30a04 authored by Camil Staps's avatar Camil Staps 🐧

Move Monad instances

- The Generics library had to be adapted because of moving instance
  Monad Maybe to Data.Maybe (=StdMaybe) and a name conflict with
  GenMonad's Monad class. GenMonad cannot use Control.Monad due to
  uniqueness attributes.
- Generics use mapMaybe from StdMaybe. This cannot be replaced with
  fmap, because of uniqueness attributes.
parent 003e6ee4
Pipeline #8610 passed with stage
in 1 minute and 38 seconds
......@@ -12,6 +12,16 @@ unwrapMonad :: !(WrappedMonad m a) -> m a
getConst :: !(Const a b) -> a
class Applicative f | Functor f
where
pure :: a -> f a
(<*>) infixl 4 :: !(f (a -> b)) (f a) -> f b
class Alternative f | Applicative f
where
empty :: f a
(<|>) infixl 3 :: !(f a) (f a) -> f a
instance Functor (Const m)
instance Functor (WrappedMonad m) | Monad m
instance Applicative (Const m) | Monoid m
......@@ -23,14 +33,6 @@ instance Alternative (WrappedMonad m) | MonadPlus m
instance Semigroup (Const a b) | Semigroup a
instance Monoid (Const a b) | Monoid a
class Applicative f | Functor f where
pure :: a -> f a
(<*>) infixl 4 :: !(f (a -> b)) (f a) -> f b
class Alternative f | Applicative f where
empty :: f a
(<|>) infixl 3 :: !(f a) (f a) -> f a
some :: (f a) -> f [a] | Alternative f
many :: (f a) -> f [a] | Alternative f
......
......@@ -2,9 +2,9 @@ definition module Control.Monad
from Control.Applicative import class Applicative
from Data.Functor import class Functor
from Data.Maybe import :: Maybe
class Monad m | Applicative m where
class Monad m | Applicative m
where
bind :: !(m a) (a -> m b) -> m b
(>>=) infixl 1 :: (m a) (a -> m b) -> m b | Monad m
......@@ -19,19 +19,10 @@ class Monad m | Applicative m where
(=<<) infixr 1 :: (a -> m b) (m a) -> m b | Monad m
(=<<) f x :== x >>= f
instance Monad ((->) r)
instance Monad []
instance Monad Maybe
class MonadPlus m | Monad m where
mzero :: m a
mplus :: !(m a) (m a) -> m a
instance MonadPlus []
instance MonadPlus Maybe
class MonadPlus m | Monad m
where
mzero :: m a
mplus :: !(m a) (m a) -> m a
sequence :: !.[a b] -> a [b] | Monad a
sequence_ :: !.[a b] -> a () | Monad a
......
......@@ -3,31 +3,10 @@ implementation module Control.Monad
from Control.Applicative import class Applicative (..), lift
from Data.Functor import class Functor (..)
from Data.List import map, zipWith, replicate
from Data.Maybe import :: Maybe, Nothing, Just
from StdList import foldr, ++
from StdList import foldr
from StdFunc import flip, id, o, const
from StdInt import class +, instance + Int
instance Monad ((->) r) where
bind ma a2mb = \r -> a2mb (ma r) r
instance Monad [] where
bind m k = foldr ((++) o k) [] m
instance Monad Maybe where
bind (Just x) k = k x
bind Nothing _ = Nothing
instance MonadPlus [] where
mzero = []
mplus xs ys = xs ++ ys
instance MonadPlus Maybe where
mzero = Nothing
mplus Nothing ys = ys
mplus xs _ = xs
sequence :: !.[a b] -> a [b] | Monad a
sequence ms = foldr k (lift []) ms
where
......
......@@ -3,7 +3,7 @@ implementation module Control.Monad.Fix
from StdFunc import o
from StdMisc import abort
import Control.Applicative
from Control.Monad import class Monad, instance Monad []
from Control.Monad import class Monad
from Data.Func import fix
import Data.List
import Data.Maybe
......
......@@ -2,9 +2,11 @@ definition module Data.Func
from Data.Functor import class Functor
from Control.Applicative import class Applicative
from Control.Monad import class Monad
instance Functor ((->) r)
instance Applicative ((->) r)
instance Monad ((->) r)
/**
* Function application.
......
......@@ -3,15 +3,21 @@ implementation module Data.Func
from StdFunc import const, o
import Data.Functor
import Control.Applicative
import Control.Monad
instance Functor ((->) r)
where
fmap f g = \x -> (f o g) x
instance Applicative ((->) r) where
instance Applicative ((->) r)
where
pure x = const x
(<*>) f g = \x -> f x (g x)
instance Monad ((->) r)
where
bind ma a2mb = \r -> a2mb (ma r) r
seqSt :: !(a .st -> .st) ![a] !.st -> .st
seqSt f [] st = st
seqSt f [x:xs] st = seqSt f xs (f x st)
......
definition module Data.Functor
from System.IO import :: IO
from StdFunc import const
class Functor f where
class Functor f
where
fmap :: (a -> b) !(f a) -> f b
(<$>) infixl 4 :: (a -> b) !(f a) -> f b | Functor f
......
definition module Data.Generics.GenBimap
// from StdGeneric import generic bimap
import StdGeneric
from StdMaybe import :: Maybe
from Data.Maybe import :: Maybe
derive bimap Maybe, [], (,), (,,), (,,,), (,,,,), (,,,,,), (,,,,,,), (,,,,,,,)
definition module Data.Generics.GenCompress
import StdGeneric, StdMaybe
import StdGeneric
from Data.Maybe import :: Maybe
:: BitVector :== {#Int}
......
implementation module Data.Generics.GenCompress
import StdGeneric, StdEnv, StdMaybe, Data.Generics._Array
import StdGeneric, StdEnv, Data.Generics._Array
from Data.Maybe import :: Maybe(..)
//--------------------------------------------------
// uncompressor monad
......
......@@ -7,4 +7,3 @@ generic gDefault a :: a
derive gDefault Int, Real, String, PAIR, EITHER, CONS, FIELD, OBJECT
derive gDefault [], (,), (,,), (,,,), (,,,,), (,,,,,), (,,,,,,), (,,,,,,,)
......@@ -16,4 +16,3 @@ gDefault{|FIELD|} df = FIELD df
gDefault{|OBJECT|} do = OBJECT do
derive gDefault [], (,), (,,), (,,,), (,,,,), (,,,,,), (,,,,,,), (,,,,,,,)
definition module Data.Generics.GenFMap
import StdGeneric, StdMaybe
import StdGeneric
from Data.Maybe import :: Maybe
:: FMap v
derive bimap FMap
......
implementation module Data.Generics.GenFMap
import StdGeneric, StdEnv, StdMaybe, Data.Generics._Array, Data.Generics.GenMonad
import StdGeneric, StdEnv, Data.Generics._Array, Data.Generics.GenMonad
from Data.Maybe import :: Maybe(..)
derive bimap (,), []
......
......@@ -8,4 +8,3 @@ Out :: !u:(Fix v:a) -> v:(a w:(Fix v:a)), [u <= w]
hylo :: ((.f .b) -> .b) (.a -> (.f .a)) -> (.a -> .b) | gMap{|*->*|} f
cata :: (u:(f .a) -> .a) -> (Fix u:f) -> .a | gMap{|*->*|} f
ana :: (.a -> u:(f .a)) -> .a -> (Fix u:f) | gMap{|*->*|} f
......@@ -15,4 +15,3 @@ cata f = hylo f Out
ana :: (.a -> u:(f .a)) -> .a -> (Fix u:f) | gMap{|*->*|} f
ana f = hylo In f
......@@ -16,4 +16,3 @@ gMap{|{}|} f xs = mapArray f xs
gMap{|{!}|} f xs = mapArray f xs
derive gMap [], (,), (,,), (,,,), (,,,,), (,,,,,), (,,,,,,), (,,,,,,,)
......@@ -9,4 +9,3 @@ derive gMapLSt [], (,), (,,), (,,,), (,,,,), (,,,,,), (,,,,,,), (,,,,,,,)
generic gMapRSt a b :: .a .st -> (.b, .st)
derive gMapRSt c, UNIT, PAIR, EITHER, FIELD, CONS, OBJECT, {}, {!}
derive gMapRSt [], (,), (,,), (,,,), (,,,,), (,,,,,), (,,,,,,), (,,,,,,,)
......@@ -52,4 +52,3 @@ mapStFIELD f (FIELD x) st
mapStOBJECT f (OBJECT x) st
# (x, st) = f x st
= (OBJECT x, st)
definition module Data.Generics.GenMonad
import StdGeneric, StdMaybe, StdList
import StdGeneric
import StdList
from Data.Maybe import :: Maybe
class Monad m where
class Monad m
where
ret :: a:a -> m:(m a:a), [m <= a]
(>>=) infixl 5 :: u:(m .a) v:(.a -> u:(m .b)) -> u:(m .b), [u <= v]
......@@ -17,4 +20,3 @@ derive gMapLM [], Maybe, (,), (,,), (,,,), (,,,,), (,,,,,), (,,,,,,), (,,,,,,,)
generic gMapRM a b :: a:a -> m:(m b:b) | Monad m, [m <= b]
derive gMapRM c, UNIT, PAIR, EITHER, CONS, FIELD, OBJECT
derive gMapRM [], Maybe, (,), (,,), (,,,), (,,,,), (,,,,,), (,,,,,,), (,,,,,,,)
implementation module Data.Generics.GenMonad
import StdGeneric, StdMaybe, StdList, StdFunc
import StdGeneric, StdList, StdFunc
from Data.Maybe import :: Maybe(..)
generic gMapLM a b :: a:a -> m:(m b:b) | Monad m, [m <= b]
gMapLM{|c|} x = ret x
......
definition module Data.Generics.GenParse
import StdGeneric, StdMaybe
import StdGeneric
from Data.Maybe import :: Maybe
class ParseInput s where
parseInput :: s -> (Maybe Char, s)
......
implementation module Data.Generics.GenParse
import StdGeneric, StdEnv, StdMaybe
import StdGeneric, StdEnv
from Data.Maybe import :: Maybe(..)
from StdMaybe import mapMaybe
//---------------------------------------------------------------------------
......
......@@ -24,4 +24,3 @@ generic gPrint a :: a (PrintState *s) -> (PrintState *s) | PrintOutput s
derive gPrint Int, Real, Char, Bool, String, UNIT, PAIR, EITHER, RECORD of d, FIELD of d, CONS of d, OBJECT, [], {!}, {}
//derive bimap PrintState
implementation module Data.Generics.GenPrint
import StdGeneric, StdEnv, StdMaybe
import StdGeneric, StdEnv
from Data.Maybe import :: Maybe(..)
//-------------------------------------------------------------------------------------
......
......@@ -13,5 +13,3 @@ derive gReduceRSt [], (,), (,,), (,,,), (,,,,), (,,,,,), (,,,,,,), (,,,,,,,)
generic gReduceLSt t :: .t .st -> .st
derive gReduceLSt c, UNIT, PAIR, EITHER, CONS, FIELD, OBJECT
derive gReduceLSt [], (,), (,,), (,,,), (,,,,), (,,,,,), (,,,,,,), (,,,,,,,)
......@@ -42,5 +42,3 @@ derive gReduceLSt [], (,), (,,), (,,,), (,,,,), (,,,,,), (,,,,,,), (,,,,,,,)
reduceEITHER fl fr (LEFT x) st = fl x st
reduceEITHER fl fr (RIGHT x) st = fr x st
definition module Data.Generics.GenZip
import StdMaybe, StdGeneric
import StdGeneric
from Data.Maybe import :: Maybe
generic gZip a b c :: .a .b -> .c
derive gZip Int, Bool, Char, Real, String, UNIT, EITHER, PAIR, CONS, FIELD, OBJECT
......
......@@ -2,7 +2,8 @@ implementation module Data.Generics.GenZip
import StdGeneric
import StdEnv
import StdMaybe
from Data.Maybe import :: Maybe(..)
from StdMaybe import mapMaybe
derive bimap Maybe
......@@ -41,4 +42,3 @@ derive gMaybeZip [], (,), (,,), (,,,), (,,,,), (,,,,,), (,,,,,,), (,,,,,,,)
zipMaybe :: .(.a -> .(.b -> .c)) !(Maybe .a) (Maybe .b) -> (Maybe .c)
zipMaybe f (Just x) (Just y) = Just (f x y)
zipMaybe f _ _ = Nothing
......@@ -18,12 +18,12 @@ implementation module Data.Graph.Inductive.PatriciaTree
import Data.Graph.Inductive.Graph
from Control.Monad import class Monad (..), >>=, instance Monad []
from Control.Monad import class Monad (..), >>=
from Control.Applicative import liftA2, class Applicative (..)
import Data.Functor
from Data.IntMap.Strict import :: IntMap, instance == (IntMap a), instance Functor IntMap
import qualified Data.IntMap.Strict as IM
from Data.List import instance Functor [], instance Applicative []
from Data.List import instance Functor [], instance Applicative [], instance Monad []
import qualified Data.List as DL
import StdList, StdTuple, StdMisc, StdOrdList
import StdClass, StdFunc, StdOverloaded
......
......@@ -9,13 +9,13 @@ from StdList import instance < [a]
import qualified StdList as SL
from StdTuple import instance < (a,b)
import StdClass
from Control.Monad import class Monad (..), instance Monad Maybe
from Control.Monad import class Monad (..)
from Data.Functor import class Functor (..)
from StdBool import class == (..), ||, &&
from Control.Applicative import class Applicative(..)
from Control.Monad import liftM
import qualified Data.Foldable as Foldable
from Data.Maybe import :: Maybe (..), fromMaybe, maybe, instance Functor Maybe, instance Applicative Maybe
from Data.Maybe import :: Maybe (..), fromMaybe, maybe, instance Functor Maybe, instance Applicative Maybe, instance Monad Maybe
from Data.Either import :: Either (..)
from Data.Monoid import class Monoid(..), class Semigroup (..)
from Data.Functor import <$>
......
......@@ -5,10 +5,13 @@ from Data.Generics.GenEq import generic gEq
from Data.Functor import class Functor
from Data.Maybe import :: Maybe
from Control.Applicative import class Applicative, class Alternative
from Control.Monad import class Monad, class MonadPlus
instance Functor []
instance Applicative []
instance Alternative []
instance Monad []
instance MonadPlus []
/**
* An element in the list, or Nothing if it does not exist.
......
......@@ -24,6 +24,15 @@ instance Alternative [] where
empty = []
(<|>) fa fa` = fa ++ fa`
instance Monad []
where
bind m k = foldr ((++) o k) [] m
instance MonadPlus []
where
mzero = []
mplus xs ys = xs ++ ys
(!?) infixl 9 :: ![.a] !Int -> Maybe .a
(!?) [x:_] 0 = Just x
(!?) [_:xs] i = xs !? (i-1)
......
......@@ -3,6 +3,7 @@ definition module Data.Maybe
from StdOverloaded import class ==(..)
from Data.Functor import class Functor
from Control.Applicative import class Applicative, class Alternative
from Control.Monad import class Monad, class MonadPlus
/**
* The Maybe type represents an optional value by providing a constructor
......@@ -18,6 +19,8 @@ instance == (Maybe x) | == x
instance Functor Maybe
instance Applicative Maybe
instance Alternative Maybe
instance Monad Maybe
instance MonadPlus Maybe
/**
* Apply a function to the the contents of a Just value and directly return
......
......@@ -5,6 +5,7 @@ import StdFunc
import StdMisc
import Data.Functor
import Control.Applicative
import Control.Monad
:: Maybe a = Nothing | Just a
......@@ -33,6 +34,17 @@ where
(<|>) Nothing r = r
(<|>) l _ = l
instance Monad Maybe
where
bind (Just x) k = k x
bind Nothing _ = Nothing
instance MonadPlus Maybe
where
mzero = Nothing
mplus Nothing ys = ys
mplus xs _ = xs
maybe :: w:b v:(.a -> w:b) !.(Maybe .a) -> w:b
maybe x _ Nothing = x
maybe _ f (Just x) = f x
......
......@@ -11,7 +11,7 @@ import Text.URI
import Data.Functor
import Control.Applicative
from Control.Monad import class Monad, instance Monad Maybe
from Control.Monad import class Monad
import qualified Control.Monad as CM
import TCPIP
......
module gentest
import StdEnv, Data.Generics, System.CommandLine, StdDebug
from Data.Maybe import :: Maybe(..)
:: Tree a b = Tip a | Bin b (Tree a b) (Tree a b)
:: Rose a = Rose a .[Rose a]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment