Path: utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!mailrus!iuvax!jwmills From: jwmills@iuvax.cs.indiana.edu (Jonathan Mills) Newsgroups: comp.lang.prolog Subject: Re: incrementing values Keywords: optimization, mangle Message-ID: <34699@iuvax.cs.indiana.edu> Date: 2 Feb 90 12:43:19 GMT References: <17467@megaron.cs.arizona.edu> <31462@shemp.CS.UCLA.EDU> Reply-To: jwmills@iuvax.cs.indiana.edu (Jonathan Mills) Organization: Indiana University, Bloomington Lines: 34 > Saumya K. Debray writes: > This is an excerpt from an article posted recently to another newsgroup: >| increment(Variable) :- >| retract(value(Variable, OldValue)), >| NewValue is OldValue + 1, >| assert(value(Variable, NewValue)). > Michael Coleman writes: > >Every once in a while something comes along to help us remember why languages >like COBOL, Fortran, and friends still reign supreme. > It's not _quite_ as bad as that. Retract/modify/assert sequences such as this can be optimized to destructive assignment. A clever implementation can turn the whole clause into an indexed increment in 68K machine language. Prologs that don't implement such an optimization may provide a destructive assignment operator so the user can at least avoid retract & assert. Hidden away in ALS Prolog is such an operator, appropriately named "mangle". Thus increment/1 could be implemented as: increment(Variable) :- clause(VarList,true), arg(Variable,VarList,Value), NewValue is Value + 1, mangle(Variable,VarList,NewValue). However, mangle has a clean-up cost associated with it. The ALS manual warns that mangle is not necessarily more efficient than term-copying for that reason. I suspect, though, that it is _much_ more efficient than retract/modify/assert.