<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 12/24/2014 08:26 PM, Stefan Scott
Alexander wrote:<br>
</div>
<blockquote
cite="mid:CAFwK6au2rLWHfWmNUfo-pFFUyvqxEwhw-Rn90kp=4-2+EvAg9w@mail.gmail.com"
type="cite">
<div dir="ltr">
<div><span style="font-family:monospace,monospace">Right now I'm
experimenting with 'eq' (which I assume tests whether two
lists are equal) and I'm getting parse errors, for something
(supposedly) simple like:<br>
<br>
val nums1 = 1 :: 2 :: 3 :: []<br>
val nums2 = 101 :: 102 :: 103 :: []<br>
<br>
eq nums1 nums2<br>
</span></div>
</div>
</blockquote>
<br>
This code doesn't parse the way you think. It's not allowed to have
several declarations followed by a freestanding expression. The
grammar in the Ur/Web manual should give all the details, but
changing the last line like so would at least lead to a parse more
likely to be what you expect:<br>
val eq_result = eq nums1 nums2<br>
<br>
Your code above actually tries to put the [eq] call onto the end of
the value of [nums2]!<br>
<br>
Also, [x = y] is syntactic sugar for [eq x y]. It would not be
normal to write [eq] explicitly like above.<br>
<br>
<blockquote
cite="mid:CAFwK6au2rLWHfWmNUfo-pFFUyvqxEwhw-Rn90kp=4-2+EvAg9w@mail.gmail.com"
type="cite">
<div dir="ltr">
<div><span style="font-family:monospace,monospace">Would it be
correct for me to call 'eq' a "library function" - or is it
actually something else (perhaps something involving type
classes)? <br>
</span></div>
</div>
</blockquote>
<br>
[eq] in the initial environment is a type-class-parameterized
function. The same name happens to be reused for a different
purpose in the List module.<br>
<br>
<blockquote
cite="mid:CAFwK6au2rLWHfWmNUfo-pFFUyvqxEwhw-Rn90kp=4-2+EvAg9w@mail.gmail.com"
type="cite">
<div dir="ltr">(1) Here is the signature of the (library function?
type class?) 'eq' in list.urs:<br>
<div><span style="font-family:monospace,monospace"><br>
datatype t = datatype Basis.list<br>
<br>
val eq : a ::: Type -> eq a -> eq (t a)<br>
</span></div>
</div>
</blockquote>
<br>
This is a rule for concluding that [list a] belongs to the [eq] type
class if [a] belongs to it. It's not a function that you'd call to
test equality of lists. Just compare two lists with infix operator
[=] like Joe Average Programmer would expect, and everything should
work, if the List module is in scope.<br>
<br>
<blockquote
cite="mid:CAFwK6au2rLWHfWmNUfo-pFFUyvqxEwhw-Rn90kp=4-2+EvAg9w@mail.gmail.com"
type="cite">
<div dir="ltr">
<div><span style="font-family:monospace,monospace"> (2) Here is
(part of) the signature of (library function?) 'eq' in
basis.urs:<br>
<br>
class eq<br>
val eq : t ::: Type -> eq t -> t -> t -> bool<br>
<br>
I believe the above two lines from basis.urs are saying:
"'eq' is a function (defined for Type t, where t admits
equality - similar to type classes in Haskell), which takes
two arguments of Type t, and returns a bool."<br>
</span></div>
</div>
</blockquote>
<br>
Yes, type classes are exactly what's going on here.<br>
<br>
<blockquote
cite="mid:CAFwK6au2rLWHfWmNUfo-pFFUyvqxEwhw-Rn90kp=4-2+EvAg9w@mail.gmail.com"
type="cite">
<div dir="ltr">
<div><span style="font-family:monospace,monospace">(3) And here
is the definition of the library function 'eq' in list.ur:<br>
<br>
val eq = fn [a] (_ : eq a) =><br>
let<br>
fun eq' (ls1 : list a) ls2 =<br>
case (ls1, ls2) of<br>
([], []) => True<br>
| (x1 :: ls1, x2 :: ls2) => x1 =
x2 && eq' ls1 ls2<br>
| _ => False<br>
in<br>
mkEq eq'<br>
end<br>
<br>
The recursive part in the above definition, involving the
three pattern-matchings, seems quite clear and standard.<br>
<br>
However, there are several other things which are confusing:<br>
<br>
- Why is there a "don't care" wildcard for the second
argument on the first line?<br>
</span></div>
</div>
</blockquote>
<br>
Type class quantification in Ur works by passing explicit witness
objects. Instance resolution uses them automatically as needed,
hence there is no need to assign names to those parameters.<br>
<br>
<blockquote
cite="mid:CAFwK6au2rLWHfWmNUfo-pFFUyvqxEwhw-Rn90kp=4-2+EvAg9w@mail.gmail.com"
type="cite">
<div dir="ltr">
<div><span style="font-family:monospace,monospace">- Why do the
two args on the first line look so different? I would expect
'eq' to take two args of the same type<br>
</span></div>
</div>
</blockquote>
<br>
I hope my earlier answer helps: this is a definition of an inference
rule for type classes, not an equality-testing function.<br>
<br>
<blockquote
cite="mid:CAFwK6au2rLWHfWmNUfo-pFFUyvqxEwhw-Rn90kp=4-2+EvAg9w@mail.gmail.com"
type="cite">
<div dir="ltr">
<div><span style="font-family:monospace,monospace">However, I
can't find the definition of mkEq anywhere - only its
signature - so I have no idea what mkEq does.<br>
</span></div>
</div>
</blockquote>
<br>
It's the function to be used in creating new instance witnesses for
the [eq] type class.<br>
<br>
<blockquote
cite="mid:CAFwK6au2rLWHfWmNUfo-pFFUyvqxEwhw-Rn90kp=4-2+EvAg9w@mail.gmail.com"
type="cite">
<div dir="ltr">
<div><span style="font-family:monospace,monospace">- The first
argument ls1 to eq' is declared to be of type 'list a'. Why
is there no similar declaration for the second argument,
ls2? (Maybe there is some type inference going on?)</span><br>
</div>
</div>
</blockquote>
<br>
Yes, type inference does it.<br>
<br>
<blockquote
cite="mid:CAFwK6au2rLWHfWmNUfo-pFFUyvqxEwhw-Rn90kp=4-2+EvAg9w@mail.gmail.com"
type="cite">
<div dir="ltr">
<div><span style="font-family:monospace,monospace">Am I going
about this all wrong? Should I be starting by reading some
simpler documentation on SML or something? I would welcome
any suggestions. Thanks!</span><br>
</div>
</div>
</blockquote>
<br>
All of the Ur/Web documentation does assume serious familiarity with
both Haskell and some ML language.<br>
</body>
</html>