[Haskell-cafe] newbie question how to pass data

Stephen Tetley stephen.tetley at gmail.com
Tue Apr 20 04:21:43 EDT 2010


If you are working with characteristic functions (Point -> Bool or
Point -> Colour...) the common way to do this is to manufacture a Num
instance for functions. This gives you syntax overloading of the (+,
-, *) operators. Similarly you might want to overload (or have to
overload) Floating, Fractional...

Examples using this technique are Jerzy Karczmarczuk's Clastic, Conal
Elliott's Vertigo, Tangible Values, Pan etc.

To overload Num you have to define Show and Eq instances for functions
as well. Something along the lines of this is adequate:

type CF = (Double,Double) -> Bool

instance Show CF where
  show _ = "<function>"

instance Eq CF where
 (==) _ _ = error "No Eq on  Characteristic functions"

instance Num CF where
  f + g = \pt -> f pt + g pt
  -- ...
  negate f = \(x,y) -> f (negate x, negate y)

  -- ... rest follows this pattern, Floating, Fractional similar

If you characteristic function is Point -> Bool then you also need a
Num instance for Bool.

All that said, I think your formulation of func above is slightly
wrong to fit this style. Its forming a function (-> Point) "to point"
rather than a characteristic function Point -> Bool.

Best wishes


More information about the Haskell-Cafe mailing list