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:21d443b519d037049ce1f7dff0716d304afc654f
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.321d443b519d037049ce1f7dff0716d304afc654f75http://packs.ndrix.com/mavis/mavis-0.2.3.tgz

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.

Why?

We love dynamic types. That's one reason we love Prolog. But sometimes more precise types are a helpful tool. They 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

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/mndrix/mavis

author
- Michael Hendricks <michael@ndrix.org>
license
- BSD

Contents of pack "mavis"

Pack contains 6 files holding a total of 11.5K bytes.