Movatterモバイル変換


[0]ホーム

URL:


{-# LANGUAGE Trustworthy #-}{-# LANGUAGE DeriveGeneric #-}{-# LANGUAGE GeneralizedNewtypeDeriving #-}{-# LANGUAGE NoImplicitPrelude #-}------------------------------------------------------------------------------- |-- Module      :  Control.Applicative-- Copyright   :  Conor McBride and Ross Paterson 2005-- License     :  BSD-style (see the LICENSE file in the distribution)---- Maintainer  :  libraries@haskell.org-- Stability   :  experimental-- Portability :  portable---- This module describes a structure intermediate between a functor and-- a monad (technically, a strong lax monoidal functor).  Compared with-- monads, this interface lacks the full power of the binding operation-- '>>=', but---- * it has more instances.---- * it is sufficient for many uses, e.g. context-free parsing, or the--   'Data.Traversable.Traversable' class.---- * instances can perform analysis of computations before they are--   executed, and thus produce shared optimizations.---- This interface was introduced for parsers by Niklas R&#xF6;jemo, because-- it admits more sharing than the monadic interface.  The names here are-- mostly based on parsing work by Doaitse Swierstra.---- For more details, see-- <http://www.soi.city.ac.uk/~ross/papers/Applicative.html Applicative Programming with Effects>,-- by Conor McBride and Ross Paterson.moduleControl.Applicative(-- * Applicative functorsApplicative(..),-- * AlternativesAlternative(..),-- * InstancesConst(..),WrappedMonad(..),WrappedArrow(..),ZipList(..),-- * Utility functions(<$>),(<$),(<**>),liftA,liftA3,optional,)whereimportControl.Categoryhiding((.),id)importControl.ArrowimportData.MaybeimportData.TupleimportData.EqimportData.OrdimportData.Foldable(Foldable(..))importData.Functor((<$>))importData.Functor.Const(Const(..))importGHC.BaseimportGHC.GenericsimportGHC.List(repeat,zipWith,drop)importGHC.Read(Read)importGHC.Show(Show)newtypeWrappedMonadma=WrapMonad{unwrapMonad::ma}deriving(Generic-- ^ @since 4.7.0.0,Generic1-- ^ @since 4.7.0.0,Monad-- ^ @since 4.7.0.0)-- | @since 2.01instanceMonadm=>Functor(WrappedMonadm)wherefmapf(WrapMonadv)=WrapMonad(liftMfv)-- | @since 2.01instanceMonadm=>Applicative(WrappedMonadm)wherepure=WrapMonad.pureWrapMonadf<*>WrapMonadv=WrapMonad(f`ap`v)liftA2f(WrapMonadx)(WrapMonady)=WrapMonad(liftM2fxy)-- | @since 2.01instanceMonadPlusm=>Alternative(WrappedMonadm)whereempty=WrapMonadmzeroWrapMonadu<|>WrapMonadv=WrapMonad(u`mplus`v)newtypeWrappedArrowabc=WrapArrow{unwrapArrow::abc}deriving(Generic-- ^ @since 4.7.0.0,Generic1-- ^ @since 4.7.0.0)-- | @since 2.01instanceArrowa=>Functor(WrappedArrowab)wherefmapf(WrapArrowa)=WrapArrow(a>>>arrf)-- | @since 2.01instanceArrowa=>Applicative(WrappedArrowab)wherepurex=WrapArrow(arr(constx))liftA2f(WrapArrowu)(WrapArrowv)=WrapArrow(u&&&v>>>arr(uncurryf))-- | @since 2.01instance(ArrowZeroa,ArrowPlusa)=>Alternative(WrappedArrowab)whereempty=WrapArrowzeroArrowWrapArrowu<|>WrapArrowv=WrapArrow(u<+>v)-- | Lists, but with an 'Applicative' functor based on zipping.newtypeZipLista=ZipList{getZipList::[a]}deriving(Show-- ^ @since 4.7.0.0,Eq-- ^ @since 4.7.0.0,Ord-- ^ @since 4.7.0.0,Read-- ^ @since 4.7.0.0,Functor-- ^ @since 2.01,Foldable-- ^ @since 4.9.0.0,Generic-- ^ @since 4.7.0.0,Generic1-- ^ @since 4.7.0.0)-- See Data.Traversable for Traversable instance due to import loops-- |-- > f '<$>' 'ZipList' xs1 '<*>' ... '<*>' 'ZipList' xsN-- >     = 'ZipList' (zipWithN f xs1 ... xsN)---- where @zipWithN@ refers to the @zipWith@ function of the appropriate arity-- (@zipWith@, @zipWith3@, @zipWith4@, ...). For example:---- > (\a b c -> stimes c [a, b]) <$> ZipList "abcd" <*> ZipList "567" <*> ZipList [1..]-- >     = ZipList (zipWith3 (\a b c -> stimes c [a, b]) "abcd" "567" [1..])-- >     = ZipList {getZipList = ["a5","b6b6","c7c7c7"]}---- @since 2.01instanceApplicativeZipListwherepurex=ZipList(repeatx)liftA2f(ZipListxs)(ZipListys)=ZipList(zipWithfxsys)-- | @since 4.11.0.0instanceAlternativeZipListwhereempty=ZipList[]ZipListxs<|>ZipListys=ZipList(xs++drop(lengthxs)ys)-- extra functions-- | One or none.optional::Alternativef=>fa->f(Maybea)optionalv=Just<$>v<|>pureNothing

[8]ページ先頭

©2009-2025 Movatter.jp