<div dir="ltr">David,<div>Thank you for you informative and helpful reply.</div><div>I think that are two issues impeding my understanding of the original code.</div><div>1. The use of lambda</div><div>2. The structure of the class and instance</div><div><br></div><div>1. The use of lambda<br></div><div>Is seems that the arguments to (+!) , below ,must be functions. I was trying to use values</div><div><div><div>data Time = Time Double </div><div><br></div><div>type Moving v = Time -> v</div><div><br></div><div>(+!) a b = \t -> (a t) Prelude.+ (b t)</div><div><br></div><div>b :: Moving Double</div><div><br></div><div>b (Time x) = x * 1.5</div><div><br></div><div>test = (b +! b) (Time 10.0)</div></div></div><div><br></div><div>Is my take on this correct?</div><div><br></div><div><br></div><div>2. The structure of the class and instance.<br></div><div>Recall the original code:</div><div><div><br></div><div>type Moving v = Time -> v</div><div><br></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>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><div><br></div><div>I believe that this would have to be changed to avoid a clash with the Prelude definitions.</div><div>Is the following structuring reasonable?</div><div><br></div><div><div>module MovingPoint 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> sqr :: a -> a</div><div> sqrt :: a -> a</div><div><br></div><div>instance (Floating v) => Number (Moving v) 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> sqr a = \t -> (a t) Prelude.* (a t)</div><div> sqrt a = \t -> Prelude.sqrt (a t)</div><div><br></div><div>b :: Moving Double</div><div>b (Time x) = x Prelude.* 1.5</div><div>test = (b MovingPoint.+ b) (Time 10.0)</div></div><div><br></div><div>Thanks,</div><div>Pat</div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On 3 October 2017 at 15:01, David McBride <span dir="ltr"><<a href="mailto:toad3k@gmail.com" target="_blank">toad3k@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div><div><div>You can get some intuition for how this works by replacing "Moving v" with its definition "Time -> v". Let's look at the + operation.<br><br></div><div>class Number a where<br></div><div> (+) :: a -> a -> a<span class=""><br>instance Number v => Number (Moving v)<br></span>instance Number v => Number (Time -> v)<br></div><div>(+) :: Number v => (Time -> v) -> (Time -> v) -> (Time -> v)<br><br></div><div>So each argument of + must take a Time, the end result must also take a Time, and whatever each argument returns must be a Number (and thus has + defined for it). So you can sort of see how it works. + for a Moving v takes a time, then passes that time to each of its arguments, then adds the result.<br><br></div><span class=""><div>(+) a b = \t -> (a t) Prelude.+ (b t)<br><br></div></span>data Time = Time Double -- For example.<br><br></div><div>Then you can make formulas that are rooted in time. For example (contrived) if you are throwing a ball, the distance of the ball from you at time f could be something like the following:<br><br></div><div>balldistance :: Moving Double<br></div><div>balldistance (Time f) = f * 1.2<br></div><div><br></div><div>ball1 :: Moving Double<br></div>ball1 = balldistance <br><br><div>ball2 :: Moving Double<br></div>ball2 = balldistance<br><br></div><div>-- the combined distance of both balls at time f<br></div><div>bothballs :: Moving Double<br></div><div>bothballs = ball1 + ball2<br><br></div><div>Then you can get the combined distance of both balls after 12 seconds, for example.<br><br></div><div>test :: Double<br></div><div>test = bothballs (Time 12.0)<br></div><div><br></div></div><br><div><div><div><div><div> <br></div><div><br></div></div></div></div></div></div><div class="gmail_extra"><br><div class="gmail_quote"><div><div class="h5">On Tue, Oct 3, 2017 at 9:07 AM, PATRICK BROWNE <span dir="ltr"><<a href="mailto:patrick.browne@dit.ie" target="_blank">patrick.browne@dit.ie</a>></span> wrote:<br></div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div class="h5"><div dir="ltr"><div>Hi,</div><div>I am trying to compile, run, and understand the following code from [1].</div><div><br></div><div>type Moving v = Time -> v</div><div><br></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>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>I followed the compiler advice to produce the following version which compiles:</div><div><br></div><div>{-# LANGUAGE FlexibleInstances #-}</div><div>{-# LANGUAGE TypeSynonymInstances #-}</div><div>module MovingPoint where</div><div>type Time = Float -- Type synonym assumed, could it be data type??</div><div>type Moving v = Time -> v</div><div><br></div><div>class Number a where</div><div> (+), (-), (*) :: a -> a -> a</div><div> sqr :: a -> a</div><div> sqrt :: a -> a</div><div><br></div><div>instance (Floating v) => Number (Moving v) 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> sqr a = \t -> (a t) Prelude.* (a t)</div><div> sqrt a = \t -> Prelude.sqrt (a t)</div><div><br></div><div>I do not know how to invoke any of the operations. In general I do know how to execute lambdas.</div><div>I do not understand the bracketed pairs e.g. (a t).</div><div>Any help on understanding and running the program would be appreciated.</div><div>Thanks,</div><div>Pat</div><div><br></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" target="_blank">http://citeseerx.ist.psu.edu/v<wbr>iewdoc/download?doi=10.1.1.113<wbr>.9804&rep=rep1&type=pdf</a></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>______________________________<wbr>_________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">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-bi<wbr>n/mailman/listinfo/beginners</a><br>
<br></blockquote></div><br></div>
</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>