Download (direct link):
/* No problem if the conversion of ‘Qual’ binds ‘V’, This clause is just like I* Chapter 4, section 3,2.3 except that ‘substitute’ is done within ‘convdesc’, */ convqual ((Dl or D2) is qual (V, Qual), Results, Rule) :-
148 A PROLOG PROGRAM FOR CONVERTING D&Qs
convqual (Qual, Resl, Rulel), nonvai (V),
convdesc(Dl or D2, V, Res2, Rule2), union(Resl, Res2, Results), conjunct ([Rulel, Rule2], Rule)
/* If ‘V’ is still variable after doing ‘convqual’, then be careful! */ convqual ((Dl or D2) is qual(V, Qual), Results, Rule) : — convqual (Qual, Resl, Rulel), var (V),
convdisjunct (Dl or D2, V, Resl, Rulel, Results, Rule).
/* See 2.3 above. */
convdesc (Dl & D2, W, Results, Rule)
convdesc(Dl 01 D2, W, Results, Rule)
/* Since ‘V’ is already bound it is not affected by ‘substitute’, so ‘Dl’ and ‘D2’ /* are dealt with independently. Notice that ‘V’ is slipped into the result spec.; /* thus in 2..3 above, ‘place.custcode’ gets into the results, */ convdesc (Dl or D2, V, Results, (Rulel or Rule2)) : — convdesc (Dl, Wl, Resl, Rl), substitute (Wl, V, R2), conjunct ([Rl, R2], Rulel), convdesc(D2, W2, Res2, R3), substitute(W2, V, R4), conjunct ([R3, R4], Rule2), union([V| Resl], Res2, Results).
/* ‘VI’ is variable, and it is assumed that it gets bound by ‘convdesc’ (‘checkdisj’ /* checks this). Thus every occurrence of ‘VI’ has to be replaced by ‘V2’ so that /* the DESCRIPTION ‘D2’ can be converted subsequently. ‘VI’ is added to the /* result spec, ‘Resl’. It is likely that ‘Resl’ and ‘Res2’ (from the second /* conjunct) will have an overlap. Any redundancy should be eliminated by /* ‘union’. */
convdisjunct (Dl or D2, VI, Resl A, R1A, Results, (Rulel or Rule2)) !, mk (VI, V2, Resl A, Res2A), mk (VI, V2, Rl A, R2A), convdesc (Dl, VI, ReslB, RIB), conjunct ([Rl A, RIB], Rulel), union (Resl A, [VI | ReslB], Resl), checkdisj (VI, Dl),
convdisjunct (D2, V2, Res2A, R2A, Res2, Rule2), union (Resl, Res2, Results),
/* The last disjunct is converted just as in Chapter 4, section .3 2..3 except that /* ‘substitute’ is done automatically by PROLOG unification */ convdisjunct (Desc, V, Resl, Rulel, Results, Rule) : —
A PROLOG PROGRAM FOR CONVERTING D&Qs 149
convdesc(Desc, V, Res2, Rule2), conjunct ([Rule 1 |Rule2], Rule), union(Resl, Res2, Results), checkdisj (V, Desc).
checkdisj (VI, _) :— nonvar (VI), ! checkdisj (VI, Dl) : —
error (‘No constraints on variable in disjunct’, Dl is qual (VI, true))
/* This predicate could be improved (to recognise repeated rules, for example) */
conjunct ([true | Tail], Rule) :—!, conjunct(Tail, Rule).
conjunct ([Rule, true], Rule) :—!
conjunct ([Rulel, Rule2], Rulel & Rule2):—!
conjunct ([Rulel j Tail], Rule.3) :—!, conjunct (Tail, Rule2),
conjunct ([Rulel, Rule2], Rule3).
conjunct ([ ], true)
/* Union appends two lists dropping duplicated members */ union([H | T], LI, L2) idmemb(H,Ll),!,union(T, LI, L2), union ([HIT], LI, [H | L2]) !,union(T,Ll, L2) union([ ], L, L)
idmemb(Hl, [H2| J) :-Hl = =H2,! idmemb(X, [H | Tail]) idmemb (X, Tail).
/* Finally two nasty little predicates that unpick PROLOG structures, replace /* ‘VI’ with ‘V2’ wherever it occurs, and put them together again. */
mk(Vl, V2,W1,V2) :-Vl = = Wl,!.
mk(Vl, V2, X, X) (atomic(X); var(X)), !.
mk(Vl, V2,W1,W2) :-Wl= ., [H | Tl], ml (VI, V2, Tl, T2),
w: - .. [h i T2]
ml (VI, V2, [HI i Tl], [H2 | T2]) !,mk(Vl, V2, HI, H2), ml(VI, V2, Tl, T2).
The election and COPSE databases
1. THE QPROC ELECTION DATABASE
Built-in Domains: date
Entity Domains: constituency election marginality (person)
THE ELECTION AND COPSE DATABASES 151
1.2 Domain hierarchy
I ___________ area
Relation: Attribute: Domain:
person id person
person age year
person title title
constituency id constituency
constituency area area
constituency marginality marginality
constituency member person
election id election
election constituency constituency
election date date
marginality id marginality
marginality party party
candidate candidate person
candidate election election
candidate vote vote
candidate party party
152 THE ELECTION AND COPSE DATABASES
Theie aie four entities
Identifying Domain: Attribute:
Derived Relation: contest
2 THE COPSE (CUSTOMERS, ORDERS, PRODUCTS AND STOCK ENQUIRIES) DATABASE
2 1 Domains
Built-in Domains: date location measure person thing
Entity Domains: customer product order salesman stock