1% ----------------------------------------------------------------------
2% BEGIN LICENSE BLOCK
3% Version: CMPL 1.1
4%
5% The contents of this file are subject to the Cisco-style Mozilla Public
6% License Version 1.1 (the "License"); you may not use this file except
7% in compliance with the License.  You may obtain a copy of the License
8% at www.eclipse-clp.org/license.
9%
10% Software distributed under the License is distributed on an "AS IS"
11% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.  See
12% the License for the specific language governing rights and limitations
13% under the License.
14%
15% The Original Code is  The ECLiPSe Constraint Logic Programming System.
16% The Initial Developer of the Original Code is  Cisco Systems, Inc.
17% Portions created by the Initial Developer are
18% Copyright (C) 1991-2006 Cisco Systems, Inc.  All Rights Reserved.
19%
20% Contributor(s): ECRC GmbH
21% Contributor(s): IC-Parc, Imperal College London
22%
23% END LICENSE BLOCK
24%
25% System:	ECLiPSe Constraint Logic Programming System
26% Version:	$Id: numbervars.pl,v 1.3 2010/03/15 01:55:15 jschimpf Exp $
27% ----------------------------------------------------------------------
28
29%
30% SEPIA PROLOG LIBRARY MODULE
31%
32% IDENTIFICATION:       numbervars.pl
33%
34% AUTHOR:               Joachim Schimpf
35%
36% CONTENTS:             numbervars/3
37%
38
39:- module(numbervars).
40
41:- comment(categories, ["Algorithms","Compatibility"]).
42:- comment(summary, "C-Prolog style numbervars predicate").
43:- comment(author, "Joachim Schimpf, ECRC Munich").
44:- comment(copyright, "Cisco Systems, Inc").
45:- comment(date, "$Date: 2010/03/15 01:55:15 $").
46:- comment(desc, html("
47    Implements the numbervars(Term, From, To) predicate of C-Prolog.  Term
48    is any term, From and To are integer numbers.  All variables in Term
49    are instantiated to terms of the form
50    <PRE>
51	$VAR(N)
52    </PRE>
53    where N is an integer number.  The first encountered variable will be
54    coded by the number From, on exit To is instantiated to the next
55    unused number.
56    <P>
57    This predicate can thus be used to encode nonground term using a
58    ground representation.  Note that metaterms can be used for the same
59    purpose, but their use is both more efficient and more general,
60    because the variables are not actually instantiated and so they can be
61    used again as variables when needed.
62    ")).
63
64:- export numbervars/3.
65
66
67numbervars('$VAR'(N), N, N1) :- !,
68	N1 is N + 1.
69numbervars(Term, N, Next) :-
70	arity(Term, Arity),
71	numbervars(0, Arity, Term, N, Next).
72
73numbervars(Arity, Arity, _, N, Next) :- !, N = Next.
74numbervars(I, Arity, Term, N0, N) :-
75	I1 is I + 1,
76        arg(I1, Term, Arg),
77        numbervars(Arg, N0, N1),
78        numbervars(I1, Arity, Term, N1, N).
79
80