Send Beginners mailing list submissions to beginners@haskell.org To subscribe or unsubscribe via the World Wide Web, visit http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners or, via email, send a message with subject or body 'help' to beginners-requ...@haskell.org
You can reach the person managing the list at beginners-ow...@haskell.org When replying, please edit your Subject line so it is more specific than "Re: Contents of Beginners digest..." Today's Topics: 1. Re: Return type from class method (David McBride) 2. Re: Return type from class method (PATRICK BROWNE) ---------------------------------------------------------------------- Message: 1 Date: Mon, 9 Oct 2017 08:53:39 -0400 From: David McBride <toa...@gmail.com> To: Patrick Browne <patrick.bro...@dit.ie>, The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] Return type from class method Message-ID: <CAN+Tr43WVqGO5_bi=OsWWGN1WvzF0=FaGwSnTTww_fZ0cWO3=w...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" 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.bro...@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 <tjak...@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@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> > > _______________________________________________ > Beginners mailing list > Beginners@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-0001.html> ------------------------------ Message: 2 Date: Mon, 9 Oct 2017 16:53:42 +0100 From: PATRICK BROWNE <patrick.bro...@dit.ie> To: David McBride <toa...@gmail.com> Cc: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] Return type from class method Message-ID: <cagflrkd-sbzfte3bmqyzunvz-6xjcb9wsveceqdkvnd3e0m...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" David, Thanks very much for clarifying my confusion. I would never have thought of the rather subtle import nor the instance Number P.Float. Your help is greatly appreciated, Pat On 9 October 2017 at 13:53, David McBride <toa...@gmail.com> wrote: > 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.bro...@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 <tjak...@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@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> >> >> _______________________________________________ >> 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> -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.haskell.org/pipermail/beginners/attachments/20171009/45a36551/attachment.html> ------------------------------ Subject: Digest Footer _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners ------------------------------ End of Beginners Digest, Vol 112, Issue 12 ******************************************