Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell-style type families

In Haskell, I might write a typeclass with a type declaration to create a type family, like so:

class ListLike k where
    type Elem ::  * -> *
    fromList :: [Elem k] -> k

And then write instances like this:

instance ListLike [a] where
    type Elem [a] = a
    fromList = id

instance ListLike Bytestring where
    type Elem Bytestring = Char
    fromList = pack

I'm aware that you can create typeclasses and type-level functions in Idris, but the methods operate on data of the given type, not the type itself.

How can I make a typeclass-constrained type family in Idris like the one above?

like image 389
AJF Avatar asked Jun 03 '15 12:06

AJF


1 Answers

I have no clue if you will ever find a use for this, but I think the obvious translation should be

class ListLike k where
    llElem : Type
    fromList : List llElem -> k

instance ListLike (List a) where
    llElem = a
    fromList = id

instance ListLike (Maybe a) where
  llElem = a
  fromList [] = Nothing
  fromList (a::_) = Just a

usage

λΠ> the (Maybe Int) (fromList [3])
Just 3 : Maybe Int
λΠ> the (List Int) (fromList [3])
[3] : List Int
like image 128
Random Dev Avatar answered Sep 20 '22 08:09

Random Dev