Cs 378
Cs 378
1
Course Topics
• Introduction
• Clojure
• List and tree representation
• Tree recursion and tree search
• Evaluation and interpretation
• Pattern matching, substitution, rewrite rules
• Symbolic mathematics: algebra, calculus, unit
conversion
• Program generation from trees
• Predicate calculus, rule representation
• Backchaining and Prolog; program composition
• Rule-based expert systems
• Semantic grammar and Natural language interfaces
• Functional programs, MapReduce
• Symbolic programming in ordinary languages
2
Symbolic Programming
• solve equations
• translate a program to another language
• specialize a generic program
• convert units of measurement
• translate English into a database query
• compose program components
3
Functional Programming
guacamole:
season(mash(slice(peel(wash(avocado)))))
4
Clojure
1
The name Clojure is a pun on closure, a runtime object that combines a function and its environment
(variable values), with a j for Java.
5
Why Symbolic and Functional Programming?
6
Class Projects
7
Clojure
(+ i 2) ; i + 2
8
Quotation
>car
ford
>’car
car
>(quote car)
car
9
List
10
Constructing a List
11
Access to Parts of a List
The two fields of a cons cell are called first, the first
item in the list, and rest, the rest of the list after the
first element.2
2
In the original Lisp, first was called car, and rest was called cdr.
12
List Access Functions
There are easy rules for the access functions using the
parenthesized representation of lists:
first returns the first thing in a list. A thing is:
rest returns the rest of a list after the first thing. Simply
move the left parenthesis to the right past the first thing.
13
IF statement
(if (+ 2 3) 2 3) -> 2
14
Tests on cons
15
Recursion
1. Tests for a base case and computes the value for this
case directly.
2. Otherwise,
(a) calls itself recursively to do smaller parts of the
job,
(b) computes the answer in terms of the answers to
the smaller parts.
(load-file "cs378/trace.clj")
user=> (trace (* 2 3))
TRACE: 6
6
17
Designing Recursive Functions
18
Design Pattern for Recursive Functions
19
Recursive Processing of List
20
Recursive List Design Pattern
(defn fn [lst]
(if (empty? lst) ; test for base case
baseanswer ; answer for base case
(some-combination-of
(something-about (first lst))
(fn (rest lst))) ) ) ; recursive call
21
Filter a List
(2 4 3 8)
22
Tail Recursive Processing of List
3
Note that we define the helper function first: otherwise, the Clojure compiler will generate an error when
the main function calls it before it is defined.
23
Tail Recursive List Design Pattern
24
Constructive Tail Recursive Reverse
25
Tail Recursive Reverse Execution
26
Append
(defn append [x y]
(if (empty? x)
y
(cons (first x)
(append (rest x) y)) ) )
27
Append: Big O Hazard
user=> (listofn 5)
(0 1 2 3 4)
28
Beware the Bermuda Triangle
i=1
29
Set as Linked List
4
Clojure has sets as a built-in type, which is more efficient for large sets than what is shown here.
30
Intersection
(defn intersection [x y]
(if (empty? x)
’()
(if (member (first x) y)
(cons (first x)
(intersection (rest x) y))
(intersection (rest x) y) ) ) )
31
Tail-Recursive Intersection
32
Union and Set Difference
33
Association List
5
The function assoc, traditionally the name of this function in Lisp, is used for a Clojure lookup on the
built-in map datatype. The Clojure version would be much more efficient for a large map.
34
Let
(let [ d (* 2.0 r)
c (* Math/PI d) ] ... )
35
Map
36
Some and Every
Note that in this case we both tested for the desired item
(> x 3) and then returned the item itself, since we want
that answer rather than true as returned by the test.
The every? function (∀ or for all in math notation)
applies a given function to each element of a list, returning
true if the function is not nil or false for every list
element.
(every? f unction list )
37
println, do, when, str
(when (> 5 3)
(println "Whew!")
(println "I’d be worried otherwise."))
38
doseq and sort
((z a p) (a p z))
39
Trees
40
Arithmetic Expressions as Trees
y = m * x + b (= y (+ (* m x) b))
41
Computer Programs as Trees
if ( x > y )
j = 3;
else
j = 1;
42
English Sentences as Trees
43
Representations of Trees
44
First-Child / Next-Sibling Tree
y = m * x + b
Tree: Representation:
45
Linked List Tree
We can think of a linked list as a tree. The first node of
the list contains the contents of the node, and the rest
of the list is a list of the children. The children, in turn,
can be lists; a non-list is a leaf node. This is similar to
first-child / next-sibling.
46
Binary Tree Recursion
47
Design Pattern: Binary Tree Recursion
This pattern is like the one for lists, except that it calls
itsefl twice for interior nodes. This is essentially the same
as the divide-and-conquer design pattern.
(defn myf un [tree]
(if (cons? tree)
(combine (myf un (first tree)) ; left
(myf un (rest tree))) ; right
(baseanswer tree) )) ; leaf node
48
Flattening Binary Tree
49
Examples: Flattening Tree
(y = m * x + b)
50
Tracing Flattening Binary Tree
51
Postorder
53
State Space Search
operator
State --------------> New State
54
Depth-First Order: Recursive Default
55
Tree Recursion
56
DFS: Code to Build Answer
apply op search
state ---------> newstate --- ... ---> Goal
<- (cons op path) <- path = ( ops ) <- ()
57
Answer for State Space Search
58
Design Pattern: Depth-first Search
Complications:
59
Comments on Search Algorithm
60
Tail-Recursive Search
61
Robot Mouse in Maze
Depth-first search of an implicit tree can simulate a robot
mouse in a maze. The goal is to return a sequence of steps
to guide the mouse to the cheese.
(defn mouse [maze x y prev]
(if (or (= (nth (nth maze y) x) ’*) ; hit wall
(member (list x y) prev)) ; been there
nil ; fail
(if (= (nth (nth maze y) x) ’c) ; cheese
’() ; success
(let [path (mouse maze (- x 1) y ; go west
(cons (list x y) prev))]
(if path (cons ’w path)
(let [path (mouse maze x (- y 1)
(cons (list x y) prev))]
(if path (cons ’n path)
(let [path (mouse maze (+ x 1) y
(cons (list x y) prev))]
(if path (cons ’e path)
(let [path (mouse maze x (+
(cons (list x y) prev))]
(if path (cons ’s path)
nil))))))))))) ;
62
Robot Mouse Program
63
Robot Mouse Example
(def maze
; 0 1 2 3 4 5 6 7 8 9
’((* * * * * * * * * *) ; 0
(* 0 0 * * * * * * *) ; 1
(* 0 * * * * * * * *) ; 2
(* 0 * * * * * * * *) ; 3
(* 0 0 0 0 0 0 * * *) ; 4
(* * * * 0 * 0 * * *) ; 5
(* * * * 0 * 0 * c *) ; 6
(* * * * 0 * 0 * 0 *) ; 7
(* * * * 0 * 0 0 0 *) ; 8
(* * * * 0 * * * * *))) ; 9
64
Tracing the Robot Mouse
65
Tracing the Robot Mouse ...
66
Depth-First Search
Disadvantages:
67
Big O for Trees
68
Bounded Depth-First Search
69
Iterative Deepening
Disadvantage:
70
Cost of Iterative Deepening
7
Korf, Richard E., “Depth-First Iterative-Deepening: An Optimal Admissible Tree Search,” Artificial
Intelligence. vol. 27, no. 1, pp. 97-112, Sept. 1985.
71
Depth-First Search
72
Solving Equations
• Base cases:
– If the lhs of e is v, return e.
– If the rhs of e is v, rewrite e to switch the lhs
and rhs of e, and return that.
– If only an undesired variable or constant is on the
right, (rhs is not a cons), fail by returning nil.
• Recursive case: Rewrite e using an algebraic law,
and try to solve that equation. Return the first result
that is not nil.
Often, there are two possible ways to rewrite an
equation; it is necessary to try both. Thus, the process
will be a binary tree search.
We are rewriting an equation in every possible legal
way; most of these will not be what we want, but one
may work. If we find one that works, we return it.
74
Examples: Base Cases
(= x 3)
(= x 3)
nil
75
Recursive Cases: Operators
76
Recursive Tree Search
(= X (- Y B))
(= X (/ (- Y B) M))
77
Recursive Tree Search Example
78
Big O and Termination
79
Solving a Physics Problem
80
Solving Sets of Equations
Given:
• a set of equations
fall:
81
Solving a Set of Equations by Search
82
Solving Physics Story Problems
12.566370614359172
12.279920495357862
122.583125
83
Generating Code from Equations
84
Eliminating Unused Equations
85
Conservation Laws
• Common sense:
There is no such thing as a free lunch.
You can’t have your cake and eat it too.
• Physics:
Mass-energy is neither created nor destroyed.
For every action, there is an equal and opposite
reaction.
• Finance:
Money is neither created nor destroyed.
Every transaction requires an equal and
opposite transaction.
86
Double-Entry Bookkeeping
87
Representing Financial Contracts
88
Finance Combinators8
8
S. Peyton Jones and J.M. Eber, “How to write a financial contract”, in The Fun of Programming, ed
Gibbons and de Moor, Palgrave Macmillan 2003
89
Pattern Matching Overview
90
Copy Tree and Substitute
91
Substitution Examples
; for in
>(subst ’axolotl ’banana ’(banana pudding))
(axolotl pudding)
>(def pi 3.1415926535897933)
92
Loop Unrolling
is expanded into:
Loop: Unrolled:
Instructions: 20 12
Executed: 57 12
The second form runs faster, and it may generate less
code. This is a useful optimization when the size of the
loop is known to be a small constant at compile time.
93
Loop Unrolling Code
(unroll ’(dotimes [i 3]
(println (get arr i))) )
94
Binding Lists
95
Multiple Substitutions
; replace by by in
>(sublis ’((rose peach) (smell taste))
’(a rose by any other name
would smell as sweet))
96
Instantiating Design Patterns
sublis can be used to instantiate design patterns. For
example, we can instantiate a tree-recursive accumulator
pattern to make various functions:
(def pattern
’(defn ?fun [tree]
(if (cons? tree)
(?combine (?fun (first tree))
(?fun (rest tree)))
(if (?test tree) ?trueval ?falseval))))
(defn equal [x y]
(if (cons? x)
(and (cons? y)
(equal (first x) (first y))
(equal (rest x) (rest y)))
(= x y) ))
true
Some say that two trees are equal if they print the same.
Note that this function treats a cons as a binary
first-rest tree rather than as a lhs-rhs tree.
98
Tracing Equal
99
Design Pattern: Nested Tree Recursion
100
Tracing Nested Tree Recursion
>(nnums ’(+ (* x 3) (/ z 7)))
1> (NNUMSB (+ (* X 3) (/ Z 7)) 0)
2> (NNUMSB + 0)
<2 (NNUMSB 0)
2> (NNUMSB ((* X 3) (/ Z 7)) 0)
3> (NNUMSB (* X 3) 0)
4> (NNUMSB * 0)
<4 (NNUMSB 0)
4> (NNUMSB (X 3) 0)
5> (NNUMSB X 0)
<5 (NNUMSB 0)
5> (NNUMSB (3) 0)
6> (NNUMSB 3 0)
<6 (NNUMSB 1)
6> (NNUMSB NIL 1)
<6 (NNUMSB 1)
<5 (NNUMSB 1)
<4 (NNUMSB 1)
<3 (NNUMSB 1)
3> (NNUMSB ((/ Z 7)) 1)
4> (NNUMSB (/ Z 7) 1)
5> (NNUMSB / 1)
<5 (NNUMSB 1)
5> (NNUMSB (Z 7) 1)
6> (NNUMSB Z 1)
<6 (NNUMSB 1)
6> (NNUMSB (7) 1)
7> (NNUMSB 7 1)
<7 (NNUMSB 2)
7> (NNUMSB NIL 2)
<7 (NNUMSB 2)
<6 (NNUMSB 2)
...
2
101
Pattern Matching
102
Specifications of Match
103
Match Function
(defn equal [x y]
(if (cons? x)
(and (cons? y)
(equal (first x) (first y))
(equal (rest x) (rest y)))
(= x y) ))
105
Transformation by Patterns
106
Solving Equations with Patterns
107
Symbolic Differentiation
108
Repetitive Transformation
(+ (* x 0) y)
(+ 0 y)
y
109
Optimization by Patterns
(def optpatterns
’( ((+ ?x 0) ?x )
((* ?x 0) 0 )
((* ?x 1) ?x )
((- (- ?x ?y)) (- ?y ?x) )
((- 1 1) 0 )
... ))
opt: 2
110
Constant Folding
(sqrt 2.0)
>(println "foo")
foo
nil
111
Correctness of Transformations
( (> (* ?n ?x)
(* ?n ?y)) (> ?x ?y) )
112
Knuth-Bendix Algorithm
9
Knuth, D. E and Bendix, P. E., “Simple word problems in universal algebras”, in J. Leech (ed.),
Computational Problems in Abstract Algebra, Pergammon Press, 1970, pp. 263-297.
113
Programs and Trees
114
Macros
> (neq 2 3)
true
> (macroexpand ’(neq 2 3))
(not (= 2 3))
115
In-line Compilation
Disadvantages:
116
Partial Evaluation
117
Partial Evaluation10
10
Neil D. Jones, Carsten K. Gomard, and Peter Sestoft, Partial Evaluation and Automatic Program
Generation, Prentice-Hall, 1993; ACM Computing Surveys, vol. 28, no. 3 (Sept. 1996), pp. 480-503.
118
Example
(defun power (x n)
(if (= n 0)
1
(if (evenp n)
(square (power x (/ n 2)))
(* x (power x (- n 1))))))
119
Simple Partial Evaluator
120
Simple Partial Evaluator...
121
Examples
>(load "/u/novak/cs394p/mix.lsp")
>(mix ’x ’((x . 4)))
4
>(mix ’(if (> x 2) ’more ’less) ’((x . 4)))
’MORE
(defun power (x n)
(if (= n 0)
1
(if (evenp n)
(square (power x (/ n 2)))
(* x (power x (- n 1)))) ) )
122
Examples
123
Binding-Time Analysis
124
Futamura Projections11
output = [ source]]s[input]
= [ int]][source, input]
•
= [ [ mix]][int, source]]][input]
= [ target]][input]
Therefore, target = [ mix]][int, source].
target = [ mix]][int, source]
• = [ [ mix]][mix, int]]][source]
= [ compiler]][source]
Thus, compiler = [ mix]][mix, int] = [ cogen]][int]
11
Y. Futamura, “Partial Evaluation of Computation Process – An Approach to a Compiler-Compiler”,
Systems, Computers, Controls, 2(5):45-50, 1971. The presentation here follows Jones et al.
125
Interpreter
(defun plus ()
(let ((rhs (pop *stack*)))
(pushopnd (+ (pop *stack*) rhs))))
>(specialize ’topinterp
’(’(+ (* a b) c))
’expr1 ’(a b c))
>(pp expr1)
(LAMBDA-BLOCK EXPR1 (A B C)
(PROGN
(PUSH A *STACK*)
(PUSH B *STACK*)
(TIMES)
(PUSH C *STACK*)
(PLUS)
(POP *STACK*)))
>(expr1 3 4 5)
17
127
Parameterized Programs
128
Pitfalls of Partial Evaluation
129
Pitfalls ...
12
Jones et al., p. 119.
130
Language Translation
Language translation:
(defpatterns ’lisptojava
’( ((aref ?x ?y) ("" ?x "[" ?y "]"))
((incf ?x) ("++" ?x))
((setq ?x ?y) ("" ?x " = " ?y))
((+ ?x ?y) ("(" ?x " + " ?y ")"))
((= ?x ?y) ("(" ?x " == " ?y ")"))
((and ?x ?y) ("(" ?x " && " ?y ")"))
((if ?c ?s1 ?s2) ("if (" ?c ")" #\Tab
#\Return ?s1
#\Return ?s2))
131
Program Transformation using Lisp
>code
(IF (AND (= J 7) (/= K 3))
(PROGN (SETQ X (+ (AREF A I) 3))
(SETQ I (+ I 1))))
132
Max and Min of a Function
(def cannonball
’(= y (- (* (* v (sin theta)) t)
(* (/ g 2) (expt t 2)))) )
133
Knowledge Representation and Reasoning
134
Representation Hypothesis
13
Newell, A., Physical Symbol Systems, Cognitive Science, 1980, 4, 135-183.
14
Diagram by John Sowa, from “The Challenge of Knowledge Soup,” 2005.
135
Kinds of Knowledge
15
Miller, George A., “The magical number seven, plus or minus two: some limits on our capacity for
processing information”, Psychological Review vol. 63, pp. 81-97, 1956.
136
Logic
137
Logical Representation
138
Propositional Logic
Math C meaning
Negation ¬ or ∼ ! not
Conjunction ∧ (“wedge”) && and
Disjunction ∨ (“vee”) || or
Implication → or ⊃ ? : implies, if-then
↔ == iff (if and only if)
←∧→
139
Interpretation in Propositional Logic
140
Equivalent Formula Laws
• Implication:
F → G = ¬F ∨ G
F ↔ G = (F → G) ∧ (G → F )
A ∧ B ∧ C → D = ¬A ∨ ¬B ∨ ¬C ∨ D
• De Morgan’s Laws:
¬(F ∨ G) = ¬F ∧ ¬G
¬(F ∧ G) = ¬F ∨ ¬G
• Distributive:
F ∨ (G ∧ H) = (F ∨ G) ∧ (F ∨ H)
F ∧ (G ∨ H) = (F ∧ G) ∨ (F ∧ H)
Inference Rules
P, P →Q
• Modus Ponens: Q or P, P → Q ` Q
141
Ways to Prove Theorems
142
Rules for Backward Chaining
143
Backward Chaining
144
Backchaining
Backchaining is easily implemented as a recursive tree
search (file backch.clj).
The function (backchain goal rules facts) tries to
prove a goal given a set of rules and facts.
goal is a symbol (atom or propositional variable).
facts is a list of atoms that are known to be true.
rules is a list of rules of the form
(conclusion prem1 ... premn); each rule states that the
conclusion is true if all of the premises are true.
For example, the rule A ∧ B → C would be written
C ← A ∧ B or (c a b), similar to Prolog, c :- a,b .
145
Backchaining Code
backchain works as follows: if the goal is known to be a
fact, return true. Otherwise, try rules to see if some rule
has the goal as conclusion and has premises that are true
(using backchain).
146
(backchain goal rules facts)
147
Backchaining Code, version 2
We can add facts to our list of clauses by making a fact
a premise clause with no antecedents; this is the form
used in Prolog. Since the premise list is empty, every?
returns true.
148
Fact = Rule with No Premises
149
Normal Forms
150
Satisfiability Checking (Model Checking)
151
SAT Solvers
152
Uses of SAT Solvers
153
Predicate Calculus (First-order Logic)
154
Order of Quantifiers
155
Skolemization
156
Unification
157
Unification Algorithm
158
Examples of Unification
159
Substitutions
= (P (A) (F (B)))
160
Unification Code
161
Unification Code ...
162
Unification Examples
163
Soundness and Completeness
164
Resolution
165
Conjunctive Normal Form
166
Resolution Example
167
Resolution Example
169
Backchaining Theorem Prover
171
Deductive Composition of Astronomical
Software from Subroutine Libraries 16 17
16
M. Stickel, R. Waldinger, M. Lowry, T. Pressburger, I. Underwood: ”Deductive Composition of
Astronomical Software from Subroutine Libraries”, Proc. 12th Int. Conf on Automated Deduction
(CADE’94), Nancy (France), June 994, LNAI 814, Springer Verlag, pp. 341-355.
17
Steve Roach and Jeffrey Van Baalen, “Experience Report on Automated Procedure Construction for
Deductive Synthesis”, Proc. Automated Software Engineering Conf., Sept. 2002, pp. 69-78.
172
Difficulty of Programming
173
Domain Theory
175
Problem Difficulty
176
Where is the shadow of Io on Jupiter?
177
Shadow of Io Theorem
(all (time-voyager-2-c)
(find (shadow-point-c)
(exists
(time-sun sun-spacetime-loc time-io io-spacetime-loc
time-jupiter jupiter-spacetime-loc time-voyager-2
voyager-2-spacetime-loc shadow-point jupiter-ellipsoid
ray-sun-to-io)
(and
(= ray-sun-to-io
(two-points-to-ray
(event-to-position sun-spacetime-loc)
(event-to-position io-spacetime-loc)))
(= jupiter-ellipsoid
(body-and-time-to-ellipsoid jupiter time-jupiter))
(= shadow-point
(intersect-ray-ellipsoid ray-sun-to-io jupiter-ellipsoid))
(lightlike? jupiter-spacetime-loc voyager-2-spacetime-loc)
(lightlike? io-spacetime-loc jupiter-spacetime-loc)
(lightlike? sun-spacetime-loc io-spacetime-loc)
(= voyager-2-spacetime-loc
(ephemeris-object-and-time-to-event voyager-2 time-voyager-2))
(= jupiter-spacetime-loc
(ephemeris-object-and-time-to-event jupiter time-jupiter))
(= io-spacetime-loc
(ephemeris-object-and-time-to-event io time-io))
(= sun-spacetime-loc
(ephemeris-object-and-time-to-event sun time-sun))
(= shadow-point (abs (coords-to-point j2000) shadow-point-c))
(= time-voyager-2
(abs ephemeris-time-to-time time-voyager-2-c))))))
178
Shadow of Io Program
179
Performance
180
Deductive Composition of Programs
181
Logic Form of Rules
182
Navigation in the Plane
183
Navigation Predicates
184
Example Navigation Problem
185
Difficulties with Deductive Synthesis
186
Knowledge Rep. in Predicate Calculus
(DOG DOG1)
(NAME DOG1 FIDO)
(HOUND DOG1)
(LOVES JOHN MARY)
or
187
Rules
188
Backward Chaining
189
Importance of Backchaining
W IF E(John, M ary)
W IF E(Bill, Jane)
...
Suppose we want to prove LOVES(John,Mary). Back-
ward chaining will bind x and y in the theorem, do a single
database lookup of WIFE(John,Mary), and succeed.
Forward chaining will assert the LOVES relationship for
every WIFE pair in the database until it happens to hit
LOVES(John,Mary).
190
PROLOG
191
Predicate Calculus as Programming Language
192
When to Use Logic
193
Unit Conversion
18
G. Novak, “Conversion of Units of Measurement”, IEEE Transactions on Software Engineering, vol. 21,
no. 8 (August 1995), pp. 651-661.
194
Conversion Using SI Units
195
Unit Checking
196
Special Conversions
197
Unit Simplification
AMPERE
198
Problem Solving by Unit Conversion
199
Fixing Conversion Errors
200
Units in Programming Languages
201
Expert Systems19
19
These slides jointly authored with Bruce Porter.
20
Duda, R. O. and Shortliffe, E. H., “Expert Systems Research”, Science, vol. 220, no. 4594,15 April
1983, pp. 261-268.
202
Power-Based Strategy
Some have hoped that powerful theorem-proving methods
on fast computers would allow useful reasoning from a set
of axioms. Several problems have kept this power-based
strategy from succeeding.
Knowledge-Based Strategy
203
Expert Reasoning
21
Parker, T., Rules of Thumb, Boston, MA: Houghton Mifflin Publishers, 1983.
204
Expert Knowledge
205
Choosing a Domain
Problem Characteristics
206
Rule-Based Systems
Example: MYCIN23
Rule 27
If 1) the gram stain of the organism is gram
negative, and
2) the morphology of the organism is rod, and
3) the aerobicity of the organism is
anaerobic,
Then: There is suggestive evidence (0.6) that
the identity of the organism is
Bacteroides.
23
Shortliffe, Edward H., Computer Based Medical Consultations: MYCIN, American Elsevier, 1976.
Buchanan, Bruce G. and Shortliffe, Edward H., Rule-Based Expert Systems, Addison-Wesley, 1984.
207
Advantages of Rules
208
EMYCIN
209
Rule-Based Expert Systems24
24
Shortliffe, E. Computer-based medical consultations: MYCIN. New York: Elsevier, 1976.
25
provelist calls prove with each condition of LHS
210
Reasoning Under Uncertainty
211
Bayes’ Theorem
Bayes’ Theorem
P (AB) = P (A|B) ∗ P (B) = P (B|A) ∗ P (A)
therefore P (A|B) = P (B|A) ∗ P (A) / P (B)
212
Uses of Bayes’ Theorem
213
Joint Probability Distribution
26
Example from Russell & Norvig.
214
Chain Rule
215
Bayesian Networks
27
Example from Russell & Norvig.
216
Computing with Bayesian Networks
217
A Heretical View
218
EMYCIN’s Certainty Factors
• Data CF
• CF from antecedent of a rule
• CF due to rule as a whole
• Combination of CF’s from multiple rules.
219
Certainty Factor Meaning
MB 0 - 1 Measure of Belief
MD 0 - 1 Measure of Disbelief
CF = MB - MD -1 to 1 Certainty Factor
-1 Definitely False
0 No information, or cancellation
+1 Definitely True
When a data parameter is True/False, “False” is
represented as “True” with a CF of -1.
220
EMYCIN Data CF’s
221
EMYCIN Antecedent CF
222
Rule Certainty Factors
Premise:
Action:
(CONCLUDE CNTXT
IDENTITY PSEUDOMONAS-AERUGINOSA
TALLY 400)
223
Certainty Factor Combination
• Commutative: A · B = B · A
• Associative: A · (B · C) = (A · B) · C
224
Certainty Factor Combination
225
Summary of CF Computations
• CF > .2 threshold
• $AND takes minimum CF in premise
• conclude CF = CFpremise ∗ CFrule
• CF combination algorithm
Examples:
If: A (.6)
and B (.3)
and C (.4)
Then: conclude D tally 700
226
Contradictions:
227
Duplicate Rules
228
Rule Subsumption
28
Bennett, J. S. and Engelmore, R. S., “SACON: a Knowledge-based Consultant for Structural Analysis”,
Proc. IJCAI-79, pp. 47-49, Morgan Kaufmann Publishers.
229
Increasing Certainty
230
EMYCIN CF vs. Probability Theory
If: A (.01)
and B (.02)
and C (.001)
and D (as yet unknown) ...
231
Sensitivity Analysis
232
Explanation
233
Expert Systems vs. Decision Trees
234
Rule Induction
235
Sample Decision Tree:
236
Example of Rule Induction29
Classifications = { +, -}
Features = {size, shape, color}
Domains:
size = {large, small}
shape = {square, triangle, circle}
color = {blue, red}
Training set:
small, circle, blue: +
small, square, blue: -
large, square, red: -
large, circle, red: -
large, square, blue: -
small, circle, red: -
large, triangle, blue: +
large, circle, blue: +
29
Quinlan, “Learning Efficient Classification Procedures”, Machine Learning, Morgan Kaufmann Publ.,
1983.
237
Final Decision Tree with Classifications
238
Algorithm for Rule Induction
Instances: a set of training instances
Features: feature vector (f1, f2, ..., fn) input from teacher
Domains: set of domains for Features {d1, d2, ..., dm}
Classes: set of classes {c1, c2, ..., ck }
(simply {+, −} for single concept learning.)
Function formrule (Instances, Features, Domains,
Classes)
For some class ∈ Classes
If all members of Instances fall into class
then return class
else f ← select-feature (Features, Instances)
d ← domain from set Domains corresponding to f
return a tree of the form:
239
Alternatives for select-feature
240
Limitations of Rule Induction
241
Digital Low-Pass Filter
242
Getting Knowledge From Expert
243
Interaction with Expert
244
Conceptual Islands
245
Advantages of Conceptual Islands
246
Expansion with Conceptual Islands
247
Orthogonal Knowledge Sources
248
Example of Orthogonal Knowledge Sources
249
The Tuning Fallacy
31
Receiver Operating Characteristic
250
Natural Language Processing (NLP)
32
METAL system, University of Texas Linguistics Research Center.
251
Why Study Natural Language?
Theoretical:
Practical:
252
Model of Natural Language Communication
253
Minimality of Natural Language
254
Zipf ’s Law
255
Areas of Natural Language
256
Computer Language Understanding
257
Problems in Understanding Language ...
258
Morphology
went --> go + ed
34
Winograd, T., in Understanding Natural Language, Academic Press, 1972, presents a simple algorithm
for suffix stripping. A thorough treatment can be found in Slocum, J., “An English Affix Analyzer with
Intermediate Dictionary Lookup”, Technical Report LRC-81-01, Linguistics Research Center, University of
Texas at Austin, 1981.
259
Lexicon
260
Lexical Features
35
slide by Robert F. Simmons.
261
Size of Lexicon
262
Statistical Natural Language Processing
36
corpus (Latin for body) is singular, corpora is plural. A corpus is a collection of natural language text,
sometimes analyzed and annotated by humans.
37
D. Jurafsky and J. Martin, Speech and Language Processing, Prentice-Hall, 2000.
263
Part-of-Speech Tagging
38
from Jabberwocky, by Lewis Carroll.
39
Jurafsky, op. cit.
264
AI View of Syntax
40
James Howell, A New English Grammar, Prescribing as certain Rules as the Language will bear, for
Forreners to learn English, London, 1662.
265
Grammar
266
Language Generation
<s>
<np> <vp>
<art> <noun> <vp>
the <noun> <vp>
the dog <vp>
the dog <verb> <np>
the dog <verb> <art> <noun>
the dog <verb> the <noun>
the dog bit the <noun>
the dog bit the boy
267
Parsing
268
Ambiguity
269
Sources of Ambiguity
• Lexical Ambiguity:
Words often have multiple meanings (homographs)
and often multiple parts of speech.
• Grammatical Ambiguity:
Different ways of parsing (assigning structure to) a
given sentence.
I saw the man on the hill with the
telescope.
270
Foreign Languages
271
Formal Syntax
272
Notation
a|b either a or b
273
Phrase Structure Grammar
41
See, for example, Aho, A. V. and Ullman, J. D., The Theory of Parsing, Translation, and Compiling,
Prentice-Hall, 1972; Hopcroft, J. E. and Ullman, J. D., Formal Languages and their Relation to Automata,
Addison-Wesley, 1969.
274
Recognizing Automaton
275
Regular Languages
Productions: A → xB
A→x
A, B ∈ N
x ∈ T∗
276
Context Free Languages
Productions: A → α
A∈N
α ∈V∗
277
What Kind of Language is English?
42
Gazdar, G., “NLs, CFLs, and CF-PSGs”, in Sparck Jones, K. and Wilks, Y., Eds., Automatic Natural
Language Processing, Ellis Horwood Ltd., West Sussex, England, 1983.
43
Higginbotham, J., “English is Not a Context Free Language”, Linguistic Inquiry 15, 119-126, 1984.
278
Parsing
279
Top-down Parser
44
See the Language Generation slide earlier in this section.
280
Bottom-up Parsing
<NP>
/ \
<art> <noun>
| |
The man ate fish
281
Augmented Transition Networks
45
Woods, W. A., “Transition Network Grammars for Natural Language Analysis”, Communications of the
ACM, Oct. 1970
282
Augmented Transition Networks
283
Separability of Components
Lexicon
/ \
/ \
Sentence --> Syntax --> Semantics
|
V
Pragmatics
|
V
Output
284
Problems with Separability
• Lexicon:
– New uses of words.
You can verb anything. – William Safire
– Metaphor: The computer is down.
• Syntax:
– Ambiguity: hundreds of syntactically possible
interpretations of ordinary sentences.
– Agreement:
Bill and John love Mary.
– Elision: omission of parts of a sentence.
He gave John fruit and Mary candy.
• Discourse:
– The meaning of a sentence depends on context.
285
Combining Syntax and Semantics
286
How to Combine Syntax & Semantics
287
Natural Language as an AI Problem
288
Reference
Determiners:
• Indefinite: a
Make a new object.
• Definite: the, one, etc.
Find an existing object;
else, find something closely related
to an existing object;
else, make a new one.
289
Referent Identification
290
English
291
46
Expression Trees to English
292
Generating English
%clojure
>(load-file "cs378/expenglish.clj")
>(exp->english ’x)
(X)
293
Simple Language Processing: ELIZA
Problems:
294
Spectrum of Language Descriptions
• ELIZA:
Too restricted. A large application, PARRY – an
artificial paranoid – was attempted, but failed to get
good enough coverage even with 10,000 patterns.
• General English Grammar:
Too ambiguous. Hundreds of interpretations of
ordinary sentences.
295
Semantic Grammar
Advantages:
296
Semantic Grammar: Extended Pattern
Matching
297
Example Semantics for a Semantic Grammar
298
Compositional Semantics
299
Additional Language Features
• Spelling correction:
What is the lentgh of Kennedy?
= length
Because we know from the grammar that a
<ship-prop> is expected, the list of possible ship
properties can be used as input to a spelling corrector
algorithm to automatically correct the input.
• Sentence fragments:
What is the length of Kennedy?
speed
= What is the speed of Kennedy?
If the input can be parsed as a part of the previous
parse tree, the rest of the input can be filled in.
300
Recursive Descent
S -> NP VP
(defn s []
(and (np)
(vp)) )
A -> A ...
301
Parsing English
302
47
ATN Program
47
file atn.clj
303
Parsing Functions
; $$ $1 $2
; (loc -> (in (city)) (restrict ’city $2))
(defn locfn []
(saveptr)
(let [$1 (and (= atnword (quote in)) atnword)]
(if $1
(do (nextword)
(let [$2 (wordcat (quote city))]
(if $2
(do (nextword)
(success)
(restrict (quote city) $2))
(fail))))
(fail))))
304
48
Grammar Compiler
; $$ $1 $2
(rulecompr ’(loc -> (in (city))
(restrict ’city $2))
’locfn)
(defn locfn []
(saveptr)
(let [$1 (and (= atnword (quote in)) atnword)]
(if $1
(do (nextword)
(let [$2 (wordcat (quote city))]
(if $2
(do (nextword)
(success)
(restrict (quote city) $2))
(fail))))
(fail))))
48
file gramcom.clj
305
Sentence Pointer Handling
306
Sentence Pointer Handling ...
307
Lexicon Example
(def lexicon
’((a/an (a an some))
(i/you (i you one))
(get (get find obtain))
(quality ((good 2.5) ))
(restword (restaurant (restaurants restaurant)))
(kindfood (american bakery chinese))
(city (palo-alto berkeley los-altos))
(county (santa-clara))
(area (bay-area))
(street (el-camino-real))
))
308
Word Category Testing
309
Database Access
310
Database Access
311
Building Database Access
312
Grammar Rules
; (gramcom grammar)
(def grammar
’((command -> (show me) true)
(command -> (what is) true)
(qual -> ((quality))
(restrictb ’>= ’rating $1))
(qualb -> (rated above (number))
(restrictb ’>= ’rating $3))
(resttype -> ((kindfood))
(restrict ’foodtype $1))
(loc -> (in (city)) (restrict ’city $2))
(loc -> (in (county)) (restrict ’county $2))
(s -> ((command) (a/an)? (qual)? (resttype)?
(restword) (qualb)? (loc)?)
(retrieve ’restaurant) )
(s -> (how many (qual)? (resttype)? food ?
(restword) (loc)?)
(do (retrieve ’restaurant)
(postpr ’(length (quote $$)))) )
314
Notes on Database Grammar
315
Restaurant Queries
% clojure
user=> (load-file "cs378/restaurant.clj")
user=> (load-files)
user=> (gramcom grammar)
316
Physics Problems49
(def lexicon
’((propname (radius diameter circumference area
volume height velocity time
weight power height work speed mass))
(a/an (a an))
(the/its (the its))
(objname (circle sphere fall lift)) ))
(def grammar ’(
(param -> ((the/its)? (propname)) $2)
(quantity -> ((number)) $1)
(object -> ((a/an)? (objname) with (objprops))
(cons ’object (cons $2 $4)))
(objprop -> ((a/an)? (propname) of ? (quantity)
(list $2 $4))
(objprop -> ((propname) = (quantity))
(list $1 $3))
(objprops -> ((objprop) and (objprops))
(cons $1 $3))
(objprops -> ((objprop)) (list $1))
(s -> (what is (param) of (object))
(list ’calculate $3 $5)) ))
49
file physgram.lsp
317
Physics Queries
% clojure
user=> (load-file "cs378/physics.clj")
user=> (load-files)
user=> (gramcom grammar)
318
Physics Units
3.2429278661312964
4869.478351881704
319
Physics Changes
inverse-square
4.0
These questions can be answered easily as follows:
1. Find an equation that relates these variables.
2. Solve the equation for the desired variable.
3. Evaluate the rhs of the solved equation with all
variables set to 1.
4. Change the variable(s) that change to 2.0 (or the
appropriate value) and evaluate again.
5. The ratio of the two evaluations gives the answer.
320
Natural Language Interfaces
• Database access.
• Particular technical areas.
• Consumer services (e.g., banking).
Benefits:
321
Problems with NL Interfaces
322
Mapping
323
Implementation of Mapping
324
Functional Programming
325
Associative and Commutative
An operation ◦ is associative if a ◦ (b ◦ c) = (a ◦ b) ◦ c.
An operation ◦ is commutative if a ◦ b = b ◦ a.
If an operation ◦ is both associative and commutative,
then the arguments of the operation can be in any order,
and the result will be the same. For example, the
arguments of integer + can be in any order.
This gives great freedom to process the arguments of a
function independently on multiple processors.
In many cases, parts of the operation (e.g. partial sums)
can be done independently as well.
326
Computation as Simulation
(defn string+ [x y]
(str ; phi inverse
(+ ; + in model space
(read-string x) ; phi
(read-string y)))) ; phi
327
Mapping in Lisp
(1 4 9 289)
(3 6 9 25)
328
Mapcat and Filter
329
Input Filtering and Mapping
330
Reduce
23
102
331
Combining Map and Reduce
2.0
2.8284271247461903
3.1622776601683795
332
MapReduce and Massive Data
333
Distributed Programming is Hard!
334
What MapReduce Does for Us
335
Map Sort Reduce
mr.collect_map("z", list("1"));
336
Simplified MapReduce
337
Mapreduce in Clojure
((D 3) (C 6) (B 9) (A 3))
338
Simple MapReduce Example
>(mapreduce identity +
’(((a 3) (b 2) (c 1))
((b 7) (d 3) (c 5))) t)
Mapping: ((A 3) (B 2) (C 1))
Emitted: (A 3)
Emitted: (B 2)
Emitted: (C 1)
Mapping: ((B 7) (D 3) (C 5))
Emitted: (B 7)
Emitted: (D 3)
Emitted: (C 5)
Reducing: D (3) = 3
Reducing: C (5 1) = 6
Reducing: B (7 2) = 9
Reducing: A (3) = 3
((D 3) (C 6) (B 9) (A 3))
339
MapReduce Example
>(nutrition ’bun)
340
Hamburger Example
>(mapreduce ’nutrition ’+
’(hamburger bun cheese lettuce tomato mayo) t)
Mapping: HAMBURGER
Emitted: (CALORIES 80)
Emitted: (FAT 8)
Emitted: (PROTEIN 20)
Mapping: BUN
Emitted: (CALORIES 200)
Emitted: (CARBS 40)
Emitted: (PROTEIN 8)
Emitted: (FIBER 4)
Mapping: CHEESE
Emitted: (CALORIES 100)
Emitted: (FAT 15)
Emitted: (SODIUM 150)
Mapping: LETTUCE
Emitted: (CALORIES 10)
Emitted: (FIBER 2)
Mapping: TOMATO
Emitted: (CALORIES 20)
Emitted: (FIBER 2)
Mapping: MAYO
Emitted: (CALORIES 40)
Emitted: (FAT 5)
Emitted: (SODIUM 20)
Reducing: SODIUM (20 150) = 170
Reducing: FIBER (2 2 4) = 8
Reducing: CARBS (40) = 40
Reducing: PROTEIN (8 20) = 28
Reducing: FAT (5 15 8) = 28
Reducing: CALORIES (40 20 10 100 200 80) = 450
341
How MapReduce Works
342
Map Worker
343
Buffering
344
Load Balancing
345
Reduce Worker
346
PageRank
347
PageRank Example
A B C
1.00000000 1.00000000 1.00000000
1.00000000 0.57500000 1.42500000
1.36125000 0.57500000 1.06375000
1.05418750 0.72853125 1.21728125
1.18468906 0.59802969 1.21728125
1.18468906 0.65349285 1.16181809
1.13754537 0.65349285 1.20896178
1.17761751 0.63345678 1.18892571
1.16058685 0.65048744 1.18892571
1.16058685 0.64324941 1.19616374
1.16673918 0.64324941 1.19001141
1.16150970 0.64586415 1.19262615
1.16373223 0.64364162 1.19262615
...
1.16336914 0.64443188 1.19219898
51
http://pr.efactory.de/e-pagerank-algorithm.shtml
348
Running PageRank Example
Starting MapReduce on:
((a (1.0 (b c))) (b (1.0 (c))) (c (1.0 (a))))
mapping: key = a val = (1.0 (b c))
emitting: key = b val = (0.5)
emitting: key = c val = (0.5)
emitting: key = a val = ((b c))
mapping: key = b val = (1.0 (c))
emitting: key = c val = (1.0)
emitting: key = b val = ((c)) ...
reducing: key = a val = (((b c)) (1.0))
result: key = a val = (1.0 (b c))
reducing: key = b val = ((0.5) ((c)))
result: key = b val = (0.575 (c))
reducing: key = c val = ((0.5) (1.0) ((a)))
result: key = c val = (1.425 (a))
349
Advanced Performance
350
Performance Techniques in MapReduce
351
Algorithm Failure
352
Atomic Commit
353