Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sun-barr!ccut!kyu-cs!nodoka.csce.kyushu-u!zhou From: zhou@hiromi.csce.kyushu-u.junet (Neng Fa Zhou) Newsgroups: comp.lang.prolog Subject: Re: Compiler smartness? Message-ID: Date: 20 Nov 90 05:59:19 GMT References: Sender: zhou@hiromi.csce.kyushu-u.ac.jp Distribution: comp Organization: Dept. of Comp. Sci. and Comm. Eng., Kyushu Univ., Fukuoka, Japan Lines: 51 In-reply-to: dowding@ai.sri.com's message of 16 Nov 90 22:01:50 GMT In article dowding@ai.sri.com (John Dowding) writes: > I was poking around in some old code, and came across the following > clauses (names changed to protect the inocent): > > foo(Term, Term):- > (var(Term) ; atomic(Term)), > !. > foo([Head|Tail], Result):- > ... > > > I was wondering if the following is more efficient: > > foo(Term, Term):- > var(Term), > !. > foo(Term, Term):- > atomic(Term), > !. > foo([Head|Tail], Result):- > ... Yes, it is more efficient than the original one. > > Are Prolog compilers (particularly, Quintus version 2.4) smart enough > index correctly on the 1st argument in either or both of these cases? > The compiler for the TOAM is smart enough to compile this program to efficient code. The compiler first infers modes for a program, then transforms the program to a canonical form based on the inferred modes. Suppose [d,f] is the mode of foo/2, the canonical form of the predicate is foo(Term, NewTerm):- var(Term), !, NewTerm = Term. foo(Term, NewTerm):- atomic(Term), !, NewTerm = Term. foo([Head|Tail], Result):- ... The code of this canonical form is efficient because (1) no choice point will be created, and (2) it does not contain instructions for the cuts. Neng-Fa Zhou Kyushu University