Skip to content

Commit

Permalink
Merge pull request github-linguist#321 from mndrix/patch-1
Browse files Browse the repository at this point in the history
Add a misclassified Prolog file
  • Loading branch information
tnm committed Mar 10, 2013
2 parents d9d9e01 + c794c6e commit a41ec3a
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions samples/Prolog/mavis.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
:- module(mavis, [the/2]).
/** <module> Optional type declarations
The =mavis= module (because she helps with typing ;-) allows one to
give optional type declarations to Prolog code. During *development*,
these declarations throw informative exceptions if values don't match
types. In *production*, the declarations do nothing.
---+ Why?
We love dynamic types. That's one reason we love Prolog. But sometimes
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
---+ How?
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.
==
Our code can use that definiton like
==
%% frobnify(+A, -B)
frobnify(A, B) :-
the(integer, A),
the(even_integer, 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 freeze/2 so they apply as soon as a variable is bound.
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.
@author Michael Hendricks <[email protected]>
@license BSD
*/

%% the(+Type, ?Value) is det.
%
% Declare that Value has the given Type.
% Succeeds if Value is bound to a value that's compatible
% with Type. Throws an informative exception if Value
% is bound to a value that's not compatible with Type.
% If Value is not bound, the type check is delayed until
% Value becomes bound.
%
% When optimizations are enabled
% (=|current_prolog_flag(optimise, true)|= a macro removes =the=
% entirely so that it always succeeds.
:- if(current_prolog_flag(optimise,true)).
the(_,_). % avoid "Exported procedure mavis:the/2 is not defined"
user:goal_expansion(the(_,_), true).
:- else.
the(Type, Value) :-
freeze(Value, must_be(Type, Value)).
:- endif.

0 comments on commit a41ec3a

Please sign in to comment.