# [Haskell-cafe] running and understanding a lifting program

Patrick Browne patrick.browne at dit.ie
Sun Oct 24 09:12:18 EDT 2010

```hi,
I am trying to run and understand a lifting program from [1].
The program lifts points to moving points that vary their position over
time.
I made some effort to run the progrm but I do not know how to overide
the +,-,*,sqr, and sqrt from the Num class. Below is my current attempt.

I do not wish to change or imporve the code, rather I wish to understand
it as it stands and find out what needs to be added to get the outputs
shown below i.e. distance between points p1 and p2 --> 1.55  and the
distance between moving points mp1 and mp2  for time 2 ----> 5.83.

data Point = Point Float Float
data Line = Line Point Point
data Polygon = Polygon [Point]
type Time = Float

-- Functor Changing, which adds time parameter t to its input value.
-- For example, Changing Float indicates a changing floating number
(i.e. a function of time).
type Changing v = Time -> v

-- The abstract lifting functions
class Lifts a where
lift0 :: a -> f a
lift1 :: (a -> b) -> f a -> f b
lift2 :: (a -> b -> c) -> f a -> f b -> f c

instance Lifts Changing Float where
lift0 a = \t -> a
lift1 op a = \t -> op (a t)
lift2 op a b = \t -> op (a t) op (b t)

class Number a where
(+), (-), (*) :: a -> a -> a
sqr, sqrt :: a -> a
sqr a = a * a

-- The class point which support vector plus and minus as well as
-- distance operation is defined as follow
class Number s => Points p s where
x, y :: p s -> s
x (Point x1 y1) = x1
y (Point x1 y1) = y1
(+), (-) :: p s -> p s -> p s
(+) a b = Point (x a + x b) (y a + y b)
(-) a b = Point (x a - x b) (y a - y b)
dist :: p s -> p s -> s
dist a b = sqrt(sqr((x a)-(x b))+sqr((y a)-(y b)))

-- The lifting the operations for numbers shoukd provide a distance
-- function which can be used for both static and moving points:
instance Number v => Number (Changing v) where
(+) = lift2 (+)
(-) = lift2 (-)
(*) = lift2 (*)
sqrt = lift1 (sqrt)

-- Running the code
-- If p1 and p2 are two 2D static points,
-- their distance d is calculated as follows:
p1, p2 :: Point Float
p1 = Point 3.4 5.5
p2 = Point 4.5 4.5

-- distance between p1 and p2 --> 1.55
d = dist p1 p2

-- For 2D moving points mp1 and mp2, their distance md,
-- which is a function of time, is calculated as follows:
mp1, mp2 :: Point (Changing Float)
mp1 = Point (\t -> 4.0 + 0.5 * t)(\t -> 4.0 - 0.5 * t)
mp2 = Point (\t -> 0.0 + 1.0 * t)(\t -> 0.0 - 1.0 * t)
--  distance between mp1 and mp2
md = dist mp1 mp2
-- distance md for time 2 ----> 5.83
md 2

[1] A Mathematical Tool to Extend 2D Spatial Operations
to Higher Dimensions:  by Farid Karimipour1,2, Mahmoud R. Delavar1, and
Andrew U. Frank2