• Aucun résultat trouvé

23 Property Lists

Dans le document 2 The Atom Table and the Number Table (Page 47-50)

Ordinary atoms have values, and it is often convenient to imagine that an ordinary atom has several values. This can be accomplished by making a list of these values and using this list asthe value whose elements are the desired several values. Frequently, however, these multiple values are used to encode properties. This is so common that LISP provides special features to accomodate properties.

Abstractly, a property is a function. LISP only provides for properties which apply to ordinary atoms and whose values are S-expressions, and it establishes and uses a special set of lists to tabulate input-output pairs of such properties. SupposeAis an ordinary atom andg is an abstract property function. Then the value of g on A is called the g-property-value of A. Rather than computing a particular property-value of an atom when it is needed, LISP provides a means to record such property-values along with the corresponding ordinary atom, so that a property-value may be retrieved rather than computed.

Each ordinary atomAhas an associated list provided called theproperty listofA. The typed-pointer to this list is stored in the plist field of the ordinary atom A. This list may be thought of as an

“alternate value” of A. Generally the property list of an ordinary atomAis either NIL, or it is a list of dotted-pairs. Each such dotted-pair,(p . w), in the property list forAis interpreted as meaning that the value of property p on A is w. Thus properties and property-values are represented by S-expressions. In fact, usually properties are represented by ordinary atoms.

The following functions are provided in order to aid in handling property lists.

• PUTPROP – function with a side-effect

v[(PUTPROP a p w)] =v[a], and the property, property-value dotted-pair (v[p] . v[w]) is in-serted in the property list for the atom v[a]. If another duplicate property, property-value dotted-pair for the property v[p] and the property-value v[w] is present, it is removed. v[a]

must be an ordinary atom, andv[p] and v[w] are arbitrary S-expressions.

• GETPROP – function

v[(GETPROP a p)] = the current property-value which is dotted with the property v[p] on the property list for the ordinary atomv[a]. If a dotted-pair for the property v[p] does not exist on the property list of v[a], then NIL is returned.

• REMPROP – function with a side-effect

v[(REMPROP a p w)] =v[a], and the property, property-value dotted-pair(v[p] . v[w]) for the propertyv[p] is removed from the property list of the ordinary atomv[a] if such a dotted-pair exists on the property list forv[a].

Exercise 23.1: Define a special form calledSPUTPROPanalogous toPUTPROP which does not have its arguments evaluated.

Solution 23.1: (SETQ SPUTPROP (SPECIAL (A P W) (PUTPROP A P W))).

The functionsPUTPROP,GETPROP, andREMPROPcan all be defined in terms of the basic builtin functions

GETPLIST and PUTPLIST, where v[(GETPLIST a)] = the property list for the ordinary atom v[a], and wherev[(PUTPLIST a s)] =v[a], with the side-effect that the property list of the ordinary atomv[a]

is replaced with the list v[s]. Now PUTPROP,GETPROP, and REMPROPare definable as follows.

(SETQ GETPROP (LAMBDA (A P)

(ASSOC (GETPLIST A) P))) (SETQ ASSOC (LAMBDA (L P)

(COND ((NULL L) NIL)

(T (COND ((EQUAL P (CAR (CAR L))) (CDR (CAR L))) (T (ASSOC (CDR L) P)))))))

(SETQ REMPROP (LAMBDA (A P W)

(PUTPLIST A (NPROP NIL (GETPLIST A) (CONS P W))))) (SETQ NPROP (LAMBDA (H L P)

(COND ((NULL L) (REVERSE H))

((EQUAL P (CAR L)) (APPEND (REVERSE H) (CDR L))) (T (NPROP (CONS (CAR L) H) (CDR L) P))))) (SETQ PUTPROP (LAMBDA (A P W)

(PUTPLIST A (CONS (CONS P W)

(GETPLIST (REMPROP A P W))))))

Exercise 23.2: Why don’t we define PUTPROPas

(LAMBDA (A P W)

(COND ((EQ (GETPROP A P) W) A) (T (PUTPLIST (CONS (CONS P W)

(GETPLIST A))))))?

Exercise 23.3: Can the same property,value pair occur on the same property list several times?

Solution 23.3: No, not if only PUTPROP is used to build property lists. And, in fact, REMPROP

assumes duplicates are not present.

Exercise 23.4: Why isREVERSE used inNPROP? Is it necessary?

Exercise 23.5: Give definitions of PUTPROP, GETPROP, and REMPROP, so that properties can be relations rather than just functions. Thus multiple (p . w) dotted-pairs occuring on the same property list with the same p-value and differing w-values are to be handled properly.

Solution 23.5: Only GETPROP needs redefinition, so that it returns a list of property-values.

It may be a beneficial practice to keep all properties functional however. This is no real hard-ship, since, for example, (COLORS . RED), (COLORS . BLUE), and (COLORS . GREEN) can be recast as

(COLORS . (RED BLUE GREEN)).

48 23 PROPERTY LISTS Exercise 23.6: One useful device that is commonly used is to specify class subset relations with T-valued properties. For example, we might execute:

(PUTPROP ’MEN ’MORTAL T), (PUTPROP ’MAN ’MEN T), and (PUTPROP ’SOCRATES ’MAN T).

But note that v[(GETPROP ’SOCRATES ’MORTAL)] =NIL.

Write a LISP function LINKGETPROP which will trace all property lists of atom properties with the associated value T reachable from the property list of an input atom A in order to find the specified property-value pair (P,T)and returnT when this pair is found. The value NIL is to be returned when no chain leads to the sought-for property Pwith a Tvalue.

Solution 23.6:

(SETQ LINKGETPROP (LAMBDA (A P)

(COND ((EQ (GETPROP A P) T) T) (T (SEARCH (GETPLIST A) P))))) (SETQ SEARCH (LAMBDA (L P)

(COND ((NULL L) L)

((AND (EQ (CDR (CAR L)) T) (ATOM (CAR (CAR L)))

(EQ (LINKGETPROP (CAR (CAR L)) P) T)) T) (T (SEARCH (CDR L) P)))))

Exercise 23.7: Write a LISP functionCKPROPsuch that(CKPROP a p w)returnsTif(v[p] . v[w])

is on the property list for the atom v[a], and NIL otherwise. Now write a generalized form of

CKPROPcalledTCPROPwhich is defined such that(TCPROP a p w)returnsTif there exists a sequence of atoms m0,m1,. . .,mk, withk≥1, such that v[a] =m0,v[w] =mk, and (v[p] . mi+1) is on the property list ofmi fori= 0,1, . . . , k−1. “TC” stands for transitive closure.

The property list functions defined above, especially REMPROP, and hence PUTPROP, are slow, and moreover they can create lots of garbage unreachable list-nodes. It would be more efficient if we could just change property lists as required, rather than rebuild them with CONSing each time a

REMPROP is done.

There are two low-level functions with side-effects defined in LISP which provide the ability to change existing list-nodes which have been created with CONS. These functions are RPLACA and

RPLACD.

• RPLACA – function with a side-effect

v[(RPLACA x y)] =v[x], afterthecar field of the list node representing the dotted-pairv[x] is changedto the typed-pointer v[y].

• RPLACD – function with a side-effect

v[(RPLACD x y)] =v[x], after the cdr field of the list node representing the dotted-pairv[x] is changedto the typed-pointer v[y].

NowREMPROP can be programmed as follows.

(SETQ REMPROP (LAMBDA (A P W)

(PUTPLIST A (NAX (GETPLIST A) (CONS P W))))) (SETQ NAX (LAMBDA (L P)

(COND ((NULL L) NIL)

((EQUAL (CAR L) P) (CDR L)) (T (DO (NX L P) L))))) (SETQ NX (LAMBDA (L P)

(COND ((NULL (CDR L)) NIL)

((EQUAL P (CAR (CDR L))) (RPLACD L (CDR L))) (T (NX (CDR L) P)))))

Exercise 23.8: CouldNXbe defined with an empty formal argument list ()rather than (L P), and be called as (NX) inNAX?

Note this version ofREMPROPcan only be safely used when we can guarantee that the backbone nodes of property lists are not shared within other list structures.

Exercise 23.9: Program a version ofAPPEND, calledNCONC, which usesRPLACD to append one list onto another by physical relinking.

Dans le document 2 The Atom Table and the Number Table (Page 47-50)

Documents relatifs