<div dir="ltr"><div>Thomas,</div><div>Thanks for your response. I agree that using (+) in this manner leads to name clashes. </div><div>My question is prompted by a research paper [1] where a form of lifting is attempted using type classes. </div><div>I think that the original code in paper (below) is intended to be illustrative rather than practical. </div><div>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.</div><div>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))</div><div>Regards,</div><div>Pat</div><div>---------------------------------------------------------------------------------------------</div><div>Original code [1]:</div><div>------------------------------------------------------------------------------------------</div><div>class Number a where</div><div>(+), (-), (*) :: a -> a -> a</div><div>sqr, sqrt :: a -> a</div><div>sqr a = a * a</div><div><br></div><div>type Moving v = Time -> v</div><div><br></div><div>instance Number v => Number (Moving v) where</div><div> (+) a b = \t -> (a t) + (b t)</div><div> (-) a b = \t -> (a t) - (b t)</div><div> (*) a b = \t -> (a t) * (b t)</div><div> sqrt a = \t -> sqrt (a t)</div><div><br></div><div>class Number s => Points p s where</div><div> x, y :: p s -> s</div><div> xy :: s -> s -> p s</div><div> dist :: p s -> p s -> s</div><div> dist a b = sqrt (sqr ((x a) - (x b)) +</div><div> sqr ((y a) - (y b)))</div><div><br></div><div>data Point f = Point f f</div><div><br></div><div>instance Number v => Points Point v where</div><div> x (Point x1 y1) = x1</div><div> y (Point x1 y1) = y1</div><div> xy x1 y1 = Point x1 y1</div><div><br></div><div>instance Number v => (Point v) where</div><div> (+) a b = xy (x a + x b) (y a + y b)</div><div> (-) a b = xy (x a - x b) (y a - y b)</div><div><br></div><div><br></div><div>np1, np2 :: Point (Moving Float)</div><div>np1 = xy (\t -> 4.0 + 0.5 * t) (\t -> 4.0 - 0.5 * t)</div><div>np2 = xy (\t -> 0.0 + 1.0 * t) (\t -> 0.0 - 1.0 * t)</div><div>movingDist_1_2 = dist np1 np2</div><div>dist_at_1 = movingDist_1_2 1.0</div><div><br></div><div><br></div><div>----------------------------------------------------------------------------------------------</div><div>My attempt at getting above code to run:</div><div><div>-----------------------------------------------------------------------------------------------</div><div>{-# LANGUAGE MultiParamTypeClasses #-}</div><div>{-# LANGUAGE FlexibleInstances #-}</div><div>{-# LANGUAGE TypeSynonymInstances #-}</div><div>module Moving where</div><div>data Time = Time Double</div><div>type Moving v = Time -> v</div><div>data Point v = Point v v deriving Show</div><div><br></div><div>class Number a where</div><div> (+),(-),(*) :: a -> a -> a</div><div> sqrt :: a -> a</div><div><br></div><div><br></div><div>instance (Fractional a,Floating a) => Number (Moving a) where</div><div> (+) a b = \t -> ((a t) Prelude.+ (b t))</div><div> (-) a b = \t -> ((a t) Prelude.- (b t))</div><div> (*) a b = \t -> ((a t) Prelude.* (b t))</div><div> sqrt a = \t -> Prelude.sqrt (a t)</div><div><br></div><div>a,b :: Moving Double</div><div>a (Time x) = 4.0</div><div>b (Time x) = 4.0</div><div>testPlus ::(Moving Double)</div><div>testPlus = (a Moving.+ b)</div><div>testPlusArg = (a Moving.+ b) (Time 2.0)</div><div>testSqrt = (Moving.sqrt a) (Time 2.0) </div><div><br></div><div><br></div><div><br></div><div>class (Number s) => Points p s where</div><div> x, y :: p s -> s</div><div> xy :: s -> s -> p s</div><div> dist :: p s -> p s -> s</div><div> dist a b = Moving.sqrt (sqr ((x a) Moving.- (x b)) Moving.+ sqr ((y a) Moving.- (y b)))</div><div> where sqr z = z Moving.* z</div><div><br></div><div><br></div><div><br></div><div>-- instance (Floating v,Number v) => Points Point v where</div><div>instance (Number s) => Points Point s where</div><div> x (Point x1 y1) = x1</div><div> y (Point x1 y1) = y1</div><div> xy x1 y1 = Point x1 y1</div><div><br></div><div>instance Number v => Number (Point v) where</div><div> (+) a b = xy (x a Moving.+ x b) (y a Moving.+ y b)</div><div> (-) a b = xy (x a Moving.- x b) (y a Moving.- y b)</div><div><br></div><div><br></div><div>md1,md2,md3,md4 :: Moving Double</div><div>md1 (Time x) = 0.0</div><div>md2 (Time x) = 0.0</div><div>md3 (Time x) = 10.0</div><div>md4 (Time x) = 10.0</div><div>testMD1 = (md1 (Time 2.0))</div><div>testX = x (Point md1 md2) (Time 2.0)</div><div>testY = y (Point md1 md2) (Time 2.0)</div><div>testXY = (xy md1 md2)::(Point (Moving Double))</div><div>testX' = x testXY (Time 1.0)</div><div>testD = dist (Point md1 md2) (Point md3 md4) (Time 1.0)</div></div><div><br></div><div>--- I cannot get the rest to work</div><div><br></div><div>[1] Ontology for Spatio-temporal Databases</div><div><a href="http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.113.9804&rep=rep1&type=pdf">http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.113.9804&rep=rep1&type=pdf</a></div></div><div class="gmail_extra"><br><div class="gmail_quote">On 7 October 2017 at 23:30, Thomas Jakway <span dir="ltr"><<a href="mailto:tjakway@nyu.edu" target="_blank">tjakway@nyu.edu</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div bgcolor="#FFFFFF" text="#000000">
<p>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.</p>
<p>Haskell deliberately doesn't allow overloading. Having (+)
return something other than Num would be extremely confusing.<br>
</p><div><div class="h5">
<br>
<div class="m_-4553323283209527145moz-cite-prefix">On 10/07/2017 05:07 AM, PATRICK BROWNE
wrote:<br>
</div>
</div></div><blockquote type="cite"><div><div class="h5">
<div dir="ltr">
<div>Hi,</div>
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.
<div> Can I make it (Moving Double) ?
<div>Thanks,</div>
<div>Pat<br>
<div>
<div><br>
<div>
<div>{-# LANGUAGE FlexibleInstances #-}</div>
<div>{-# LANGUAGE TypeSynonymInstances #-}</div>
<div>module Moving where</div>
<div>data Time = Time Double</div>
<div>type Moving v = Time -> v</div>
<div><br>
</div>
<div>class Number a where</div>
<div> (+) :: a -> a -> a</div>
<div><br>
</div>
<div>instance Number (Moving Double) where</div>
<div> (+) a b = \t -> ((a t) Prelude.+ (b t))</div>
<div><br>
</div>
<div>a,b :: Moving Double</div>
<div>a (Time x) = 2.0</div>
<div>b (Time x) = 2.0</div>
<div>testPlus ::(Moving Double)</div>
<div>testPlus = (a Moving.+ b)</div>
<div>testPlusArg = (a Moving.+ b) (Time 2.0)</div>
</div>
</div>
</div>
</div>
</div>
</div>
<br>
</div></div><p><span lang="EN-GB"><font size="2">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. <a href="http://www.dit.ie/" target="_blank">www.dit.ie</a></font></span></p>
<p><font size="2">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. <a href="http://www.dit.ie/" target="_blank">www.dit.ie</a></font></p>
<p><a href="http://www.dit.ie/grangegorman" target="_blank"><font size="2">Tá ITBÁC ag aistriú go
Gráinseach Ghormáin – DIT is on the move to Grangegorman</font></a></p>
<br>
<fieldset class="m_-4553323283209527145mimeAttachmentHeader"></fieldset>
<br>
<pre>______________________________<wbr>_________________
Beginners mailing list
<a class="m_-4553323283209527145moz-txt-link-abbreviated" href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a>
<a class="m_-4553323283209527145moz-txt-link-freetext" href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" target="_blank">http://mail.haskell.org/cgi-<wbr>bin/mailman/listinfo/beginners</a>
</pre>
</blockquote>
<br>
</div>
<br>______________________________<wbr>_________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-<wbr>bin/mailman/listinfo/beginners</a><br>
<br></blockquote></div><br></div>
<br>
<p><span lang="EN-GB"><font size="2">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. <a href="http://www.dit.ie/" target="_blank">www.dit.ie</a></font></span></p><p><font size="2">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. <a href="http://www.dit.ie/" target="_blank">www.dit.ie</a></font></p><p><a href="http://www.dit.ie/grangegorman" target="_blank"><font size="2">Tá ITBÁC ag aistriú go Gráinseach Ghormáin – DIT is on the move to Grangegorman</font></a></p>