Îõ³h*´^;      !"#$%&'()*+,-./0123456789:2.2.0.0 Safe-InferredÝð,;<=;A type class witnessing that a type has finite cardinality.(C) Koz Ross, 2019-2020GPL version 3.0 or later ExperimentalGHC only Trustworthy1=ÂÃÄÅËÍÚÛÝìðBfinitary!Witnesses an isomorphism between a and some (KnownNat n) => Finite n5. Effectively, a lawful instance of this shows that a has exactly n (non-_|_1) inhabitants, and that we have a bijection with  and  as each 'direction'. For any type a with an instance of Finitary, for every non-_|_ x :: a, we have a unique index  i :: Finite n!. We will also refer to any such x as an  inhabitant of a.. We can convert inhabitants to indexes using toFinite0, and also convert indexes to inhabitants with  fromFinite.LawsThe main laws state that  should be a bijection, with  as its inverse, and É must be a truthful representation of the cardinality of the type. Thus:è\texttt{fromFinite} \circ \texttt{toFinite} = \texttt{toFinite} \circ \texttt{fromFinite} = \texttt{id}ˆ\forall x, y :: \texttt{Finite} \; (\texttt{Cardinality} \; a) \; \texttt{fromFinite} \; x = \texttt{fromFinite} \; y \rightarrow x = yð\forall x :: \texttt{Finite} \; (\texttt{Cardinality} \; a) \; \exists y :: a \mid \texttt{fromFinite} \; x = y Furthermore, * should be _order-preserving_. Namely, if a is an instance of Ord, we must have:’\forall i, j :: \texttt{Finite} \; (\texttt{Cardinality} \; a) \; \texttt{fromFinite} \; i \leq \texttt{fromFinite} \; j \rightarrow i \leq j ÅLastly, if you define any of the other methods, these laws must hold:Ý a \neq \emptyset \rightarrow \texttt{start} :: a = \texttt{fromFinite} \; \texttt{minBound} Û a \neq \emptyset \rightarrow \texttt{end} :: a = \texttt{fromFinite} \; \texttt{maxBound} ‹ \forall x :: a \; \texttt{end} :: a \neq x \rightarrow \texttt{next} \; x = (\texttt{fromFinite} \circ + 1 \circ \texttt{toFinite}) \; x ‘ \forall x :: a \; \texttt{start} :: a \neq x \rightarrow \texttt{previous} \; x = (\texttt{fromFinite} \circ - 1 \circ \texttt{toFinite}) \; x Together with the fact that Finite n is well-ordered whenever  KnownNat n holds, a law-abiding Finitary instance for a type a defines a constructive  (https://en.wikipedia.org/wiki/Well-order well-order, witnessed by  and , which agrees with the Ord instance for a , if any.We strongly suggest that  fromFinite and toFinite should have time complexity  \Theta(1), or, if that's not possible, O(\texttt{Cardinality} \; a)9. The latter is the case for instances generated using GenericsØ-based derivation, but not for 'basic' types; thus, these functions for your derived types will only be as slow as their 'structure', rather than their 'contents', provided the contents are of these 'basic' types.finitaryHow many (non-_|_) inhabitants a$ has, as a typelevel natural number.finitary4Converts an index into its corresponding inhabitant.finitary2Converts an inhabitant to its corresponding index.finitary)The first inhabitant, by index, assuming a has any inhabitants.finitary(The last inhabitant, by index, assuming a has any inhabitants.finitary previous x gives >2 the inhabitant whose index precedes the index of x, or ? if no such index exists.finitarynext x gives >1 the inhabitant whose index follows the index of x, or ? if no such index exists.finitaryProduce every inhabitant of aÄ, in ascending order of indexes. If you want descending order, use Down a instead. finitaryProduce every inhabitant of aá, starting with the argument, in ascending order of indexes. If you want descending order, use Down a instead. finitaryProduce every inhabitant of aç, up to and including the argument, in ascending order of indexes. If you want descending order, use Down a instead. finitaryProduce every inhabitant of aß, starting with the first argument, up to the second argument, in ascending order of indexes. inhabitantsFromTo x y! will produce the empty list if toFinite x > toFinite y%. If you want descending order, use Down a instead.finitary"A fixed-length vector over a type a with an instance of FinitaryÄ can be thought of as a fixed-length word over an alphabet of size  Cardinality aõ. Since there are only finitely-many of these, we can index them in lex order, with the ordering determined by the  Finitary a# instance (thus, the 'first' such Vector" is the one where each element is  start :: a3, and the 'last' is the one where each element is end :: a).finitary Despite the newtype-esque nature of Down, due to the requirement that ' is order-preserving, the instance for Down a reverses the indexing.finitaryFor any newtype -esque thing over a type with a Finitary3 instance, we can just 'inherit' the behaviour of a."finitaryõThe product of two finite types will also be finite, with a cardinality equal to the product of their cardinalities.#finitaryíThe sum of two finite types will also be finite, with a cardinality equal to the sum of their cardinalities.$finitaryMaybe a/ introduces one additional inhabitant (namely, ?) to a.%finitaryÂSince any type is isomorphic to itself, it follows that a 'valid'  Finite n (meaning that n is a @) has finite cardinality.&finitaryA÷ has a finite number of inhabitants, varying by platform. This instance will determine this when the library is built.'finitaryB÷ has a finite number of inhabitants, varying by platform. This instance will determine this when the library is built.0finitaryC+ has one inhabitant per Unicode code point.   Ä      !"#$%&'()*+,-./0123456789:;<=>?@ABCDBCEBFGHIJHIKHILÍ'finitary-2.2.0.0-DtJtQ6eQ8PF4u4nGB7DzPA Data.FinitaryfinitaryData.Finitary.THFinitary Cardinality fromFinitetoFinitestartendpreviousnext inhabitantsinhabitantsFrom inhabitantsToinhabitantsFromTo $fGFinitaryM1$fGFinitary:*:$fGFinitary:+: $fGFinitaryU1 $fGFinitaryV1$fFinitaryVector$fFinitaryVector0$fFinitaryVector1$fFinitaryDown $fFinitaryMin $fFinitaryMax$fFinitaryIdentity$fFinitaryFirst$fFinitaryLast$fFinitaryDual$fFinitaryProduct $fFinitarySum$fFinitaryConst$fFinitary(,,,,,)$fFinitary(,,,,)$fFinitary(,,,)$fFinitary(,,) $fFinitary(,)$fFinitaryEither$fFinitaryMaybe$fFinitaryFinite$fFinitaryWord $fFinitaryInt$fFinitaryInt64$fFinitaryInt32$fFinitaryInt16$fFinitaryInt8$fFinitaryWord64$fFinitaryWord32$fFinitaryWord16$fFinitaryWord8$fFinitaryChar$fFinitaryOrdering $fFinitaryBit$fFinitaryBit0 $fFinitaryAll $fFinitaryAny$fFinitaryBool$fFinitaryProxy $fFinitary()$fFinitaryVoid $fGFinitaryK1charCardinality cardinalityOf adjustmentOfbase GHC.MaybeJustNothing GHC.TypeNatsKnownNatghc-prim GHC.TypesWordIntChar