Download presentation
Presentation is loading. Please wait.
Published byMalcolm Brooks Modified over 7 years ago
1
List the Parts in a list Query findsupplier($BasicPart, Supplier)? which prints all the suppliers for a given part. database({psl(PartName:string, SL:list)}). export listeachSup($P, S). listeachSup(P, S) <- ps(P, SL), eachs(SL, S). each([H|L], H). each([H|L], S) <- each(L, S). Now explain and show how this last query will be rewritten by the compiler. Rewrite the rule as: each(SL, S) <- SL= [S|L], each(L, S). Then it is clear that this is treated as a left-linear rule
2
Reverse the order of a list
% Original list is L. We start with an empty L % We traverse the original list from beginning to end % and keep adding the elements at the head in the new list. % Reversing is complete when the list is completely traversed. %psl(top_tube,[cinelli, columbus, mavic]). psl(hub,[campagnolo,suntour]). database({psl(PartName:string, SL:list)}). rev1(Part, L, [ ]) <- psl(Part,L). rev1(P, L, [H|LR]) <- rev1(P, [H|L], LR). mygoal(P, LR) <- rev1(P, [ ], LR). export mygoal(P, LR). Compilation?
3
Reverse the order of a list
% Original list is L. We start with an empty L % We traverse the original list from beginning to end % and keep adding the elements at the head in the new list. % Reversing is complete when the list is completely traversed. %psl(top_tube,[cinelli, columbus, mavic]). psl(hub,[campagnolo,suntour]). database({psl(PartName:string, SL:list)}). rev1(Part, L, [ ]) <- psl(Part,L). rev1(P, L, [H|LR]) <- rev1(P, [H|L], LR). mygoal(P, LR) <- rev1(P, [ ], LR). export mygoal(P, LR). Compilation?
4
Another list-reverse (similar to Prolog’s)
% The original list is in the head of the rule. It will shrink while the body will grow. % wevtake the head of the list head of the rule and add itto the body % When the original list is emptied we return the other list as an answer. %psl(top_tube,[cinelli, columbus, mavic]). psl(hub,[campagnolo,suntour]). database({psl(PartName:string, SL:list)}). revList(Part, LR) <- psl(Part,L), rev2(L, [], LR). rev2([H|L], Rest, LR) <- rev2(L, [H|Rest], LR). rev2( [ ], L, L). export revList(Part, LR). Compilation?
5
tree leaf
atnode(Tno, 0) <- tuples(Tno, 0). atnode(Tno, C) <- atnode(Tno, K), tuple(Tno, Col, Val), tree(K, Col, Val, C). dec(Tno, YN) <- atnode (Tno, K), leaf(K, YN).
6
Functional Dependencies
ls(a,f1). rs(f1,b). % f1: a -> b ls(b,f2). rs(f2,c). % f2: b -> c ls(c,f3). ls(d,f3). rs(f3,e). % f3: cd -> e %Write a DeAL program to determine if some FD, BD-->E is implied: % for the problem at hand we have to find E belongs to set S= BD+ Initial: S:= {B, D} repeat: if left side of fd is contained in S then add the right side to S. till: no more % Start by determining how many attributes each FD has. Then you might %want to write recursive rules that add the right side when enough of its %left sides have been inferred
7
Functional Dependencies (cont.)
ls(a,f1). rs(f1,b). % f1: a -> b ls(b,f2). rs(f2,c). % f2: b -> c ls(c,f3). ls(d,f3). rs(f3,e). % f3: cd -> e % Start by determining how many attributes each FD has. Then you might %want to write recursive rules that add the right side when enough of its %left sides have been inferred solved(b). solved(d). no_attr(F,count<A>) <- ls(A,F). svld(A) <- solved(A). lscs(F, mcount<A>) <- svld(A), ls(A, F). svld(E) <- lscs(F, C), no_attr(F, C), rs(F, E). export (X). query svld(e).
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.