Movatterモバイル変換
[0]ホーム
{-# LANGUAGE Safe #-}------------------------------------------------------------------------------- |-- Module : Data.Functor.Classes-- Copyright : (c) Ross Paterson 2013-- License : BSD-style (see the file LICENSE)---- Maintainer : libraries@haskell.org-- Stability : experimental-- Portability : portable---- Liftings of the Prelude classes 'Eq', 'Ord', 'Read' and 'Show' to-- unary and binary type constructors.---- These classes are needed to express the constraints on arguments of-- transformers in portable Haskell. Thus for a new transformer @T@,-- one might write instances like---- > instance (Eq1 f) => Eq1 (T f) where ...-- > instance (Ord1 f) => Ord1 (T f) where ...-- > instance (Read1 f) => Read1 (T f) where ...-- > instance (Show1 f) => Show1 (T f) where ...---- If these instances can be defined, defining instances of the base-- classes is mechanical:---- > instance (Eq1 f, Eq a) => Eq (T f a) where (==) = eq1-- > instance (Ord1 f, Ord a) => Ord (T f a) where compare = compare1-- > instance (Read1 f, Read a) => Read (T f a) where-- > readPrec = readPrec1-- > readListPrec = readListPrecDefault-- > instance (Show1 f, Show a) => Show (T f a) where showsPrec = showsPrec1---- @since 4.9.0.0-----------------------------------------------------------------------------moduleData.Functor.Classes(-- * Liftings of Prelude classes-- ** For unary constructorsEq1(..),eq1,Ord1(..),compare1,Read1(..),readsPrec1,readPrec1,liftReadListDefault,liftReadListPrecDefault,Show1(..),showsPrec1,-- ** For binary constructorsEq2(..),eq2,Ord2(..),compare2,Read2(..),readsPrec2,readPrec2,liftReadList2Default,liftReadListPrec2Default,Show2(..),showsPrec2,-- * Helper functions-- $examplereadsData,readData,readsUnaryWith,readUnaryWith,readsBinaryWith,readBinaryWith,showsUnaryWith,showsBinaryWith,-- ** Obsolete helpersreadsUnary,readsUnary1,readsBinary1,showsUnary,showsUnary1,showsBinary1,)whereimportControl.Applicative(Alternative((<|>)),Const(Const))importData.Functor.Identity(Identity(Identity))importData.Proxy(Proxy(Proxy))importData.List.NonEmpty(NonEmpty(..))importData.Monoid(mappend)importData.Ord(Down(Down))importGHC.Read(expectP,list,paren)importText.ParserCombinators.ReadPrec(ReadPrec,readPrec_to_S,readS_to_Prec)importText.Read(Read(..),parens,prec,step)importText.Read.Lex(Lexeme(..))importText.Show(showListWith)-- | Lifting of the 'Eq' class to unary type constructors.---- @since 4.9.0.0classEq1fwhere-- | Lift an equality test through the type constructor.---- The function will usually be applied to an equality function,-- but the more general type ensures that the implementation uses-- it to compare elements of the first container with elements of-- the second.---- @since 4.9.0.0liftEq::(a->b->Bool)->fa->fb->Bool-- | Lift the standard @('==')@ function through the type constructor.---- @since 4.9.0.0eq1::(Eq1f,Eqa)=>fa->fa->Booleq1=liftEq(==)-- | Lifting of the 'Ord' class to unary type constructors.---- @since 4.9.0.0class(Eq1f)=>Ord1fwhere-- | Lift a 'compare' function through the type constructor.---- The function will usually be applied to a comparison function,-- but the more general type ensures that the implementation uses-- it to compare elements of the first container with elements of-- the second.---- @since 4.9.0.0liftCompare::(a->b->Ordering)->fa->fb->Ordering-- | Lift the standard 'compare' function through the type constructor.---- @since 4.9.0.0compare1::(Ord1f,Orda)=>fa->fa->Orderingcompare1=liftComparecompare-- | Lifting of the 'Read' class to unary type constructors.---- Both 'liftReadsPrec' and 'liftReadPrec' exist to match the interface-- provided in the 'Read' type class, but it is recommended to implement-- 'Read1' instances using 'liftReadPrec' as opposed to 'liftReadsPrec', since-- the former is more efficient than the latter. For example:---- @-- instance 'Read1' T where-- 'liftReadPrec' = ...-- 'liftReadListPrec' = 'liftReadListPrecDefault'-- @---- For more information, refer to the documentation for the 'Read' class.---- @since 4.9.0.0classRead1fwhere{-# MINIMALliftReadsPrec|liftReadPrec#-}-- | 'readsPrec' function for an application of the type constructor-- based on 'readsPrec' and 'readList' functions for the argument type.---- @since 4.9.0.0liftReadsPrec::(Int->ReadSa)->ReadS[a]->Int->ReadS(fa)liftReadsPrecrprl=readPrec_to_S$liftReadPrec(readS_to_Precrp)(readS_to_Prec(constrl))-- | 'readList' function for an application of the type constructor-- based on 'readsPrec' and 'readList' functions for the argument type.-- The default implementation using standard list syntax is correct-- for most types.---- @since 4.9.0.0liftReadList::(Int->ReadSa)->ReadS[a]->ReadS[fa]liftReadListrprl=readPrec_to_S(list$liftReadPrec(readS_to_Precrp)(readS_to_Prec(constrl)))0-- | 'readPrec' function for an application of the type constructor-- based on 'readPrec' and 'readListPrec' functions for the argument type.---- @since 4.10.0.0liftReadPrec::ReadPreca->ReadPrec[a]->ReadPrec(fa)liftReadPrecrprl=readS_to_Prec$liftReadsPrec(readPrec_to_Srp)(readPrec_to_Srl0)-- | 'readListPrec' function for an application of the type constructor-- based on 'readPrec' and 'readListPrec' functions for the argument type.---- The default definition uses 'liftReadList'. Instances that define-- 'liftReadPrec' should also define 'liftReadListPrec' as-- 'liftReadListPrecDefault'.---- @since 4.10.0.0liftReadListPrec::ReadPreca->ReadPrec[a]->ReadPrec[fa]liftReadListPrecrprl=readS_to_Prec$\_->liftReadList(readPrec_to_Srp)(readPrec_to_Srl0)-- | Lift the standard 'readsPrec' and 'readList' functions through the-- type constructor.---- @since 4.9.0.0readsPrec1::(Read1f,Reada)=>Int->ReadS(fa)readsPrec1=liftReadsPrecreadsPrecreadList-- | Lift the standard 'readPrec' and 'readListPrec' functions through the-- type constructor.---- @since 4.10.0.0readPrec1::(Read1f,Reada)=>ReadPrec(fa)readPrec1=liftReadPrecreadPrecreadListPrec-- | A possible replacement definition for the 'liftReadList' method.-- This is only needed for 'Read1' instances where 'liftReadListPrec' isn't-- defined as 'liftReadListPrecDefault'.---- @since 4.10.0.0liftReadListDefault::Read1f=>(Int->ReadSa)->ReadS[a]->ReadS[fa]liftReadListDefaultrprl=readPrec_to_S(liftReadListPrec(readS_to_Precrp)(readS_to_Prec(constrl)))0-- | A possible replacement definition for the 'liftReadListPrec' method,-- defined using 'liftReadPrec'.---- @since 4.10.0.0liftReadListPrecDefault::Read1f=>ReadPreca->ReadPrec[a]->ReadPrec[fa]liftReadListPrecDefaultrprl=list(liftReadPrecrprl)-- | Lifting of the 'Show' class to unary type constructors.---- @since 4.9.0.0classShow1fwhere-- | 'showsPrec' function for an application of the type constructor-- based on 'showsPrec' and 'showList' functions for the argument type.---- @since 4.9.0.0liftShowsPrec::(Int->a->ShowS)->([a]->ShowS)->Int->fa->ShowS-- | 'showList' function for an application of the type constructor-- based on 'showsPrec' and 'showList' functions for the argument type.-- The default implementation using standard list syntax is correct-- for most types.---- @since 4.9.0.0liftShowList::(Int->a->ShowS)->([a]->ShowS)->[fa]->ShowSliftShowListspsl=showListWith(liftShowsPrecspsl0)-- | Lift the standard 'showsPrec' and 'showList' functions through the-- type constructor.---- @since 4.9.0.0showsPrec1::(Show1f,Showa)=>Int->fa->ShowSshowsPrec1=liftShowsPrecshowsPrecshowList-- | Lifting of the 'Eq' class to binary type constructors.---- @since 4.9.0.0classEq2fwhere-- | Lift equality tests through the type constructor.---- The function will usually be applied to equality functions,-- but the more general type ensures that the implementation uses-- them to compare elements of the first container with elements of-- the second.---- @since 4.9.0.0liftEq2::(a->b->Bool)->(c->d->Bool)->fac->fbd->Bool-- | Lift the standard @('==')@ function through the type constructor.---- @since 4.9.0.0eq2::(Eq2f,Eqa,Eqb)=>fab->fab->Booleq2=liftEq2(==)(==)-- | Lifting of the 'Ord' class to binary type constructors.---- @since 4.9.0.0class(Eq2f)=>Ord2fwhere-- | Lift 'compare' functions through the type constructor.---- The function will usually be applied to comparison functions,-- but the more general type ensures that the implementation uses-- them to compare elements of the first container with elements of-- the second.---- @since 4.9.0.0liftCompare2::(a->b->Ordering)->(c->d->Ordering)->fac->fbd->Ordering-- | Lift the standard 'compare' function through the type constructor.---- @since 4.9.0.0compare2::(Ord2f,Orda,Ordb)=>fab->fab->Orderingcompare2=liftCompare2comparecompare-- | Lifting of the 'Read' class to binary type constructors.---- Both 'liftReadsPrec2' and 'liftReadPrec2' exist to match the interface-- provided in the 'Read' type class, but it is recommended to implement-- 'Read2' instances using 'liftReadPrec2' as opposed to 'liftReadsPrec2',-- since the former is more efficient than the latter. For example:---- @-- instance 'Read2' T where-- 'liftReadPrec2' = ...-- 'liftReadListPrec2' = 'liftReadListPrec2Default'-- @---- For more information, refer to the documentation for the 'Read' class.-- @since 4.9.0.0classRead2fwhere{-# MINIMALliftReadsPrec2|liftReadPrec2#-}-- | 'readsPrec' function for an application of the type constructor-- based on 'readsPrec' and 'readList' functions for the argument types.---- @since 4.9.0.0liftReadsPrec2::(Int->ReadSa)->ReadS[a]->(Int->ReadSb)->ReadS[b]->Int->ReadS(fab)liftReadsPrec2rp1rl1rp2rl2=readPrec_to_S$liftReadPrec2(readS_to_Precrp1)(readS_to_Prec(constrl1))(readS_to_Precrp2)(readS_to_Prec(constrl2))-- | 'readList' function for an application of the type constructor-- based on 'readsPrec' and 'readList' functions for the argument types.-- The default implementation using standard list syntax is correct-- for most types.---- @since 4.9.0.0liftReadList2::(Int->ReadSa)->ReadS[a]->(Int->ReadSb)->ReadS[b]->ReadS[fab]liftReadList2rp1rl1rp2rl2=readPrec_to_S(list$liftReadPrec2(readS_to_Precrp1)(readS_to_Prec(constrl1))(readS_to_Precrp2)(readS_to_Prec(constrl2)))0-- | 'readPrec' function for an application of the type constructor-- based on 'readPrec' and 'readListPrec' functions for the argument types.---- @since 4.10.0.0liftReadPrec2::ReadPreca->ReadPrec[a]->ReadPrecb->ReadPrec[b]->ReadPrec(fab)liftReadPrec2rp1rl1rp2rl2=readS_to_Prec$liftReadsPrec2(readPrec_to_Srp1)(readPrec_to_Srl10)(readPrec_to_Srp2)(readPrec_to_Srl20)-- | 'readListPrec' function for an application of the type constructor-- based on 'readPrec' and 'readListPrec' functions for the argument types.---- The default definition uses 'liftReadList2'. Instances that define-- 'liftReadPrec2' should also define 'liftReadListPrec2' as-- 'liftReadListPrec2Default'.---- @since 4.10.0.0liftReadListPrec2::ReadPreca->ReadPrec[a]->ReadPrecb->ReadPrec[b]->ReadPrec[fab]liftReadListPrec2rp1rl1rp2rl2=readS_to_Prec$\_->liftReadList2(readPrec_to_Srp1)(readPrec_to_Srl10)(readPrec_to_Srp2)(readPrec_to_Srl20)-- | Lift the standard 'readsPrec' function through the type constructor.---- @since 4.9.0.0readsPrec2::(Read2f,Reada,Readb)=>Int->ReadS(fab)readsPrec2=liftReadsPrec2readsPrecreadListreadsPrecreadList-- | Lift the standard 'readPrec' function through the type constructor.---- @since 4.10.0.0readPrec2::(Read2f,Reada,Readb)=>ReadPrec(fab)readPrec2=liftReadPrec2readPrecreadListPrecreadPrecreadListPrec-- | A possible replacement definition for the 'liftReadList2' method.-- This is only needed for 'Read2' instances where 'liftReadListPrec2' isn't-- defined as 'liftReadListPrec2Default'.---- @since 4.10.0.0liftReadList2Default::Read2f=>(Int->ReadSa)->ReadS[a]->(Int->ReadSb)->ReadS[b]->ReadS[fab]liftReadList2Defaultrp1rl1rp2rl2=readPrec_to_S(liftReadListPrec2(readS_to_Precrp1)(readS_to_Prec(constrl1))(readS_to_Precrp2)(readS_to_Prec(constrl2)))0-- | A possible replacement definition for the 'liftReadListPrec2' method,-- defined using 'liftReadPrec2'.---- @since 4.10.0.0liftReadListPrec2Default::Read2f=>ReadPreca->ReadPrec[a]->ReadPrecb->ReadPrec[b]->ReadPrec[fab]liftReadListPrec2Defaultrp1rl1rp2rl2=list(liftReadPrec2rp1rl1rp2rl2)-- | Lifting of the 'Show' class to binary type constructors.---- @since 4.9.0.0classShow2fwhere-- | 'showsPrec' function for an application of the type constructor-- based on 'showsPrec' and 'showList' functions for the argument types.---- @since 4.9.0.0liftShowsPrec2::(Int->a->ShowS)->([a]->ShowS)->(Int->b->ShowS)->([b]->ShowS)->Int->fab->ShowS-- | 'showList' function for an application of the type constructor-- based on 'showsPrec' and 'showList' functions for the argument types.-- The default implementation using standard list syntax is correct-- for most types.---- @since 4.9.0.0liftShowList2::(Int->a->ShowS)->([a]->ShowS)->(Int->b->ShowS)->([b]->ShowS)->[fab]->ShowSliftShowList2sp1sl1sp2sl2=showListWith(liftShowsPrec2sp1sl1sp2sl20)-- | Lift the standard 'showsPrec' function through the type constructor.---- @since 4.9.0.0showsPrec2::(Show2f,Showa,Showb)=>Int->fab->ShowSshowsPrec2=liftShowsPrec2showsPrecshowListshowsPrecshowList-- Instances for Prelude type constructors-- | @since 4.9.0.0instanceEq1MaybewhereliftEq_NothingNothing=TrueliftEq_Nothing(Just_)=FalseliftEq_(Just_)Nothing=FalseliftEqeq(Justx)(Justy)=eqxy-- | @since 4.9.0.0instanceOrd1MaybewhereliftCompare_NothingNothing=EQliftCompare_Nothing(Just_)=LTliftCompare_(Just_)Nothing=GTliftComparecomp(Justx)(Justy)=compxy-- | @since 4.9.0.0instanceRead1MaybewhereliftReadPrecrp_=parens(expectP(Ident"Nothing")*>pureNothing)<|>readData(readUnaryWithrp"Just"Just)liftReadListPrec=liftReadListPrecDefaultliftReadList=liftReadListDefault-- | @since 4.9.0.0instanceShow1MaybewhereliftShowsPrec___Nothing=showString"Nothing"liftShowsPrecsp_d(Justx)=showsUnaryWithsp"Just"dx-- | @since 4.9.0.0instanceEq1[]whereliftEq_[][]=TrueliftEq_[](_:_)=FalseliftEq_(_:_)[]=FalseliftEqeq(x:xs)(y:ys)=eqxy&&liftEqeqxsys-- | @since 4.9.0.0instanceOrd1[]whereliftCompare_[][]=EQliftCompare_[](_:_)=LTliftCompare_(_:_)[]=GTliftComparecomp(x:xs)(y:ys)=compxy`mappend`liftComparecompxsys-- | @since 4.9.0.0instanceRead1[]whereliftReadPrec_rl=rlliftReadListPrec=liftReadListPrecDefaultliftReadList=liftReadListDefault-- | @since 4.9.0.0instanceShow1[]whereliftShowsPrec_sl_=sl-- | @since 4.10.0.0instanceEq1NonEmptywhereliftEqeq(a:|as)(b:|bs)=eqab&&liftEqeqasbs-- | @since 4.10.0.0instanceOrd1NonEmptywhereliftComparecmp(a:|as)(b:|bs)=cmpab`mappend`liftComparecmpasbs-- | @since 4.10.0.0instanceRead1NonEmptywhereliftReadsPrecrdPrdLps=readParen(p>5)(\s'->do(a,s'')<-rdP6s'(":|",s''')<-lexs''(as,s'''')<-rdLs'''return(a:|as,s''''))s-- | @since 4.10.0.0instanceShow1NonEmptywhereliftShowsPrecshwPshwLp(a:|as)=showParen(p>5)$shwP6a.showString" :| ".shwLas-- | @since 4.9.0.0instanceEq2(,)whereliftEq2e1e2(x1,y1)(x2,y2)=e1x1x2&&e2y1y2-- | @since 4.9.0.0instanceOrd2(,)whereliftCompare2comp1comp2(x1,y1)(x2,y2)=comp1x1x2`mappend`comp2y1y2-- | @since 4.9.0.0instanceRead2(,)whereliftReadPrec2rp1_rp2_=parens$paren$dox<-rp1expectP(Punc",")y<-rp2return(x,y)liftReadListPrec2=liftReadListPrec2DefaultliftReadList2=liftReadList2Default-- | @since 4.9.0.0instanceShow2(,)whereliftShowsPrec2sp1_sp2__(x,y)=showChar'('.sp10x.showChar','.sp20y.showChar')'-- | @since 4.9.0.0instance(Eqa)=>Eq1((,)a)whereliftEq=liftEq2(==)-- | @since 4.9.0.0instance(Orda)=>Ord1((,)a)whereliftCompare=liftCompare2compare-- | @since 4.9.0.0instance(Reada)=>Read1((,)a)whereliftReadPrec=liftReadPrec2readPrecreadListPrecliftReadListPrec=liftReadListPrecDefaultliftReadList=liftReadListDefault-- | @since 4.9.0.0instance(Showa)=>Show1((,)a)whereliftShowsPrec=liftShowsPrec2showsPrecshowList-- | @since 4.9.0.0instanceEq2EitherwhereliftEq2e1_(Leftx)(Lefty)=e1xyliftEq2__(Left_)(Right_)=FalseliftEq2__(Right_)(Left_)=FalseliftEq2_e2(Rightx)(Righty)=e2xy-- | @since 4.9.0.0instanceOrd2EitherwhereliftCompare2comp1_(Leftx)(Lefty)=comp1xyliftCompare2__(Left_)(Right_)=LTliftCompare2__(Right_)(Left_)=GTliftCompare2_comp2(Rightx)(Righty)=comp2xy-- | @since 4.9.0.0instanceRead2EitherwhereliftReadPrec2rp1_rp2_=readData$readUnaryWithrp1"Left"Left<|>readUnaryWithrp2"Right"RightliftReadListPrec2=liftReadListPrec2DefaultliftReadList2=liftReadList2Default-- | @since 4.9.0.0instanceShow2EitherwhereliftShowsPrec2sp1___d(Leftx)=showsUnaryWithsp1"Left"dxliftShowsPrec2__sp2_d(Rightx)=showsUnaryWithsp2"Right"dx-- | @since 4.9.0.0instance(Eqa)=>Eq1(Eithera)whereliftEq=liftEq2(==)-- | @since 4.9.0.0instance(Orda)=>Ord1(Eithera)whereliftCompare=liftCompare2compare-- | @since 4.9.0.0instance(Reada)=>Read1(Eithera)whereliftReadPrec=liftReadPrec2readPrecreadListPrecliftReadListPrec=liftReadListPrecDefaultliftReadList=liftReadListDefault-- | @since 4.9.0.0instance(Showa)=>Show1(Eithera)whereliftShowsPrec=liftShowsPrec2showsPrecshowList-- Instances for other functors defined in the base package-- | @since 4.9.0.0instanceEq1IdentitywhereliftEqeq(Identityx)(Identityy)=eqxy-- | @since 4.9.0.0instanceOrd1IdentitywhereliftComparecomp(Identityx)(Identityy)=compxy-- | @since 4.9.0.0instanceRead1IdentitywhereliftReadPrecrp_=readData$readUnaryWithrp"Identity"IdentityliftReadListPrec=liftReadListPrecDefaultliftReadList=liftReadListDefault-- | @since 4.9.0.0instanceShow1IdentitywhereliftShowsPrecsp_d(Identityx)=showsUnaryWithsp"Identity"dx-- | @since 4.9.0.0instanceEq2ConstwhereliftEq2eq_(Constx)(Consty)=eqxy-- | @since 4.9.0.0instanceOrd2ConstwhereliftCompare2comp_(Constx)(Consty)=compxy-- | @since 4.9.0.0instanceRead2ConstwhereliftReadPrec2rp___=readData$readUnaryWithrp"Const"ConstliftReadListPrec2=liftReadListPrec2DefaultliftReadList2=liftReadList2Default-- | @since 4.9.0.0instanceShow2ConstwhereliftShowsPrec2sp___d(Constx)=showsUnaryWithsp"Const"dx-- | @since 4.9.0.0instance(Eqa)=>Eq1(Consta)whereliftEq=liftEq2(==)-- | @since 4.9.0.0instance(Orda)=>Ord1(Consta)whereliftCompare=liftCompare2compare-- | @since 4.9.0.0instance(Reada)=>Read1(Consta)whereliftReadPrec=liftReadPrec2readPrecreadListPrecliftReadListPrec=liftReadListPrecDefaultliftReadList=liftReadListDefault-- | @since 4.9.0.0instance(Showa)=>Show1(Consta)whereliftShowsPrec=liftShowsPrec2showsPrecshowList-- Proxy unfortunately imports this module, hence these instances are placed-- here,-- | @since 4.9.0.0instanceEq1ProxywhereliftEq___=True-- | @since 4.9.0.0instanceOrd1ProxywhereliftCompare___=EQ-- | @since 4.9.0.0instanceShow1ProxywhereliftShowsPrec____=showString"Proxy"-- | @since 4.9.0.0instanceRead1ProxywhereliftReadPrec__=parens(expectP(Ident"Proxy")*>pureProxy)liftReadListPrec=liftReadListPrecDefaultliftReadList=liftReadListDefault-- | @since 4.12.0.0instanceEq1DownwhereliftEqeq(Downx)(Downy)=eqxy-- | @since 4.12.0.0instanceOrd1DownwhereliftComparecomp(Downx)(Downy)=compxy-- | @since 4.12.0.0instanceRead1DownwhereliftReadsPrecrp_=readsData$readsUnaryWithrp"Down"Down-- | @since 4.12.0.0instanceShow1DownwhereliftShowsPrecsp_d(Downx)=showsUnaryWithsp"Down"dx-- Building blocks-- | @'readsData' p d@ is a parser for datatypes where each alternative-- begins with a data constructor. It parses the constructor and-- passes it to @p@. Parsers for various constructors can be constructed-- with 'readsUnary', 'readsUnary1' and 'readsBinary1', and combined with-- @mappend@ from the @Monoid@ class.---- @since 4.9.0.0readsData::(String->ReadSa)->Int->ReadSareadsDatareaderd=readParen(d>10)$\r->[res|(kw,s)<-lexr,res<-readerkws]-- | @'readData' p@ is a parser for datatypes where each alternative-- begins with a data constructor. It parses the constructor and-- passes it to @p@. Parsers for various constructors can be constructed-- with 'readUnaryWith' and 'readBinaryWith', and combined with-- '(<|>)' from the 'Alternative' class.---- @since 4.10.0.0readData::ReadPreca->ReadPrecareadDatareader=parens$prec10reader-- | @'readsUnaryWith' rp n c n'@ matches the name of a unary data constructor-- and then parses its argument using @rp@.---- @since 4.9.0.0readsUnaryWith::(Int->ReadSa)->String->(a->t)->String->ReadStreadsUnaryWithrpnameconskws=[(consx,t)|kw==name,(x,t)<-rp11s]-- | @'readUnaryWith' rp n c'@ matches the name of a unary data constructor-- and then parses its argument using @rp@.---- @since 4.10.0.0readUnaryWith::ReadPreca->String->(a->t)->ReadPrectreadUnaryWithrpnamecons=doexpectP$Identnamex<-steprpreturn$consx-- | @'readsBinaryWith' rp1 rp2 n c n'@ matches the name of a binary-- data constructor and then parses its arguments using @rp1@ and @rp2@-- respectively.---- @since 4.9.0.0readsBinaryWith::(Int->ReadSa)->(Int->ReadSb)->String->(a->b->t)->String->ReadStreadsBinaryWithrp1rp2nameconskws=[(consxy,u)|kw==name,(x,t)<-rp111s,(y,u)<-rp211t]-- | @'readBinaryWith' rp1 rp2 n c'@ matches the name of a binary-- data constructor and then parses its arguments using @rp1@ and @rp2@-- respectively.---- @since 4.10.0.0readBinaryWith::ReadPreca->ReadPrecb->String->(a->b->t)->ReadPrectreadBinaryWithrp1rp2namecons=doexpectP$Identnamex<-steprp1y<-steprp2return$consxy-- | @'showsUnaryWith' sp n d x@ produces the string representation of a-- unary data constructor with name @n@ and argument @x@, in precedence-- context @d@.---- @since 4.9.0.0showsUnaryWith::(Int->a->ShowS)->String->Int->a->ShowSshowsUnaryWithspnamedx=showParen(d>10)$showStringname.showChar' '.sp11x-- | @'showsBinaryWith' sp1 sp2 n d x y@ produces the string-- representation of a binary data constructor with name @n@ and arguments-- @x@ and @y@, in precedence context @d@.---- @since 4.9.0.0showsBinaryWith::(Int->a->ShowS)->(Int->b->ShowS)->String->Int->a->b->ShowSshowsBinaryWithsp1sp2namedxy=showParen(d>10)$showStringname.showChar' '.sp111x.showChar' '.sp211y-- Obsolete building blocks-- | @'readsUnary' n c n'@ matches the name of a unary data constructor-- and then parses its argument using 'readsPrec'.---- @since 4.9.0.0{-# DEPRECATEDreadsUnary"Use readsUnaryWith to define liftReadsPrec"#-}readsUnary::(Reada)=>String->(a->t)->String->ReadStreadsUnarynameconskws=[(consx,t)|kw==name,(x,t)<-readsPrec11s]-- | @'readsUnary1' n c n'@ matches the name of a unary data constructor-- and then parses its argument using 'readsPrec1'.---- @since 4.9.0.0{-# DEPRECATEDreadsUnary1"Use readsUnaryWith to define liftReadsPrec"#-}readsUnary1::(Read1f,Reada)=>String->(fa->t)->String->ReadStreadsUnary1nameconskws=[(consx,t)|kw==name,(x,t)<-readsPrec111s]-- | @'readsBinary1' n c n'@ matches the name of a binary data constructor-- and then parses its arguments using 'readsPrec1'.---- @since 4.9.0.0{-# DEPRECATEDreadsBinary1"Use readsBinaryWith to define liftReadsPrec"#-}readsBinary1::(Read1f,Read1g,Reada)=>String->(fa->ga->t)->String->ReadStreadsBinary1nameconskws=[(consxy,u)|kw==name,(x,t)<-readsPrec111s,(y,u)<-readsPrec111t]-- | @'showsUnary' n d x@ produces the string representation of a unary data-- constructor with name @n@ and argument @x@, in precedence context @d@.---- @since 4.9.0.0{-# DEPRECATEDshowsUnary"Use showsUnaryWith to define liftShowsPrec"#-}showsUnary::(Showa)=>String->Int->a->ShowSshowsUnarynamedx=showParen(d>10)$showStringname.showChar' '.showsPrec11x-- | @'showsUnary1' n d x@ produces the string representation of a unary data-- constructor with name @n@ and argument @x@, in precedence context @d@.---- @since 4.9.0.0{-# DEPRECATEDshowsUnary1"Use showsUnaryWith to define liftShowsPrec"#-}showsUnary1::(Show1f,Showa)=>String->Int->fa->ShowSshowsUnary1namedx=showParen(d>10)$showStringname.showChar' '.showsPrec111x-- | @'showsBinary1' n d x y@ produces the string representation of a binary-- data constructor with name @n@ and arguments @x@ and @y@, in precedence-- context @d@.---- @since 4.9.0.0{-# DEPRECATEDshowsBinary1"Use showsBinaryWith to define liftShowsPrec"#-}showsBinary1::(Show1f,Show1g,Showa)=>String->Int->fa->ga->ShowSshowsBinary1namedxy=showParen(d>10)$showStringname.showChar' '.showsPrec111x.showChar' '.showsPrec111y{- $exampleThese functions can be used to assemble 'Read' and 'Show' instances fornew algebraic types. For example, given the definition> data T f a = Zero a | One (f a) | Two a (f a)a standard 'Read1' instance may be defined as> instance (Read1 f) => Read1 (T f) where> liftReadPrec rp rl = readData $> readUnaryWith rp "Zero" Zero <|>> readUnaryWith (liftReadPrec rp rl) "One" One <|>> readBinaryWith rp (liftReadPrec rp rl) "Two" Two> liftReadListPrec = liftReadListPrecDefaultand the corresponding 'Show1' instance as> instance (Show1 f) => Show1 (T f) where> liftShowsPrec sp _ d (Zero x) => showsUnaryWith sp "Zero" d x> liftShowsPrec sp sl d (One x) => showsUnaryWith (liftShowsPrec sp sl) "One" d x> liftShowsPrec sp sl d (Two x y) => showsBinaryWith sp (liftShowsPrec sp sl) "Two" d x y-}
[8]ページ先頭