Feb 26, 2010

Learning Ursala (3)

In the last post I introduced constructors in Ursala. In cooperation with deconstructors, constructors may be used in pointer expressions to take apart and then put together data (deconstruct and reconstruct data).

An example of this type of manipulation was already presented in the previous post.

    ~&rlX (1,2)
(2,1)


The above example first deconstructs the pair into its right and left component respectively and then constructs a new pair from these values.

Since constructors take two arguments to the left, a problem occurs when chaining complicated expressions. An expression such as ~&httC is invalid because the C constructor takes only 2 arguments (tt in this case), so the C constructor attempts to cons 2 tails of a list (an invalid operation) and h is then used to address this result.

The P constructor simplifies the writing of complex pointer expressions. P takes two deconstructors on its left and bonds them as one indivisible decontructor, applying them in their respective orders. If you know J, this is analogous to the bond verb (@)

    ~&httPC <1,2,3,4>
<1,3,4>
    ~&lrPrlPX ((1,2),(3,4))
(2,3)


Because pairing (or bonding) deconstructors in such a way is very common, Ursala allows you to place a natural number in a pointer expression to indicate the number of consecutive P constructors.

    ~&htt1C <1,2,3,4>
<1,3,4>
    ~&htttPPC <1,2,3,4>
<1,4>
    ~&httt2C <1,2,3,4>
<1,4>


Two other constructors are provided, G (glooming) and I (pairwise relative addressing). Both these constructors accept two left expression arguments. It is easier to discuss these constructors in terms of the transformations they perform on their arguments.

The G constructor transforms an argument of the form (a,(b,c)) into one of the form ((a,b),(a,c)). To quote from the Ursala manual [pdf], "a copy of the left is paired up with each side of the right". A consequence of this is that the expression ~&lrlPXlrrPXX is equivalent to ~&lrG. For example:

    ~&llPrX ((1,2),(3,4))
# (a,(b,c))
(1,(3,4))
    ~&llPrG ((1,2),(3,4))
# ((a,c),(a,d))
((1,3),(1,4))


The I constructor's actions are specified for four cases outlined in the table below.



The function of the I constructor is unstable right now, it is subject to future change and any uses deviating from the table are considered unspecified behaviour.

(Continued later)

No comments:

Post a Comment