ling 388 language and computers lecture 7 9/23/03 sandiway fong

21
LING 388 Language and Computers Lecture Lecture 7 7 9/23/03 9/23/03 Sandiway FONG Sandiway FONG

Post on 21-Dec-2015

219 views

Category:

Documents


0 download

TRANSCRIPT

LING 388Language and Computers

Lecture Lecture 77

9/23/039/23/03

Sandiway FONGSandiway FONG

Administrivia

ReminderReminder ThursdayThursday

Computer Lab Class Part 2…Computer Lab Class Part 2…

Exercise 1: Finite State Automata (FSA)

s x

y

aa

b

b

L = {aL = {a++bb++}}

Exercise 1

From Lecture 4From Lecture 4…… A possible Prolog encoding strategy:A possible Prolog encoding strategy:

Define one predicate for each stateDefine one predicate for each state• NName of state = name of predicateame of state = name of predicate• Arity of predicate = 1Arity of predicate = 1

– taking one argument (the input string)taking one argument (the input string)

Exercise 1

Basic Idea 1Basic Idea 1:: TTreat the input string as a listreat the input string as a list Consume one input character corresponding to Consume one input character corresponding to

transition arc from state, andtransition arc from state, and Call next state with remaining input stringCall next state with remaining input string

Basic Idea 2Basic Idea 2:: For end state(s), match empty inputFor end state(s), match empty input

i.e. we can stop only when all input has been i.e. we can stop only when all input has been consumed and we’re in an end stateconsumed and we’re in an end state

Exercise 1

• Machine for L = {aMachine for L = {a++bb++}} State s: State s:

s([a|L]) :- x(L).s([a|L]) :- x(L).match input string beginning with match input string beginning with aa and call state and call state xx with with

remainder of inputremainder of input State x:State x:

x([a|L]) :- x(L).x([a|L]) :- x(L). x([b|L]) :- y(L).x([b|L]) :- y(L).

State y: (end state)State y: (end state) y([]).y([]). y([b|L]) :- y(L). y([b|L]) :- y(L).

Exercise 1 Consult FSAConsult FSA Turn on tracing facilityTurn on tracing facility

?- trace.?- trace. Run query step-by-stepRun query step-by-step

?- s([a,a,b,b]).?- s([a,a,b,b]). Run query Run query

?- s([a,b,a]).?- s([a,b,a]). Run queryRun query

?- s([a,X]).?- s([a,X]).

Exercise 1

Homework Question (A):Homework Question (A): What and how many answers does the query What and how many answers does the query

?- s([X,Y,Z]).?- s([X,Y,Z]).rreturn?eturn?

Homework Question (B):Homework Question (B): If the clauseIf the clause

x([a|L]) :- x(L).x([a|L]) :- x(L).is removed from the program, what language is removed from the program, what language

does the revised FSA accept?does the revised FSA accept?

Exercise 2: Finite State Automata (FSA)

Also from Lecture 4Also from Lecture 4…… Formally:Formally:

Set of states: {s,x,y}Set of states: {s,x,y}Start state: s, end state: yStart state: s, end state: yAlphabet: {a, b}Alphabet: {a, b}Transition function Transition function with signature: with signature:

character x state -> statecharacter x state -> state(a,s)=x(a,s)=x (a,x)=x(a,x)=x(b,x)=y(b,x)=y (b,y)=y(b,y)=y

Exercise 2

Basic IdeaBasic Idea:: ImplementImplement details of FSA as database facts details of FSA as database facts Write a generic program to call database factsWrite a generic program to call database facts

Note:Note: Compare implementation with Exercise 1 Compare implementation with Exercise 1

……where FSA details such as the layout of the where FSA details such as the layout of the FSA and program control, e.g. in the sense FSA and program control, e.g. in the sense of what state to call next, are mergedof what state to call next, are merged

Exercise 2

Database Facts:Database Facts: startState(s).startState(s). endState(y).endState(y).

transition(s,a,x).transition(s,a,x). transition(x,a,x).transition(x,a,x). transition(x,b,y).transition(x,b,y). transition(y,b,y).transition(y,b,y).

Exercise 2

Generic Program:Generic Program: fsa(L) :-fsa(L) :-

startState(S),startState(S),fsa(S,L).fsa(S,L).

fsa(S,[C|L]) :- fsa(S,[C|L]) :- transition(S,C,T),transition(S,C,T),fsa(T,L).fsa(T,L).

fsa(S,[]) :- endState(S).fsa(S,[]) :- endState(S).

Exercise 2

Consult FSA and turn on tracing facilityConsult FSA and turn on tracing facility Re-run queries from Exercise 1:Re-run queries from Exercise 1:

?- fsa([a,a,b,b]).?- fsa([a,a,b,b]). ?- fsa([a,b,a]).?- fsa([a,b,a]). ?- fsa([a,X]).?- fsa([a,X]).

Exercise 2

Homework Question (A):Homework Question (A): What happens when we run the following query?What happens when we run the following query?

?- fsa(L).?- fsa(L). Homework Question (B):Homework Question (B):

Does the corresponding query for the program in Does the corresponding query for the program in Exercise 1 Exercise 1

?- s(L).?- s(L).do the same thing?do the same thing?

Homework Question Homework Question (C):(C): Explain the behavior of the programsExplain the behavior of the programs

Exercise 3

We’re going to implement a Finite State We’re going to implement a Finite State TransducerTransducer i.e. a FSA that not only accepts input but i.e. a FSA that not only accepts input but

produces output as wellproduces output as well StrategyStrategy::

Modify an existing FSA acceptor to take Modify an existing FSA acceptor to take one more parameter (the output list)one more parameter (the output list)

Exercise 3: Finite State Transducer

a by : ies

X : X

Notation:• input : output•Variable denotes any character

Example: convert word ending in –y to word ending in –ies

Exercise 3

Acceptor Program:Acceptor Program: a([y|L]) :- b(L).a([y|L]) :- b(L). aa([X|L]) :- a(L).([X|L]) :- a(L). bb([]).([]).

Note:Note: FSA is non-deterministicFSA is non-deterministic

Exercise 3

Run queries:Run queries: ?- a([f,l,y]).?- a([f,l,y]). ?- a([b,a,b,y]).?- a([b,a,b,y]). ?- a([a,p,p,l,e]).?- a([a,p,p,l,e]).

Exercise 3

Convert FSA into transducer:Convert FSA into transducer: a([y|L],M) :- b(L,M).a([y|L],M) :- b(L,M). aa([X|L],[X|M]) :- a(L,M).([X|L],[X|M]) :- a(L,M). bb([],[([],[ii,e,s]).,e,s]).

Notes:Notes: M represents the output listM represents the output list

Exercise 3

Re-do previous queries with the extra Re-do previous queries with the extra output parameter:output parameter: ?- a([f,l,y],X).?- a([f,l,y],X). ?- a([b,a,b,y],X).?- a([b,a,b,y],X). ?- a([a,p,p,l,e],X).?- a([a,p,p,l,e],X).

Exercise 3

Homework Question:Homework Question: How does the transducer handle more How does the transducer handle more

than one “y” in the input?than one “y” in the input?Example: a([y,u,c,k,y],X).Example: a([y,u,c,k,y],X).Hint: turn on tracingHint: turn on tracing