[Haskell-cafe] Trouble with type signatures and type families

Stephan Friedrichs deduktionstheorem at web.de
Wed Apr 22 04:43:57 EDT 2009


Hi,

consider the following code:

----------------------------
class Foo f where
    type Bar f :: *
    type Baz f :: *

    from :: Bar f -> Baz f
    to   :: Baz f -> Bar f

data Tree a b c
    = Empty
    | Tree b c (Tree a b c) (Tree a b c)

--singleton :: (Foo f) => Bar f -> Tree a (Bar f) (Baz f)
singleton x = Tree x (from x) Empty Empty
----------------------------

what type does 'singleton' have? ghci-6.10.2 says:

> :t singleton
singleton :: forall f a. (Foo f) => Bar f -> Tree a (Bar f) (Baz f)

which is no surprise. But when I uncomment the type signature in the
above code sample, loading it to ghci gives the following error:

> :r
[1 of 1] Compiling Main             ( T.hs, interpreted )

T.hs:14:22:
    Couldn't match expected type `Baz f1' against inferred type `Baz f'
    In the second argument of `Tree', namely `(from x)'
    In the expression: Tree x (from x) Empty Empty
    In the definition of `singleton':
        singleton x = Tree x (from x) Empty Empty

T.hs:14:27:
    Couldn't match expected type `Bar f' against inferred type `Bar f1'
    In the first argument of `from', namely `x'
    In the second argument of `Tree', namely `(from x)'
    In the expression: Tree x (from x) Empty Empty
Failed, modules loaded: none.

I don't get it... what's wrong, I just copied the inferred type?

//Stephan


-- 

Früher hieß es ja: Ich denke, also bin ich.
Heute weiß man: Es geht auch so.

 - Dieter Nuhr


More information about the Haskell-Cafe mailing list