Movatterモバイル変換
[0]ホーム
{-# LANGUAGE Trustworthy #-}{-# LANGUAGE NoImplicitPrelude #-}{-# LANGUAGE TypeOperators #-}------------------------------------------------------------------------------- |-- Module : Control.Monad.Fix-- Copyright : (c) Andy Gill 2001,-- (c) Oregon Graduate Institute of Science and Technology, 2002-- License : BSD-style (see the file libraries/base/LICENSE)-- Maintainer : libraries@haskell.org-- Stability : experimental-- Portability : portable---- Monadic fixpoints.---- For a detailed discussion, see Levent Erkok's thesis,-- /Value Recursion in Monadic Computations/, Oregon Graduate Institute, 2002.-------------------------------------------------------------------------------moduleControl.Monad.Fix(MonadFix(mfix),fix)whereimportData.EitherimportData.Function(fix)importData.MaybeimportData.Monoid(Dual(..),Sum(..),Product(..),First(..),Last(..),Alt(..),Ap(..))importData.Ord(Down(..))importGHC.Base(Monad,NonEmpty(..),errorWithoutStackTrace,(.))importGHC.GenericsimportGHC.List(head,tail)importControl.Monad.ST.ImpimportSystem.IO-- | Monads having fixed points with a \'knot-tying\' semantics.-- Instances of 'MonadFix' should satisfy the following laws:---- [/purity/]-- @'mfix' ('return' . h) = 'return' ('fix' h)@---- [/left shrinking/ (or /tightening/)]-- @'mfix' (\\x -> a >>= \\y -> f x y) = a >>= \\y -> 'mfix' (\\x -> f x y)@---- [/sliding/]-- @'mfix' ('Control.Monad.liftM' h . f) = 'Control.Monad.liftM' h ('mfix' (f . h))@,-- for strict @h@.---- [/nesting/]-- @'mfix' (\\x -> 'mfix' (\\y -> f x y)) = 'mfix' (\\x -> f x x)@---- This class is used in the translation of the recursive @do@ notation-- supported by GHC and Hugs.class(Monadm)=>MonadFixmwhere-- | The fixed point of a monadic computation.-- @'mfix' f@ executes the action @f@ only once, with the eventual-- output fed back as the input. Hence @f@ should not be strict,-- for then @'mfix' f@ would diverge.mfix::(a->ma)->ma-- Instances of MonadFix for Prelude monads-- | @since 2.01instanceMonadFixMaybewheremfixf=leta=f(unJusta)inawhereunJust(Justx)=xunJustNothing=errorWithoutStackTrace"mfix Maybe: Nothing"-- | @since 2.01instanceMonadFix[]wheremfixf=casefix(f.head)of[]->[](x:_)->x:mfix(tail.f)-- | @since 4.9.0.0instanceMonadFixNonEmptywheremfixf=casefix(f.neHead)of~(x:|_)->x:|mfix(neTail.f)whereneHead~(a:|_)=aneTail~(_:|as)=as-- | @since 2.01instanceMonadFixIOwheremfix=fixIO-- | @since 2.01instanceMonadFix((->)r)wheremfixf=\r->leta=farina-- | @since 4.3.0.0instanceMonadFix(Eithere)wheremfixf=leta=f(unRighta)inawhereunRight(Rightx)=xunRight(Left_)=errorWithoutStackTrace"mfix Either: Left"-- | @since 2.01instanceMonadFix(STs)wheremfix=fixST-- Instances of Data.Monoid wrappers-- | @since 4.8.0.0instanceMonadFixDualwheremfixf=Dual(fix(getDual.f))-- | @since 4.8.0.0instanceMonadFixSumwheremfixf=Sum(fix(getSum.f))-- | @since 4.8.0.0instanceMonadFixProductwheremfixf=Product(fix(getProduct.f))-- | @since 4.8.0.0instanceMonadFixFirstwheremfixf=First(mfix(getFirst.f))-- | @since 4.8.0.0instanceMonadFixLastwheremfixf=Last(mfix(getLast.f))-- | @since 4.8.0.0instanceMonadFixf=>MonadFix(Altf)wheremfixf=Alt(mfix(getAlt.f))-- | @since 4.12.0.0instanceMonadFixf=>MonadFix(Apf)wheremfixf=Ap(mfix(getAp.f))-- Instances for GHC.Generics-- | @since 4.9.0.0instanceMonadFixPar1wheremfixf=Par1(fix(unPar1.f))-- | @since 4.9.0.0instanceMonadFixf=>MonadFix(Rec1f)wheremfixf=Rec1(mfix(unRec1.f))-- | @since 4.9.0.0instanceMonadFixf=>MonadFix(M1icf)wheremfixf=M1(mfix(unM1.f))-- | @since 4.9.0.0instance(MonadFixf,MonadFixg)=>MonadFix(f:*:g)wheremfixf=(mfix(fstP.f)):*:(mfix(sndP.f))wherefstP(a:*:_)=asndP(_:*:b)=b-- Instances for Data.Ord-- | @since 4.12.0.0instanceMonadFixDownwheremfixf=Down(fix(getDown.f))wheregetDown(Downx)=x
[8]ページ先頭