new module library/prolog.m

Fergus Henderson fjh at cs.mu.oz.au
Tue May 13 19:52:44 AEST 1997


Tom has already reviewed most of this,
but I've made a few additional changes since then,
so you may want to take another glance, Tom.

library/std_util.m:
	Move arg/3 and det_arg/3 into prolog.m.
	Change argument/3 and det_argument/3 from predicates to functions.

library/prolog.m:
	New file, contains Prolog compatibility hacks.
	Change the definition of arg/3 so that we define prolog__arg/3
	rather than overriding the builtin Prolog arg/3.

library/library.m:
	Add `import_module prolog' declaration.

cvs diff: Diffing .
Index: library.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/library/library.m,v
retrieving revision 1.31
diff -u -r1.31 library.m
--- library.m	1996/10/23 13:30:54	1.31
+++ library.m	1997/05/13 09:21:54
@@ -23,6 +23,7 @@
 :- import_module require, set, set_bbbtree, set_ordlist, set_unordlist, stack.
 :- import_module std_util, string, term, term_io, tree234, uniq_array, varset.
 :- import_module store, rbtree, parser, lexer, ops, time.
+:- import_module prolog.
 
 :- pred library__version(string::out) is det.
 
cvs diff: prolog.m is a new entry, no comparison available
Index: std_util.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/library/std_util.m,v
retrieving revision 1.86
diff -u -r1.86 std_util.m
--- std_util.m	1997/05/01 09:23:00	1.86
+++ std_util.m	1997/05/13 09:39:02
@@ -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.
 
 %-----------------------------------------------------------------------------%
 %-----------------------------------------------------------------------------%
@@ -323,16 +318,16 @@
 	%
 :- pred functor(T::in, string::out, int::out) is det.
 
-	% argument(Data, ArgumentIndex, Argument)
+	% argument(Data, ArgumentIndex) = Argument
 	% 
 	% Given a data item (Data) and an argument index
 	% (ArgumentIndex), starting at 0 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, greater than or
 	% equal to the arity of the functor or lower than 0 -- argument/3
-	% fails.  The argument has the type univ. 
+	% fails.  The argument returned has the type univ. 
 	%
-:- pred argument(T::in, int::in, univ::out) is semidet.
+:- func argument(T::in, int::in) = (univ::out) is semidet.
 
 	% det_argument(ArgumentIndex, Data, Argument)
 	% 
@@ -343,32 +338,7 @@
 	% equal to the arity of the functor or lower than 0 --
 	% det_argument/3 aborts. 
 	%
-:- 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.
+:- func det_argument(T::in, int::in) = (univ::out) is det.
 
 	% expand(Data, Functor, Arity, Arguments) 
 	% 
@@ -2405,7 +2375,7 @@
 
 }").
 
-:- pragma c_code(argument(Type::in, ArgumentIndex::in, Argument::out),
+:- pragma c_code(argument(Type::in, ArgumentIndex::in) = (Argument::out),
 		will_not_call_mercury, " 
 {
 	ML_Expand_Info info;
@@ -2450,17 +2420,9 @@
 
 }").
 
-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) :-
+det_argument(Type, ArgumentIndex) = Argument :-
 	(
-		argument(Type, ArgumentIndex, Argument0)
+		argument(Type, ArgumentIndex) = Argument0
 	->
 		Argument = Argument0
 	;

%---------------------------------------------------------------------------%
% 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.

%-----------------------------------------------------------------------------%
:- module prolog.
:- interface.
:- import_module int, std_util.

% 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.
:- import_module require.

/*********
% !/0 and !/2 currently defined in mercury_builtin.m, for historical reasons.
!.
!(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 \= (<).

% we use a module qualifier here to avoid
% overriding the builtin Prolog version
'prolog__=..'(Term, Functor - Args) :-
	expand(Term, Functor, _Arity, Args).

% we use a module qualifier here to avoid
% overriding the builtin Prolog version
prolog__arg(ArgumentIndex, Type, argument(Type, ArgumentIndex - 1)).

det_arg(ArgumentIndex, Type, Argument) :-
	( arg(ArgumentIndex, Type, Arg) ->
		Argument = Arg
	;
		error("det_arg: arg failed")
	).

%-----------------------------------------------------------------------------%

-- 
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