[mercury-users] inst syntax? [was: Types as sets?]

Douglas Auclair dauclair at msn.com
Wed Feb 12 07:36:58 AEDT 2003


From: Fergus Henderson (fjh at cs.mu.OZ.AU)
>In Mercury, subtypes can be represented using the mode system. You can 
>write this as
>
>       :- pred p(int::in(two_digit_natural), string::out) is det.       
>p(1, "one").       % ...       p(99, "ninety-nine").
>
>where `two_digit_natural' is an inst that represents the subtype, defined 
>by
>
>       :- inst two_digit_natural --->                0 ; 1 ; 2 ; 3 ; 4 ; 5 
>; 6 ; 7 ; 8 ; 9 ;
[snip rest]

Dear Fergus and all,

Wow!  What an eye-opening way to create a limited type over a preexisting 
type (er, sort of)!  In reviewing the Mercury language reference manual, I 
saw no such way to create insts (although the Mercury library reference does 
use that syntax), could a description of using ---> with insts be added to 
the manual?  Also, I found it educational that insts can be used in this way 
(and I think others would also gain from this information), could a 
description of that style of programming be added to the manual as well?  In 
the Mercury Language reference, I did learn about insts being a mode state, 
but here, it seems that insts are being used as type instances (using the 
mode system).

Also I saw ---> and == for types, but the Mercury library contains types, 
and insts, defined with = as well as ==.  I gather that ---> for inst 
signifies a discriminated union for insts.  What is the purpose of = (vice 
==) for types and insts?  I guess in your example code above that 1; 2; 3; 
... are like mode states (like free or ground or unique), is that correct?  
So, thank you very much for a lesson on using inst declarations in Mercury.  
I will spend some time studying and coding this approach (I have a chess 
problem solver that will immediately benefit from this concept).

On a related topic, can an inst be built that is infinite in size (okay, not 
"infinite", but how about "so large that it's impracticable to enumerate the 
elements")?  How does one go about doing that?

For example, the primitive type int has a large number of elements, and I 
wish to construct the positive integers as a type (or inst) from int.  Also, 
I don't wish to use the Peano series to do it.  When I need 55, I'd prefer 
to type 55 in the code, not s(s(... s(zero) ...)).  Is there a way to create 
a type or inst that does this?  Or, generally, are there ways to create 
types (or insts from pre-existing types) where enumeration is impracticable, 
but all the elements are known (e.g. from a first element, a 
successor/transformer function, and a termination test)?

Where can I learn more about insts?  It is a concept that I have only read 
about before, but that's it.  Are there papers/tutorials on insts available, 
or some code (besides list.m and std_util.m) that uses insts extensively?

By the way, writing a cryptarithmetic solver in straight Mercury was so 
easy, thanks to the modes (and insts) of lists.  Not many programming 
languages allow one to pass around a list of free variables so easily!

Sincerely,
Doug Auclair

_________________________________________________________________
The new MSN 8: smart spam protection and 2 months FREE*  
http://join.msn.com/?page=features/junkmail
--------------------------------------------------------------------------
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