mndrix / mavis

Optional types for Prolog
The Unlicense
11 stars 4 forks source link

sugar for creating types #1

Open mndrix opened 11 years ago

mndrix commented 11 years ago

Imagine the following two type definitions in mavis:

:- multifile error:has_type/2.
error:has_type(even_integer, X) :-
    0 is X mod 2.
error:has_type(north_american_country, X) :-
   memberchk(X, [usa, canada, mexico]).

That's more verbose (and error prone) than I'd like. I'd prefer to write:

:- type even_integer ---> 0 is X mod 2.
:- type north_american_country ---> usa; canada; mexico.

The basic notation is similar to Mercury while allowing arbitrary type checks (such as even_integer). This can be implemented with a relatively easy term_expansion/2 macro.

mndrix commented 11 years ago

This one is derived from the delta type in my tiny patches project. It's similar to algebraic data types in other languages.

:- type delta ---> +(atom, positive_integer)
                 ; <(positive_integer, oneof([space,tab]), positive_integer)
                 .

which should expand into

:- multifile error:has_type/2.
error:has_type(delta, +(A,B)) :-
    error:has_type(A, atom),
    error:has_type(B, positive_integer).
error:has_type(delta, <(A,B,C)) :-
    error:has_type(positive_integer, A),
    error:has_type(oneof([space,tab]), B),
    error:has_type(positive_integer, C).
mndrix commented 10 years ago

Make sure that our type syntax is very close to (if not identical with) the syntax used by Mercury and SWI-Prolog's CHR system.

mndrix commented 10 years ago

This sugary syntax should automatically generate clauses for quickcheck:arbitrary/2, if possible. In most circumstances it will be trivial to do so. We certainly don't want to maintain those definitions by hand if we can avoid it.