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 -> asqr, sqrt :: a -> asqr a = a * atype Moving v = Time -> vinstance 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 wherex, y :: p s -> sxy :: s -> s -> p sdist :: p s -> p s -> sdist a b = sqrt (sqr ((x a) - (x b)) +sqr ((y a) - (y b)))data Point f = Point f finstance Number v => Points Point v wherex (Point x1 y1) = x1y (Point x1 y1) = y1xy x1 y1 = Point x1 y1instance 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 np2dist_at_1 = movingDist_1_2 1.0------------------------------------------------------------ ------------------------------ ---- My attempt at getting above code to run:------------------------------------------------------------ ------------------------------ ----- {-# LANGUAGE MultiParamTypeClasses #-}{-# LANGUAGE FlexibleInstances #-}{-# LANGUAGE TypeSynonymInstances #-}module Moving wheredata Time = Time Doubletype Moving v = Time -> vdata Point v = Point v v deriving Showclass Number a where(+),(-),(*) :: a -> a -> asqrt :: a -> ainstance (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 Doublea (Time x) = 4.0b (Time x) = 4.0testPlus ::(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 wherex, y :: p s -> sxy :: s -> s -> p sdist :: p s -> p s -> sdist 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 whereinstance (Number s) => Points Point s wherex (Point x1 y1) = x1y (Point x1 y1) = y1xy x1 y1 = Point x1 y1instance 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 Doublemd1 (Time x) = 0.0md2 (Time x) = 0.0md3 (Time x) = 10.0md4 (Time x) = 10.0testMD1 = (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 DatabasesOn 7 October 2017 at 23:30, Thomas Jakway <tjakway@nyu.edu> wrote: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 wheredata Time = Time Doubletype 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 Doublea (Time x) = 2.0b (Time x) = 2.0testPlus ::(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
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bi n/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
_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners