200 likes | 322 Views
LING 388: Language and Computers. Sandiway Fong Lecture 5: 9/5. Administrivia. Homework 1 reviewed today This Thursday Computer Lab class meet in Social Sciences 224 Homework 2. Use database bird(tweety). “ tweety is a bird ” bird(woody). “ woody is a bird ”
E N D
LING 388: Language and Computers Sandiway Fong Lecture 5: 9/5
Administrivia • Homework 1 • reviewed today • This Thursday • Computer Lab class • meet in Social Sciences 224 • Homework 2
Use database bird(tweety). “tweety is a bird” bird(woody). “woody is a bird” (2pts) What do the following queries return as an answer? The queries ?- \+ \+ bird(tweety). ?- \+ bird(X). (6pts) Give the logical reasoning for each answer ?- \+ \+ bird(tweety). Yes logic ?- trace. Yes [trace] ?- \+ \+ bird(tweety). Call: (9) bird(tweety) ? creep Exit: (9) bird(tweety) ? creep Yes (... doesn’t really give enough information) \+ \+ bird(tweety) is true if \+ bird(tweety) is false if bird(tweety) is true but bird(tweety) is true since it’s in the database so \+ bird(tweety) is false and so \+ \+ bird(tweety) is true that’s why Prolog says Yes Homework 1 Review
Use database bird(tweety). “tweety is a bird” bird(woody). “woody is a bird” (2pts) What do the following queries return as an answer? The queries ?- \+ \+ bird(tweety). ?- \+ bird(X). (6pts) Give the logical reasoning for each answer ?- \+ bird(X). No logic [trace] ?- \+ bird(X). Call: (8) bird(_G312) ? creep Exit: (8) bird(tweety) ? creep No (... shows us X gets instantiated as tweety) \+ bird(X) is true if bird(X) is false but bird(X) is true when X=tweety so \+ bird(X) is false that’s why Prolog says No Homework 1 Review
Today’s Topics • More on recursion • Last time • we defined last/2 and len/2 to pick the last element of a list and compute the length of a list, respectively. • last([X],X). (base case) • last([X|L],Y) :- last(L,Y). (recursive case) • len([],0). (base case) • len([X|L],N) :- len(L,M), N is M+1. (recursive case)
Today’s Topics • More on recursion • Look at taking apart and putting lists together • And symbols (atoms)
Building and Taking Atoms Apart • Built-in predicate atom_chars/2 has two modes of usage • atom_chars(symbol,list) • takes names apart • ?- atom_chars(will,X). • X is the list of characters representing will • builds names from a list of characters • ?- atom_chars(X,[’J’,o,h,n]). • use quotes around capitalized J to avoid J being interpreted as a variable • ?- atom_chars(X,[w,o,n,’’’’,t]). • ’’’’ denotes the single quote (or ’\’’)
more example queries ?- atom_chars(has,[h,a,s]). Yes ?- atom_chars(will,[w,X,l,l]). X = i more example queries ?- atom_chars(X,Y). what happens here? ?- atom_chars(X,[J,o,h,n]). Building and Taking Atoms Apart
append/3 is a built-in predicate in SWI-Prolog defined as follows: append(L1,L2,L3) is true if list L3 is the linear concatenation of lists L1 and L2 [1,2] append [3,4] = [1,2,3,4] Example ?- append([1,2],[3,4],[1,2,3,4]). Note append/3 has multiple modes of usage i.e. it can be used to take apart as well as concatenate lists let’s run example queries ?- append([1],[2,3],X). ?- append(X,Y,[1,2]). ?- append(_,[X],[1,2,3]). ?- append(X,Y,Z). Note: the underscore character ‘_’ is a variable with no name it’s a variable but we don’t care about its final value no binding will be reported by the interpreter for underscores Building and Taking Lists Apart
append/3 can be defined recursively as follows: app([],L,L). Base case app([X|L1],L2,[X|L3]) :- app(L1,L2,L3). Recursive case we use the predicate name app/3 here to avoid a naming clash append/3 is already defined (built-in) Building and Taking Lists Apart
app([],L,L). Base case app([X|L1],L2,[X|L3]) :- app(L1,L2,L3). Recursive case Base case says: if we concatenate [] with an arbitrary list L, we get L Recursive case says: if I have a list headed by X and the tail is L1, i.e. [X|L1], and I want to concatenate that to another list L2 the answer is going to be a list headed by X the tail is some other list L3 where L3 is defined as the concatenation of L1 and L2 Building and Taking Lists Apart Example ?-app([1,2,3],[4,5],L). L = [1|L3] where L3 is given by ?-app([2,3],[4,5],L3).
app([],L,L). Base case app([X|L1],L2,[X|L3]) :- app(L1,L2,L3). Recursive case Base case says: if we concatenate [] with an arbitrary list L, we get L Recursive case says: if I have a list headed by X and the tail is L1, i.e. [X|L1], and I want to concatenate that to another list L2 the answer is going to be a list headed by X the tail is some other list L3 where L3 is defined as the concatenation of L1 and L2 Building and Taking Lists Apart Example ?-app([1,2,3],[4,5],L). L = [1|L3] where L3 is given by ?-app([2,3],[4,5],L3). L3 = [2|L3’] where L3’ is given by ?-app([3],[4,5],L3’).
app([],L,L). Base case app([X|L1],L2,[X|L3]) :- app(L1,L2,L3). Recursive case Base case says: if we concatenate [] with an arbitrary list L, we get L Recursive case says: if I have a list headed by X and the tail is L1, i.e. [X|L1], and I want to concatenate that to another list L2 the answer is going to be a list headed by X the tail is some other list L3 where L3 is defined as the concatenation of L1 and L2 Building and Taking Lists Apart Example ?-app([1,2,3],[4,5],L). L = [1|L3] where L3 is given by ?-app([2,3],[4,5],L3). L3 = [2|L3’] where L3’ is given by ?-app([3],[4,5],L3’). L3’ = [3|L3”] where L3” is given by ?-app([],[4,5],L3”).
app([],L,L). Base case app([X|L1],L2,[X|L3]) :- app(L1,L2,L3). Recursive case Base case says: if we concatenate [] with an arbitrary list L, we get L Recursive case says: if I have a list headed by X and the tail is L1, i.e. [X|L1], and I want to concatenate that to another list L2 the answer is going to be a list headed by X the tail is some other list L3 where L3 is defined as the concatenation of L1 and L2 Building and Taking Lists Apart Example ?-app([1,2,3],[4,5],L). L = [1|L3] where L3 is given by ?-app([2,3],[4,5],L3). L3 = [2|L3’] where L3’ is given by ?-app([3],[4,5],L3’). L3’ = [3|L3”] where L3” is given by ?-app([],[4,5],L3”). L3” = [4,5] (Base case)
app([],L,L). Base case app([X|L1],L2,[X|L3]) :- app(L1,L2,L3). Recursive case Building and Taking Lists Apart Example ?- app(L1,L2,[1,2]). matches app([],L,L). Base case L1 = [], L2 = [1,2] 1st answer
app([],L,L). Base case app([X|L1],L2,[X|L3]) :- app(L1,L2,L3). Recursive case Building and Taking Lists Apart Example ?- app(L1,L2,[1,2]). matches app([],L,L). Base case L1 = [], L2 = [1,2] head of recursive case app([X|L1’],L2’,[X|L3’]) L1 = [1|L1’] L2 = L2’ [1,2] = [X|L3’] L3’=[2] ?- app(L1’,L2’,L3’). ?- app(L1’,L2’,[2]).
app([],L,L). Base case app([X|L1],L2,[X|L3]) :- app(L1,L2,L3). Recursive case Building and Taking Lists Apart Example ?- app(L1,L2,[1,2]). matches app([],L,L). Base case L1 = [], L2 = [1,2] head of recursive case app([X|L1’],L2’,[X|L3’]) L1 = [1|L1’] L2 = L2’ [1,2] = [X|L3’] L3’=[2] ?- app(L1’,L2’,L3’). ?- app(L1’,L2’,[2]). matches app([],L,L). Base case L1’ = [] L2’ = L = [2] 2nd answer L1 = [1] L2 = [2]
app([],L,L). Base case app([X|L1],L2,[X|L3]) :- app(L1,L2,L3). Recursive case Building and Taking Lists Apart Example ?- app(L1,L2,[1,2]). matches app([],L,L). Base case L1 = [], L2 = [1,2] head of recursive case app([X|L1’],L2’,[X|L3’]) L1 = [1|L1’] L2 = L2’ [1,2] = [X|L3’] L3’=[2] ?- app(L1’,L2’,L3’). ?- app(L1’,L2’,[2]). matches app([],L,L). Base case L1’ = [] L2’ = L = [2] Example contd. ?- app(L1’,L2’,[2]). matches head of recursive case app([X|L1”],L2”,[X|L3”]) L1’ = [2|L1”] L2’ = L2” [2] = [X|L3”] L3” =[] ?- app(L1”,L2”,L3”). ?- app(L1”,L2”,[]).
app([],L,L). Base case app([X|L1],L2,[X|L3]) :- app(L1,L2,L3). Recursive case Building and Taking Lists Apart Example ?- app(L1,L2,[1,2]). matches app([],L,L). Base case L1 = [], L2 = [1,2] head of recursive case app([X|L1’],L2’,[X|L3’]) L1 = [1|L1’] L2 = L2’ [1,2] = [X|L3’] L3’=[2] ?- app(L1’,L2’,L3’). ?- app(L1’,L2’,[2]). matches app([],L,L). Base case L1’ = [] L2’ = L = [2] 3rd answer L1 = [1,2] L2 = [] Example contd. ?- app(L1’,L2’,[2]). matches head of recursive case app([X|L1”],L2”,[X|L3”]) L1’ = [2|L1”] L2’ = L2” [2] = [X|L3”] L3” =[] ?- app(L1”,L2”,L3”). ?- app(L1”,L2”,[]). matches app([],L,L). Base case L1” = [] L2”=L=[]
app([],L,L). Base case app([X|L1],L2,[X|L3]) :- app(L1,L2,L3). Recursive case Building and Taking Lists Apart Example ?- app(L1,L2,[1,2]). has 3 answers L1 = [], L2 = [1,2] L1 = [1], L2 = [2] L1 = [1,2], L2 = [] in each case it is true that L1 concatenated with L2 is [1,2]