h$3.      !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~Safe3finNat natural numbers.Better than GHC's built-in   for some use cases.fin displaying a structure of .explicitShow 0"Z"explicitShow 2 "S (S Z)"fin displaying a structure of .finFold .cata [] ('x' :) 2"xx"finConvert  to  toNatural 00 toNatural 22toNatural $ S $ S $ Z2finConvert  to  fromNatural 44explicitShow (fromNatural 4)"S (S (S (S Z)))"fin)import qualified Data.Universe.Class as Utake 10 (U.universe :: [Nat])[0,1,2,3,4,5,6,7,8,9]fin is printed as .To see explicit structure, use  or     Trustworthy Trustworthy'(/23!+finDivision by two.  is 0 and  is 1 as a remainder.$:kind! DivMod2 Nat7 == '(Nat3, True)%DivMod2 Nat7 == '(Nat3, True) :: Bool= 'True%:kind! DivMod2 Nat4 == '(Nat2, False)&DivMod2 Nat4 == '(Nat2, False) :: Bool= 'True,fin Multiplication by two. Doubling.#reflect (snat :: SNat (Mult2 Nat4))8-finMultiplication.'reflect (snat :: SNat (Mult Nat2 Nat3))6.fin Addition.'reflect (snat :: SNat (Plus Nat1 Nat2))3/finConvert from GHC .:kind! FromGHC 7FromGHC 7 :: Nat%= 'S ('S ('S ('S ('S ('S ('S 'Z))))))0finConvert to GHC .:kind! ToGHC Nat5ToGHC Nat5 :: GHC.Nat...= 51finType family used to implement   from Data.Type.Equality module.2fin Implicit 4.In an unorthodox singleton way, it actually provides an induction function.2The induction should often be fully inlined. See test/Inspection.hs.:set -XPolyKinds+newtype Const a b = Const a deriving (Show)induction (Const 0) (coerce ((+2) :: Int -> Int)) :: Const Int Nat3Const 64fin Singleton of .7finConstruct explicit 4 value.8fin Constructor 2 dictionary from 4.9finReflect type-level  to the term level.:finAs 9 but with any .;finReify .reify nat3 reflect3<finConvert 4 to .snatToNat (snat :: SNat Nat1)1=finConvert 4 to !snatToNatural (snat :: SNat Nat0)0!snatToNatural (snat :: SNat Nat2)2>fin&Decide equality of type-level numbers.(eqNat :: Maybe (Nat3 :~: Plus Nat1 Nat2) Just Refl(eqNat :: Maybe (Nat3 :~: Mult Nat2 Nat2)Nothing?fin&Decide equality of type-level numbers.6decShow (discreteNat :: Dec (Nat3 :~: Plus Nat1 Nat2)) "Yes Refl"@fin&Decide equality of type-level numbers.)cmpNat :: GOrdering Nat3 (Plus Nat1 Nat2)GEQ)cmpNat :: GOrdering Nat3 (Mult Nat2 Nat2)GLT)cmpNat :: GOrdering Nat5 (Mult Nat2 Nat2)GGTAfin Induction on ', functor form. Useful for computation.BfinUnfold n steps of a general recursion.Note: Always  benchmark". This function may give you both bad properties: a lot of code (increased binary size), and worse performance. For known n B# will unfold recursion, for example B ( ::  ') f = f (f (f (fix f))) Cfin  0 + n = nDfin  n + 0 = nEfin  0 * n = 0Ffin  n * 0 = 0Gfin  1 * n = nHfin  n * 1 = nKfinLfinMfinNfinOfinPfin3fin zero casefininduction stepAfin zero casefininduction step: !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGH:456<=2378;9:>1?@AB.-,+0/ *)('&%$#"!CDEFGHSafe'(/23>?5SfinTotal order of , less-than-or-Equal-to,  \le .UfinAn evidence of n \le m.  zero+succ definition.Xfin Constructor S dictionary from U.Yfin \forall n : \mathbb{N}, 0 \le n Zfin8\forall n\, m : \mathbb{N}, n \le m \to 1 + n \le 1 + m [fin8\forall n\, m : \mathbb{N}, 1 + n \le 1 + m \to n \le m \fin \forall n : \mathbb{N}, n \le n ]fin4\forall n\, m : \mathbb{N}, n \le m \to n \le 1 + m ^fin4\forall n\, m : \mathbb{N}, 1 + n \le m \to n \le m _fin?\forall n\, m : \mathbb{N}, n \le m \to m \le n \to n \equiv m `fin\forall n\, m\, p : \mathbb{N}, n \le m \to m \le p \to n \le p afin;\forall n\, m : \mathbb{N}, \neg (n \le m) \to 1 + m \le n bfin;\forall n\, m : \mathbb{N}, n \le m \to \neg (1 + m \le n) leProof :: LEProof Nat2 Nat3LESucc (LESucc LEZero)/leSwap (leSwap' (leProof :: LEProof Nat2 Nat3))LESucc (LESucc (LESucc LEZero))8lePred (leSwap (leSwap' (leProof :: LEProof Nat2 Nat3)))LESucc (LESucc LEZero)cfin Find the U n m, i.e. compare numbers.dfin0\forall n\ : \mathbb{N}, n \le 0 \to n \equiv 0 gfinU values are unique (not  though!).hfinifinSTUVWXYZ[\]^_`abcdSTUVWXcYZ\]_`ab^[dSafe'(/>?mfinLess-Than-or. <. Well-founded relation on .GHC can solve this for us!ltProof :: LTProof Nat0 Nat4 LESucc LEZeroltProof :: LTProof Nat2 Nat4LESucc (LESucc (LESucc LEZero))ltProof :: LTProof Nat3 Nat3... ...error......ofin An evidence n < m# which is the same as (1 + n le m).qfin'\forall n : \mathbb{N}, n < n \to \bot rfin5\forall n\, m : \mathbb{N}, n < m \to m < n \to \bot sfin:\forall n\, m\, p : \mathbb{N}, n < m \to m < p \to n < p mnopqrsmnopqrsSafe'(/23>?nufinAn evidence of n \le m.  refl+step definition.xfin Convert from  zero+succ to  refl+step definition. Inverse of y.yfinConvert  refl+step to  zero+succ definition. Inverse of x.zfin \forall n : \mathbb{N}, 0 \le n {fin8\forall n\, m : \mathbb{N}, n \le m \to 1 + n \le 1 + m |fin8\forall n\, m : \mathbb{N}, 1 + n \le 1 + m \to n \le m }fin \forall n : \mathbb{N}, n \le n ~fin4\forall n\, m : \mathbb{N}, n \le m \to n \le 1 + m fin4\forall n\, m : \mathbb{N}, 1 + n \le m \to n \le m fin?\forall n\, m : \mathbb{N}, n \le m \to m \le n \to n \equiv m fin\forall n\, m\, p : \mathbb{N}, n \le m \to m \le p \to n \le p fin;\forall n\, m : \mathbb{N}, \neg (n \le m) \to 1 + m \le n fin;\forall n\, m : \mathbb{N}, n \le m \to \neg (1 + m \le n) fin Find the u n m, i.e. compare numbers.fin0\forall n\ : \mathbb{N}, n \le 0 \to n \equiv 0 finfinfinThe other variant ( ) isn't  , because  requires 4 evidence.finu values are unique (not  though!).uvwxyz{|}~uvwxyz{}~|Safe '(/23+finFinite numbers: [0..n-1].finMirror the values,  becomes , etc.#map mirror universe :: [Fin N.Nat4] [3,2,1,0] reverse universe :: [Fin N.Nat4] [3,2,1,0]finMultiplicative inverse. Works for  n where n3 is coprime with an argument, i.e. in general when n is prime.$map inverse universe :: [Fin N.Nat5] [0,1,3,2,4];zipWith (*) universe (map inverse universe) :: [Fin N.Nat5] [0,1,1,1,1]Adaptation of https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm#Modular_integerspseudo-code in Wikipediafin displaying a structure of .explicitShow (0 :: Fin N.Nat1)"FZ"explicitShow (2 :: Fin N.Nat3) "FS (FS FZ)"fin displaying a structure of .finFold .fin Convert to .fin Convert from .$fromNat N.nat1 :: Maybe (Fin N.Nat2)Just 1$fromNat N.nat1 :: Maybe (Fin N.Nat1)Nothingfin Convert to .fin All values. [minBound .. maxBound] won't work for  *.universe :: [Fin N.Nat3][0,1,2]finLike  but ."universe1 :: NonEmpty (Fin N.Nat3) 0 :| [1,2]fin! which will be fully inlined, if n is known at compile time.inlineUniverse :: [Fin N.Nat3][0,1,2]fin(inlineUniverse1 :: NonEmpty (Fin N.Nat3) 0 :| [1,2]fin * is not inhabited.finCounting to one is boring.boring0finReturn a one less.isMin (FZ :: Fin N.Nat1)Nothing*map isMin universe :: [Maybe (Fin N.Nat3)][Nothing,Just 0,Just 1,Just 2]finReturn a one less.isMax (FZ :: Fin N.Nat1)Nothing*map isMax universe :: [Maybe (Fin N.Nat3)][Just 0,Just 1,Just 2,Nothing]fin)map weakenRight1 universe :: [Fin N.Nat5] [1,2,3,4]fin(map weakenLeft1 universe :: [Fin N.Nat5] [0,1,2,3]finmap (weakenLeft (Proxy :: Proxy N.Nat2)) (universe :: [Fin N.Nat3])[0,1,2]finmap (weakenRight (Proxy :: Proxy N.Nat2)) (universe :: [Fin N.Nat3])[2,3,4]fin Append two  s together.6append (Left fin2 :: Either (Fin N.Nat5) (Fin N.Nat4))27append (Right fin2 :: Either (Fin N.Nat5) (Fin N.Nat4))7fin Inverse of ..split fin2 :: Either (Fin N.Nat2) (Fin N.Nat3)Right 0.split fin1 :: Either (Fin N.Nat2) (Fin N.Nat3)Left 18map split universe :: [Either (Fin N.Nat2) (Fin N.Nat3)]'[Left 0,Left 1,Right 0,Right 1,Right 2]fin(U.cardinality :: U.Tagged (Fin N.Nat3) Natural) == U.Tagged (genericLength (U.universeF :: [Fin N.Nat3]))Truefinfinfin works only on  n where n is prime.finOperations module n.3map fromInteger [0, 1, 2, 3, 4, -5] :: [Fin N.Nat3] [0,1,2,0,1,1]fromInteger 42 :: Fin N.Nat0*** Exception: divide by zero...signum (FZ :: Fin N.Nat1)0signum (3 :: Fin N.Nat4)12 + 3 :: Fin N.Nat412 * 3 :: Fin N.Nat42fin is printed as .To see explicit structure, use  or $$Safe-./9>?.fin'Constraint for the class that computes .fin'Constraint for the class that computes .finCompute the size from the type.finGeneric enumerations. Examples:from ()0 to 0 :: ()() to 0 :: BoolFalsemap to F.universe :: [Bool] [False,True]map (to . (+1) . from) [LT, EQ, GT] :: [Ordering] -- Num Fin is modulo arithmetic [EQ,GT,LT]finThe size of an enumeration.finConverts a value to its index.fin*Converts from index to the original value.finGeneric version of .finGeneric version of .fin ~ +fin ~ 3fin ~ 2fin() ~ 1finVoid ~ 0    !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~eijklmnopqrstwxuv{       fin-0.2.1-3oDnWsPrUnxOWzFV6M5KlData.FinData.Nat Data.Type.NatData.Type.Nat.LEData.Type.Nat.LTData.Type.Nat.LE.ReflStep Data.Fin.Enum GHC.TypeLitsNatTrustworthyCompatData.Type.Equality==LEPRoofleReflbaseGHC.Real toIntegerZS explicitShowexplicitShowsPreccata toNatural fromNaturalnat0nat1nat2nat3nat4nat5nat6nat7nat8nat9 $fUniverseNat $fFunctionNat$fCoArbitraryNat$fArbitraryNat $fHashableNat $fNFDataNat $fEnumNat $fIntegralNat $fRealNat$fNumNat$fOrdNat $fShowNat$fEqNat $fDataNatNat9Nat8Nat7Nat6Nat5Nat4Nat3Nat2Nat1Nat0DivMod2Mult2MultPlusFromGHCToGHCEqNatSNatI inductionSNatSZSSsnatwithSNatreflect reflectToNumreify snatToNat snatToNaturaleqNat discreteNatcmpNat induction1 unfoldedFixproofPlusZeroNproofPlusNZeroproofMultZeroNproofMultNZero proofMultOneN proofMultNOne$fSNatIS$fSNatIZ$fGCompareNatSNat $fGEqNatSNat$fGNFDataNatSNat $fNFDataSNat$fGShowNatSNat $fBoringSNat$fTestEqualityNatSNat $fShowSNatLEleProofLEProofLEZeroLESucc withLEProofleZeroleSucclePredleStepleStepLleAsymleTransleSwapleSwap'decideLEproofZeroLEZero$fDecidableLEProof $fOrdLEProof $fEqLEProof$fAbsurdLEProof$fBoringLEProof$fLESm$fLEZm $fShowLEProofLTltProofLTProof withLTProof ltReflAbsurd ltSymAbsurdltTrans$fLTnmLEReflLEStep fromZeroSucc toZeroSucc$fCategoryNatLEProofFinFZFSmirrorinversetoNatfromNatuniverse universe1inlineUniverseinlineUniverse1absurdboringisMinisMax weakenRight1 weakenLeft1 weakenLeft weakenRightappendsplitfin0fin1fin2fin3fin4fin5fin6fin7fin8fin9 $fFiniteFin $fUniverseFin $fFunctionFin$fCoArbitraryFin $fAbsurdFin $fHashableFin $fNFDataFin $fEnumFin $fIntegralFin $fRealFin$fNumFin $fShowFin $fBoundedFin$fArbitraryFin$fOrdFin$fEqFinGToGFrom GEnumSizeEnumEnumSizefromtogfromgto $fGFromRepU1 $fGFromRepV1 $fGFromRepM1 $fGFromRep:+: $fGToRepU1 $fGToRepV1 $fGToRepM1 $fGToRep:+: $fEnumEither$fEnumOrdering $fEnumBool$fEnum() $fEnumVoidGHC.Showshow showsPrec GHC.NaturalNaturalghc-primGHC.Primcoerce:~:Refl TestEquality testEquality GHC.TypesFalseTrueGHC.NumNum Data.ProxyProxy!boring-0.2-DaIcLpIEXDhFnk9pCeVM7j Data.BoringBoringControl.CategoryCategoryGHC.EnumminBoundmaxBoundGHC.BaseNonEmptyquot Data.EitherEitherOrderingBool