[mercury-users] Square types

Ralph Becket rbeck at microsoft.com
Thu Jun 7 23:04:38 AEST 2001


> From: Michael Day [mailto:mikeday at corplink.com.au]
> Sent: 07 June 2001 11:51
> 
> Is it possible to define a type in Mercury that can hold N items,
where N
> is equal to X*X for any X?
> 
> Basically a list restricted to holding either 1, 4, 9, 16 ... items
rather
> than an arbitrary amount.
> 
> Any ideas?

Looks like a problem for non-uniform types.

For example, you can come up with a type for perfectly balanced trees:

:- type balanced_tree(T)
    --->    leaf(T)
    ;       branch(balanced_tree({T, T})).

A type containing only an odd number of items:

:- type odd(T)
    --->    one(T)
    ;       odd(T, T, sq(T)).

We can have a type containing only square numbers of items using the
fact that the nth square is equal to the sum of the first n odd
numbers:

:- type square(T) == sq(T, T).

where

:- type sq(N, T)
    --->    zero
    ;       sq(N, sq({T, T, N}, T)).

I admit that last one took some thought...

- Ralph
--------------------------------------------------------------------------
mercury-users mailing list
post:  mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-users-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the users mailing list