Retain object when tag is True.
ModuleAgda-2.7.0.1Haskell2010
Agda.Utils.Maybe
Extend Maybe by common operations for the Maybe type.
Note: since this module is usually imported unqualified, we do not use short names, but all names contain Maybe, Just, or 'Nothing.
- 1 type
- 28 values
- PackageAgda-2.7.0.1
- Exports29
- LanguageHaskell2010
- LicenceMIT
- SourceMaybe.hs
Monadic version of fromMaybe.
Version of mapMaybe with different argument ordering.
Filtering a singleton list.
filterMaybe p a = listToMaybe (filter p [a])Lazy version of allJust . sequence.
(allJust = mapM for the Maybe monad.)
Only executes monadic effect while isJust.
caseMaybeM without the Just case.
unionWith for collections of size <= 1.
unionsWith for collections of size <= 1.
Unzipping a list of length <= 1.
caseMaybe with flipped branches.
Monadic version of maybe.
caseMaybeM with flipped branches.
caseMaybeM without the Nothing case.
Lift a maybe to an Alternative.
The Maybe type encapsulates an optional value. A value of type
Maybe a either contains a value of type a (represented as Just a),
or it is empty (represented as Nothing). Using Maybe is a good way to
deal with errors or exceptional cases without resorting to drastic
measures such as error.
The Maybe type is also a monad. It is a simple kind of error monad, where all errors are represented by Nothing. A richer error monad can be built using the Either type.
Instances145Monad, Functor, MonadFix, MonadFail, Applicative, Foldable, …
Monad MaybeDefined in ghc-internal-9.1003.0 · GHC.Internal.BaseFunctor MaybeDefined in ghc-internal-9.1003.0 · GHC.Internal.BaseMonadFix MaybeDefined in ghc-internal-9.1003.0 · GHC.Internal.Control.Monad.FixMonadFail MaybeDefined in ghc-internal-9.1003.0 · GHC.Internal.Control.Monad.FailApplicative MaybeDefined in ghc-internal-9.1003.0 · GHC.Internal.BaseFoldable MaybeDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.FoldableTraversable MaybeDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.TraversableAlternative MaybeDefined in ghc-internal-9.1003.0 · GHC.Internal.BaseMonadPlus MaybeDefined in ghc-internal-9.1003.0 · GHC.Internal.BaseMonadZip MaybeDefined in base-4.20.2.0 · Control.Monad.ZipEq1 MaybeDefined in base-4.20.2.0 · Data.Functor.ClassesOrd1 MaybeDefined in base-4.20.2.0 · Data.Functor.ClassesRead1 MaybeDefined in base-4.20.2.0 · Data.Functor.ClassesShow1 MaybeDefined in base-4.20.2.0 · Data.Functor.ClassesNFData1 MaybeDefined in deepseq-1.5.0.0 · Control.DeepSeqArbitrary1 MaybeDefined in QuickCheck-2.15.0.1 · Test.QuickCheck.ArbitraryMonadThrow MaybeDefined in exceptions-0.10.9 · Control.Monad.CatchHashable1 MaybeDefined in hashable-1.4.7.0 · Data.Hashable.ClassAlt MaybeDefined in semigroupoids-6.0.1 · Data.Functor.AltApply MaybeDefined in semigroupoids-6.0.1 · Data.Functor.Bind.ClassBind MaybeDefined in semigroupoids-6.0.1 · Data.Functor.Bind.ClassExtend MaybeDefined in semigroupoids-6.0.1 · Data.Functor.ExtendPlus MaybeDefined in semigroupoids-6.0.1 · Data.Functor.PlusAlign MaybeDefined in semialign-1.3.1 · Data.Semialign.InternalSemialign MaybeDefined in semialign-1.3.1 · Data.Semialign.InternalUnalign MaybeDefined in semialign-1.3.1 · Data.Semialign.InternalCrosswalk MaybeDefined in semialign-1.3.1 · Data.CrosswalkRepeat MaybeDefined in semialign-1.3.1 · Data.Semialign.InternalUnzip MaybeDefined in semialign-1.3.1 · Data.Semialign.InternalZip MaybeDefined in semialign-1.3.1 · Data.Semialign.InternalFilterable MaybeDefined in witherable-0.5 · WitherableWitherable MaybeDefined in witherable-0.5 · WitherableFromJSON1 MaybeDefined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONToJSON1 MaybeDefined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONUpdater1 MaybeDefined in Agda-2.7.0.1 · Agda.Utils.UpdateGeneric1 MaybeDefined in ghc-internal-9.1003.0 · GHC.Internal.GenericsMonadError () MaybeDefined in mtl-2.3.1 · Control.Monad.Error.ClassFoldableWithIndex () MaybeDefined in indexed-traversable-0.1.4 · WithIndexFunctorWithIndex () MaybeDefined in indexed-traversable-0.1.4 · WithIndexTraversableWithIndex () MaybeDefined in indexed-traversable-0.1.4 · WithIndexRepeatWithIndex () MaybeDefined in semialign-1.3.1 · Data.Semialign.InternalSemialignWithIndex () MaybeDefined in semialign-1.3.1 · Data.Semialign.InternalZipWithIndex () MaybeDefined in semialign-1.3.1 · Data.Semialign.InternalFilterableWithIndex () MaybeDefined in witherable-0.5 · WitherableWitherableWithIndex () MaybeDefined in witherable-0.5 · WitherableMonadBase Maybe MaybeDefined in transformers-base-0.4.6 · Control.Monad.BaseMonadBaseControl Maybe MaybeDefined in monad-control-1.0.3.1 · Control.Monad.Trans.ControlLift a => Lift (Maybe a)Defined in template-haskell-2.22.0.0 · Language.Haskell.TH.SyntaxSingleton a (Maybe a)Defined in Agda-2.7.0.1 · Agda.Utils.SingletonCMaybe a (Maybe a)Defined in Agda-2.7.0.1 · Agda.Utils.SingletonEq a => Eq (Maybe a)Defined in ghc-internal-9.1003.0 · GHC.Internal.MaybeData a => Data (Maybe a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.DataOrd a => Ord (Maybe a)Defined in ghc-internal-9.1003.0 · GHC.Internal.MaybeRead a => Read (Maybe a)Defined in ghc-internal-9.1003.0 · GHC.Internal.ReadShow a => Show (Maybe a)Defined in ghc-internal-9.1003.0 · GHC.Internal.ShowGeneric (Maybe a)Defined in ghc-internal-9.1003.0 · GHC.Internal.GenericsSemigroup a => Semigroup (Maybe a)Defined in ghc-internal-9.1003.0 · GHC.Internal.BaseSemigroup a => Monoid (Maybe a)Defined in ghc-internal-9.1003.0 · GHC.Internal.BaseLift a semigroup into Maybe forming a Monoid according to http://en.wikipedia.org/wiki/Monoid: "Any semigroup
Smay be turned into a monoid simply by adjoining an elementenot inSand defininge*e = eande*s = s = s*efor alls ∈ S."Since 4.11.0: constraint on inner
avalue generalised from Monoid to Semigroup.SingKind a => SingKind (Maybe a)Defined in ghc-internal-9.1003.0 · GHC.Internal.GenericsNFData a => NFData (Maybe a)Defined in deepseq-1.5.0.0 · Control.DeepSeqPretty a => Pretty (Maybe a)Defined in pretty-1.1.3.6 · Text.PrettyPrint.Annotated.HughesPJClassPretty a => Pretty (Maybe a)Defined in pretty-1.1.3.6 · Text.PrettyPrint.HughesPJClassFinite a => Finite (Maybe a)Defined in random-1.2.1.3 · System.Random.GFiniteArbitrary a => Arbitrary (Maybe a)Defined in QuickCheck-2.15.0.1 · Test.QuickCheck.ArbitraryCoArbitrary a => CoArbitrary (Maybe a)Defined in QuickCheck-2.15.0.1 · Test.QuickCheck.ArbitraryFunction a => Function (Maybe a)Defined in QuickCheck-2.15.0.1 · Test.QuickCheck.FunctionTestable prop => Testable (Maybe prop)Defined in QuickCheck-2.15.0.1 · Test.QuickCheck.PropertyBinary a => Binary (Maybe a)Defined in binary-0.8.9.3 · Data.Binary.ClassHashable a => Hashable (Maybe a)Defined in hashable-1.4.7.0 · Data.Hashable.ClassFromJSON a => FromJSON (Maybe a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.FromJSONToJSON a => ToJSON (Maybe a)Defined in aeson-2.2.3.0 · Data.Aeson.Types.ToJSONHashable a => Hashable (Maybe a)Defined in data-hash-0.2.0.1 · Data.Hash.InstancesHashable32 a => Hashable32 (Maybe a)Defined in murmur-hash-0.1.0.11 · Data.Digest.Murmur32Hashable64 a => Hashable64 (Maybe a)Defined in murmur-hash-0.1.0.11 · Data.Digest.Murmur64Pretty a => Pretty (Maybe a)Defined in Agda-2.7.0.1 · Agda.Syntax.Common.PrettyIsInstantiatedMeta a => IsInstantiatedMeta (Maybe a)Defined in Agda-2.7.0.1 · Agda.TypeChecking.Monad.MetaVarsPretty a => Pretty (Maybe a)Defined in Agda-2.7.0.1 · Agda.Compiler.JS.PrettyGlobals a => Globals (Maybe a)Defined in Agda-2.7.0.1 · Agda.Compiler.JS.SyntaxHasRange a => HasRange (Maybe a)Defined in Agda-2.7.0.1 · Agda.Syntax.PositionMakeStrict a => MakeStrict (Maybe a)Defined in Agda-2.7.0.1 · Agda.Compiler.MAlonzo.StrictSubst a => Subst (Maybe a)Defined in Agda-2.7.0.1 · Agda.TypeChecking.Substitute · orphanPrettyTCM a => PrettyTCM (Maybe a)Defined in Agda-2.7.0.1 · Agda.TypeChecking.PrettyToConcrete a => ToConcrete (Maybe a)Defined in Agda-2.7.0.1 · Agda.Syntax.Translation.AbstractToConcreteNull (Maybe a)Defined in Agda-2.7.0.1 · Agda.Utils.NullReduce t => Reduce (Maybe t)Defined in Agda-2.7.0.1 · Agda.TypeChecking.ReduceSimplify t => Simplify (Maybe t)Defined in Agda-2.7.0.1 · Agda.TypeChecking.ReduceInstantiate t => Instantiate (Maybe t)Defined in Agda-2.7.0.1 · Agda.TypeChecking.ReduceNormalise t => Normalise (Maybe t)Defined in Agda-2.7.0.1 · Agda.TypeChecking.ReduceHilite a => Hilite (Maybe a)Defined in Agda-2.7.0.1 · Agda.Interaction.Highlighting.FromAbstractEncodeTCM a => EncodeTCM (Maybe a)Defined in Agda-2.7.0.1 · Agda.Interaction.JSONSubstExpr a => SubstExpr (Maybe a)Defined in Agda-2.7.0.1 · Agda.Syntax.AbstractKillRange a => KillRange (Maybe a)Defined in Agda-2.7.0.1 · Agda.Syntax.PositionSetRange a => SetRange (Maybe a)Defined in Agda-2.7.0.1 · Agda.Syntax.PositionAPatternLike a => APatternLike (Maybe a)Defined in Agda-2.7.0.1 · Agda.Syntax.Abstract.PatternMapNamedArgPattern a => MapNamedArgPattern (Maybe a)Defined in Agda-2.7.0.1 · Agda.Syntax.Abstract.PatternBoundAndUsed a => BoundAndUsed (Maybe a)Defined in Agda-2.7.0.1 · Agda.Syntax.Abstract.UsedNamesDeclaredNames a => DeclaredNames (Maybe a)Defined in Agda-2.7.0.1 · Agda.Syntax.Abstract.ViewsExprLike a => ExprLike (Maybe a)Defined in Agda-2.7.0.1 · Agda.Syntax.Abstract.ViewsAllAreOpaque a => AllAreOpaque (Maybe a)Defined in Agda-2.7.0.1 · Agda.Syntax.CommonAnyIsAbstract a => AnyIsAbstract (Maybe a)Defined in Agda-2.7.0.1 · Agda.Syntax.CommonLensNamed (Maybe a)Defined in Agda-2.7.0.1 · Agda.Syntax.CommonPartialOrd a => PartialOrd (Maybe a)Defined in Agda-2.7.0.1 · Agda.Utils.PartialOrdExprLike a => ExprLike (Maybe a)Defined in Agda-2.7.0.1 · Agda.Syntax.Concrete.GenericCPatternLike p => CPatternLike (Maybe p)Defined in Agda-2.7.0.1 · Agda.Syntax.Concrete.PatternGetDefs a => GetDefs (Maybe a)Defined in Agda-2.7.0.1 · Agda.Syntax.Internal.DefsTermLike a => TermLike (Maybe a)Defined in Agda-2.7.0.1 · Agda.Syntax.Internal.GenericAllMetas a => AllMetas (Maybe a)Defined in Agda-2.7.0.1 · Agda.Syntax.Internal.MetaVarsNamesIn a => NamesIn (Maybe a)Defined in Agda-2.7.0.1 · Agda.Syntax.Internal.NamesFree t => Free (Maybe t)Defined in Agda-2.7.0.1 · Agda.TypeChecking.Free.LazyToAbstract c => ToAbstract (Maybe c)Defined in Agda-2.7.0.1 · Agda.Syntax.Translation.ConcreteToAbstractBinder a => Binder (Maybe a)Defined in Agda-2.7.0.1 · Agda.Syntax.Translation.InternalToAbstractAbsTerm a => AbsTerm (Maybe a)Defined in Agda-2.7.0.1 · Agda.TypeChecking.AbstractPrecomputeFreeVars a => PrecomputeFreeVars (Maybe a)Defined in Agda-2.7.0.1 · Agda.TypeChecking.Free.PrecomputeAbstract t => Abstract (Maybe t)Defined in Agda-2.7.0.1 · Agda.TypeChecking.Substitute · orphanDropArgs a => DropArgs (Maybe a)Defined in Agda-2.7.0.1 · Agda.TypeChecking.DropArgsMentionsMeta t => MentionsMeta (Maybe t)Defined in Agda-2.7.0.1 · Agda.TypeChecking.MetaVars.MentionExpandPatternSynonyms a => ExpandPatternSynonyms (Maybe a)Defined in Agda-2.7.0.1 · Agda.TypeChecking.Patterns.AbstractComputeOccurrences a => ComputeOccurrences (Maybe a)Defined in Agda-2.7.0.1 · Agda.TypeChecking.PositivitySemiRing a => SemiRing (Maybe a)Defined in Agda-2.7.0.1 · Agda.Utils.SemiRingStarSemiRing a => StarSemiRing (Maybe a)Defined in Agda-2.7.0.1 · Agda.Utils.SemiRingFromTerm a => FromTerm (Maybe a)Defined in Agda-2.7.0.1 · Agda.TypeChecking.PrimitivePrimTerm a => PrimTerm (Maybe a)Defined in Agda-2.7.0.1 · Agda.TypeChecking.PrimitivePrimTerm a => PrimType (Maybe a)Defined in Agda-2.7.0.1 · Agda.TypeChecking.PrimitiveToTerm a => ToTerm (Maybe a)Defined in Agda-2.7.0.1 · Agda.TypeChecking.PrimitiveInstantiateFull t => InstantiateFull (Maybe t)Defined in Agda-2.7.0.1 · Agda.TypeChecking.ReduceApply t => Apply (Maybe t)Defined in Agda-2.7.0.1 · Agda.TypeChecking.Substitute · orphanChooseFlex a => ChooseFlex (Maybe a)Defined in Agda-2.7.0.1 · Agda.TypeChecking.Rules.LHS.ProblemEmbPrj a => EmbPrj (Maybe a)Defined in Agda-2.7.0.1 · Agda.TypeChecking.Serialise.Instances.Common · orphanSingI 'NothingDefined in ghc-internal-9.1003.0 · GHC.Internal.GenericsStrict (Maybe a) (Maybe a)Defined in strict-0.5.1 · Data.Strict.ClassesInversePermute [Maybe a] (IntMap a)Defined in Agda-2.7.0.1 · Agda.Utils.PermutationInversePermute [Maybe a] [Maybe a]Defined in Agda-2.7.0.1 · Agda.Utils.PermutationInversePermute [Maybe a] [(Int, a)]Defined in Agda-2.7.0.1 · Agda.Utils.PermutationSingI a2 => SingI ('Just a2)Defined in ghc-internal-9.1003.0 · GHC.Internal.GenericsInversePermute (Int -> a) [Maybe a]Defined in Agda-2.7.0.1 · Agda.Utils.Permutationtype Rep (Maybe a) = D1 ('MetaDataDefined in ghc-internal-9.1003.0 · GHC.Internal.Generics"Maybe"
"GHC.Internal.Maybe"
"ghc-internal"
'False) (C1 ('MetaCons"Nothing"
'PrefixI 'False) U1 :+: C1 ('MetaCons"Just"
'PrefixI 'False) (S1 ('MetaSel 'Nothing 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 a)))type Rep1 Maybe = D1 ('MetaDataDefined in ghc-internal-9.1003.0 · GHC.Internal.Generics"Maybe"
"GHC.Internal.Maybe"
"ghc-internal"
'False) (C1 ('MetaCons"Nothing"
'PrefixI 'False) U1 :+: C1 ('MetaCons"Just"
'PrefixI 'False) (S1 ('MetaSel 'Nothing 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) Par1))type DemoteRep (Maybe a) = Maybe (DemoteRep a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Genericsdata SingDefined in ghc-internal-9.1003.0 · GHC.Internal.Genericstype StM Maybe a = aDefined in monad-control-1.0.3.1 · Control.Monad.Trans.Controltype SubstArg (Maybe a) = SubstArg aDefined in Agda-2.7.0.1 · Agda.TypeChecking.Substitute · orphantype ConOfAbs (Maybe a) = Maybe (ConOfAbs a)Defined in Agda-2.7.0.1 · Agda.Syntax.Translation.AbstractToConcretetype AbsOfCon (Maybe c) = Maybe (AbsOfCon c)Defined in Agda-2.7.0.1 · Agda.Syntax.Translation.ConcreteToAbstracttype ADotT (Maybe a) = ADotT aDefined in Agda-2.7.0.1 · Agda.Syntax.Abstract.Patterntype NameOf (Maybe a) = aDefined in Agda-2.7.0.1 · Agda.Syntax.Common
The catMaybes function takes a list of Maybes and returns a list of all the Just values.
Examples
Basic usage:
catMaybes [Just 1, Nothing, Just 3][1,3]
When constructing a list of Maybe values, catMaybes can be used to return all of the "success" results (if the list is the result of a map, then mapMaybe would be more appropriate):
import GHC.Internal.Text.Read ( readMaybe )[readMaybe x :: Maybe Int | x <- ["1", "Foo", "3"] ][Just 1,Nothing,Just 3]catMaybes $ [readMaybe x :: Maybe Int | x <- ["1", "Foo", "3"] ][1,3]
The fromMaybe function takes a default value and a Maybe value. If the Maybe is Nothing, it returns the default value; otherwise, it returns the value contained in the Maybe.
Examples
Basic usage:
fromMaybe "" (Just "Hello, World!")"Hello, World!"
fromMaybe "" Nothing""
Read an integer from a string using readMaybe. If we fail to
parse an integer, we want to return 0 by default:
import GHC.Internal.Text.Read ( readMaybe )fromMaybe 0 (readMaybe "5")5fromMaybe 0 (readMaybe "")0
The listToMaybe function returns Nothing on an empty list
or Just a where a is the first element of the list.
Examples
Basic usage:
listToMaybe []Nothing
listToMaybe [9]Just 9
listToMaybe [1,2,3]Just 1
Composing maybeToList with listToMaybe should be the identity on singleton/empty lists:
maybeToList $ listToMaybe [5][5]maybeToList $ listToMaybe [][]
But not on lists with more than one element:
maybeToList $ listToMaybe [1,2,3][1]
The maybeToList function returns an empty list when given Nothing or a singleton list when given Just.
Examples
Basic usage:
maybeToList (Just 7)[7]
maybeToList Nothing[]
One can use maybeToList to avoid pattern matching when combined with a function that (safely) works on lists:
import GHC.Internal.Text.Read ( readMaybe )sum $ maybeToList (readMaybe "3")3sum $ maybeToList (readMaybe "")0
The maybe function takes a default value, a function, and a Maybe value. If the Maybe value is Nothing, the function returns the default value. Otherwise, it applies the function to the value inside the Just and returns the result.
Examples
Basic usage:
maybe False odd (Just 3)True
maybe False odd NothingFalse
Read an integer from a string using readMaybe. If we succeed,
return twice the integer; that is, apply (*2) to it. If instead
we fail to parse an integer, return 0 by default:
import GHC.Internal.Text.Read ( readMaybe )maybe 0 (*2) (readMaybe "5")10maybe 0 (*2) (readMaybe "")0
Apply show to a Maybe Int. If we have Just n, we want to show
the underlying Int n. But if we have Nothing, we return the
empty string instead of (for example) "Nothing":
maybe "" show (Just 5)"5"maybe "" show Nothing""
The isNothing function returns True iff its argument is Nothing.
Examples
Basic usage:
isNothing (Just 3)False
isNothing (Just ())False
isNothing NothingTrue
Only the outer constructor is taken into consideration:
isNothing (Just Nothing)False
The mapMaybe function is a version of map which can throw
out elements. In particular, the functional argument returns
something of type Maybe b. If this is Nothing, no element
is added on to the result list. If it is Just b, then b is
included in the result list.
Examples
Using mapMaybe f x is a shortcut for catMaybes $ map f x
in most cases:
import GHC.Internal.Text.Read ( readMaybe )let readMaybeInt = readMaybe :: String -> Maybe IntmapMaybe readMaybeInt ["1", "Foo", "3"][1,3]catMaybes $ map readMaybeInt ["1", "Foo", "3"][1,3]
If we map the Just constructor, the entire list should be returned:
mapMaybe Just [1,2,3][1,2,3]
The fromJust function extracts the element out of a Just and throws an error if its argument is Nothing.
Examples
Basic usage:
fromJust (Just 1)1
2 * (fromJust (Just 10))20
2 * (fromJust Nothing)*** Exception: Maybe.fromJust: Nothing...
WARNING: This function is partial. You can use case-matching instead.
The isJust function returns True iff its argument is of the
form Just _.
Examples
Basic usage:
isJust (Just 3)True
isJust (Just ())True
isJust NothingFalse
Only the outer constructor is taken into consideration:
isJust (Just Nothing)True