constants and functions without arguments

Tom Pledger Tom.Pledger@peace.com
Sat, 31 Mar 2001 16:41:50 +1200


Andreas Leitner writes:
 :
 | The classic examlple for this is class POINT, which has an interface
 | like that:
 | --
 | POINT
 | 	-- group 1
 | 	x: DOUBLE
 | 	y: DOUBLE
 | 	-- group 2
 | 	rho: DOUBLE
 | 	abs: DOUBLE
 | --
 | Now, you can choose to either implement group 1 as attributes, or
 | group 2 and calculate the other one. With the uniform access
 | principle, which says that accessing attributes and functions with no
 | arguments happen in the same syntactic way, you can change the
 | implementaion without notice. No user of this class needs to know.
 | 
 | I am to new to FP to say whether this is equaly important in
 | FP-languages, since the semantics are different (constants vs.
 | attributes and immutable values vs. objects), but for now I am just
 | currious wether it is possible theoretically.

That uniformity of access can certainly be achieved in Haskell.
Here's the Point example.  I've done it with two constructors so as to
get fewer floating point imprecisions, but some other possible
representations (e.g. one with only the Polar constructor) would look
identical outside the module.

    module Point(Point, cartesian, polar, x, y, r, theta) where

    -- data type with hidden constructors
    data Point = Cartesian Double Double    -- corresponds to group 1
               | Polar     Double Double    -- corresponds to group 2

    -- proxy constructor functions
    cartesian x y = Cartesian x y
    polar r theta = Polar r theta

    -- attribute extraction functions
    x     (Cartesian x y) = x
    x     (Polar r theta) = r * cos theta
    y     (Cartesian x y) = y
    y     (Polar r theta) = r * sin theta
    r     (Cartesian x y) = sqrt (x*x + y*y)
    r     (Polar r theta) = r
    theta (Cartesian x y) = atan2 y x
    theta (Polar r theta) = theta