Did you know ... Search Documentation:
Packs (add-ons) for SWI-Prolog

Package "mavis"

Title:Optional type declarations
Rating:Not rated. Create the first rating!
Latest version:0.2.3
SHA1 sum:f9a9da9771678e64cab92b6818e371e718d388dc
Author:Michael Hendricks <michael@ndrix.org>
Maintainer:Michael Hendricks <michael@ndrix.org>
Packager:Michael Hendricks <michael@ndrix.org>
Home page:http://packs.ndrix.com/mavis/index.html
Download URL:http://packs.ndrix.com/mavis/mavis-0.2.3.tgz
Requires:list_util
quickcheck

Reviews

No reviews. Create the first review!.

Details by download location

VersionSHA1#DownloadsURL
0.0.29f7f1028ee0526b803178c75bd98d09b2fa173e81http://commondatastorage.googleapis.com/ndrix/mavis-0.0.2.zip
0.0.436d34726ccc28cbb1b0676dfc1318bb8c8433ce51http://commondatastorage.googleapis.com/ndrix/mavis-0.0.4.zip
0.0.57af366aef92c7a27560bc8906b4bba9f143817443http://commondatastorage.googleapis.com/ndrix/mavis-0.0.5.zip
0.1.0bed402eef6d0570e862b3b7f858759f6549adce71http://packs.ndrix.com/mavis/mavis-0.1.0.tgz
0.1.16fe986f296b990dbe8ed787473fe8061244b97661http://packs.ndrix.com/mavis/mavis-0.1.1.tgz
0.2.015e4a5b63c1d6330a06e856488e0fa316c25fbb02http://packs.ndrix.com/mavis/mavis-0.2.0.tgz
0.2.189066a79f3b034d06939d6f46be6bf586e28b9716http://packs.ndrix.com/mavis/mavis-0.2.1.tgz
0.2.2fbe9ae3c8774d078d402bbff4d008a95a8d1cf565http://packs.ndrix.com/mavis/mavis-0.2.2.tgz
0.2.321d443b519d037049ce1f7dff0716d304afc654f109http://packs.ndrix.com/mavis/mavis-0.2.3.tgz
67a6076544ee2f0bac16c0c9229550a02e4153ce6https://github.com/GavinMendelGleason/mavis.git
d230853af006f994d22b06a9423079982ce2ff453https://github.com/GavinMendelGleason/mavis.git
f9a9da9771678e64cab92b6818e371e718d388dc2https://github.com/GavinMendelGleason/mavis.git

Synopsis

:- use_module(library(mavis)).

%% even(+X:integer) is semidet.
even(X) :-
    0 is X mod 2.

Description

The mavis module (because she helps with typing ;-) allows one to use optional type declarations in Prolog code. During development, these declarations throw informative exceptions when values don't match types. A typical development environment converts this into a helpful stack track which assists in locating the error.

In production, the declarations are completely removed by macros and do nothing. Production time is defined as any time when optimization is enabled: current_prolog_flag(optimise, true).

Type declarations can be give manually by calling the/2. mavis also inserts type declarations for you based on your PlDoc structured comments. For example, during development, the definition of even above becomes

even(A) :-
    the(integer, A),
    0 is A mod 2.

The library also takes into account groundedness and determinsm as specified in the mode line given to PlDoc. Currently the library recognises

`erroneous`, `failure`,`semidet`,`det`,`multi`,`nondet`

The different determinism qualifiers are interpreted as follows:

  • failure: 0 solutions
  • semidet: 0 or 1 solution
  • det: 1 solution
  • multi: more than one solution
  • nondet: Any number of solutions including 0 The groundedness currently must be one of:
    `++`,`+`,`?`,`--`,`-`,`:`,`@`,`!`

    These are interpreted as follows:

  • ++ means completely ground on entry.
  • + means ground in a way compatible with type declaration. For any, this provides no checkable information.
  • ? means either ground, unground or mixed. If it is not a variable, we will demote the determinism as follows:
    • det => semidet
    • multi => nondet
  • -- means variable input, and type compatible output.
  • - means an output parameter. The output should be compatible with the type. If it is not a variable, we will demote the determinism as follows:
    • det => semidet
    • multi => nondet
  • : means a goal. Currently no checking is done.
  • @ means not further bound than on input. Currently no checking is done.
  • ! means side-effectable variable. Currently no checking is done. "Compatibility" means that running the double-negated type over the variable is successful, i.e. the input structure which is defined does not contradict the type.

Why?

We love dynamic types. That's one reason we love Prolog. But sometimes it is useful to distinguish between a failure, and an incorrect utlisation of the calling contract. Types can:

  • offer documentation to those reading our code
  • help find errors during development
  • structure our thinking during development
  • provide data for static analysis tools

Defining new types

Mavis types are defined using error:has_type/2. We might define an even_integer type with

error:has_type(even_integer, X) :-
    0 is X mod 2.

We can use the definition manually:

frobnify(A, B) :-
    the(integer, A),
    the(even_integer, B),
    B is 2*A.

or simply add it to our PlDoc comments:

%% frobnify(+A:integer, -B:even_integer)
frobnify(A, B) :-
    B is 2*A.

We can declare types for bound variables, like A, and not-yet-bound variables, like B. The type constraints are implemented with when/2 so they apply as soon as a variable is ground.

To disable type checking in production, start Prolog with the -O command line argument. A macro eliminates calls to the/2 so they have no runtime overhead.

Changes in this Version

  • Fix packaging error
  • Add determinism checking
  • Add groundedness checking

TODO

There should be a less ad-hoc method of mode selection. It would also be useful to extend the groundedness criteria

In future versions we hope to incorporate a gradual typing discipline using abstract interpretation. This could potentially find type, groundedness and determinacy errors before we have run the program. Ultimately it may also provide performance improvements.

It would also be very nice to include polymorphism, however, this requires that we have some way to select a type. As there is no principle typing, this is potentially a (very interesting) can of worms.

Also of some interest would be dependent type checking, which at least in the dynamic case, might be tractable.

Issues

When using metapredicates such as maplist, goal expansion will get confused and generate incorrect clauses unless the predicate has the appropriate number of arguments. This can be achieved by wrapping the call in a suitable lambda form. e.g. If p is given a modeline then:

maplist(p,Xs,Ys)

should be replaced with:

maplist([X,Y]>>(p(X,Y)),Xs,Ys

Installation

Using SWI-Prolog 6.3.16 or later:

$ swipl
1 ?- pack_install(mavis).

Source code available and pull requests accepted on GitHub: https://github.com/GavinMendelGleason/mavis

Authors

  • Michael Hendricks <michael@ndrix.org>
  • Gavin Mendel-Gleason <gavin@datachemist.com>

Contents of pack "mavis"

Pack contains 12 files holding a total of 30.4K bytes.