[Ur] (1) Syntax errors when destructuring a record of records on a field value; (2) Is it ok to define a function which returns something of kind `Name`?

Adam Chlipala adamc at csail.mit.edu
Mon Jul 27 09:32:14 EDT 2015


On 07/27/2015 12:42 AM, Stefan Scott Alexander wrote:
> 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):
>
> [...]
>
> 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:
>
>   (a) myCol
>   (b) myColNam

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.

Just extracting the name field is easier and doesn't require variants.

> I think the two functions below should do the trick:
>
>   fun getCol aNam someCols =
>     case someCols of
>         { colNam = {
>             Nam = aNam,
>             Show = sh,
>             Widget = wg },
> otherOuterFields } =>
>         [ colNam = [
>             Nam = aNam,
>             Show = sh,
>             Widget = wg ] ]
>       | _ => error

I see two kinds of wishful thinking in this code:
1) Patterns in ML and Haskell, and thus Ur, are /linear/, so you aren't 
allowed to mention a preexisting variable name to implement an equality 
test, as you do above with [aNam].
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.

This operation is impossible without using a [folder] to do iteration.

> (2) In function getCol, am I using { } and [ ] correctly - ie:
>
> - { } for record *patterns*, and
> - [ ] for record *expressions* (values) ?

No.  Brackets are only for type-level records.  Your code just above 
only uses value-level records, which are always written with curly braces.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.impredicative.com/pipermail/ur/attachments/20150727/841a14e4/attachment.html>


More information about the Ur mailing list