morley-0.7.0: Developer tools for the Michelson Language

Safe HaskellNone
LanguageHaskell2010

Util.Peano

Contents

Description

Type-nat utilities.

We take Peano numbers as base for operations because they make it much easer to prove things to compiler. Their performance does not seem to introduce a problem, because we use nats primarily along with stack which is a linked list with similar performance characteristics.

Many of things we introduce here are covered in type-natural package, but unfortunatelly it does not work with GHC 8.6 at the moment of writing this module. We use Vinyl as source of Peano Nat for now.

Synopsis

General

type Peano = Nat Source #

A convenient alias.

We are going to use Peano numbers for type-dependent logic and normal Nats in user API, need to distinguish them somehow.

data Nat #

A mere approximation of the natural numbers. And their image as lifted by -XDataKinds corresponds to the actual natural numbers.

Constructors

Z 
S !Nat 
Instances
SingI Z Source # 
Instance details

Defined in Util.Peano

Methods

sing :: Sing Z #

RecSubset (Rec :: (k -> Type) -> [k] -> Type) ([] :: [k]) (ss :: [k]) ([] :: [Nat]) 
Instance details

Defined in Data.Vinyl.Lens

Associated Types

type RecSubsetFCtx Rec f :: Constraint #

Methods

rsubsetC :: (Functor g, RecSubsetFCtx Rec f) => (Rec f [] -> g (Rec f [])) -> Rec f ss -> g (Rec f ss) #

rcastC :: RecSubsetFCtx Rec f => Rec f ss -> Rec f [] #

rreplaceC :: RecSubsetFCtx Rec f => Rec f [] -> Rec f ss -> Rec f ss #

(RElem r ss i, RSubset rs ss is) => RecSubset (Rec :: (k -> Type) -> [k] -> Type) (r ': rs :: [k]) (ss :: [k]) (i ': is) 
Instance details

Defined in Data.Vinyl.Lens

Associated Types

type RecSubsetFCtx Rec f :: Constraint #

Methods

rsubsetC :: (Functor g, RecSubsetFCtx Rec f) => (Rec f (r ': rs) -> g (Rec f (r ': rs))) -> Rec f ss -> g (Rec f ss) #

rcastC :: RecSubsetFCtx Rec f => Rec f ss -> Rec f (r ': rs) #

rreplaceC :: RecSubsetFCtx Rec f => Rec f (r ': rs) -> Rec f ss -> Rec f ss #

(SingI n, KnownPeano n) => SingI (S n :: Nat) Source # 
Instance details

Defined in Util.Peano

Methods

sing :: Sing (S n) #

IndexWitnesses ([] :: [Nat]) 
Instance details

Defined in Data.Vinyl.TypeLevel

Methods

indexWitnesses :: [Int] #

Eq (Sing n) Source # 
Instance details

Defined in Util.Peano

Methods

(==) :: Sing n -> Sing n -> Bool #

(/=) :: Sing n -> Sing n -> Bool #

Show (Sing n) Source # 
Instance details

Defined in Util.Peano

Methods

showsPrec :: Int -> Sing n -> ShowS #

show :: Sing n -> String #

showList :: [Sing n] -> ShowS #

(IndexWitnesses is, NatToInt i) => IndexWitnesses (i ': is) 
Instance details

Defined in Data.Vinyl.TypeLevel

Methods

indexWitnesses :: [Int] #

data Sing (_ :: Nat) Source # 
Instance details

Defined in Util.Peano

data Sing (_ :: Nat) where

type family ToPeano (n :: Nat) :: Peano where ... Source #

Equations

ToPeano 0 = Z 
ToPeano a = S (ToPeano (a - 1)) 

type family FromPeano (n :: Peano) :: Nat where ... Source #

Equations

FromPeano Z = 0 
FromPeano (S a) = 1 + FromPeano a 

class KnownPeano (n :: Peano) where Source #

Methods

peanoVal :: proxy n -> Natural Source #

Instances
KnownPeano Z Source # 
Instance details

Defined in Util.Peano

Methods

peanoVal :: proxy Z -> Natural Source #

KnownPeano a => KnownPeano (S a) Source # 
Instance details

Defined in Util.Peano

Methods

peanoVal :: proxy (S a) -> Natural Source #

data family Sing (a :: k) :: Type #

The singleton kind-indexed data family.

Instances
Eq (Sing n) Source # 
Instance details

Defined in Util.Peano

Methods

(==) :: Sing n -> Sing n -> Bool #

(/=) :: Sing n -> Sing n -> Bool #

Show (Sing n) Source # 
Instance details

Defined in Util.Peano

Methods

showsPrec :: Int -> Sing n -> ShowS #

show :: Sing n -> String #

showList :: [Sing n] -> ShowS #

data Sing (a :: Bool) 
Instance details

Defined in Data.Singletons.Prelude.Instances

data Sing (a :: Bool) where
data Sing (a :: Ordering) 
Instance details

Defined in Data.Singletons.Prelude.Instances

data Sing (a :: Ordering) where
data Sing (n :: Nat) 
Instance details

Defined in Data.Singletons.TypeLits.Internal

data Sing (n :: Nat) where
data Sing (n :: Symbol) 
Instance details

Defined in Data.Singletons.TypeLits.Internal

data Sing (n :: Symbol) where
data Sing (a :: ()) 
Instance details

Defined in Data.Singletons.Prelude.Instances

data Sing (a :: ()) where
data Sing (a :: Void) 
Instance details

Defined in Data.Singletons.Prelude.Instances

data Sing (a :: Void)
data Sing (a :: All) 
Instance details

Defined in Data.Singletons.Prelude.Semigroup.Internal

data Sing (a :: All) where
data Sing (a :: Any) 
Instance details

Defined in Data.Singletons.Prelude.Semigroup.Internal

data Sing (a :: Any) where
data Sing (_ :: Nat) Source # 
Instance details

Defined in Util.Peano

data Sing (_ :: Nat) where
data Sing (a :: CT) Source #

Instance of data family Sing for CT.

Instance details

Defined in Michelson.Typed.Sing

data Sing (a :: CT) where
data Sing (a :: T) Source #

Instance of data family Sing for T. Custom instance is implemented in order to inject Typeable constraint for some of constructors.

Instance details

Defined in Michelson.Typed.Sing

data Sing (a :: T) where
data Sing (b :: [a]) 
Instance details

Defined in Data.Singletons.Prelude.Instances

data Sing (b :: [a]) where
  • SNil :: forall a (b :: [a]). Sing ([] :: [a])
  • SCons :: forall a (b :: [a]) (n1 :: a) (n2 :: [a]). Sing n1 -> Sing n2 -> Sing (n1 ': n2)
data Sing (b :: Maybe a) 
Instance details

Defined in Data.Singletons.Prelude.Instances

data Sing (b :: Maybe a) where
data Sing (b :: Min a) 
Instance details

Defined in Data.Singletons.Prelude.Semigroup.Internal

data Sing (b :: Min a) where
data Sing (b :: Max a) 
Instance details

Defined in Data.Singletons.Prelude.Semigroup.Internal

data Sing (b :: Max a) where
data Sing (b :: First a) 
Instance details

Defined in Data.Singletons.Prelude.Semigroup.Internal

data Sing (b :: First a) where
data Sing (b :: Last a) 
Instance details

Defined in Data.Singletons.Prelude.Semigroup.Internal

data Sing (b :: Last a) where
data Sing (a :: WrappedMonoid m) 
Instance details

Defined in Data.Singletons.Prelude.Semigroup.Internal

data Sing (a :: WrappedMonoid m) where
data Sing (b :: Option a) 
Instance details

Defined in Data.Singletons.Prelude.Semigroup.Internal

data Sing (b :: Option a) where
data Sing (b :: Identity a) 
Instance details

Defined in Data.Singletons.Prelude.Instances

data Sing (b :: Identity a) where
data Sing (b :: First a) 
Instance details

Defined in Data.Singletons.Prelude.Monoid

data Sing (b :: First a) where
data Sing (b :: Last a) 
Instance details

Defined in Data.Singletons.Prelude.Monoid

data Sing (b :: Last a) where
data Sing (b :: Dual a) 
Instance details

Defined in Data.Singletons.Prelude.Semigroup.Internal

data Sing (b :: Dual a) where
data Sing (b :: Sum a) 
Instance details

Defined in Data.Singletons.Prelude.Semigroup.Internal

data Sing (b :: Sum a) where
data Sing (b :: Product a) 
Instance details

Defined in Data.Singletons.Prelude.Semigroup.Internal

data Sing (b :: Product a) where
data Sing (b :: Down a) 
Instance details

Defined in Data.Singletons.Prelude.Ord

data Sing (b :: Down a) where
data Sing (b :: NonEmpty a) 
Instance details

Defined in Data.Singletons.Prelude.Instances

data Sing (b :: NonEmpty a) where
data Sing (b :: Endo a) 
Instance details

Defined in Data.Singletons.Prelude.Foldable

data Sing (b :: Endo a) where
data Sing (b :: MaxInternal a) 
Instance details

Defined in Data.Singletons.Prelude.Foldable

data Sing (b :: MaxInternal a) where
data Sing (b :: MinInternal a) 
Instance details

Defined in Data.Singletons.Prelude.Foldable

data Sing (b :: MinInternal a) where
data Sing (c :: Either a b) 
Instance details

Defined in Data.Singletons.Prelude.Instances

data Sing (c :: Either a b) where
data Sing (c :: (a, b)) 
Instance details

Defined in Data.Singletons.Prelude.Instances

data Sing (c :: (a, b)) where
data Sing (c :: Arg a b) 
Instance details

Defined in Data.Singletons.Prelude.Semigroup

data Sing (c :: Arg a b) where
newtype Sing (f :: k1 ~> k2) 
Instance details

Defined in Data.Singletons.Internal

newtype Sing (f :: k1 ~> k2) = SLambda {}
data Sing (b :: StateL s a) 
Instance details

Defined in Data.Singletons.Prelude.Traversable

data Sing (b :: StateL s a) where
  • SStateL :: forall s a (b :: StateL s a) (x :: s ~> (s, a)). Sing x -> Sing (StateL x)
data Sing (b :: StateR s a) 
Instance details

Defined in Data.Singletons.Prelude.Traversable

data Sing (b :: StateR s a) where
  • SStateR :: forall s a (b :: StateR s a) (x :: s ~> (s, a)). Sing x -> Sing (StateR x)
data Sing (d :: (a, b, c)) 
Instance details

Defined in Data.Singletons.Prelude.Instances

data Sing (d :: (a, b, c)) where
data Sing (c :: Const a b) 
Instance details

Defined in Data.Singletons.Prelude.Const

data Sing (c :: Const a b) where
data Sing (e :: (a, b, c, d)) 
Instance details

Defined in Data.Singletons.Prelude.Instances

data Sing (e :: (a, b, c, d)) where
data Sing (f :: (a, b, c, d, e)) 
Instance details

Defined in Data.Singletons.Prelude.Instances

data Sing (f :: (a, b, c, d, e)) where
  • STuple5 :: forall a b c d e (f :: (a, b, c, d, e)) (n1 :: a) (n2 :: b) (n3 :: c) (n4 :: d) (n5 :: e). Sing n1 -> Sing n2 -> Sing n3 -> Sing n4 -> Sing n5 -> Sing ((,,,,) n1 n2 n3 n4 n5)
data Sing (g :: (a, b, c, d, e, f)) 
Instance details

Defined in Data.Singletons.Prelude.Instances

data Sing (g :: (a, b, c, d, e, f)) where
  • STuple6 :: forall a b c d e f (g :: (a, b, c, d, e, f)) (n1 :: a) (n2 :: b) (n3 :: c) (n4 :: d) (n5 :: e) (n6 :: f). Sing n1 -> Sing n2 -> Sing n3 -> Sing n4 -> Sing n5 -> Sing n6 -> Sing ((,,,,,) n1 n2 n3 n4 n5 n6)
data Sing (h :: (a, b, c, d, e, f, g)) 
Instance details

Defined in Data.Singletons.Prelude.Instances

data Sing (h :: (a, b, c, d, e, f, g)) where
  • STuple7 :: forall a b c d e f g (h :: (a, b, c, d, e, f, g)) (n1 :: a) (n2 :: b) (n3 :: c) (n4 :: d) (n5 :: e) (n6 :: f) (n7 :: g). Sing n1 -> Sing n2 -> Sing n3 -> Sing n4 -> Sing n5 -> Sing n6 -> Sing n7 -> Sing ((,,,,,,) n1 n2 n3 n4 n5 n6 n7)

peanoVal' :: forall n. KnownPeano n => Natural Source #

peanoValSing :: forall n. KnownPeano n => Sing n -> Natural Source #

Get runtime value from singleton.

Lists

type family Length l :: Peano where ... Source #

Equations

Length l = RLength l 

type family At (n :: Peano) s where ... Source #

Equations

At Z (x ': _) = x 
At (S n) (_ ': xs) = At n xs 
At a '[] = TypeError (Text "You try to access to non-existing element of the stack, n = " :<>: ShowType (FromPeano a)) 

type family Drop (n :: Peano) (s :: [k]) :: [k] where ... Source #

Equations

Drop Z s = s 
Drop (S _) '[] = '[] 
Drop (S n) (_ ': s) = Drop n s 

type family Take (n :: Peano) (s :: [k]) :: [k] where ... Source #

Equations

Take Z _ = '[] 
Take _ '[] = '[] 
Take (S n) (a ': s) = a ': Take n s 

Morley-specific utils

type family IsLongerThan (l :: [k]) (a :: Peano) :: Bool where ... Source #

Comparison of type-level naturals, as a function.

It is as lazy on the list argument as possible - there is no need to know the whole list if the natural argument is small enough. This property is important if we want to be able to extract reusable parts of code which are aware only of relevant part of stack.

Equations

IsLongerThan (_ ': _) Z = True 
IsLongerThan (_ ': xs) (S a) = IsLongerThan xs a 
IsLongerThan '[] _ = False 

type LongerThan l a = IsLongerThan l a ~ True Source #

Comparison of type-level naturals, as a constraint.

class (RequireLongerThan' l a, LongerThan l a) => RequireLongerThan (l :: [k]) (a :: Peano) Source #

Instances
(RequireLongerThan' l a, LongerThan l a) => RequireLongerThan (l :: [k]) a Source # 
Instance details

Defined in Util.Peano

type family IsLongerOrSameLength (l :: [k]) (a :: Peano) :: Bool where ... Source #

Similar to IsLongerThan, but returns True when list length equals to the passed number.

type LongerOrSameLength l a = IsLongerOrSameLength l a ~ True Source #

IsLongerOrSameLength in form of constraint that gives most information to GHC.

class (RequireLongerOrSameLength' l a, LongerOrSameLength l a) => RequireLongerOrSameLength (l :: [k]) (a :: Peano) Source #

We can have `RequireLongerOrSameLength = (RequireLongerOrSameLength' l a, LongerOrSameLength l a)`, but apparently the printed error message can be caused by LongerOrSameLength rather than RequireLongerOrSameLength'. We do not know for sure how it all works, but we think that if we require constraint X before Y (using multiple `=>`s) then X will always be evaluated first.

Instances
(RequireLongerOrSameLength' l a, LongerOrSameLength l a) => RequireLongerOrSameLength (l :: [k]) a Source # 
Instance details

Defined in Util.Peano

Orphan instances

SingI Z Source # 
Instance details

Methods

sing :: Sing Z #

(SingI n, KnownPeano n) => SingI (S n :: Nat) Source # 
Instance details

Methods

sing :: Sing (S n) #

Eq (Sing n) Source # 
Instance details

Methods

(==) :: Sing n -> Sing n -> Bool #

(/=) :: Sing n -> Sing n -> Bool #

Show (Sing n) Source # 
Instance details

Methods

showsPrec :: Int -> Sing n -> ShowS #

show :: Sing n -> String #

showList :: [Sing n] -> ShowS #