Lists of (Type,Value) pairs can be expressed on Idris as:
data List : Type where
Cons : (t : Type ** t) -> List -> List
Nil : List
example : List
example = Cons (Nat ** 3) (Cons (Bool ** True) Nil)
What is the syntax to express those on Haskell?
If you are using an interactive Haskell prompt (like GHCi) you can type :t <expression> and that will give you the type of an expression. e.g. or e.g.
Haskell also incorporates polymorphic types---types that are universally quantified in some way over all types. Polymorphic type expressions essentially describe families of types. For example, (forall a)[a] is the family of types consisting of, for every type a, the type of lists of a.
=> separates two parts of a type signature: On the left, typeclass constraints.
() is very often used as the result of something that has no interesting result. For example, an IO action that is supposed to perform some I/O and terminate without producing a result will typically have type IO () .
Note that if you construct such List
you cannot do anything with the
elements, as you cannot pattern match on the types.
However it's entirely possible in Haskell usign GADTs
data List where
Cons :: t -> List -> List
Nil :: List
example :: List
example = Cons (3 :: Int) (Cons True Nil)
You can extend that with a constraint, e.g. Typeable
so you get
run-time type information to do things on elements in the list:
data CList (c :: * -> Constraint) where
CCons :: Typeable t => t -> List c -> List c
CNil :: CList c
exampleC :: CList Typeable
exampleC = CCons (3 :: Int) (CCons True CNil)
Or you can use HList
data HList (xs :: [*]) where
HCons :: x -> List xs -> List (x ': xs)
HNil :: '[]
exampleH :: HList '[Int, Bool]
exampleH = HCons 3 (HConst True HNil)
In particular dependent pairs (or sums!) (Idris docs) are possible with in Haskell to, Yet we have to make a GADT for a function! The http://hackage.haskell.org/package/dependent-sum is one many use
If Idris version is
data DPair : (a : Type) -> (P : a -> Type) -> Type where
MkDPair : {P : a -> Type} -> (x : a) -> P x -> DPair a P
the Haskell is not that much different when a = Type
:
data DPair (p :: * -> *) where
MkDPair :: p a -> DPair p
and p
is encoded with a GADT
. In examples above, it's sliced into the
definitions.
You can also make dependent pairs with something else than a type as a first element. But then you have to read about singletons.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With