[Haskell-beginners] Re: dependent types

Maurí­cio briqueabraque at yahoo.com
Mon Jul 20 16:46:14 EDT 2009


> The intended semantics are:
>     Two objects are equal if they have the same name.
>     The type of the name depends on the type of the object

> 3) How would I write an instance of Named that implements the name
> function correctly.

I think (but someone with better knowledge) the term "dependent
type" means something else:

http://en.wikipedia.org/wiki/Dependent_types

I'm sure you'll be interested in type families, which offer a
better and richer syntax to what you want:

http://www.haskell.org/ghc/docs/latest/html/users_guide/type-families.html

I tried rewriting your example using ghc-6.8.2 but I wasn't
able to build it, and since ghc-6.8.* isn't actually supposed
to properly support type families I though I could give you a
bad example even if I get it to build :) So I'm leaving it here
just in case someone wants to use it as a base:

------
module Main (main) where

data (Eq a) => Object a = Object a

class (Eq (NameType o)) => Named o where
   type NameType o :: *
   name :: o -> NameType o
   equals :: o -> o -> Bool
   equals o1 o2 = (name o1) == (name o2)

instance (Named a) => Eq a where
   (==) = equals

instance Named (Object Integer) where
   type NameType (Object Integer) = Integer
   name (Object i) = i

f,g :: Object Integer
f = Object 1
g = Object 2

main = putStrLn $ show $ f == g
------

Best,
Maurício



More information about the Beginners mailing list