!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~ NoneINU  failed =  . &generalize a function that fails with Nothing. &generalize a function that fails with []. &generalize a function that fails with Left. specialization specialization specialization makes an *unsafely*-partial function (i.e. a function that throws exceptions or that has inexhaustive pattern matching) into a *safely*-partial function (i.e. that explicitly returns in a monad that supports failure).#handles the following exceptions:  $Evaluate a value to normal form and H any exceptions are thrown during evaluation. For any error-free value,  spoon = Just.taken from the  Ghttps://hackage.haskell.org/package/spoon-0.3.1/docs/Control-Spoon.htmlspoon package. ;the eliminator as a function and the introducer as a stringThelper for declaring Show instances of datatypes without visible constructors (like Map which is shown as an list). "the power set of a set of values. 2(powerset2matrix . powerSet . Set.fromList) [1..3]*[[],[1],[2],[3],[1,2],[1,3],[2,3],[1,2,3]] 2(powerset2matrix . dropEach . Set.fromList) [1..3][[1,2],[1,3],[2,3]]Bconvert a power set to an isomorphic matrix, sorting the entries. (for doctest)     None (1345IKLNU wrap any (Bounded a, Enum a) to be a  Enumerable via .(avoids OverlappingInstances).(see "Data.Enumerate.Reify.getJectivityM"8"Generic Enumerable", lifted to unary type constructors.2a (safely-)partial function. i.e. a function that:fails only via the  method of succeeds only via the  method of Xenumerate the set of all values in a (finitely enumerable) type. enumerates depth first. generalizes 7s to any finite/discrete type. an Enumerable is either:an Enuma product of Enumerablesa sum of Enumerables)can be implemented automatically via its  instance.laws: consistent:  =  so you can index the $ with a nonnegative index below the . distinct:  (Eq a) => nub  ==  complete: x `elem' coincides with Bounded Enums: ( a,  a) =>  ==  ( a) =>  == (Bounded2 constraint elided for convenience, but relevant.),("inputs" a type, outputs a list of values).for non- Bounded Enums: instance Enumerable _ where  = boundedEnumerated  =  for non- Bounded Enums.2behavior may be undefined when the cardinality of a# is larger than the cardinality of Int. this should be okay, as Int is at least as big as Int64;, which is at least as big as all the monomorphic types in base that instantiate Bounded. you can double-check with:AboundedCardinality (const(undefined::Int)) -- platform specific18446744073709551616 6-- i.e. 1 + 9223372036854775807 - -9223372036854775808/works with non-zero-based Enum instances, like Int64 or a custom toEnum/fromEnum=. assumes the enumeration's numbering is contiguous, e.g. if  fromEnum 0 and  fromEnum 2 both exist, then  fromEnum 1 should exist too.for non- Enums: instance Enumerable ... where  = enumEnumerated !the enum should still be bounded. for non- Bounded Indexed () types: instance Enumerable _ where  = indexedEnumerated  = ! !for non- Bounded Indexed () types."\enumerate only when the cardinality is small enough. returns the cardinality when too large.)enumerateBelow 2 :: Either Natural [Bool]Left 2+enumerateBelow 100 :: Either Natural [Bool]Right [False,True]useful when you've established that traversing a list below some length and consuming its values is reasonable for your application. e.g. after benchmarking, you think you can process a billion entries within a minute.#menumerate only when completely evaluating the list doesn't timeout (before the given number of microseconds).@enumerateTimeout (2 * 10^6) :: IO (Maybe [Bool]) -- two secondsJust [False,True]$the  is the cardinality of the   of a, i.e. 2^|a|@. warning: it grows quickly. don't try to take the power set of  ! or even .the l call is efficient (depending on the efficiency of the base type's call). you should be able to safely call "1, unless the arithmetic itself becomes too large.%the cardinality is 1. &-the cardinality is product of cardinalities. ,the product type.the ( is the product of the cardinalities of a and b.. the sum type.the $ is the sum of the cardinalities of a and b./0there are only a million (1,114,112) characters. ord minBound0 ord maxBound1114111length [chr 0..]11141121%(maxBound::Int16) - (minBound::Int16)655353#(maxBound::Int8) - (minBound::Int8)2569ignore datatype metadata:ignore constructor metadata;ignore selector metadata<add lists with (<>)=multiply lists with  concatMap>call ?singleton list@ empty list2 !"#$%&'()*+,-./0123456789:;<=>?@ !"#2@?>=<;:9876543210/.-,+*)('&%$ !"#) !"#$%&'()*+,-./0123456789:;<=>?@NoneINU Areify a total function. >>> reifyFunction  [(False,True),(True,False)] B4reify a total function at any subset of the domain. CQreify a (safely-)partial function into a map (which is implicitly partial, where  Map.lookup is like ($).D1reify a (safely-)partial function at any domain.  use the functions suffixed with M9 when your function is explicitly partial, i.e. of type $(forall m. MonadThrow m => a -> m b)(. when inside a function arrow, like:  lreifyFunctionAtM :: [a] -> (forall m. MonadThrow m => a -> m b) -> [(a,b)] reifyFunctionAtM domain f = ... the Rank2* type (and non-concrete types) means that fG can only use parametric polymorphic functions, or the methods of the  MonadThrow class (namely ), or methods of  MonadThrow superclasses (namely , et cetera).  is a class from the  exceptions= package that generalizes failibility. it has instances for Maybe, Either, [], IO, and more.  use the functions suffixed with At when your domain isn't +, or when you want to restrict the domain.)the most general function in this module.:{9let uppercasePartial :: (MonadThrow m) => Char -> m Char " uppercasePartial c = case c of 'a' -> return 'A' 'b' -> return 'B' 'z' -> return 'Z'% _ -> failed "uppercasePartial":} >>> reifyFunctionAtM ['a'..'c'] uppercasePartial [(a,A),(b,B)] $if your function doesn't fail under , see: reifyFunctionAtMaybereifyFunctionAtListreifyFunctionAtEitherE reifyPredicateAt =  F@reify a (safely-)partial function that fails specifically under Maybe. G@reify a (safely-)partial function that fails specifically under []. H@reify a (safely-)partial function that fails specifically under Either SomeException. Izreifies an *unsafely*-partial function (i.e. a function that throws exceptions or that has inexhaustive pattern matching).!forces the function to be strict. >>> import Data.Ratio (Ratio) >>> fmap (1/) [0..3 :: Ratio Integer] [*** Exception: Ratio has zero denominator >>> let (1/) = reciprocal >>> reifyFunctionSpoonAt [0..3 :: Ratio Integer] reciprocal [(1 % 1,1 % 1),(2 % 1,1 % 2),(3 % 1,1 % 3)] *normal caveats from violating purity (via unsafePerformIO) and from catchalls (via (e :: SomeExceptions -> _)) apply.Jreify a binary total functionK,reify a binary total function at some domainL(reify a binary (safely-)partial functionM8reify a binary (safely-)partial function at some domain ABCDEFGHIJKLM ABCDEFGHIJKLM ABCDEFGHIJKLM ABCDEFGHIJKLMNoneINUN#convert a total function to a map. >>> fromFunction ' fromList [(False,True),(True,False)] O.convert a (safely-)partial function to a map. wraps C. P2convert a map to a function, if the map is total. `>>> let Just not' = toFunction (Map.fromList [(False,True),(True,False)]) >>> not' False True Q.convert a (safely-)partial function to a map. lookup failures are n as a . >>> let idPartial = toFunctionM (Map.fromList [(True,True)]) >>> idPartial True True >>> idPartial False *** Exception: toFunctionM Rwraps  S.does the map contain every key in its domain? 5isMapTotal (Map.fromList [(False,True),(True,False)])True #isMapTotal (Map.fromList [('a',0)])False Tsafely invert any map. U'refines the partial function, if total.:{'let myNotM :: Monad m => Bool -> m Bool myNotM False = return True myNotM True = return False :} let Just myNot = isTotalM myNotM myNot FalseTrueVthe  =https://en.wikipedia.org/wiki/Partial_function#Basic_conceptsdomain- of a partial function is the subset of the  input where it's defined.  i.e. when x `member` (domainM f) then fromJust (f x) is defined. domainM uppercasePartial['a','b','z'] W(right name?) corange _ = enumeratedX corangeM _ = enumeratedYthe image of a total function.  imageM f = map f includes duplicates. Zthe image (not the [) of a partial function.  imageM f = mapMaybe f includes duplicates. [,the codomain of a function. it contains the Y. codomain _ = enumerated]invert a total function. (invert f) b is: [] wherever f is not surjective [y] wherever f is uniquely defined (_:_) wherever f is not injective  invert f = ^ (return.f)^invert a partial function. (invertM f) b is: [] wherever f is partial [] wherever f is not surjective [y] wherever f is uniquely defined (_:_) wherever f is not injective a Map0 is stored internally, with as many keys as the Y of f.  see also f.a3returns the inverse of the injection, if injective.refines  (b -> [a]) (i.e. the type of ^) to (b -> Maybe a). unlike f, doesn't need an (Enumerable b)W constraint. this helps when you want to ensure a function into an infinite type (e.g. :) is injective. and still reasonably efficient, given the (Ord b) constraint. b7converts the list into a set, if it has no duplicates. dNreturns the inverse of the surjection, if surjective. i.e. when a function's \ equals its Z. refines  (b -> [a]) (i.e. the type of ^) to (b -> NonEmpty a). can short-circuit. f3returns the inverse of the bijection, if bijective.refines  (b -> [a]) (i.e. the type of ^) to (b -> a). can short-circuit. NOPQRSTUVWXYZ[\]^_`abcdefNOPQRSTUVWXYZ[\]^_`abcdefNOPQRSTUVWXYZ[\]^_`abcdefNOPQRSTUVWXYZ[\]^_`abcdefNoneN ghijklghijklghijkl ghijklNoneFnfinite but too big. 2^64 is over a billion billion (1,000,000,000,000). e.g.  ? (which takes time linear in the domain) on a function of type (:: Int -> Bool)2, even a lazy one, won't terminate anytime soon. mnopponmmnopNoneFNqwraps  2(unsafeFromList [(False,True),(True,False)]) FalseTrue1(unsafeFromList [(False,True),(True,False)]) TrueFalsersee s s[(a,b)] is a mapping,  [[(a,b)]] is a list of mappings. Dlet orderingPredicates = mappingEnumeratedAt [LT,EQ,GT] [False,True]!print $ length orderingPredicates8"printMappings $ orderingPredicates@ (LT,False) (EQ,False) (GT,False) (LT,False) (EQ,False) (GT,True) (LT,False) (EQ,True) (GT,False) (LT,False) (EQ,True) (GT,True) (LT,True) (EQ,False) (GT,False) (LT,True) (EQ,False) (GT,True) (LT,True) (EQ,True) (GT,False) (LT,True) (EQ,True) (GT,True) (LT,False) (EQ,False) (GT,False) (LT,False) (EQ,False) (GT,True) (LT,False) (EQ,True) (GT,False) (LT,False) (EQ,True) (GT,True) (LT,True) (EQ,False) (GT,False) (LT,True) (EQ,False) (GT,True) (LT,True) (EQ,True) (GT,False) (LT,True) (EQ,True) (GT,True)where the (total) mapping:  (LT,False) (EQ,False) (GT,True) is equivalent to the function: ,\case LT -> False EQ -> False GT -> True t?let crossOrderingBoolean = crossProduct [LT,EQ,GT] [False,True]$printMappings $ crossOrderingBoolean (LT,False) (LT,True) (EQ,False) (EQ,True) (GT,False) (GT,True){the length of the outer list is the size of the first set and the length of the inner list is the size of the second set. #print $ length crossOrderingBoolean3*print $ length (head crossOrderingBoolean)2u print not*unsafeFromList [(False,True),(True,False)]bbecause functions are curried, the instance is recursive, and it works on functions of any arity:  print (&&)vunsafeFromList [(False,unsafeFromList [(False,False),(True,False)]),(True,unsafeFromList [(False,False),(True,True)])]v%brute-force function extensionality. Pwarning: the size of the domain grows exponentially in the number of arguments. <(\case LT -> False; EQ -> False; GT -> False) == const FalseTrue;(\case LT -> False; EQ -> False; GT -> False) == const TrueFalsebbecause functions are curried, the instance is recursive, and it works on functions of any arity:  J -- De Morgan's laws >> (\x y -> not (x && y)) == (\x y -> not x || not y)>True >>> (x y -> not (x || y)) == (x y -> not x && not y) Truewthe exponential type. the  is the cardinality of b raised to the cardinality a, i.e. |b|^|a|. warning: it grows very quickly. dmight be useful for generating random functions on small types, like to fuzz test type class laws. the  call is efficient (depending on the efficiency of the base type's call). you should be able to safely (WRT performance) call "7, unless the arithmetic itself becomes too expensive.  instance ( a, Enumerable b, 7 a, Ord b) => Enumerable (a -> b) where enumerated = r qrstuvwqrstwvuqrstqrstuvw NoneA !"#ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklNone0235Ux(for documentation)Kdemonstrates: empty type, unit type, product type, sum type, type variable.with .{-# LANGUAGE DeriveGeneric, DeriveAnyClass #-} , the derivation is a one-liner: 5data Demo a = ... deriving (Show,Generic,Enumerable) ~(for documentation) demoEnumerated = enumeratedtraverse print demoEnumerated Demo1Demo2 False NothingDemo2 False (Just False)Demo2 False (Just True)Demo2 True NothingDemo2 True (Just False)Demo2 True (Just True) Demo3 False Demo3 True xyz{|}~ xyz{|}~ }xyz{|~xyz{|}~   !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJK LMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~enume_JKgjgKJmt4OADqxOmIVCKSData.Enumerate.ExtraData.Enumerate.TypesData.Enumerate.ReifyData.Enumerate.MapData.Enumerate.EnumData.Enumerate.LargeData.Enumerate.FunctionData.Enumerate.Example Enumerate reifyFunctionData.Enumeratefailed maybe2throw list2throw either2throw throw2maybe throw2either throw2listtotalizeFunctiondefaultPartialityHandlers spoonWith showsPrecWith int2naturalpowerSetdropEachpowerset2matrixWrappedBoundedEnumunwrapBoundedEnum Jectivity Injective Surjective Bijective GEnumerable genumerated gcardinalityPartial Enumerable enumerated cardinalityboundedEnumeratedboundedCardinalityenumEnumeratedindexedEnumeratedindexedCardinalityenumerateBelowenumerateTimeout$fEnumerableSet$fEnumerableRec$fEnumerableRec0$fEnumerable(,,,,,,)$fEnumerable(,,,,,)$fEnumerable(,,,,)$fEnumerable(,,,)$fEnumerable(,,)$fEnumerable(,)$fEnumerableMaybe$fEnumerableEither$fEnumerableChar$fEnumerableWord16$fEnumerableInt16$fEnumerableWord8$fEnumerableInt8$fEnumerableOrdering$fEnumerableBool$fEnumerable()$fEnumerableVoid$fEnumerableWrappedBoundedEnum$fGEnumerableM1$fGEnumerableM10$fGEnumerableM11$fGEnumerable:+:$fGEnumerable:*:$fGEnumerableK1$fGEnumerableU1$fGEnumerableV1reifyFunctionAtreifyFunctionMreifyFunctionAtMreifyPredicateAtreifyFunctionMaybeAtreifyFunctionListAtreifyFunctionEitherAtreifyFunctionSpoonAtreifyFunction2reifyFunction2AtreifyFunction2MreifyFunction2AtM fromFunction fromFunctionM toFunction toFunctionMunsafeToFunction isMapTotal invertMapisTotalMdomainMcorangecorangeMimageimageMcodomain codomainMinvertinvertM getJectivityM isInjective isInjectiveMisUnique isSurjective isSurjectiveM isBijective isBijectiveMminBound_enumerablemaxBound_enumerabletoEnum_enumerablefromEnum_enumerablearray_enumerabletable_enumerable$fEnumerableWord64$fEnumerableInt64$fEnumerableWord32$fEnumerableInt32unsafeFromListfunctionEnumeratedmappingEnumeratedAt crossProduct $fShow(->)$fEq(->)$fEnumerable(->)DemoDemo0Demo1Demo2Demo3maindemoEnumeratedarray_DemoBooltable_DemoBool $fEnumDemo $fBoundedDemoexcep_Jkl94O78MPXBSznLIarVn2Control.Monad.CatchthrowMbaseGHC.IO.Exception userError GHC.ExceptionArithExceptionArrayException ErrorCallControl.Exception.BasePatternMatchFail MonadThrowGHC.BasereturnMonadGHC.EnumEnum GHC.GenericsGeneric Data.FoldablelengthBoundedGHC.ArrIxghc-prim GHC.TypesCharGHC.WordWord8 GHC.ClassesnotflipGHC.Listfilterconta_2C3ZI8RgPO2LBMidXKTvIU Data.Map.BaselookupGHC.Showshow __fromJust____bug__nat2intOrd