
Thomas,
Thanks for your response. I agree that using (+) in this manner leads to
name clashes.
My question is prompted by a research paper [1] where a form of lifting is
attempted using type classes.
I think that the original code in paper (below) is intended to be
illustrative rather than practical.
I have edited the code to compile and run (after a fashion, see below). But
I cannot get the functions to return the lifted type.
Also I have a problem compiling the lines with two lambdas, e.g. (np1 = xy
(\t -> 4.0 + 0.5 * t) (\t -> 4.0 - 0.5 * t))
Regards,
Pat
---------------------------------------------------------------------------------------------
Original code [1]:
------------------------------------------------------------------------------------------
class Number a where
(+), (-), (*) :: a -> a -> a
sqr, sqrt :: a -> a
sqr a = a * a
type Moving v = Time -> v
instance Number v => Number (Moving v) where
(+) a b = \t -> (a t) + (b t)
(-) a b = \t -> (a t) - (b t)
(*) a b = \t -> (a t) * (b t)
sqrt a = \t -> sqrt (a t)
class Number s => Points p s where
x, y :: p s -> s
xy :: s -> s -> p s
dist :: p s -> p s -> s
dist a b = sqrt (sqr ((x a) - (x b)) +
sqr ((y a) - (y b)))
data Point f = Point f f
instance Number v => Points Point v where
x (Point x1 y1) = x1
y (Point x1 y1) = y1
xy x1 y1 = Point x1 y1
instance Number v => (Point v) where
(+) a b = xy (x a + x b) (y a + y b)
(-) a b = xy (x a - x b) (y a - y b)
np1, np2 :: Point (Moving Float)
np1 = xy (\t -> 4.0 + 0.5 * t) (\t -> 4.0 - 0.5 * t)
np2 = xy (\t -> 0.0 + 1.0 * t) (\t -> 0.0 - 1.0 * t)
movingDist_1_2 = dist np1 np2
dist_at_1 = movingDist_1_2 1.0
----------------------------------------------------------------------------------------------
My attempt at getting above code to run:
-----------------------------------------------------------------------------------------------
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeSynonymInstances #-}
module Moving where
data Time = Time Double
type Moving v = Time -> v
data Point v = Point v v deriving Show
class Number a where
(+),(-),(*) :: a -> a -> a
sqrt :: a -> a
instance (Fractional a,Floating a) => Number (Moving a) where
(+) a b = \t -> ((a t) Prelude.+ (b t))
(-) a b = \t -> ((a t) Prelude.- (b t))
(*) a b = \t -> ((a t) Prelude.* (b t))
sqrt a = \t -> Prelude.sqrt (a t)
a,b :: Moving Double
a (Time x) = 4.0
b (Time x) = 4.0
testPlus ::(Moving Double)
testPlus = (a Moving.+ b)
testPlusArg = (a Moving.+ b) (Time 2.0)
testSqrt = (Moving.sqrt a) (Time 2.0)
class (Number s) => Points p s where
x, y :: p s -> s
xy :: s -> s -> p s
dist :: p s -> p s -> s
dist a b = Moving.sqrt (sqr ((x a) Moving.- (x b)) Moving.+ sqr ((y a)
Moving.- (y b)))
where sqr z = z Moving.* z
-- instance (Floating v,Number v) => Points Point v where
instance (Number s) => Points Point s where
x (Point x1 y1) = x1
y (Point x1 y1) = y1
xy x1 y1 = Point x1 y1
instance Number v => Number (Point v) where
(+) a b = xy (x a Moving.+ x b) (y a Moving.+ y b)
(-) a b = xy (x a Moving.- x b) (y a Moving.- y b)
md1,md2,md3,md4 :: Moving Double
md1 (Time x) = 0.0
md2 (Time x) = 0.0
md3 (Time x) = 10.0
md4 (Time x) = 10.0
testMD1 = (md1 (Time 2.0))
testX = x (Point md1 md2) (Time 2.0)
testY = y (Point md1 md2) (Time 2.0)
testXY = (xy md1 md2)::(Point (Moving Double))
testX' = x testXY (Time 1.0)
testD = dist (Point md1 md2) (Point md3 md4) (Time 1.0)
--- I cannot get the rest to work
[1] Ontology for Spatio-temporal Databases
http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.113.9804&rep=rep1&type=pdf
On 7 October 2017 at 23:30, Thomas Jakway
You could hide Prelude and define it yourself in a different module but that would be a pretty bad idea. Everyone who wanted to use it would have to import your module qualified and refer to it as MyModule.+, which defeats the point of making it (+) and not `myAdditionFunction` in the first place.
Haskell deliberately doesn't allow overloading. Having (+) return something other than Num would be extremely confusing.
On 10/07/2017 05:07 AM, PATRICK BROWNE wrote:
Hi, Is there a way rewriting the definition of (+) so that testPlusArg returns a (Moving Double). My current intuition is that the signature [(+) :: a -> a -> a] says that the type should be the same as the arguments. And indeed (:t testPlus) confirms this. But the type of testPlusArg is a Double. Can I make it (Moving Double) ? Thanks, Pat
{-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE TypeSynonymInstances #-} module Moving where data Time = Time Double type Moving v = Time -> v
class Number a where (+) :: a -> a -> a
instance Number (Moving Double) where (+) a b = \t -> ((a t) Prelude.+ (b t))
a,b :: Moving Double a (Time x) = 2.0 b (Time x) = 2.0 testPlus ::(Moving Double) testPlus = (a Moving.+ b) testPlusArg = (a Moving.+ b) (Time 2.0)
This email originated from DIT. If you received this email in error, please delete it from your system. Please note that if you are not the named addressee, disclosing, copying, distributing or taking any action based on the contents of this email or attachments is prohibited. www.dit.ie
Is ó ITBÁC a tháinig an ríomhphost seo. Má fuair tú an ríomhphost seo trí earráid, scrios de do chóras é le do thoil. Tabhair ar aird, mura tú an seolaí ainmnithe, go bhfuil dianchosc ar aon nochtadh, aon chóipeáil, aon dáileadh nó ar aon ghníomh a dhéanfar bunaithe ar an ábhar atá sa ríomhphost nó sna hiatáin seo. www.dit.ie
Tá ITBÁC ag aistriú go Gráinseach Ghormáin – DIT is on the move to Grangegorman http://www.dit.ie/grangegorman
_______________________________________________ Beginners mailing listBeginners@haskell.orghttp://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
-- This email originated from DIT. If you received this email in error, please delete it from your system. Please note that if you are not the named addressee, disclosing, copying, distributing or taking any action based on the contents of this email or attachments is prohibited. www.dit.ie Is ó ITBÁC a tháinig an ríomhphost seo. Má fuair tú an ríomhphost seo trí earráid, scrios de do chóras é le do thoil. Tabhair ar aird, mura tú an seolaí ainmnithe, go bhfuil dianchosc ar aon nochtadh, aon chóipeáil, aon dáileadh nó ar aon ghníomh a dhéanfar bunaithe ar an ábhar atá sa ríomhphost nó sna hiatáin seo. www.dit.ie Tá ITBÁC ag aistriú go Gráinseach Ghormáin – DIT is on the move to Grangegorman http://www.dit.ie/grangegorman