[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`?
Stefan Scott Alexander
stefanscottalexx at gmail.com
Mon Jul 27 00:42:31 EDT 2015
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):
val theCols : colsMeta(int, string)
val theCols = [
FooColumn = [
Nam = "foo",
Show = (fn s => s),
Widget = (fn w => w) ],
BarColumn = [
Nam = "bar",
Show = (fn s => s),
Widget = (fn w => w) ]
]
In this case, I have a value of type `string` (eg, "bar") which corresponds
to the value of one of the fields of one of the "inner" records of
'theCols'.
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
---------
So, given a record of records, eg:
val theCols = [
FooColumn = [
Nam = "foo",
Show = (fn s => s),
Widget = (fn w => w) ],
BarColumn = [
Nam = "bar",
Show = (fn s => sh),
Widget = (fn w => w) ]
]
And given a *string* which equals one of the 'Nam' fields, eg:
"bar"
The goal is to do the following two things:
(a) Assign 'myCol' to the entire field BarColumn (ie, the field name, and
the field value):
val myCol =
[ BarColumn = [
Nam = "bar",
Show = (fn s => s),
Widget = (fn w => w) ]
]
(b) And assign 'myNam' to *just* the field name:
val myColNam = #BarColumn
---------
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
fun getNam aNam someCols =
case someCols of
{ colNam = {
Nam = aNam ,
otherInnerFields },
otherOuterfields } => #colNam
| _ => error
val myCol = getCol "bar" theCols
val myColNam = getNam "bar" theCols
---------
Remark:
The patterns 'otherOuterFields' and 'otherInnerField' could probably be
replaced by wildcards `_`.
---------
Questions:
(1) I'm concerned about 'colNam', because it is of kind `Name`.
(a) Is it possible to have a function which returns something of kind
`Name`?
(b) Is it possible to say something like:
val myColNam = getNam "bar" theCols
which attempts to assign something of kind `Name` to the identifier
'myColNam'?
(2) In function getCol, am I using { } and [ ] correctly - ie:
- { } for record *patterns*, and
- [ ] for record *expressions* (values) ?
---------
Syntax error:
So far, the above fragments (currently part of a larger body of code)
aren't parsing - I'm getting a syntax error on the 'of' in the 'case'
statement, saying "replacing OF with LPAREN".
I'm going to keep whittling my code down to make it smaller and smaller
until I can isolate the syntax error.
---------
Question:
Aside from the syntax errors, is the overall approach above approximately
correct?
---------
Comparison with the `as` keyword in ML:
Initially I though I could use something similar to ML's "layered
patterns", which use the keyword `as`.
Citations:
(1) "Tips for Computer Scientists on Standard ML (Revised)" by Mads Tofte,
page 6:
https://www.itu.dk/people/tofte/publ/tips.pdf
(2) "Elementary Standard ML", by G. Michaelson, section 6.9, page 138:
https://books.google.com.br/books?id=Zf0aq8BtSLAC&pg=PA138&lpg=PA138&dq=ML++%22layered+patterns%22&source=bl&ots=g2aDg-Ma6Q&sig=mAHDVFIRTWkqRAJt5zJj0nH7fvQ&hl=en&sa=X&redir_esc=y#v=onepage&q=ML%20%20%22layered%20patterns%22&f=false
But I don't think Ur/Web supports ML's "layered patterns" using the `as`
keyword, so the above functions 'getCol' and 'getNam' do the destructuring
using a "bigger" pattern.
###
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.impredicative.com/pipermail/ur/attachments/20150727/99a16331/attachment-0001.html>
More information about the Ur
mailing list