[Haskell-cafe] class and instance

Patrick Browne patrick.browne at dit.ie
Sun Jul 10 12:49:44 CEST 2011


Hi,
I am trying to understand the following code.
I have written my current (mis-)understanding and questions below.
I do not wish to improve the code, it is from a research paper[1] that I
am trying to understand.

Pat
[1] ftp://ftp.geoinfo.tuwien.ac.at/medak/phdmedak.pdf

-- A specification. The class Points takes two type variables.
-- The functions take variables of type p, a; return a value of type a.
 class Points p a where
  getX :: p a -> a
  getY :: p a -> a

-- A parameterized representation
-- One constructor takes two elements of type a and produces a Point.
 data Point a = Pt a a

-- An implementation of the class Points on the data type (Point a)
-- In  Pt b c constructor the variables b and c areboth of type a.
-- The type a is left undefined until evaluation
 instance Points Point a where
  getX (Pt b c) = b
  getY (Pt b c) = c

-- This runs with any type e.g. Integers
-- getX(Pt 1 2)
-- :t getX(Pt 1 2)
-- getX(Pt 1 2) :: forall t. (Num t) => t


My main question is in understanding the relationship between the
arguments of the functions getX and getY in the class and in the
instance. It seems to me that the constructor Pt 1 2 produces one
element of type Point which has two components. How does this square
with the class definition of getX which has two arguments?
Is there a difference between:
getX :: p a -> a
 and
getX :: p -> a -> a

This message has been scanned for content and viruses by the DIT Information Services E-Mail Scanning Service, and is believed to be clean. http://www.dit.ie



More information about the Haskell-Cafe mailing list