diff: new library module `prolog'

Fergus Henderson fjh at cs.mu.oz.au
Wed Apr 30 20:44:30 AEST 1997


Hi,

Tom, can you please review this one?
(Comments from anyone else interested would be appreciated too.)

library/prolog.m:
	New file, contains Prolog compatibility hacks.

library/std_util.m:
	Move arg/3 and det_arg/3 into prolog.m.

%-----------------------------------------------------------------------------%
:- module prolog.
:- interface.

% We define !/0 (and !/2 for dcgs) to be equivalent to `true'.  This is for
% backwards compatibility with Prolog systems.  But of course it only works
% if all your cuts are green cuts.

/********
cut is currently defined in mercury_builtin.m, for historical reasons.

:- pred ! is det.

:- pred !(T, T).
:- mode !(di, uo) is det.
:- mode !(in, out) is det.
********/

% Prolog arithmetic operators.

:- pred T =:= T.			% In Mercury, just use =
:- mode in =:= in is semidet.

:- pred T =\= T.			% In Mercury, just use \=
:- mode in =\= in is semidet.

/*******
is/2 is currently defined in int.m, for historical reasons.

:- pred is(T, T) is det.		% In Mercury, just use =
:- mode is(uo, di) is det.
:- mode is(out, in) is det.
******/

% Prolog term comparison operators.

:- pred T == T.				% In Mercury, just use =
:- mode in == in is semidet.

:- pred T \== T.			% In Mercury, just use \=
:- mode in \== in is semidet.

:- pred T @< T.
:- mode in @< in is semidet.

:- pred T @=< T.
:- mode in @=< in is semidet.

:- pred T @> T.
:- mode in @> in is semidet.

:- pred T @>= T.
:- mode in @>= in is semidet.

% Prolog's so-called "univ" operator, `=..'.
% Note: this is not related to Mercury's "univ" type!
% In Mercury, use `expand' (defined in module `std_util') instead.

:- pred T =.. univ_result.
:- mode in =.. out is det.
	%
	% Note that the Mercury =.. is a bit different to the Prolog
	% one.  We could make it slightly more similar by overloading '.'/2,
	% but that would cause ambiguities that might prevent type
	% inference in a lot of cases.
	% 
% :- type univ_result ---> '.'(string, list(univ)).
:- type univ_result == pair(string, list(univ)).

	% arg/3.  In Mercury, use argument/3 (defined in module std_util)
	% instead:
	%      arg(ArgNum, Term, Data) :- argument(Term, ArgNum - 1, Data).
	%
:- pred arg(int::in, T::in, univ::out) is semidet.

	% det_arg/3: like arg/3, but calls error/1 rather than failing
	% if the index is out of range.
	%
:- pred det_arg(int::in, T::in, univ::out) is det.
%-----------------------------------------------------------------------------%

:- implementation.

!.
!(X, X).

X == X.
X \== Y :- X \= Y.

X =:= X.
X =\= Y :- X \= Y.

X @< Y :- compare(<, X, Y).
X @> Y :- compare(>, X, Y).
X @=< Y :- compare(R, X, Y), R \= (>).
X @>= Y :- compare(R, X, Y), R \= (<).

Term =.. Functor - Args :-
	expand(Term, Functor, _Arity, Args).

% Term =.. [Functor | Args] :-
%	expand(Term, Functor, _Arity, Args).

arg(ArgumentIndex, Type, Argument) :-
	ArgumentIndex1 is ArgumentIndex - 1,
	argument(Type, ArgumentIndex1, Argument).

det_arg(ArgumentIndex, Type, Argument) :-
	ArgumentIndex1 is ArgumentIndex - 1,
	det_argument(Type, ArgumentIndex1, Argument).

%-----------------------------------------------------------------------------%
Index: std_util.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/library/std_util.m,v
retrieving revision 1.81
diff -u -r1.81 std_util.m
--- std_util.m	1997/04/28 12:12:12	1.81
+++ std_util.m	1997/04/28 13:59:30
@@ -10,11 +10,6 @@
 
 % This file is intended for all the useful standard utilities
 % that don't belong elsewhere, like <stdlib.h> in C.
-%
-% It contains the predicates solutions/2, semidet_succeed/0,
-% semidet_fail/0, functor/3, arg/3, det_arg/3, expand/4; the types univ,
-% unit, maybe(T), pair(T1, T2); and some predicates which operate on
-% those types.
 
 %-----------------------------------------------------------------------------%
 %-----------------------------------------------------------------------------%
@@ -350,31 +345,6 @@
 	%
 :- pred det_argument(T::in, int::in, univ::out) is det.
 
-	% arg(ArgumentIndex, Data, Argument) 
-	% 
-	% Given a data item (Data) and an argument index
-	% (ArgumentIndex), starting at 1 for the first argument, binds
-	% Argument to that argument of the functor of the data item. If
-	% the argument index is out of range -- that is, higher than the
-	% arity of the functor or lower than 1 -- arg/3 fails.  The
-	% argument has the type univ. 
-	%
-	% NOTE: `arg' is provided for Prolog compatability - the order
-	% of parameters, and first argument number in `arg' are
-	% different from `argument'.
-	%
-:- pred arg(int::in, T::in, univ::out) is semidet.
-
-	% det_arg(ArgumentIndex, Data, Argument) 
-	% 
-	% Given a data item (Data) and an argument index
-	% (ArgumentIndex), starting at 1 for the first argument, binds
-	% Argument to that argument of the functor of the data item. If
-	% the argument index is out of range -- that is, higher than the
-	% arity of the functor or lower than 1 -- det_arg/3 aborts. 
-	%
-:- pred det_arg(int::in, T::in, univ::out) is det.
-
 	% expand(Data, Functor, Arity, Arguments) 
 	% 
 	% Given a data item (Data), binds Functor to a string
@@ -2477,14 +2447,6 @@
 	SUCCESS_INDICATOR = success;
 
 }").
-
-arg(ArgumentIndex, Type, Argument) :-
-	ArgumentIndex1 is ArgumentIndex - 1,
-	argument(Type, ArgumentIndex1, Argument).
-
-det_arg(ArgumentIndex, Type, Argument) :-
-	ArgumentIndex1 is ArgumentIndex - 1,
-	det_argument(Type, ArgumentIndex1, Argument).
 
 det_argument(Type, ArgumentIndex, Argument) :-
 	(

%---------------------------------------------------------------------------%
% Copyright (C) 1997 University of Melbourne.
% This file may only be copied under the terms of the GNU Library General
% Public License - see the file COPYING.LIB in the Mercury distribution.
%---------------------------------------------------------------------------%

% File: prolog.m.
% Main author: fjh.

% This file contains predicates that are intended to help people
% porting Prolog programs, or writing programs in the intersection
% of Mercury and Prolog.


-- 
Fergus Henderson <fjh at cs.mu.oz.au>   |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>   |  of excellence is a lethal habit"
PGP: finger fjh at 128.250.37.3         |     -- the last words of T. S. Garp.



More information about the developers mailing list