Newsgroups: comp.lang.prolog
Path: cantaloupe.srv.cs.cmu.edu!das-news2.harvard.edu!fas-news.harvard.edu!newspump.wustl.edu!news.ecn.bgu.edu!news.moneng.mei.com!howland.reston.ans.net!news.sprintlink.net!news.ecrc.de!ecrc!acrab60!thom
From: thom@ecrc.de (Thom Fruehwirth)
Subject: Re: speed of prolog
Message-ID: <D9v04L.AxK@ecrc.de>
Summary: as fast
Keywords: constraint handling rules
Sender: news@ecrc.de
Reply-To: thom@ecrc.de
Organization: European Computer-Industry Research Centre GmbH.
References: <D9szyK.H10@ecrc.de>
Date: Thu, 8 Jun 1995 14:55:32 GMT
Lines: 67

I ran the example proposed by Van Caneghem 
and reported in this newsgroup by C. Holzbaur
using the 'math-lazy' or 'math-eager' solvers 
written in Constraint Handling Rules (CHRs)
(a special-purpose high-level rule language 
for writing constraint solvers)
available in ECLiPSe 3.5.1. The speed
compares well with the other approaches.
The real catch is how simple it is in CHRs
to write the code - see below.

I got the following results also on a 50 MHz Sparc:

       |	 ECLiPSe 3.5.1 solvers:
     n | lib(r)	 CHR lib 'math-eager'  CHR lib 'math-lazy'
   ----+--------------------------------------------------
    10 |    0.19	  0.24		    0.25
    20 |    1.82	  1.96		    1.93
    50 |   60.42	 50.59		   49.89
   100 |  974.67	844.23     	  839.27

This means that high-level code can pay off
(because this benchmark depends mostly on the
speed of rational number multiplication ;-) -
in general ECLiPSe 3.5.1 lib(r) is faster than 
the CHR libs).

The real catch is how simple it is in CHRs
to write the code - here it is for 'math-lazy'
- just four rules (excluding rules for inequations 
and auxiliary predicates for adding and multiplying)!
The code works for floats and rational numbers.
The above problem can be solved with floats (at the
expense of precision) - then the complexity is
lower. For n=100 the running time is 189.89 secs.

An equation is represented by the constraint predicate

	List_of_Monomials equals Constant

-------------------------------
% An empty list of monomials must be equal to zero
[] equals C1 <=> zero(C1).

% If there is just one variable left, unify it with is value
[X*C2] equals C1 <=> nonground(X),nonzero(C2) | 
	is_div(C1,C2,X).  % is_div is division with some error-correction for floats

% If a variable has been unified, remove it from the equation
P0 equals C1 <=> delete(X*C2,P0,P),ground(X) | 
	is_mul(X,C2,XC2), % is_mul multiplies with some error-correction for floats
	C is XC2+C1, 
	P equals C.       % simplified equation

% eliminate a variable in the second equation using the first equation
[X*C2X|PX] equals C1X \ [X*C2|P] equals C1 <=>  
	is_div(C2,C2X,CX), 
	mult_const(eq0(C1X,PX),CX,P2),	
        add_eq0(eq0(C1,P),P2,eq0(C3,P3)),
	sort1(P3,P4),
	P4 equals C3.	  % new "second" equation

-----------------------------------
thom
www: http://www.ecrc.de/staff/thom/


