Upload
votuong
View
220
Download
3
Embed Size (px)
Citation preview
Programming Languages Classification Overview
Logic Languages Features
Prolog Evaluation
Robinson Unification Algorithm
15.-19. 4. 2013 Dušan Kolář 2
Programming language is an intermediary mean between common speech and sequence of usually binary digits
A finite set of commands of a specific syntactic form with strictly defined semantics
15.-19. 4. 2013 Dušan Kolář 3
Various viewpoints ◦ Data abstraction level
no data abstraction atomic data types structures objects terms
◦ Control flow paradigms imperative / declarative structured, OO / functional, logic, …
◦ Domain applicability data manipulation languages control/signal flow languages
◦ etc.
15.-19. 4. 2013 Dušan Kolář 4
Imperative paradigm requires specification of ◦ What is to be processed
◦ How it is to be processed
Execution model - processing is done by consecutive modification of a program internal state ◦ Mimics von Neumann architecture
◦ For instance: language C defines a clear state with every semicolon (;)
15.-19. 4. 2013 Dušan Kolář 5
Non-structured languages ◦ old (Basic), scripting ones (shell), …
Structured languages ◦ C, Pascal, Ada, …
OO languages ◦ C++, Java, C#, …
Modularity? ◦ Orthogonal feature
15.-19. 4. 2013 Dušan Kolář 6
Declarative paradigm requires specification of ◦ What is to be processed
Internal state and order of processing is “unknown” for us
Order is denoted by a compiler and an evaluation strategy – we know this, though!
Repetitive processing is expressed by recursion
15.-19. 4. 2013 Dušan Kolář 7
Usually we talk about these kinds of languages ◦ (Purely) functional languages ◦ Logic languages (formally all, from a user
viewpoint just some of them belong to this category)
◦ DML and DDL (some of them – SQL) ◦ Certain languages with GUI specification
language ◦ Some languages for HW description
Structural VHDL Behavioral VHDL (imperative language)
◦ etc.
15.-19. 4. 2013 Dušan Kolář 8
Representatives: ◦ Prolog, Parlog, Gödel, CLP(R), KL1 (KLIC)
Formal base: ◦ Subset of predicate logic
Typical features: ◦ Control flow
clauses, predicates, term unification
◦ Data terms as a single data structure over a few atomic
types
lists
Why? Automated proof
15.-19. 4. 2013 Dušan Kolář 9
Let us compare two approaches ◦ Imperative – language C
◦ Declarative – Prolog
Used algorithms ◦ Factorial
◦ Quick-sort
Not suitable for logic paradigm! ◦ But let us see…
15.-19. 4. 2013 Dušan Kolář 10
int ftrl(int n) {
if (n<0) {
error(“Negative input”);
exit(1);
}
else if (n<2) return 1;
else return n * ftrl(n-1);
}
6 effective lines
◦ optimized on lines, not speed
15.-19. 4. 2013 Dušan Kolář 11
void quicksort(int array[], int left_begin, int right_begin) { int pivot = array[(left_begin + right_begin) / 2]; int left_index, right_index, pom; left_index = left_begin; right_index = right_begin; do { while (array[left_index] < pivot && left_index < right_begin) left_index++; while (array[right_index] > pivot && right_index > left_begin) right_index--; if (left_index <= right_index) { pom = array[left_index]; array[left_index] = array[right_index]; array[right_index] = pom; if (left_index < right_begin) left_index++; if (right_index > left_begin) right_index--; } } while (left_index < right_index); if (right_index > left_begin) quicksort(array, left_begin, right_index); if (left_index < right_begin) quicksort(array, left_index, right_begin); } /* 19 effective lines */
15.-19. 4. 2013 Dušan Kolář 12
ftrl(N,_) :- N<0, !, fail. ftrl(N,1) :- N<2, !. ftrl(N,R) :- NN is N-1, ftrl(NN, RR), R is RR*N.
quick([],[]). quick([H|T],S) :- split(T, H, A, B), quick(A, A1), quick(B, B1), append(A1,[H|B1],S). split([], _, [], []). split([X|X1], Y, [X|Z1], Z2) :- X < Y, split(X1, Y, Z1, Z2). split([X|X1], Y, Z1, [X|Z2]) :- X >= Y, split(X1, Y, Z1, Z2).
15.-19. 4. 2013 Dušan Kolář 13
What a formal base is? ◦ Formal mean (calculus, algebra, …) that can be used for
description of all language constructs
What is it for? ◦ Proof of language properties (soundness, semantics,
etc.) ◦ Implementation guide
Logic Languages - Predicate logic ◦ Subset! ◦ Various approaches
Existence of a formal base is a must ◦ Automated proof ◦ Power of predicate logic
15.-19. 4. 2013 Dušan Kolář 14
Terms (functions, variables, constants) Predicates => formulas (quantifiers,
connectives) Interpretation of functions, constants,
predicates Value assignment to (free) variables Formula – partial decidability ◦ Interpretation that holds for every valuation of
variables – model ◦ Satisfiability
There is a model ◦ Validity
Formula is satisfied in all interpretations
15.-19. 4. 2013 Dušan Kolář 15
Well formed formulas – wffs wffs + modus ponens + generalization
if P (P Q) then Q
from P derive (x)P
w: wffs K: set of wffs
K w w holds in all models of K
Logical consequence
15.-19. 4. 2013 Dušan Kolář 16
Terms -> Data
Not too many basic atomic types ◦ Integers
◦ Characters
◦ Floating point numbers
◦ Boolean values
Lists
Terms ~ over previous, as in the P.L. ◦ e.g. data(john,smith,’612 66’,[12,3,75],178)
15.-19. 4. 2013 Dušan Kolář 17
Predicates
Not every predicate can be expressed
Usually closed formulas
E.g. Prolog – Horn clauses
Logic syntax D ::= A | AG | x.D | DD // ~
G ::= A | GG A ::= atomic formula
Abstract/Prolog syntax D ::= A | A :- G. | D D
G ::= A | G, G
15.-19. 4. 2013 Dušan Kolář 18
Prolog app([],X,X).
app([E|X], Y, [E|Z]) :- app(X,Y,Z).
P.L. x.(app [] x x) exyz.((app [e|x] y [e|z])(app x y z))
Closed formulas
15.-19. 4. 2013 Dušan Kolář 19
Horn clauses H B1 B2 … Bn n>0
H … head
B1 B2 … Bn … body
H
fact
Prolog syntax H :- B1 , B2 , … , Bn.
H.
15.-19. 4. 2013 Dušan Kolář 20
In general, it is infinite
We use goals to test/verify
B1 B2 … Bn n>0
:- B1 , B2 , … , Bn. n>0
15.-19. 4. 2013 Dušan Kolář 21
p(X1, … Xn) :- sg1(A11, … , A1m1), … sgk(A1k, … , Akmk).
q(1,[]).
r(tree(D,L,R),tree(DD,LL,RR)) :- …
15.-19. 4. 2013 Dušan Kolář 22
Non-typed languages usually ◦ Types determined when necessary, otherwise term
◦ Lists are heterogeneous
If typed – e.g. Gödel ◦ Explicit typing
◦ Usually much more expressive
Atoms play key role ◦ Parameter-less terms
◦ They can contain “anything”
e.g. ’12.4.2004 ~ meeting * Important!’
15.-19. 4. 2013 Dušan Kolář 23
Declaration may not be supported
A kind of declaration may appear for ◦ Dynamic clauses ◦ Exported predicates (out of the module)
Prototype = predicate (clause) name with a number of its parameters
Overloading on the level of parameters number
Type recognition does not make a sense in this case
15.-19. 4. 2013 Dušan Kolář 24
Top-down and bottom-up ◦ Both possible
Later addition of other construct is easy ◦ No declaration required
Some languages seems like they are not fully declarative ◦ Order of elements within one clause/predicate body may be
significant
Built-in predicates ◦ Optimized “user” ones ◦ Special
Side effects I/O, change of DB
Cannot be repeatedly satisfied (goal satisfaction follows)
Prolog – program modification during runtime by itself
15.-19. 4. 2013 Dušan Kolář 25
Query (we give) = goal ◦ Goal is composed from subgoals
◦ e.g. man(X), wife(X,Y), twins(Y,YY).
If we get a result then the goal is satisfied
Otherwise the goal failed
Satisfaction may be repeatable ◦ Programmers predicates
◦ e.g. man(tom). man(bob). man(jack).
15.-19. 4. 2013 Dušan Kolář 26
Flow control based on clause/fact selection ◦ Clause: head + body
◦ Horn clauses (PROLOG) a(...) :- b1(...), ..., bm(...).
(a(...) b1(...) ... bm(...)) ~ (a(...) (b1(...) ... bm(...)) ) ~ (a(...) b1(...) ... bm(...))
b1(...) satisfied then ~ (a(...) T ... bm(...))
= (a(...) F ... bm(...)) = (a(...) ... bm(...))
15.-19. 4. 2013 Dušan Kolář 27
Repetitive constructs (loops, etc.) ◦ Recursion – in general
◦ Backtracking – PROLOG (not only)
Goal (composed from subgoals) ◦ Satisfied X fails
◦ Re-satisfied subgoal
15.-19. 4. 2013 Dušan Kolář 28
SLD resolution ◦ Horn clauses
Programmer’s view: ◦ Predicate definitions (clauses) are processed in a
top-down manner, as specified in the source code
◦ Bodies of predicates are processed from left to right until the given item is processed
◦ Formally: Depth first search Until the first (leftmost) subgoal is solved the others
remain unprocessed Backtracking Re-satisfaction, side effects, …
15.-19. 4. 2013 Dušan Kolář 29
a(...) :- b(...), c(...), d(...). a(...) :- d(...), e(...).
b(...) :- c(...), d(...), c(...). b(...) :- e(...).
c(...) :- d(...). c(...) :- e(...), d(...), c(...).
d(...). d(...) :- e(...), d(...).
e(...).
a
b
c
d
e
15.-19. 4. 2013 Dušan Kolář 30
a(...) a(...) :- b(...), c(...), d(...).
b(...) :- c(...), d(...), c(...).
c(...) :- d(...).
d(...).
d(...) :- e(...), d(...).
e(...).
Unification
Flow pass
15.-19. 4. 2013 Dušan Kolář 31
Predicate parameters – terms ◦ Containing (free) variables
◦ Predicate expects terms of a certain shape
◦ Pattern matching
Mutual binding among subgoals – variables
Term equivalence must be tested/verified/forced ◦ Unification
Most general unifier (MGU)
There’s no other more general – identity/equality up to renaming
15.-19. 4. 2013 Dušan Kolář 32
Occurrence check ◦ PROLOG – no check on occurrence
Depth of unification ◦ Terms may stop the unification algorithm
Infinite/loop structures may appear ◦ Usually forbidden
◦ Tightly coupled with occurrence check
15.-19. 4. 2013 Dušan Kolář 33
tree(X, leaf, Y) tree(128, Z, tree(W,U,leaf)) ◦ the same terms – name & arity
◦ continue on parameters
MGU (most general unifier) ◦ X = 128
◦ Z = leaf
◦ Y = tree(W,U,leaf)
◦ W,U – remain “free”
15.-19. 4. 2013 Dušan Kolář 34
tree(X, leaf, Y) tree(9, Z, tree(W,Y,leaf)) ◦ similar situation as in Ex. 1
◦ watch the 3rd arguments
MGU ◦ X = 9
◦ Z = leaf
◦ Y = ???
Implementation dependent
15.-19. 4. 2013 Dušan Kolář 35
Robinson (occurrence check) mgu(t1,t2) ◦ If t1=t2 then stop (done), no unification required ◦ Find substitution for (some) disagreement pair S
If it is not found then fail If it is found then store the substitution, apply it and find
mgu S’ as mgu(t1.S,t2.S) – if found then the result is S.S’
Disagreement pair <c1, c2> processing ◦ Various terms (names) -> fail ◦ One of the c1, c2 is a variable
Occurrence check S = [term/variable]
◦ Terms of the same name -> search mgu(c1, c2) Recursion
15.-19. 4. 2013 Dušan Kolář 36
t1 = tree(X, leaf, Y) t2 = tree(128, Z, tree(W, U, leaf))
<X, 128> ◦ S1 = [128/X] ◦ t1 = tree(128, leaf, Y)
t2 = tree(128, Z, tree(W, U, leaf)) <leaf, Z> ◦ S2 = [leaf/Z] ◦ t1 = tree(128, leaf, Y)
t2 = tree(128, leaf, tree(W, U, leaf))
<Y, tree(W, U, leaf)> ◦ S3 = [tree(W, U, leaf)/Y] ◦ t1 = tree(128, leaf, tree(W, U, leaf))
t2 = tree(128, leaf, tree(W, U, leaf))
t1 = t2 S = S1.S2.S3 S = [128/X].[leaf/Z].[tree(W, U, leaf)/Y]
15.-19. 4. 2013 Dušan Kolář 37
CLP systems ◦ Constraint Logic Programming
◦ List of substitutions is not a result!
◦ Result = tightened constraints for free variables
Equations
Non-equations
...
◦ Over Q, R
◦ Today extensions of Prolog evaluators
15.-19. 4. 2013 Dušan Kolář 38
Programming language Gödel ◦ Evaluation strategy does not follow top-down and
left-to-right strategy ◦ A suitable subgoal is selected
Immediate binding values to variables
Depth of the subtree
Etc.
◦ Closer to full declarative programming ◦ Universal and existential quantifiers ◦ Etc.
15.-19. 4. 2013 Dušan Kolář 39
Any questions?
15.-19. 4. 2013 Dušan Kolář 40