Verified Commit 003e6ee4 authored by Camil Staps's avatar Camil Staps 🙂

Move Applicative and Alternative instances

parent 4b596266
......@@ -12,13 +12,6 @@ unwrapMonad :: !(WrappedMonad m a) -> m a
getConst :: !(Const a b) -> a
instance Applicative ((->) r)
instance Applicative Maybe
instance Applicative []
instance Alternative Maybe
instance Alternative []
instance Functor (Const m)
instance Functor (WrappedMonad m) | Monad m
instance Applicative (Const m) | Monoid m
......
......@@ -39,28 +39,6 @@ instance Alternative (WrappedMonad m) | MonadPlus m where
empty = WrapMonad mzero
(<|>) (WrapMonad u) (WrapMonad v) = WrapMonad (mplus u v)
instance Applicative ((->) r) where
pure x = const x
(<*>) f g = \x -> f x (g x)
instance Applicative Maybe where
pure x = Just x
(<*>) Nothing _ = Nothing
(<*>) (Just f) ma = fmap f ma
instance Applicative [] where
pure x = [x]
(<*>) xs x = liftA2 id xs x
instance Alternative Maybe where
empty = Nothing
(<|>) Nothing r = r
(<|>) l _ = l
instance Alternative [] where
empty = []
(<|>) fa fa` = fa ++ fa`
some :: (f a) -> f [a] | Alternative f
some v = some_v
where many_v = some_v <|> lift []
......
definition module Data.Func
from Data.Functor import class Functor
from Control.Applicative import class Applicative
instance Functor ((->) r)
instance Applicative ((->) r)
/**
* Function application.
......
implementation module Data.Func
from StdFunc import o
from StdFunc import const, o
import Data.Functor
import Control.Applicative
instance Functor ((->) r)
where
fmap f g = \x -> (f o g) x
instance Applicative ((->) r) where
pure x = const x
(<*>) f g = \x -> f x (g x)
seqSt :: !(a .st -> .st) ![a] !.st -> .st
seqSt f [] st = st
seqSt f [x:xs] st = seqSt f xs (f x st)
......
......@@ -19,11 +19,11 @@ implementation module Data.Graph.Inductive.PatriciaTree
import Data.Graph.Inductive.Graph
from Control.Monad import class Monad (..), >>=, instance Monad []
from Control.Applicative import liftA2, class Applicative (..), instance Applicative [], instance Applicative Maybe
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 []
from Data.List import instance Functor [], instance Applicative []
import qualified Data.List as DL
import StdList, StdTuple, StdMisc, StdOrdList
import StdClass, StdFunc, StdOverloaded
......
implementation module Data.Heap
import qualified Data.List as L
from Control.Applicative import class Applicative (..), :: WrappedMonad (..), unwrapMonad, instance Applicative (WrappedMonad m), instance Applicative [], instance Functor (WrappedMonad m)
from Control.Applicative import class Applicative (..), :: WrappedMonad (..), unwrapMonad, instance Applicative (WrappedMonad m), instance Functor (WrappedMonad m)
from Control.Monad import liftM, class Monad
import Data.Monoid
from Data.Func import on
from Data.Functor import class Functor (..)
from Data.List import instance Functor []
from Data.List import instance Functor [], instance Applicative []
import qualified Data.Traversable as Traversable
from Data.Traversable import class Traversable (..), instance Traversable []
from Data.Foldable import class Foldable (..), instance Foldable []
......
......@@ -12,10 +12,10 @@ import StdClass
from Control.Monad import class Monad (..), instance Monad Maybe
from Data.Functor import class Functor (..)
from StdBool import class == (..), ||, &&
from Control.Applicative import class Applicative(..), instance Applicative Maybe
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
from Data.Maybe import :: Maybe (..), fromMaybe, maybe, instance Functor Maybe, instance Applicative Maybe
from Data.Either import :: Either (..)
from Data.Monoid import class Monoid(..), class Semigroup (..)
from Data.Functor import <$>
......
definition module Data.List
import StdList
from Data.Generics.GenEq import generic gEq
from Data.Functor import class Functor
from Data.Maybe import :: Maybe
import StdList, Data.Generics.GenEq
from Control.Applicative import class Applicative, class Alternative
instance Functor []
instance Applicative []
instance Alternative []
/**
* An element in the list, or Nothing if it does not exist.
......@@ -143,5 +149,3 @@ strictTRZip2Acc :: ![a] ![b] ![(!a, !b)] -> [(!a, !b)]
strictTRZipWith3 :: !(a b c -> d) ![a] ![b] ![c] -> [d]
strictTRZipWith3Rev :: !(a b c -> d) ![a] ![b] ![c] -> [d]
strictTRZipWith3Acc :: !(a b c -> d) ![a] ![b] ![c] ![d] -> [d]
instance Functor []
implementation module Data.List
import Data.Maybe, StdTuple, StdBool, StdEnum, StdFunc, StdList, StdOrdList, Data.Functor, Data.Generics.GenEq
from StdMisc import abort
import StdBool
import StdEnum
import StdFunc
import StdList
import StdOrdList
import StdTuple
import Data.Functor
import Data.Generics.GenEq
import Data.Maybe
import Control.Applicative
instance Functor []
where
fmap f l = [f e \\ e <- l]
instance Applicative [] where
pure x = [x]
(<*>) xs x = liftA2 id xs x
instance Alternative [] where
empty = []
(<|>) fa fa` = fa ++ fa`
(!?) infixl 9 :: ![.a] !Int -> Maybe .a
(!?) [x:_] 0 = Just x
......@@ -106,10 +127,6 @@ minimum xs = foldl1 min xs
getItems :: ![a] ![Int] -> [a]
getItems list indexes = [x \\ x <- list & idx <- [0..] | isMember idx indexes]
instance Functor []
where
fmap f l = [f e \\ e <- l]
scanl :: (a -> .(.b -> a)) a [.b] -> .[a]
scanl f q ls = [q : (case ls of
[] -> []
......@@ -437,4 +454,3 @@ strictTRZipWith3Acc :: !(a b c -> d) ![a] ![b] ![c] ![d] -> [d]
strictTRZipWith3Acc f [a:as] [b:bs] [c:cs] acc
= strictTRZipWith3Acc f as bs cs [f a b c : acc]
strictTRZipWith3Acc _ _ _ _ acc = acc
......@@ -2,6 +2,7 @@ definition module Data.Maybe
from StdOverloaded import class ==(..)
from Data.Functor import class Functor
from Control.Applicative import class Applicative, class Alternative
/**
* The Maybe type represents an optional value by providing a constructor
......@@ -15,6 +16,8 @@ from Data.Functor import class Functor
instance == (Maybe x) | == x
instance Functor Maybe
instance Applicative Maybe
instance Alternative Maybe
/**
* Apply a function to the the contents of a Just value and directly return
......
......@@ -4,6 +4,7 @@ import StdBool
import StdFunc
import StdMisc
import Data.Functor
import Control.Applicative
:: Maybe a = Nothing | Just a
......@@ -20,6 +21,18 @@ where
fmap f Nothing = Nothing
fmap f (Just a) = Just (f a)
instance Applicative Maybe
where
pure x = Just x
(<*>) Nothing _ = Nothing
(<*>) (Just f) ma = fmap f ma
instance Alternative Maybe
where
empty = Nothing
(<|>) Nothing r = r
(<|>) l _ = l
maybe :: w:b v:(.a -> w:b) !.(Maybe .a) -> w:b
maybe x _ Nothing = x
maybe _ f (Just x) = f x
......
......@@ -6,6 +6,7 @@ from StdFunc import flip
from StdTuple import fst, snd
from StdOrdList import minList, maxList
from StdOverloaded import class toReal
import Data.Generics.GenEq
import Data.List
import Data.Maybe
from Data.Set import :: Set, instance == (Set a), instance < (Set 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