<div dir="ltr"><blockquote>
<pre style="" class="lang-hs prettyprint prettyprinted"><br></pre><p>The below code is from this tutorial <a href="http://dev.stephendiehl.com/hask/" rel="nofollow">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 style="" class="lang-hs prettyprint prettyprinted"><code><span class="com">{-# LANGUAGE DataKinds #-}</span><span class="pln">
</span><span class="com">{-# LANGUAGE KindSignatures #-}</span><span class="pln">
</span><span class="com">{-# LANGUAGE MultiParamTypeClasses #-}</span><span class="pln">
</span><span class="com">{-# LANGUAGE FunctionalDependencies #-}</span><span class="pln">
</span><span class="com">{-# LANGUAGE FlexibleInstances #-}</span><span class="pln">
</span><span class="com">{-# LANGUAGE FlexibleContexts #-}</span><span class="pln">
</span><span class="com">{-# LANGUAGE StandaloneDeriving #-}</span><span class="pln">
</span><span class="com">{-# LANGUAGE ExistentialQuantification #-}</span><span class="pln">
</span><span class="com">{-# LANGUAGE ConstraintKinds #-}</span><span class="pln">
</span><span class="kwd">import</span><span class="pln"> GHC.TypeLits
</span><span class="kwd">newtype</span><span class="pln"> Field </span><span class="pun">(</span><span class="pln">n </span><span class="pun">::</span><span class="pln"> Symbol</span><span class="pun">)</span><span class="pln"> v </span><span class="pun">=</span><span class="pln"> Field </span><span class="pun">{</span><span class="pln"> unField </span><span class="pun">::</span><span class="pln"> v </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">deriving</span><span class="pln"> Show
</span><span class="kwd">data</span><span class="pln"> Person1 </span><span class="pun">=</span><span class="pln"> Person1
</span><span class="pun">{</span><span class="pln"> </span><span class="kwd">_</span><span class="pln">age </span><span class="pun">::</span><span class="pln"> Field </span><span class="str">"age"</span><span class="pln"> Int
</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">_</span><span class="pln">name </span><span class="pun">::</span><span class="pln"> Field </span><span class="str">"name"</span><span class="pln"> String
</span><span class="pun">}</span><span class="pln">
</span><span class="kwd">data</span><span class="pln"> Person2 </span><span class="pun">=</span><span class="pln"> Person2
</span><span class="pun">{</span><span class="pln"> </span><span class="kwd">_</span><span class="pln">age' </span><span class="pun">::</span><span class="pln"> Field </span><span class="str">"age"</span><span class="pln"> Int
</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">_</span><span class="pln">name' </span><span class="pun">::</span><span class="pln"> Field </span><span class="str">"name"</span><span class="pln"> String
</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">_</span><span class="pln">lib' </span><span class="pun">::</span><span class="pln"> Field </span><span class="str">"lib"</span><span class="pln"> String
</span><span class="pun">}</span><span class="pln">
</span><span class="kwd">deriving</span><span class="pln"> </span><span class="kwd">instance</span><span class="pln"> Show Person1
</span><span class="kwd">deriving</span><span class="pln"> </span><span class="kwd">instance</span><span class="pln"> Show Person2
</span><span class="kwd">data</span><span class="pln"> Label </span><span class="pun">(</span><span class="pln">l </span><span class="pun">::</span><span class="pln"> Symbol</span><span class="pun">)</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> Get
</span><span class="kwd">class</span><span class="pln"> Has a l b </span><span class="pun">|</span><span class="pln"> a l </span><span class="pun">-></span><span class="pln"> b </span><span class="kwd">where</span><span class="pln">
from </span><span class="pun">::</span><span class="pln"> a </span><span class="pun">-></span><span class="pln"> Label l </span><span class="pun">-></span><span class="pln"> b
</span><span class="kwd">instance</span><span class="pln"> Has Person1 </span><span class="str">"age"</span><span class="pln"> Int </span><span class="kwd">where</span><span class="pln">
from </span><span class="pun">(</span><span class="pln">Person1 a </span><span class="kwd">_</span><span class="pun">)</span><span class="pln"> </span><span class="kwd">_</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> unField a
</span><span class="kwd">instance</span><span class="pln"> Has Person1 </span><span class="str">"name"</span><span class="pln"> String </span><span class="kwd">where</span><span class="pln">
from </span><span class="pun">(</span><span class="pln">Person1 </span><span class="kwd">_</span><span class="pln"> a</span><span class="pun">)</span><span class="pln"> </span><span class="kwd">_</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> unField a
</span><span class="kwd">instance</span><span class="pln"> Has Person2 </span><span class="str">"age"</span><span class="pln"> Int </span><span class="kwd">where</span><span class="pln">
from </span><span class="pun">(</span><span class="pln">Person2 a </span><span class="kwd">_</span><span class="pln"> </span><span class="kwd">_</span><span class="pun">)</span><span class="pln"> </span><span class="kwd">_</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> unField a
</span><span class="kwd">instance</span><span class="pln"> Has Person2 </span><span class="str">"name"</span><span class="pln"> String </span><span class="kwd">where</span><span class="pln">
from </span><span class="pun">(</span><span class="pln">Person2 </span><span class="kwd">_</span><span class="pln"> a </span><span class="kwd">_</span><span class="pun">)</span><span class="pln"> </span><span class="kwd">_</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> unField a
age </span><span class="pun">::</span><span class="pln"> Has a </span><span class="str">"age"</span><span class="pln"> b </span><span class="pun">=></span><span class="pln"> a </span><span class="pun">-></span><span class="pln"> b
age pnt </span><span class="pun">=</span><span class="pln"> from pnt </span><span class="pun">(</span><span class="pln">Get </span><span class="pun">::</span><span class="pln"> Label </span><span class="str">"age"</span><span class="pun">)</span><span class="pln">
name </span><span class="pun">::</span><span class="pln"> Has a </span><span class="str">"name"</span><span class="pln"> b </span><span class="pun">=></span><span class="pln"> a </span><span class="pun">-></span><span class="pln"> b
name pnt </span><span class="pun">=</span><span class="pln"> from pnt </span><span class="pun">(</span><span class="pln">Get </span><span class="pun">::</span><span class="pln"> Label </span><span class="str">"name"</span><span class="pun">)</span><span class="pln">
</span><span class="com">-- Parameterized constraint kind for "Simon-ness" of a record.</span><span class="pln">
</span><span class="kwd">type</span><span class="pln"> Simon a </span><span class="pun">=</span><span class="pln"> </span><span class="pun">(</span><span class="pln">Has a </span><span class="str">"name"</span><span class="pln"> String</span><span class="pun">,</span><span class="pln"> Has a </span><span class="str">"age"</span><span class="pln"> Int</span><span class="pun">)</span><span class="pln">
spj </span><span class="pun">::</span><span class="pln"> Person1
spj </span><span class="pun">=</span><span class="pln"> Person1 </span><span class="pun">(</span><span class="pln">Field </span><span class="lit">56</span><span class="pun">)</span><span class="pln"> </span><span class="pun">(</span><span class="pln">Field </span><span class="str">"Simon Peyton Jones"</span><span class="pun">)</span><span class="pln">
smarlow </span><span class="pun">::</span><span class="pln"> Person2
smarlow </span><span class="pun">=</span><span class="pln"> Person2 </span><span class="pun">(</span><span class="pln">Field </span><span class="lit">38</span><span class="pun">)</span><span class="pln"> </span><span class="pun">(</span><span class="pln">Field </span><span class="str">"Simon Marlow"</span><span class="pun">)</span><span class="pln"> </span><span class="pun">(</span><span class="pln">Field </span><span class="str">"rts"</span><span class="pun">)</span><span class="pln">
catNames </span><span class="pun">::</span><span class="pln"> </span><span class="pun">(</span><span class="pln">Simon a</span><span class="pun">,</span><span class="pln"> Simon b</span><span class="pun">)</span><span class="pln"> </span><span class="pun">=></span><span class="pln"> a </span><span class="pun">-></span><span class="pln"> b </span><span class="pun">-></span><span class="pln"> String
catNames a b </span><span class="pun">=</span><span class="pln"> name a </span><span class="pun">++</span><span class="pln"> name b
addAges </span><span class="pun">::</span><span class="pln"> </span><span class="pun">(</span><span class="pln">Simon a</span><span class="pun">,</span><span class="pln"> Simon b</span><span class="pun">)</span><span class="pln"> </span><span class="pun">=></span><span class="pln"> a </span><span class="pun">-></span><span class="pln"> b </span><span class="pun">-></span><span class="pln"> Int
addAges a b </span><span class="pun">=</span><span class="pln"> age a </span><span class="pun">+</span><span class="pln"> age b
names </span><span class="pun">::</span><span class="pln"> String
names </span><span class="pun">=</span><span class="pln"> name smarlow </span><span class="pun">++</span><span class="pln"> </span><span class="str">","</span><span class="pln"> </span><span class="pun">++</span><span class="pln"> name spj
</span><span class="com">-- "Simon Marlow,Simon Peyton Jones"</span><span class="pln">
ages </span><span class="pun">::</span><span class="pln"> Int
ages </span><span class="pun">=</span><span class="pln"> age spj </span><span class="pun">+</span><span class="pln"> age smarlow
</span><span class="com">-- 94</span></code></pre>
</blockquote>
</div>