Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Don't understand this collection class syntax

class Collection c where
  empty :: c key value
  singleton :: key -> value -> c key value
  insert
    :: Ord key
    => key -> value -> c key value -> c key value
  lookup :: Ord key => key -> c key value -> Maybe value
  delete :: Ord key => key -> c key value -> c key value
  keys :: c key value -> [key]
  values :: c key value -> [value]
  toList :: c key value -> [(key, value)]
  fromList :: Ord key => [(key,value)] -> c key value

This is unlike what I’ve read about typeclasses, or the syntax I’m used to. I don’t understand what c key value represents. Also, what is empty supposed to be? It doesn’t resemble a function. Appreciate and am thankful for any help, I’m new to haskell and this is confusing for me

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The type parameter c will here not unify with a "concrete" type, but with a type constructor that still expects two parameters.

A simple collection could be:

data ListMap a b = ListMap [(a, b)]

then we define an instance of Collection with:

instance Collection ListMap where
    empty = ListMap []

    -- …

Indeed, empty here has as type c key value, so ListMap key value, which is then a concrete type.

In this case, a function like fromList :: Ord key => [(key,value)] -> c key value will thus result in fromList :: Ord key => [(key,value)] -> ListMap key value.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading