Download presentation
Presentation is loading. Please wait.
1
LING 388 Language and Computers Lecture 2 9/04/03 Sandiway FONG
2
Administrivia Course Webpage http://www.u.arizona.edu/~sandiway/ling388/ Course Webpage http://www.u.arizona.edu/~sandiway/ling388/ If you don’t have PC access… If you don’t have PC access… Computer Laboratory SBS 224 Mon-Fri 8am-6pm My email: My email: sandiway@email.arizona.edu TA: TA: Charles Lin (clin@email.arizona.edu) clin@email.arizona.edu
3
Administrivia
4
Next Tuesday (9th) Next Tuesday (9th) A computer lab class Change of location: SBS 224 First Homework First Homework Out next Tuesday Due the following Tuesday Homeworks should be submitted electronically to the TA
5
Today’s Lecture Introduction to PROLOG Introduction to PROLOG PROLOG = Programming in Logic Based on a subset of first order logic Advantages: Looks like logic not C/C++/JavaLooks like logic not C/C++/Java Very high-level and easy to useVery high-level and easy to use –no nasty primitive programming stuff like pointers or memory allocation or variable typing
6
Today’s Lecture We’ll find it useful for: We’ll find it useful for: Formalizing or defining concepts Also happens to be a convenient high-level programming language Background Background Resolution theorem proving (Robinson, 65) Invented by Colmerauer and Kowalski in the early 70s Designed to support natural language processing… grammar rules
7
Highlights Backtrack mechanism for non-determinism Backtrack mechanism for non-determinism Can return more than one answer Depth-first search Depth-first search Memory efficient way to look for answers Unification Unification Mechanism used to assign values to variables Negation-as-failure (to prove) Negation-as-failure (to prove) Limited form of logical negation Definite Clause Grammar (DCG) notation Definite Clause Grammar (DCG) notation Useful for writing grammar rules
8
Task 1 Install Prolog on your personal computer Install Prolog on your personal computer Free download Free download SWI-Prolog (University of Amsterdam) Platforms supported: Windows, Unix/Linux, MacOS X,… Website: Website: http://www.SWI-Prolog.org/ http://www.SWI-Prolog.org/ Will be also available on the machines in Computer Lab SBS 224 Will be also available on the machines in Computer Lab SBS 224
9
Prolog: Basic Data Structures Constants Constants Names (begin with lower case letter) Examples: john mary theMan x Exception: May begin with an upper case letter if enclosed in single quotesMay begin with an upper case letter if enclosed in single quotes –Example: ‘John’ ‘X’ Numbers Examples: 100 3.14159
10
Prolog: Basic Data Structures Variables (begin with an upper case letter) Variables (begin with an upper case letter) Examples: X Y23 Variable John Exception: May also begin with an underscore Examples: _ _12 _X _ is special: called the “anonymous variable”_ is special: called the “anonymous variable” Initially has no value (unbound) May only be assigned a value once X = john means “X is bound to the constant john”means “X is bound to the constant john” cf. X := 1; Y := 2; X := 2 … in many languages
11
Prolog: Basic Data Structures Variables Variables Scope of variable is a clause meaning: if some variable is mentioned more than once in a clause, it’s the same variablemeaning: if some variable is mentioned more than once in a clause, it’s the same variable clause = smallest independent program fragmentclause = smallest independent program fragment Example: animate(X) :- man(X). read as: “X is animate if X is a man”read as: “X is animate if X is a man” both Xs denote the same variableboth Xs denote the same variable Example: animate(X) :- woman(X). the X here is independent of the X in the previous clausethe X here is independent of the X in the previous clause
12
Prolog: More Data Structures Lists (delimited by square brackets) Lists (delimited by square brackets) Examples: [] empty list [1] list with one element ‘1’ [1,2,3] list with three elements 1,2 and 3 Alternative notation (using | ): [1|[2,3]] same as [1,2,3] [1|[]] same as [1] Other Examples: [X,mary] list containing a variable X and a constant mary [1|X] list with 1st element 1 and remainder denoted by a variable [[1,2],[3,4],[5,6]] list with 3 elements, each a list
13
Prolog: More Data Structures Structures (functor/argument(s)) Structures (functor/argument(s)) functor must be a name argument can be anything… name, variable or another structure Examples: np(john) structure with functor np and a constant argument john structure with functor np and a constant argument john s(np,vp) structure with functor s and 2 arguments np and vpstructure with functor s and 2 arguments np and vp s(np(john),vp(X)) - nested structures- nested structures Note: We can represent parse trees using nested structures Note: We can represent parse trees using nested structures
14
Prolog: Clauses Clauses may be program or database facts Clauses may be program or database facts facts resemble structures + ‘.’ (period) Examples: man(john). woman(mary). modal(should). modal(could). aux(be). aux(do). lessThanTwo([]). lessThanTwo([_]).
15
Prolog: Clauses Clauses may be rules with conditions Clauses may be rules with conditions General form: A :- B, C In a rule: the connective :- means “if” the connective, means “and” A is the head of the clause B, C is the body of the clause Reading: “A is true if B is true and C is true”
16
Prolog: Clauses Examples: Examples: animate(X) :- man(X). “X is animate if X is a man” animate(X) :- woman(X). “X is animate if X is a woman” Note: two clauses for predicate animate/1 Notation: /1 means arity 1 - “has one argument”Notation: /1 means arity 1 - “has one argument” Implies there are two ways to satisfy animate/1 Two clauses form a disjunctive definition for animate/1
17
Prolog: Clauses Alternative notation: Alternative notation: animate(X) :- man(X) ; woman(X). connective ; means “or” means “X is animate if X is a man or X is a woman” Note the scope of X Negation ( \+ restricted to the body of a clause) Negation ( \+ restricted to the body of a clause) Example: Define a predicate contractedNegative/1 to be read as “(has a) contracted negative” contractedNegative(X) :- modal(X).contractedNegative(X) :- modal(X). contractedNegative(X) :- aux(X).contractedNegative(X) :- aux(X). Exception: *mayn’t contractedNegative(X) :- modal(X), \+ X = may.contractedNegative(X) :- modal(X), \+ X = may.
18
Prolog: Unification Unification ( = ) Unification ( = ) mechanism for matching and valuing variables Examples: john = john “john unifies with john” Note: john = mary will failNote: john = mary will fail X = john or john = X “john unifies with X” “X is bound to john” “john unifies with X” “X is bound to john” Note: unification is symmetricNote: unification is symmetric noun(X) = noun(john) “unifies with X bound to john” Note: match the functors, then match the argumentsNote: match the functors, then match the arguments
19
Prolog: Queries Query ( ?- ) Query ( ?- ) query = fact prefixed by ?- matches against facts and rules in the program database Examples: modal(should). modal(could). ?- modal(X). X = should ;X = should ; X = couldX = could Note: type ; to the Prolog interpreter to request more answers Note: answers are returned according to program database order
20
Prolog Queries Queries without variable binding Queries without variable binding for verification only - yes/no answer Examples: ?- modal(should). Yes (Prolog interpreter response) ?- modal(be). No (Prolog interpreter response)
21
Prolog Queries Negative queries Negative queries Examples: ?- \+ modal(be). Yes ?- \+ modal(should). No Question: What does the query ?- \+ modal(X). return? Explain why
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.