Title: Programming Language Concepts
1Programming Language Concepts
- Dr. Cong Xing
- Dept of Math Computer Science
2 3Reasons for Studying Concepts of PL
- To increase capacity to express ideas
Express ideas/alg
Knowing more PL concepts
Enrich thoughts
4- To improve background for choosing appropriate
languages
project
programmer
available PLs
his favorable PLs
5- To increase ability to learn new languages
Java
programmer
Formal concepts of OO
6- To better understand the significance of
implementation
implementation
Efficient programming
Lang concepts (eg, recursion, iteration)
7- To achieve overall advancement of computing
Current situation
Most popular lang
Best lang available
?
Decision maker learning PL concepts
how?
wanted
Best lang available
Most popular lang
8Programming Domains
- Brief descriptions of CS applications and their
associated languages.
9(No Transcript)
10Language Evaluation Criteria
- Readability
- The ease w/ which programs can be read and
understood - Affected by the following
- Simplicity as simple as possible but not simpler
(in the sense of of instructions wrt CISC and
RISC) - Orthogonality algebraic properties (e.g., if int
int is a type, then so are intbool and
(intint)int) - Control structures loops, selections, etc
11- Data types and structures facility to define new
data types - Syntactic form wording
- Writability
- The ease w/ which to create programs
- Affected by the following
- All that affects readability
- Support for abstraction what is an abstraction?
Ways to define and use complicated structures so
that details (of these structures) can be ignored
when used.
12- Expressivity means to specify computations.
(tradeoff with readability) - Reliability
- Whether a program performs to its specifications
under all circumstances - Affected by the following
- All that affects writability
- Type checking check for typing errors
- Exception handling detect run-time errors, fix
them, and keep on going
13- Aliasing two or more ways to access the same
memory cell (dangerous or convenient?)
14Influences on Language Design
- Computer architecture (hardware)
Imperative programming style
15- Programming Methodologies (software)
- structured programming
- ADT
- Object-Oriented
- Component-based
- Aspect-oriented
Appropriate Language constructs
16Language Categories
17Language Design Trade-offs
- Many language features are conflict
- Need some compromises
- Examples
- Reliability vs. speed of execution (e.g. type
checking) - Expressivity vs. readability
- Reliability vs. writability (e.g., pointers)
18Language Implementation
- Compilation
- Source code is translated (eventually) to machine
code - Fig in the next page
- Advantage fast, efficient execution
- Disadvantage implementation complexity
19(No Transcript)
20- Pure Interpretation
- Programs (statements) are interpreted and
executed one after another by the interpreter.
The interpreter serves as a virtual machine. - Fig in the next page
- Advantage easy implementation
- Disadvantage slow execution bottleneck same
statement repeatedly decoded
21(No Transcript)
22- Hybrid Systems
- Combination of compilation and pure
interpretation - Programs are compiled into some intermediate
codes which are then interpreted - Fig next page
23(No Transcript)
24Java prog
compiled
Intermediate code
Byte code
Why Java multiplatform?
interpreter
JVM
result
25- Advantage hybrid advantages from compilation and
interpretation - Disadvantage hybrid disadvantages from
compilation and interpretation
26- Ch2 Evolution of the Major PLs
27Overview
28Plankalkul
- Developed in 1945, known in 1972
- Konrad Zuse
- A calculus, never been implemented
- Could significantly impact PL designs
- Has data types, loop, selection, arrarys, and
record constructs
29Pseudocodes
- (machine code)
- Tedious and error-prone
- Difficult to modify b/c absolute addressing
- Short code (to overcome )
- First language beyond machine code
- Representation of math expressions by (fixed)
numerals - E.g. 07 -- 01 -- - 06 -- abs val
30- Speedingcoding
- John Backus
- Pseudoinstructions for IBM 701
31Fortran (on IBM 704)
- Fortran the 1st compiled high-level PL
- People John Backus
- Timeline
- Fortran 0 1954
- Fortran I 1957
- Fortran II 1958
- Fortran IV 1966
- Fortran 77 1978
- Fortran 90 1992
- Fortran 95 -- 1997
-
32- Evaluation
- Success cannot be overstated
- It dramatically changed forever the way computers
are used - All subsequent PLs owe a debt to Fortran
33Functional Programming LISP
- First functional PL
- John McCarthy at MIT in late 1950s
- Some features of LISP
- Everything is in the form of a list. E.g.
- (A B C D)
- Computations carried out by function applications
- Unlike imperative PLs, variables, assignment
statements, loops are not necessary -
34- Related languages
- Scheme dialect of LISP, 1970s
- COMMON LISP dialect of (pure) LISP, 1980s
- ML functional PL w/ imperative features, static
type inference, early 1980s - Haskell pure functional PL, lazy evaluation,
late 1980s
35ALGOL 60
- Team work ACM GAMM (a German group)
- People John Backus, Peter Naur
- Cooperation of American and European computer
scientists - Intense arguments (e.g. 56.32 or 56,32)
- (e.g. Var experssion or
- experssion var )
36- Some Americans welcomed ALGOL, others rejected it
- Limited success
- The dominant language to formally describe
algorithms - Difficult to understand and implement
- BNF (Backus-Naur Form) was introduced to describe
ALGOL
37(No Transcript)
38COBOL
- First language design for computerizing business
- People Grace Hopper
- Has been extensively used
- Has little impact on subsequent languages (unlike
ALGOL) - Sponsored and pushed by DoD (1960s)
39BASIC
- Designed by J. Kemeny and T. Kurtz at Dartmouth
in 1964 - Easy to learn, easy to use, with limited
capacity, well-suited for beginners - Influenced QBASIC and Visual BASIC
40PL/I
- Attempt to design one language that can replace
all (existing) languages - Designed at IBM in 1960s
- Ambitious motivation
- Failure (people dont like it, e.g. Dijkstra)
41SIMULA 67
- Designed by K. Nygaard and O-J. Dahl in 1960s
(Norwegian) - Extension of ALGOL 60
- The father of OOPL
42ALGOL 68
- Extension of ALGOL 60
- Two new features
- Orthogonality introduced in ALGOL 68 design
(user-defined data type) that has been carried
over to other languages - Dynamic arrays (subscript range not determined
statically)
43Some Descendants of the ALGOLs
- Pascal
- Designed by Niklaus Wirth in 1970s
- Simple and elegant
- Enjoyed high reputation as a teaching language
- Arguable (not) many new features
44- C
- Designed by Dennis Ritchie at Bell Labs in 1970s
- A system PL
- Unix
- Wide range of applications
45- Perl
- Scripting language
- Designed by Larry Wall in 1980
- Similar to an imperative language
- Combination of sh and awk (two other scripting
languages) - Was used as a Unix system admin tool
- Found applications in Web programming
46Prolog
- (The only) logic PL
- Designed by A. Colmerauer, P. Roussel and R.
Kowalski in 1970s - Computation is non-procedural dont state how to
compute the result, rather, state the form of
result. - Foundation predicate calculus/logic
47Ada
- Largest team work in programming history (1980s)
- Sponsored by DoD
- Supports data abstraction, exception handling,
generic procedure, concurrent programming - Pros enclosing state-of-the-art SE concepts.
- Cons too large and complex
48Smalltalk
- Pioneer in OOP
- Most OOP terminologies originate in Smalltalk
- Alan Kay (1960s Ph.D. dissertation)
- Not only a language but also an programming
environment - Impacts windowing and OOP
49C
- Integrating OOP features into C
- Designed by B. Stroustrup at Bell Labs in 1980s
- Enjoyed high popularity in industries and
academia. - Downward compatible to C
- Hybrid language (PO and OO)
50Java
- James Gosling in 1990s
- Initially developed for real-time consumer
electronic devices - Lucky to meet the booming of WWW
- Used for Web programming in early phases
- Multiplatform
- rapid growth
51JavaScript, PHP, and Python
- JavaScript
- Scripting language
- Developed by Netscape Sun in 1990s
- Html-embedded
- Create dynamic html document
- Browser has JavaScript interpreter
52- lthtmlgt
- ltheadgtlttitlegt ex lt/titlegt
- lt/headgt
- ltbodygt
- ltscript type "text/javascript"gt
- lt!--
- var intList new Array(99)
- var listLen, counter, sum0 result0
- listLen prompt("type the length of the
list","") - if ((listLen gt 0) (listLen lt 100))
-
- for (counter 0 counter lt listLen counter)
- intListcounter prompt("type next
number", "") - sum sum parseInt(intListcounter)
-
53- average sum/listLen
- for (counter0 counter lt listLen counter)
- if (intListcountergtaverage)
- result
-
- document.write("number is", result, "ltbr /gt")
-
- else
- document.write("illege list length ltbr /gt")
- // --gt
- lt/scriptgt
- lt/bodygt
- lt/htmlgt
54(No Transcript)
55(No Transcript)
56(No Transcript)
57- PHP
- Developed by R. Lerdorf in 1990s
- Scripting language
- Html-embedded
- Facilitate form processing
- Server-side
- Web server has PHP interpreter
58- Python
- Guido van Rossum in 1990s
- Scripting language
- OO
- System admin, CGI, form processing
59C
- state-of-the-art PL
- Microsoft, A. Heijsberg, 2002
- Based on Java and C
- Part of MS .NET framework
- Support component-based programming
- Success or failure (well see)
60- Ch15 Functional Programming
61Introduction
- Imperative programming base von Neumann
architecture computer - Functional programming base math functions
- Which one is superior?
- Pure functional programming has no side effects
62Obj a
a
x1
a.xlt2
X2
Functional
Obj a
Obj b
x2
x1
a.xlt2
63Functions
- (normal) math notation f(x) x1
- Lambda-notation ?x.x1
- Math notation has gives a name to a function
whereas lambda notation does not. - Higher-order functions functions that take a
function as argument and/or returns a function as
the result
64- E.g. function composition
-
- fcomp(f, g) g f
- Questions how can we do this w/ imperative
programming?
65Features of Functional Programming
- All computations are carried out by function
applications - No side effects (for pure functional
programming)(no need for variables and assignment
statements) - Cannot be replaced by functions in imperative
languages
66Features of Imperative Programming
- Fine processing. Computation consists of many
individual movements and computations of small
items of data - Programming by side-effect. Computation proceeds
by continually changing the state of the
machine the values stored in memory locations
by assignments. - Iteration is the predominant control structure.
Procedures, esp. recursive procedures, take a
back seat
67- The language structures, both data and control,
are fairly close to the underlying (real)
computer architecture. - Ex goto --- unconditional jump
- array --- consecutive blocks of memory
- pointer --- memory location address
- assignment --- data movement
- variable --- memory cell
68Foundations of Functional Programming Lambda
Calculus
- Introduced by Church in the 1930s to study the
computations w/ functions - Basis of functional programming
- Syntax
- M x M1M2 ?x.M
- M term, x variable, M1M2 application,
?x.M abstraction
69- Ex1 x, xx, ?x.x, ?x.(?y.xy) (pure)
- Ex2 ?x.(x1), ?y.(yy2) (extended)
- Substitution N/xM means the result of
replacing all occurrences of x in M by N - Reductions
- a axiom ?x.M ?z.z/xM (z not in M)
- ß reduction (?x.M)N N/xM
70- Examples
- M/xx M
- u/xxx uu
- u/xxy uy
- ?x.x ?y.y/xx ?y.y ?z.z
- (?x.x)/xx ?x.x
- M/x y y
- u/x ?y.y ?y.y
- Note application groups from left to right so
- MNP abbreviates (MN)P
71- u/x(?u.x) u/x(?z.x) ?z.u
- (?x.x1)1 1/x(x1) 11 2
- ?x. ?y. ?z.(xz)(yz)(?x.x)(?x.x)
- (?y.?z.((?x.x)z)(yz))(?x.x)
- (?y.?z.(z)(yz))(?x.x)
- ?z.z((?x.x)z)
- ?z.zz
72- ?x. ?y. ?z.(xz)(yz)(?x.x)(?x.x)
- (?y.?z.((?x.x)z)(yz))(?x.x)
- ?z. ((?x.x)z) ((?x.x)z)
- ?z. zz
- note two computations have different order but
have the same result. ltdiamond propertygt
73- Non-terminating reductions
- ex1 (?x.xx)(?x.xx)
- (?x.xx)(?x.xx)
- .
- Def if f(x)x, then x is called a fix point
of f - ex2 let Y ?f. (?x.f(xx)) (?x.f(xx))
74- then, Yf (?x.f(xx)) (?x.f(xx))
- f((?x.f(xx)) (?x.f(xx)))
- f (Yf)
- so, Yf is a fix point of f. Y is called the
fix - point operator of f.
75- Evaluation strategies
- Call-by-value (eager evaluation) leftmost,
innermost - Call-by-name (lazy evaluation) leftmost, outmost
- Ex (?x.xx)((?y.y) (?z.z))
- (?x.xx)(?z.z)
- (?z.z)(?z.z)
- (?z.z) (call-by-value,
eager eval)
76- (?x.xx)((?y.y) (?z.z))
- ((?y.y)(?z.z)) ((?y.y)(?z.z))
- (?z.z)((?y.y)(?z.z))
- (?y.y)(?z.z)
- ?z.z (call-by-name, lazy eval)
77- An applied Lambda Calculus
- M c x M1M2 ?x.M ( c constants)
- c true false if 0 iszero pred
succ fix - Ex a term of applied ? calculus is
- ( ( ( if x) y) true )
- which can be abbreviated as
- if x y true
78- Reduction rules for constants
- if true M N M
- if false M N N
- fix M M(fix M)
- iszero 0 true
- iszero (succk 0) false (k gt1)
- iszero (predk 0) false (k gt 1)
79- pred(succ M) M
- succ(pred M) M
- Moreover, we can intuitively regard
- 0 ? 0
- succ 0 ? 1
- succ (succ 0) succ2 0 ? 2
- pred 0 ? -1
- pred(pred 0) pred2 0 ? -2
80- Ex
- (?x. if x 0 (succ 0) ) false
- if false 0 (succ 0)
- succ 0 ( or 1)
81- Functions with more than one parameters
- Ex f(x,y) x y
- How to code this function in Lambda calculus?
- ?x.?y.(xy) (suppose
defined) - f(1,1)2
- ?x.?y.(xy)(1)(1) ?y.(1y)1 112
-
82- ML core (ML0) is a syntactically sugared applied
lambda calculus - Lambda Cal ML0
83(No Transcript)
84Relationship between recursive functions and fix
point
- f(x) .f f recursively defined
- f(x) Mf syntactic hole
- f ?x.Mf writing f in lambda notion
- (?g.?x.g/fM)f
- f Ff f is the fix pt of F,
where - F ?g.?x.g/fM
85f or f(x)
- note f is not the same as f(x)
- what do you mean?
- why?why?why?why?
86- f is the function (itself)
- f(x) is the element in the codomain to which x is
mapped under f - in terms of programming, f(x) is the value
returned by the function f when x is submitted to
f
87- ex A 1,2,3, B 2,3,4
- f is a function from A to B, (and its
behavior) is specified by - f(x) x1 x in A
- then,
- f(1) 2, f(2) 3, f(3) 4
- what do they mean?
88- Can we write out f directly?
- In traditional math?
- In lambda calculus?
89How to define recursive functions w/o using
function names?
- Fix pt answers the question
- Ex
y if x0
x y
(x-1) (y1) otherwise
is defined recursively (why?) the operation
(recursively) using fix point notation
90y if iszero x
plus x y
plus (pred x) (succ y) otherwise
Rewriting plus, we have
plus ?xy. if (iszero x) y (plus (pred x) (succ
y)) (?f. ?xy. if (iszero x) y (f (pred
x) (succ y)) ) plus
then plus fix (?f. ?xy. if (iszero x) y (f
(pred x) (succ y)) ) This is the definition of
plus w/o using its name.
91Introduction to Scheme
Logo of MIT Scheme
http//swiss.csail.mit.edu/projects/scheme/
92- To start shceme
- Type mzscheme at the Unix prompt.
- (if this does not work, make sure your path
variable include /usr/local/plt/bin) - Once scheme is stared, you can load your program
by typing - (load filename)
93- Dialect of Lisp
- Sussman and Steele, 1970s, MIT
- Small size, clean syntax and semantics,
well-suited for college functional programming
courses - Primitive numeric functions
- , -, , /
- ex ( 3 4) ? 7 ( 3 4) ? 12
94- Define functions
- w/o name ((lambda (x) ( x x)) 4 ) ? 8
- w/ name (define (doubleit x)
- ( x x)
- )
- ( doubleit 4) ? 8
- Boolean values
- t f ( or empty list () )
95- Selection structures
- (if exp then-exp else-exp )
- Ex (if ( n 0) 1 ( n n) )
- Conditional
- (cond
- (pred1 exp1)
- (pred2 exp2)
- (predn expn)
- (else exp)
- )
96- (define (f x)
- (cond
- ( ( x 0) (display "zero"))
- ( (lt x 0) (dispaly "neg"))
- ( else (display "pos"))
- )
- )
97- List manipulations
- car takes a list and returns the head of a list
- cdr takes a list and returns it w/o the head
- Ex (car (a b)) ? a
- (car (a) ) ? a
- ( cdr (a b) ) ? (b)
- cons takes two parameters and returns a list l
w/ the first parameter being (car l) and the
second parameter being (cdr l)
98- Ex ( cons a (a b) ) ? (a a b)
- Some predicates
- eq? checks to see if the two parameters are
equal - List? checks to see if the argument is a list
- Null? checks to see if the argument is an empty
list
99- Scheme examples
- Given an atom x and a list l, determine if x is
in l - (define (member x l)
- (cond
- ( (null? l) f )
- ( (eq? x (car l)) t )
- ( else (member x (cdr l)) )
- ))
100- Walk it through with
- X1 (1 3 4)
- X1 (a b c)
101- Determine if l1 and l2 are equal (l1 and l2 do
not have to be lists) - (define (equal l1 l2)
- (cond
- ( (not (list? l1)) (eq? l1 l2)
) - ( (not (list? l2)) f )
- ( (null? l1) (null? l2) )
- ( (null? l2) f )
- ( (equal (car l1) (car l2))
- (equal (cdr l1) (cdr l2)) )
- ( else f )
- ))
102- Walk it through with
- L1a, l2 (a b)
- L1 (a b), l2 a
- L1 (), l2 3
- L1 (a b) , l2 ()
- L1 (a b), l2 (a c)
- L1 ((a b), c) , l2 (c d e)
- L1 ((a b) c d), l2 ((a b) c d)
103- Combine two lists
-
- (define (app l1 l2)
- (cond
- ( (null? l1) l2)
- ( else (cons (car l1) (app (cdr
l1) l2)) ) - ))
104- Walk it through with
- L1 (a) , l2 (b)
- L1 (a), l2 (a b b)
- L1(), l2 (a b)
- L1 (), l2 2 (??)
- L1 1, l2 2 (?????)
-
-
105(No Transcript)
106(No Transcript)
107- Find common part of two lists
- (define (comm l1 l2)
- (cond
- ( (null? l1) () )
- ( (member (car l1) l2)
- (cons (car l1) (comm (cdr l1) l2)
) - )
- ( else (comm (cdr l1) l2))
- )
- )
108- Higher-order functions
- How to code composition function?
- (define (comp f g)
- (lambda (x) (f (g x)) )
- )
109- Ex in math, f(x) x1, g(x) x2
- (f g)(x) f(g(x))
f(x2) x3 - in particular, (f g)(1)
4 - In scheme,
- (define (f x)
- ( x 1))
- (define (g x)
- ( x 2))
- ((comp f g) 1) 4
110- Map-all
-
- (define (mapall f l)
- (cond
- ( (null? l) () )
- ( else (cons (f (car l))
- (mapall f
(cdr l)) ) - )
- ))
- mapall takes a function f and a list (x1,, xn)
and returns (f(x1), , f(xn))
111- Ex
- if I is the identity function, then
- (mapall I (a b c)) ? (a b c)
- if g(x) x1, then
- (mapall g (1 2)) ? (2 3)
112ML Essentials
- Why study ML?
- Scheme is not the only one
- Different style
- Its important features
- How to starts ML?
- At Unix prompt, type
- sml ltentergt, or
- sml lt foo, or
- Under the interactive mode,
- - use foo
113- Expressions
- 12
- Val it 3 int
- Ground types int, real, bool, string, char
- Arithmetic operators , , /, div, , -
- note / -- real division, div int
division - -- negative sign
114- Ex 34 1 4 3 1
- 43 ? 41 ?
- 4 3.0 ? 43.0 ?
- 4.03.0 7.0 4 mod 2 0
- 4.0 mod 2.0 ?
- 2/4 ? 2 div 4 0
- 2.0 / 4.0 0.5 3.04.0 12.0
- Note ML is (very) strongly typed (as opposed to
Scheme) - do not mix different types of operands
115Typings of Languages
- Strongly typed statically typed manifest
types (e.g. ML) - Weakly typed dynamically typed latent types
(e.g., Scheme)
116- Relational operators
- , lt, gt, lt, gt
- ex 1lt2 ? true
- 3.0 gt 1.0 ? true
- ab lt abc ? true
- 4 gt 3.0 ?
-
117- Logical operator not, andalso, orelse
- ex (1gt2) andalso (2gt1) ? false
- Conditional expression
- if E then F else G ? F when Etrue, G
otherwise - ex if (11) then 2 else 3 ? 2
118- Variable bindings (val-declarations)
- val a 1 val b 2 val c ab
- (note variable binding is not assignment
statement) -
119- Tuples and Lists
- Tuples ex (1, 2) intint
- (1, 2, 3.0) intintreal
- (1, 2, (1, 2))
intint(intint) - 1 (1,2) ? 1 int
- val t (1,2,(1,2))
- 3 t ? (1,2) intint
120- Lists
- 1,2,3 int list
- 1 int list
- a list
- a, ab string list
- Q do items in lists (or tuples) have to be of
the same type? How is this issue reflected on
the types of lists (or tuples)?
121- Operations on lists
- let l a1,a2,,an
- then hd and tl are defined as follows
- hd l a1 tl l a2,,an
- ex
- hd 1,2,3 1 int
- tl 1,2,3 2,3 int list
- hd 5 5 int tl 5 int
list
122- Concatenation of list list1 _at_ list2
- Ex
- a,b _at_ c,d
a,b,c,d - Cons ele list
- Ex
- 12,3 1,2,3 int list
- 1 1 int list
- 1 a ?
-
123- 1 1, 2 1, 1, 2 int
list list - 1 2 3
- 1 (2 (3 ))
- 1 (2 3 )
- 1 2, 3
- 1,2,3 int list
- Note is right-associative
-
124- Type constructions in ML (recall the
orthogonality issue in PL design) - If T1, T2, , Tn are types, then
- T1T2Tn is also a type
- If T is a type, then T list a also a type
- Ex intint, (intint)int, int list,
intint list - (intint)int list list
-
125 126(No Transcript)
127- Defining functions
- Ex fun sq (x real) xx
- sq real ? real
- (compare it w/ Scheme counterpart)
- (define (sq x)
- ( x x)
- )
- sq 2.0 4.0 sq 3 ?
128- Ex fun max (a,b,c)
- if agtb then
- if agtc then a
else c - else
- if bgtc then b
else c -
- val max fn intintint ? int
- note ML tries its best to infer types. (type
inference)
129- fun id x x
- val id fn a ? a
- (a is the type variable in ML)
- fun id (xint) x
- val id fn int ? int
130- fun reverse L if Lnil then nil else
- reverse(tl L)
_at_ hd L - val reverse fn a list ? a
list - ex reverse 1,2,3 3,2,1 int list
- we know the following (or do we?)
131- fun c (n,m) if m0 then 1
- else if mn then 1
- else c(n-1,m)
c(n-1,m-1) - Val c fn intint ? int
132- study the following codes and compare them
- public class T
- static int x 3
- public static int f(int a)
- return ax
- public static void main(String args)
- x10
- System.out.println(f(2))
-
result is 12
133- val x 3
- fun f a ax
- val x10
- f 2 ( value of f(2)? )
- val it 5
- x ( value of x? )
- val it 10
- This ex shows the variable diff between fun
programming and imperative programming.
134- Pattern matching in ML
- Ex Reversal of lists
- fun rev (nil) nil
- rev (xxs) rev (xs) _at_ x
- What is the type of this function?
135- Ex merge sort
-
- fun merge (nil,M) M
- merge (L,nil) L
- merge (L as xxs, M as yys) if xlty
then - x
merge(xs,M) - else y
merge(L,ys) - (what is the type of merge?)
136- Local defintions
- fun f x
- let
- val y 1 in
- xy
- end
-
- type of f ?
-
137- fun f x
- let
- fun g x x1
- in
- g x
- end
- f simulates g.
138- Chapter 16 Logic Programming and Prolog
139Introduction
- Logic programming is declarative rather than
procedural. I.e., it only specifies what we
want, not how to do it. - Logic programming languages are also called
declarative languages. - Basis of logic programming propositional and
predicate calculus (cmps 220)
140Propositions and Clausal Form
- Proposition?
- statements that are either true or false.
- Ex
- 112,
- There are two Mondays in a week.
- mom(mary, bob)
- greater(1,2)
- What are you saying?
141- Negation, and , and or
- Clausal Form (standard form, canonical form)
- Ex greater(a,c) ? greater(a,b) greater(b,c)
- like(bob,trout) ? like(bob,fish)
fish(trout) - dad(a,b) v mom(a,b) ? parent(a,b)
142Resolution (theorem proving)
- Resolution is the foundation on which logic
programming is based - Resolution is based on the tautology (on what?)
- (p ? q) (q ? r) ? (p ? r)
- or (really?)
- (not p v q) (not q v r) ? not p v r
143- i.e. not p v q
- not q v r
- ------------- (q and not q
canceled) - not p v r
- Proof?
- (not p v q) (not q v r) ? not p v r
- not(not p v q) v not(not q v r) v (not p v
r) - (p not q) v (q not r) v (not p v r)
--- ()
This is a more accessible form
144- When pT, () reduces to
- not q v (q not r) v r
- (not q v r) v (q not r)
- not(q not r) v (q not r)
- T
- when pF, () reduces to
- F v (q not r) v T
- T
- Hence, () is a tautology.
145- ex given
- father(a,b) v mother(a,b) ? parent(a,b)
- gfather(a,c) ? father(a,b) father(b,c)
- By resolution, we have,
- mother(a,b) v gfather(a,c) ? parent(a,b)
father(b,c) - Justification?
-
146.
- not parent(a,b) v father(a,b) v mother(a,b)
- not father(a,b) v not father(b,c) v gfather(a,c)
- --------------------------------------------------
----------- - not parent(a,b) v mother(a,b) v not father(b,c) v
gfather(a,c) - not (parent(a,b) father(b,c)) v (mother(a,b)
v gfather(a,c)) - mother(a,b) v gfather(a,c) ? parent(a,b)
father(b,c)
147Prolog
- Developed by Colmerauer, Roussel and Kowalski in
1970s - Many dialects exist
- Widely used one Edinburgh Prolog (manual on
website) - Limited success and use (now)
148- Conventions
- Variables strings that begin w/ an uppercase
letter. ex T, S, X, X1, Y2 - Atom strings that begin w/ a lowercase letter.
Ex mary, bill, apple, tree, dog. - Values are bound to variables.
149- Instantiation (of a variable)
- binding of a a value to a variable.
- Facts
- Propositions that are true to the system.
- headless Horn clauses.
- Ex man(bill).
- like(a,b).
- greater(a, b).
150- Note
- Facts have no intrinsic meanings. They can be
whatever you want them to be. - Ex like(a,b) a likes b or b likes a
- greater(a,b) agt b or b gt a
151- Rules implications in the form of
- B - A1, A2, , An
- (Headed Horn clause)
- Ex ancestor(mary, al) - mom(mary, al).
- dangerous(X) - bigteeth(X).
- greater(X,Z) - greater(X,Y),
greater(Y,Z).
152- Goal query submitted to the system.
- Ex ?- man(bill).
- ?- greater(two, three).
153- How to start Sussex Prolog
- At Unix prompt, type prolog
- prolog
- user.
- type facts, rules, and goal
- Alternatively, prolog ltfilegt
154- If command line prolog not working, then type
the following (under bash-shell or Borne-shell) - usepop/usr/local/poplog/v15.53
- poplocal/usr/local/poplog
- local/usr/local/poplog/local
- export usepop poplocal local
- . usepop/pop/com/poplog.sh
- And then, prolog
155- Try the following
- ex
- man(bill).
- ?- man(bill).
- yes
- ?- man(mary).
- no
156Logical Inference
- Reasoning chain
- Forward chaining (starting w/ facts and ruels)
Facts and rules
goal
157- backward chaining (starting w/ goal)
Facts and rules
goal
158- Prolog uses backward chaining.
- ex
- father(bob).
- man(X) - father(X).
- ?- man(bob).
- yes
159- Prolog searches the facts first. No success.
Then, it uses the rule. So X is instantiated to
bob and father(bob) must be satisfied. Happily,
father(bob) is a fact. So the rule is satisfied
(with X instantiated to bob)
160- Satisfaction strategy Depth-first or
breadth-first. - Depth-first satisfying the first subgoal before
satisfying the second - Breadth-first all subgoals are worked on in
parallel. - Depth-first used in Prolog.
161- Ex
- greater(1,2).
- greater(2,3).
- greater(X,Z) - greater(X,Y), greater(Y,Z).
- ?- greater(1,3)
- yes
162- Using backward chaining, Prolog takes the goal
and searches the database trying to satisfy the
goal. Facts will be searched first, no success.
Rules will be searched next. X and Z are
instantiated to 1 and 3. Can Y be instantiated
to some value so that the rule will be satisfied?
To do that, we need to satisfy the greater(1,Y)
and greater(Y,3) (two subgoals). Prolog finds 2
for Y in satisfying greager(1,Y) and this value
also satisfies greater(Y,3). So Y2. Hence the
rule is satisfied. (So are we. ?)
163- Backtracking
- When a subgoal fails, the previous subgoal needs
to be re-satisfied (by using different facts and
rules). - Ex
- color(john,blue).
- color(john,red).
- color(mary,red).
- color(mary,green).
- ?- color(john,X), color(mary,X).
- Xred yes
164- Ex
- likes(jake, chocolate).
- likes(jake, apricots).
- likes(darcie, licorice).
- likes(darcie, apricots).
- ?- likes(jake, X), likes(darcie, X).
- X apricots
165which can be graphically regarded as
Xchocolate Xapricots
-----------------
Xapricots
166- Prolog is from top to bottom and from left to
right - Top to bottom the order in which Prolog searches
the database - Left to right depth-first subgoal satisfaction
strategy.
167- Lists
- Similar to ML
- Ex a,b,c apple, orange, grape
- Head and tail X Y denotes a list where
- X is head, Y is tail
- ex mylist(a,b,c).
- mylist(john, keith).
- ?- mylist(X Y).
- X a Y b,c yes
168- Ex append two list
-
- app(, List, List).
- app( Head L1, L2, Head L3 ) -
- app(L1, L2, L3).
- ?- app(a,b,c, x,y, X).
- X a,b,c,x,y
- yes (as shown
in the box model)
169call
a,b,c/HL1, x,y/L2, X/HL3
fail
b,c/HL1, x,y/L2, L3/HL3
call
fail
c/HL1, x,y/L2, L3/HL3
call
fail
/, x,y/List, L3/List
call
fail
redo
exit
L3x,y
exit
redo
L3c,x,y
exit
redo
L3b,c,x,y
redo
exit
Xa,b,c,x,y
170- Ex
- op(,).
- op( Head Tail, List) - op(Tail,
Res), - app(Res, Head,
List). - ?- op(a,b,c,X).
- What does op do?
-
171Problems of Prolog
- Control order
- Lack of need of programmers concern over control
order of programs is supposed to be one of the
advantages of logic programming. - It is not the case with Prolog.
- Prologs order of evaluation is left-to-right(
depth-first). Consider the following code
172- ancestor(X,X).
- ancestor(X,Y) - ancestor(Z,Y), parent(X,Z).
- ?- ancestor(a,b).
- It causes an infinite loop (why?). But switching
the order of the right side of the rule fixes the
problem (while the meaning and execution of the
program not supposed to be changed).
173- Double negation
- Consider the goal
- member(X,m,f,a) ------------- (a)
- Prolog would yield yes with X instantiated to m.
Logically, - not(not (member(X,m,f,a))) --------- (b)
- would do the same thing. However, Prologs
response to (b) is yes with X uninstantiated.
174- member(X,m,f,a
-
- yes (success), X -gt m
- not(member(X,m,f,a))
-
- no (fail), uninstantiate X -gt m
- Not(not(member(X,m,f,a)))
-
- yes (success), argument failed, X not
instantiated
175- Still need algorithmic details
- Sorting example, consider the following sorting
Prolog code -
- sorted(old-l,new-l) - permute(old-l,new-l),
-
sorted(new-l). - sorted().
- sorted(X).
- sorted(X,YList) - X lt Y,
sorted(YList).
176- The system does not know how to sort, other than
producing all possible permutations of the list
until one list matches the specification (sorted
then). In this case, the Prolog sorting program
needs to specify sorting details, as is the case
in imperative and functional programming.
177- Chapter 3 Syntax and Semantics
178Syntax
- BNF Grammar
- Backus-Naur Form (BNF) is the standard formal
method for describing syntax of PLs. - ltLHSgt ltRHS1gt ltRHSngt
- --- can be replaced by
- ---- or
179- Terminals and non-terminals
- Non-terminals can be rewritten terminals cannot.
- Ex S A a, A aA b
- where
- S, A are non-terminals a,b are
terminals
180- ex grammar of Lambda Calculus
- M x ?x.M MM (untyped)
- M x ?(xa).M MM (typed,
-
simplified) - a int a ? a
181- Ex a BNF grammar
- ltproggt begin lts_listgt end
- lts_listgt ltstmtgt ltstmtgtlts_listgt
- ltstmtgt ltvargt ltexpgt
- ltvargt ABC
- ltexpgt ltvargtltvargt ltvargt-ltvargt ltvargt
182- A program written in this language is
-
- begin
- A B
- end
- with leftmost derivation as
183- ltproggt gt begin lts_listgt end
- gt begin ltstmtgt end
- gt begin ltvargtltexpgt end
- gt begin A ltexpgt end
- gt begin A ltvargt end
- gt begin A B end
184- Ex a grammar for assignment statement
- ltassgngt ltidgt ltexpgt
- ltidgt A B C
- ltexpgt ltidgt ltexpgt
- ltidgt ltexpgt
- ( ltexpgt ) ltidgt
-
185- A sentence (assignment statement) derived from
this grammar is - A B(AC)
- ltassgngt gt ltidgt ltexpgt
- gt A ltexpgt
- gt A ltidgt ltexpgt
- gt A Bltexpgt
- gt .
- gt A B(AC)
186 187- Consider A B C A
- What would be its parse tree?
188- ltassgngt
- ltidgt ltexpgt
- A ltidgt ltexpgt
- B ltidgt
ltexpgt - C
A
189- Consider A B C A
- What would be its parse tree?
190- ltassgngt
- ltidgt ltexpgt
- A ltidgt ltexpgt
- B ltidgt
ltexpgt - C
A
191- Ambiguous Grammars
- A grammar is called ambiguous if a sentence
produced by this grammar has more than one parse
trees. - Note parse trees, not derivations.
- A sentence may have two different derivations but
one unique parse tree. Ex?
192- Consider this grammar
- ltassgngt ltidgt ltexpgt
- ltidgt A B C
- ltexpgt ltexpgt ltexpgt
- ltexpgt ltexpgt
- ( ltexpgt )
- ltidgt
193- What would be the parse tree for
- A B C A ?
194(No Transcript)
195- To fix the problem, we need to rewrite the
grammar
196- Parse tree for
- A B C A
- again?
197(No Transcript)
198- Summary
- Three kinds of grammars
- good, bad, ugly
- Derivations and parse trees
- Two diff derivations may have one parse tree
- One derivation has one parse tree
- One parse tree may have two derivations
- Tow diff parse trees have diff derivations
199- More specifically,
- Derivation is concrete
- Parse tree is abstract
- Given a derivation, we can draw its corresponding
parse tree - Given a parse tree, we dont know which
derivation it corresponds to .
200Semantics
- Operational semantics
- Denotational semantics
- Axiomatic semantics
201- Operational semantics
- Specifies the meaning of a program in the
interest of execution. - Ex the operational semantics of
- if cond A then B endif
- is cond is evaluated first. If the result is
true, then A is the value of this sentence
otherwise, B is the value of this sentence.
202- In terms of evaluation rules
- If cond ? cond , then if cond then A else B
- ? if cond then A else B
- If true then A else B ? A
- If false then A else B ? B
203- Ex the operational semantics of
- (?x.M)N
- is (?x.M)N reduces to N/xM
204- Axiomatic semantics
- Semantics specified (indirectly) by inserting
preconditions and postconditions for statements. - General form P S Q, where
- P precondition
- Q postcondition
- S sentence
205- Ex a gt 1
- x a 1
- x gt 2
- xlt2, ylt7
- y 3x 1
- x y 3
- x lt 10
206- Denotational semantics
- Math meaning/model of PLs.
- Highly sophisticated.
- Involving set theory, domain theory, category
theory, and overlaps computer science, math, and
logics. - Dana Scott
207- Ex x ?(x) ? environment
- ?(xa).M f where f is a
function - from a to ß,
where ß is the - type of M
- MN MN