user1934428
user1934428

Reputation: 22301

Creating data types containing lists and defining functions on them

Informally speaking, I made a data type Tdict where each instance of it is a list of pairs, and each pair consists of a String and a List of Strings; in other languages, I would use a Hash, mapping Strings to Lists of Strings. Since I am learning Haskell, I would like to roll out this structure from scratch, instead of using the Hash package from Haskell.

This is my data type definition of Tdict, using two auxiliary type definitions:

-- Pair of two strings
data TPair = TPair String String deriving (Show)

-- Associating one String to a list of Strings
data Tassoc = Tassoc String [String] deriving (Show)

-- type synonym for list of associations
type Tdict = [Tassoc]

So far, this is accepted by Haskell, so at least it is syntactically correct. Now I want to define a function, which takes as parameter two strings and a Tdict and returns a Tdict. However, Haskell already rejects the signature definition of the function:

insertTdict :: (String k, String v, Tdict d) => k -> v -> d -> Tdict

The error message is about both parameters v and `d and reads:

error:
    • Expected kind ‘* -> Constraint’, but ‘Tdict’ has kind ‘*’
    • In the type signature:
        insertTdict :: (String k, String v, Tdict d) =>
                   k -> v -> d -> Tdict

(The error messages for parameters v and d are identical).

What did I do wrong? I have the feeling, that I am missing something fundamental in Haskell.

I used this page as a reference for defining my data types.

Upvotes: 1

Views: 105

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477533

Your types are not typeclasses. A type constraint like (String k, String v, Tdict d) => thus does not make sense.

You can implement the function with:

insertTdict :: String -> String -> Tdict -> Tdict

Upvotes: 4

Related Questions