<div dir="ltr">More suitable for the haskell-cafe. Routing.</div><div class="gmail_extra"><br><div class="gmail_quote">On 6 August 2015 at 02:52, Michael Litchard <span dir="ltr"><<a href="mailto:michael@schmong.org" target="_blank">michael@schmong.org</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"><blockquote>
<pre><br></pre><p>The below code is from this tutorial <a href="http://dev.stephendiehl.com/hask/" rel="nofollow" target="_blank">http://dev.stephendiehl.com/hask/</a></p>
<p>it illustrates very well how to operate on values from records with
"Simon-ness" (illustrated below). What I am struggling with is how to
modify values inside records with "Simon-ness", say incrementing "age". I
keep thinking it has to do with the way Label is defined with the
constructor Get. Could I add another constructor Put?</p><pre><code><span>{-# LANGUAGE DataKinds #-}</span><span>
</span><span>{-# LANGUAGE KindSignatures #-}</span><span>
</span><span>{-# LANGUAGE MultiParamTypeClasses #-}</span><span>
</span><span>{-# LANGUAGE FunctionalDependencies #-}</span><span>
</span><span>{-# LANGUAGE FlexibleInstances #-}</span><span>
</span><span>{-# LANGUAGE FlexibleContexts #-}</span><span>
</span><span>{-# LANGUAGE StandaloneDeriving #-}</span><span>
</span><span>{-# LANGUAGE ExistentialQuantification #-}</span><span>
</span><span>{-# LANGUAGE ConstraintKinds #-}</span><span>
</span><span>import</span><span> GHC.TypeLits
</span><span>newtype</span><span> Field </span><span>(</span><span>n </span><span>::</span><span> Symbol</span><span>)</span><span> v </span><span>=</span><span> Field </span><span>{</span><span> unField </span><span>::</span><span> v </span><span>}</span><span> </span><span>deriving</span><span> Show
</span><span>data</span><span> Person1 </span><span>=</span><span> Person1
</span><span>{</span><span> </span><span>_</span><span>age </span><span>::</span><span> Field </span><span>"age"</span><span> Int
</span><span>,</span><span> </span><span>_</span><span>name </span><span>::</span><span> Field </span><span>"name"</span><span> String
</span><span>}</span><span>
</span><span>data</span><span> Person2 </span><span>=</span><span> Person2
</span><span>{</span><span> </span><span>_</span><span>age' </span><span>::</span><span> Field </span><span>"age"</span><span> Int
</span><span>,</span><span> </span><span>_</span><span>name' </span><span>::</span><span> Field </span><span>"name"</span><span> String
</span><span>,</span><span> </span><span>_</span><span>lib' </span><span>::</span><span> Field </span><span>"lib"</span><span> String
</span><span>}</span><span>
</span><span>deriving</span><span> </span><span>instance</span><span> Show Person1
</span><span>deriving</span><span> </span><span>instance</span><span> Show Person2
</span><span>data</span><span> Label </span><span>(</span><span>l </span><span>::</span><span> Symbol</span><span>)</span><span> </span><span>=</span><span> Get
</span><span>class</span><span> Has a l b </span><span>|</span><span> a l </span><span>-></span><span> b </span><span>where</span><span>
from </span><span>::</span><span> a </span><span>-></span><span> Label l </span><span>-></span><span> b
</span><span>instance</span><span> Has Person1 </span><span>"age"</span><span> Int </span><span>where</span><span>
from </span><span>(</span><span>Person1 a </span><span>_</span><span>)</span><span> </span><span>_</span><span> </span><span>=</span><span> unField a
</span><span>instance</span><span> Has Person1 </span><span>"name"</span><span> String </span><span>where</span><span>
from </span><span>(</span><span>Person1 </span><span>_</span><span> a</span><span>)</span><span> </span><span>_</span><span> </span><span>=</span><span> unField a
</span><span>instance</span><span> Has Person2 </span><span>"age"</span><span> Int </span><span>where</span><span>
from </span><span>(</span><span>Person2 a </span><span>_</span><span> </span><span>_</span><span>)</span><span> </span><span>_</span><span> </span><span>=</span><span> unField a
</span><span>instance</span><span> Has Person2 </span><span>"name"</span><span> String </span><span>where</span><span>
from </span><span>(</span><span>Person2 </span><span>_</span><span> a </span><span>_</span><span>)</span><span> </span><span>_</span><span> </span><span>=</span><span> unField a
age </span><span>::</span><span> Has a </span><span>"age"</span><span> b </span><span>=></span><span> a </span><span>-></span><span> b
age pnt </span><span>=</span><span> from pnt </span><span>(</span><span>Get </span><span>::</span><span> Label </span><span>"age"</span><span>)</span><span>
name </span><span>::</span><span> Has a </span><span>"name"</span><span> b </span><span>=></span><span> a </span><span>-></span><span> b
name pnt </span><span>=</span><span> from pnt </span><span>(</span><span>Get </span><span>::</span><span> Label </span><span>"name"</span><span>)</span><span>
</span><span>-- Parameterized constraint kind for "Simon-ness" of a record.</span><span>
</span><span>type</span><span> Simon a </span><span>=</span><span> </span><span>(</span><span>Has a </span><span>"name"</span><span> String</span><span>,</span><span> Has a </span><span>"age"</span><span> Int</span><span>)</span><span>
spj </span><span>::</span><span> Person1
spj </span><span>=</span><span> Person1 </span><span>(</span><span>Field </span><span>56</span><span>)</span><span> </span><span>(</span><span>Field </span><span>"Simon Peyton Jones"</span><span>)</span><span>
smarlow </span><span>::</span><span> Person2
smarlow </span><span>=</span><span> Person2 </span><span>(</span><span>Field </span><span>38</span><span>)</span><span> </span><span>(</span><span>Field </span><span>"Simon Marlow"</span><span>)</span><span> </span><span>(</span><span>Field </span><span>"rts"</span><span>)</span><span>
catNames </span><span>::</span><span> </span><span>(</span><span>Simon a</span><span>,</span><span> Simon b</span><span>)</span><span> </span><span>=></span><span> a </span><span>-></span><span> b </span><span>-></span><span> String
catNames a b </span><span>=</span><span> name a </span><span>++</span><span> name b
addAges </span><span>::</span><span> </span><span>(</span><span>Simon a</span><span>,</span><span> Simon b</span><span>)</span><span> </span><span>=></span><span> a </span><span>-></span><span> b </span><span>-></span><span> Int
addAges a b </span><span>=</span><span> age a </span><span>+</span><span> age b
names </span><span>::</span><span> String
names </span><span>=</span><span> name smarlow </span><span>++</span><span> </span><span>","</span><span> </span><span>++</span><span> name spj
</span><span>-- "Simon Marlow,Simon Peyton Jones"</span><span>
ages </span><span>::</span><span> Int
ages </span><span>=</span><span> age spj </span><span>+</span><span> age smarlow
</span><span>-- 94</span></code></pre>
</blockquote>
</div>
<br>_______________________________________________<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-bin/mailman/listinfo/beginners</a><br>
<br></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div dir="ltr"><div>Regards</div><div dir="ltr"><div><br></div><div>Sumit Sahrawat</div></div></div></div></div></div></div>
</div>