[clean-list] Arrays: type annotation and pattern matching

Pieter Koopman pieter at cs.ru.nl
Mon Aug 25 09:28:40 MEST 2008


HI Terrence,

terrence.x.brannon at jpmchase.com wrote:
>
> I am confused on the type annotation for this function:
>
> CopyFirst :: Int *(a e) -> *(a e) | Array a e
> CopyFirst j a=:{[0]=a0} = {a & [j] = a0}
>
> It looks like the function CopyFirst takes two arguments. The first is 
> an integer. The second argument confuses me. The asterisk in front of 
> (a e) means some data type which receives destructive updates.
> But the syntax (a e) was not previously introduced.
>
> And then the type restriction confuses me...
>
> | Array a e
>
> seems to say 'provided a and e are of type Array' ?
>
> And then the actual code of the function confuses me... actually I 
> think I got it
>
> First:   CopyFirst j a=:{[0]=a0}
>
> the function takes 2 arguments, j (an integer) and then a, an array, 
> but it also sets a0 to the first element of the array a... of course 
> this looks odd. Why is a0 on the RHS instead of LHS?
>
> Then: the actual function body starts ... {a & [j] = a0}  is saying 
> 'take a and update the jth position with the value a0'
> but the problem is a0 is never defined.
>
> So, to summarize:
>
> 1 - the syntax *(a e) is used but not previously defined
See section 4.4 on page 38 of the report.
Technically arrays are a type class. This allows for instance that 
arrays and unboxed arrays are different types. This is the a in the (a 
e). The e is the type of the elements in the array (similar to the 
elements of a list in [e]).
The * says that this array must be unique such that it can be updated.
> 2 - the type restriction | Array a e is a bit confusing
This just says that the type (a e) should be an array.
> 3 - it seems odd that a0 is on the RHS of {[0] = a0} instead of the LHS
This notation indicates that the name a0 is assigned to the first 
element of the array (a.[0]). If we would do the selection on the rhs of 
the function, there would be two reference to the array on the rhs. This 
would make it too difficult for the type checker to see that the 
resulting array is unique.

Best,

Pieter Koopman
>
>
> --
> Terrence Brannon - SID W049945
> 1111 Polaris Pkwy, OH1-1141, Columbus, OH 43240
> 818-359-0893 (cell)
> 614-213-3426 (fax)


More information about the clean-list mailing list