X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.scl.runtime%2Fscl%2FPrelude.scl;h=f983251093272d2168cb9eacc05db4542660afa0;hp=03eabb66fb8ecd2ff7218fa5191db648350ee280;hb=9d780ffc68dbcb27bd90b5f80f4288de62d149fd;hpb=77e4291d372e7b4b40ee868ad61cc07c51de8c6b diff --git a/bundles/org.simantics.scl.runtime/scl/Prelude.scl b/bundles/org.simantics.scl.runtime/scl/Prelude.scl index 03eabb66f..f98325109 100644 --- a/bundles/org.simantics.scl.runtime/scl/Prelude.scl +++ b/bundles/org.simantics.scl.runtime/scl/Prelude.scl @@ -963,6 +963,9 @@ A class of monads with zero element satisfying """ class (Monad m) => MonadZero m where mzero :: m a + mfilter :: (a -> Boolean) -> m a -> m a + + mfilter p m = m >>= (\x -> if p x then return x else mzero) "Injects a boolean test to a type beloning to `MonadZero`." guard :: MonadZero m => Boolean -> m () @@ -1093,6 +1096,7 @@ class (FunctorE f) => FunctorM f where /// MonadE /// class (FunctorE m, Monad m) => MonadE m where + "An effectful version of the bind operator `(>>=)`" bindE :: m a -> (a -> m b) -> m b instance MonadE Maybe where @@ -1105,7 +1109,26 @@ instance MonadE (Either a) where instance MonadE [] where bindE l f = concatMap f l + +@inline +"An effectful version of the Kleisli composition operator `(>=>)`" +compE :: MonadE m => (a -> m b) -> (b -> m c) -> a -> m c +compE f g x = (f x) `bindE` g + +/// MZeroE /// + +class (MonadE m, MonadZero m) => MonadZeroE m where + filter :: (a -> Boolean) -> m a -> m a + + filter p m = m `bindE` (\x -> if p x then return x else mzero) +instance MonadZeroE [] where + filter = filterList + +instance MonadZeroE Maybe where + filter p (Just x) | not (p x) = Nothing + filter _ m = m + /// Category /// "Identity function." @@ -1292,6 +1315,7 @@ fromMaybe default maybeValue = match maybeValue with _ -> default "`maybe def f v` returns `def` if `v=Nothing` and `f x` if `v=Just x`." +@inline maybe :: b -> (a -> b) -> Maybe a -> b maybe n _ Nothing = n maybe _ f (Just x) = f x @@ -1503,11 +1527,11 @@ snd :: (a,b) -> b snd (x,y) = y @inline -mapFst :: (a -> b) -> (a,c) -> (b,c) +mapFst :: (a -> b) -> (a,c) -> (b,c) mapFst f (x,y) = (f x, y) @inline -mapSnd :: (a -> b) -> (c,a) -> (c,b) +mapSnd :: (a -> b) -> (c,a) -> (c,b) mapSnd f (x,y) = (x, f y) instance (Ord a, Ord b) => Ord (a, b) where @@ -1840,10 +1864,10 @@ foldr1 f l = loop (l!(len-1)) (len-2) `filter pred lst` returns those elements of `lst` that the predicate `pred` accepts. For example filter (> 3) [1, 2, 3, 4, 5, 6] = [4, 5, 6] -""" +""" @inline -filter :: (a -> Boolean) -> [a] -> [a] -filter p l = build (\empty cons -> foldl (\cur x -> if p x then cons cur x else cur) empty l) +filterList :: (a -> Boolean) -> [a] -> [a] +filterList p l = build (\empty cons -> foldl (\cur x -> if p x then cons cur x else cur) empty l) """ Takes those elements of the input list that match `(Just x)` and adds the contents to the resulting list. For example,