[Haskell-beginners] Return type from class method

David McBride toad3k at gmail.com
Mon Oct 9 12:53:39 UTC 2017


Here's a direct translation of the original code.  What is missing is an
instance for Number for Prelude.Float.  That is because it is using 4.0 +
0.5 * (t :: Time), where Time must be a P.Float, or else if it is a data
type it must having instances for Fractional, Num, and Floating, so that it
can be represented as a floating point literal (ie. 4.0).

It might be easier in the long run if you got rid of the Number class and
just used Prelude's Num class instead.  You would have to do some slightly
different handling to deal with sqrt.  Admittedly that might be difficult.

{-# LANGUAGE NoImplicitPrelude, MultiParamTypeClasses, FlexibleInstances #-}

import qualified Prelude as P ((+), (-), (*), sqrt, Float)

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

type Time = P.Float

type Moving v = Time -> v

instance Number P.Float where
  (+) = (P.+)
  (-) = (P.-)
  (*) = (P.*)
  sqrt = P.sqrt


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 => Number (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 :: Point (Moving P.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


On Sun, Oct 8, 2017 at 1:24 AM, PATRICK BROWNE <patrick.browne at dit.ie>
wrote:

> 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 <tjakway at 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 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 at haskell.orghttp://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>
>>
>>
>> _______________________________________________
>> Beginners mailing list
>> Beginners at 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>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20171009/6e2279f1/attachment.html>


More information about the Beginners mailing list