250 likes | 467 Views
Logic (Prolog) as the First Programming Language. Arthur Fleck Professor Emeritus University of Iowa. OVERVIEW. Logical basis Logic/Prolog programming ideas Advantages for the first course Technical course details Experience and Conclusions. Basic Boolean ops Logical implication.
E N D
Logic (Prolog) as the First Programming Language Arthur Fleck Professor Emeritus University of Iowa Frontiers in Education 2007
OVERVIEW • Logical basis • Logic/Prolog programming ideas • Advantages for the first course • Technical course details • Experience and Conclusions
Basic Boolean ops Logical implication And, or, and not, but written in the notation of logic ,, A critical Boolean operation in logic Approached as formal logic (truth tables) Logical basis
Predicates/relations Existential and universal quantification broOf(john_kennedy, robert_kennedy) In logic and - X.broOf(X, robert_kennedy) Logical basis
Logical basis • Syntax of well-formed formulas (and to a large extent, programs too), ideasof satisfiability and tautology • Axioms and (sound) proof rulesmodus ponens -- given formulas a and a b, conclude b
Logical basis • Truth vs. prooftruth -- dependence on unlimited values of logic variables makes direct determination impossibleproof -- concludes truth from analysis of formula syntax, is independent of variable values, and requires no truth evaluation
OVERVIEW OVERVIEW • Logical basis • Logic/Prolog programming ideas • Advantages for the first course • Technical course details • Experience and Conclusions
Prolog programming ideas • A program consists of a collection of universally quantified logical assertions -- axioms that characterize the essential properties of the problem domain • Solutions are computed by proving logical consequences of the chosen axioms
Prolog programming ideas • Prolog programs are invoked through queries -- existentially quantified logical assertions • A Prolog programming system determines if a query is a logical consequence of the program -- and if so, values of constituent unknowns must also be reported
Prolog programming example cousin(X,Y) :- grandFather(X,GF), grandFather(Y,GF), grandMother(X,GM), grandMother(Y,GM). ?- cousin(carolineKennedy, C). C= carolineKennedy
Corrected Prolog example • cousin(X,Y) :- grandFather(X,GF), grandFather(Y,GF), grandMother(X,GM), grandMother(Y,GM), father(X,F1), father(Y,F2), F1\==F2. • ?- cousin(carolineKennedy,C). % yields C=mariaSriver -- and many more • ?- cousin(C,mariaSriver). % yields C= carolineKennedy -- and many more
OVERVIEW • Logical basis • Logic/Prolog programming ideas • Advantages for the first course • Technical course details • Experience and Conclusions
Prolog programming advantages • Higher-level abstraction -- focus is on essential properties • Example -- list concatenation relationappend([ ], Suf, Suf).append([X|Pre], Suf, [X|Join]) :- append(Pre, Suf, Join).
Prolog programming advantages • The 'append' assertions comprise several traditional programs --?- append([a,b], [c,d], Ans). % yields Ans=[a,b,c,d]?- append(Ans, [c,d], [a,b,c,d]). % yields Ans=[a,b]?- append(Ans1, Ans2, [a,b,c,d]). % yields Ans1=[ ], Ans2=[a,b,c,d] and Ans1=[a], Ans2=[b,c,d] and …
Prolog programming advantages • Logic programming emphasizes modeling and abstraction • Precision in thinking and accuracy in programming are simultaneously achieved • Programming and problem solving become tightly integrated • The relational programs are innately much more general and reusable
OVERVIEW • Logical basis • Logic/Prolog programming ideas • Advantages for the first course • Technical course details • Experience and Conclusions
Course details • First phase - introduce logic 1. Discuss formula syntax and use of truth tables 2. Emphasize differences (e.g., domains, quantifiers) found in predicate logic 3. Explain the relationship between proof and truth evaluation
Course details • Second phase - use Prolog to animate logic 1. use logic formulas as syntax guide, start with familiar examples (e.g., genealogical relations) 2. emphasize relational character 3. recursion is natural and raises no conceptual complications
Course details • Third phase - introduce Prolog's backtracking search strategy for proofs1. Require substantial student use of the Prolog trace facility2. Discuss e.g. success/failure loops and negation as failure with this procedural model
Course details • Fourth phase - gain experience with integration of multiple features1. Compare the several means of expressing repetitive computation2. Study the complication of deducing implications of negative assertions3. Projects solving logic puzzles are sources requiring varied facilities
OVERVIEW • Logical basis • Logic/Prolog programming ideas • Advantages for the first course • Technical course details • Experience and Conclusions
Experience • An elective course, open only to college freshmen, was taught for six years • Students readily handled the formal logic • The interactive and incremental style of Prolog programming promoted student success
Conclusions • Students enjoyed logic/Prolog programming, and their performance met or exceeded that in the traditional first course • Analytical emphasis supports programming in whatever language follows next • The goal to reveal an intrinsic link between analytical thinking and computer solutions to problems is well served • Logic programming is a superior choice for students first programming course