Path: utzoo!utgpu!water!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!bloom-beacon!mit-eddie!ll-xn!ames!pasteur!ucbvax!decwrl!sun!pitstop!sundc!seismo!uunet!kddlab!icot32!icot21!chik From: chik@icot21.icot.junet (Chikayama Takashi) Newsgroups: comp.lang.prolog Subject: Re: common subterms Message-ID:Date: 13 Jul 88 11:23:28 GMT References: <9671@lll-winken.llnl.gov> <6899@burdvax.PRC.Unisys.COM> <6206@megaron.arizona.edu> Sender: chik@icot32.icot.junet Reply-To: chikayama@icot.junet Organization: Institute for New Generation Computer Technology, Tokyo, Japan. Lines: 57 In-reply-to: debray@arizona.edu's message of 13 Jul 88 02:42:21 GMT debray@arizona.edu (Saumya Debray) writes: > While common subterm factoring looks very attractive, one problem with > doing it incautiously is that indexing capabilities can be weakened > because a nonvariable term is being pulled from the head into the body > (as in the above example). The resulting choice point creation may well > wipe out any benefits obtained from creating fewer structures on the heap. I see no reason why the compiler cannot index clauses properly. This may be true if factoring should be done in the source level BEFORE the compilation. However, the compiler can be as clever as generating indexing code based on the original source program and then factorize common structures when generating each clause. By the way, the PSI machine has an extended WAM instructions named "get_structured_constant" and "put_structured_constant". These have two arguments, an argument register and a pointer to a structure allocated in the CODE AREA in the compilation (or rather, program loading) phase. Its spec is similar to "get_value" or "put_value" except that one of the arguments is a constant structure. The same applies to the "unify_structured_constant" instruction. Consider, for example, a predicate such as: roman(N, R) :- arg(N, f(i, ii, iii, iv, v, vi, vii, viii, ix, x), R). Using only the original WAM instructions, it will be compiled to something like: put_value A2, A3 put_functor f/10, A2 unify_constant i unify_constant ii ... unify_constant x execute arg/3 which is, at least to me, never acceptable. Of course, the call to the built-in predicate arg/3 may be compiled out, but I'm not interested in it here. The "put_structured_constant" instruction can substitute the put_functor instruction and 10 following unify_constant instructions. For this specific example, writing the predicate as: roman(1, i). roman(2, ii). ... roman(10, x). may do almost as good, but the first program looks more compact and probably more efficient when "put_structured_constant" is used. Using this, if there are many common structures, the same structure in the code area can be shared, minimizing the code size. It won't help, however, when the common structure contains variables. T.Chikayama (ICOT)