<html>
<head>
<meta content="text/html; charset=windows-1252"
http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<div class="moz-cite-prefix">On 07/27/2015 12:42 AM, Stefan Scott
Alexander wrote:<br>
</div>
<blockquote
cite="mid:CAFwK6auFkf1vpZoFU1cTeQmjpPXtcQ4THV=C8V_R8mPgrkbyYA@mail.gmail.com"
type="cite">
<div dir="ltr">
<div><font face="monospace, monospace">While attempting to
extend the Crud demos, I need to do destructuring on a
record of records, eg on a value 'theCols' of type
'colsMeta(int, string)' (slightly abbreviated below):</font></div>
<div><font face="monospace, monospace"><br>
</font></div>
[...]
<div><font face="monospace, monospace"><br>
</font></div>
<div><font face="monospace, monospace">Using just the string
"bar" and the value 'theCols', I want to use
pattern-matching and destructuring, to do a kind of lookup
on "bar" (to find the field whose 'Nam' field = "bar"), and
then assign two values:</font></div>
<div><font face="monospace, monospace"><br>
</font></div>
<div><font face="monospace, monospace"> (a) myCol</font></div>
<div><font face="monospace, monospace"> (b) myColNam</font></div>
</div>
</blockquote>
<br>
What you are asking for should be possible, using variant types.
See the declaration of [variant], and the operations right
underneath it, in lib/ur/basis.urs. It's an easy task for someone
familiar with the Ur type system, but it's probably impossible
without taking the time to actually learn how that type system
works, instead of just applying a few "design patterns" by pattern
matching on code examples. As far as I know, no one has yet
implemented this kind of string-based indexing.<br>
<br>
Just extracting the name field is easier and doesn't require
variants.<br>
<br>
<blockquote
cite="mid:CAFwK6auFkf1vpZoFU1cTeQmjpPXtcQ4THV=C8V_R8mPgrkbyYA@mail.gmail.com"
type="cite">
<div dir="ltr"><font face="monospace, monospace">I think the two
functions below should do the trick:</font>
<div><font face="monospace, monospace"><br>
</font></div>
<div><font face="monospace, monospace"> fun getCol aNam
someCols =</font></div>
<div><font face="monospace, monospace"> case someCols of</font></div>
<div><font face="monospace, monospace"> { colNam = { </font></div>
<div><font face="monospace, monospace"> Nam = aNam, </font></div>
<div><font face="monospace, monospace"> Show = sh,</font></div>
<div><font face="monospace, monospace"> Widget = wg
}, </font></div>
<div><font face="monospace, monospace">
otherOuterFields } => </font></div>
<div><font face="monospace, monospace"> [ colNam = [ </font></div>
<div><font face="monospace, monospace"> Nam = aNam, </font></div>
<div><font face="monospace, monospace"> Show = sh,</font></div>
<div><font face="monospace, monospace"> Widget = wg ]
]</font></div>
<div><font face="monospace, monospace"> | _ => error</font></div>
</div>
</blockquote>
<br>
I see two kinds of wishful thinking in this code:<br>
1) Patterns in ML and Haskell, and thus Ur, are <i>linear</i>, so
you aren't allowed to mention a preexisting variable name to
implement an equality test, as you do above with [aNam].<br>
2) You've invented a form of pattern matching on records that
matches one field, with an unknown name, and then binds a variable
to stand for all other field values. Ur supports nothing along
those lines.<br>
<br>
This operation is impossible without using a [folder] to do
iteration.<br>
<br>
<blockquote
cite="mid:CAFwK6auFkf1vpZoFU1cTeQmjpPXtcQ4THV=C8V_R8mPgrkbyYA@mail.gmail.com"
type="cite">
<div dir="ltr"><font face="monospace, monospace">(2) In function
getCol, am I using { } and [ ] correctly - ie:</font>
<div><font face="monospace, monospace"><br>
</font></div>
<div><font face="monospace, monospace">- { } for record
*patterns*, and </font></div>
<div><font face="monospace, monospace">- [ ] for record
*expressions* (values) ?</font></div>
</div>
</blockquote>
<br>
No. Brackets are only for type-level records. Your code just above
only uses value-level records, which are always written with curly
braces.<br>
</body>
</html>