Logic (Prolog) as the First Programming Language Arthur Fleck Professor Emeritus University of Iowa Frontiers in Education 2007 1 OVERVIEW • • • • • Logical basis Logic/Prolog programming ideas Advantages for the first course Technical course details Experience and Conclusions 2 Logical basis • Basic Boolean ops • And, or, and not, but written in the notation of logic ,, • Logical implication • A critical Boolean operation in logic • Approached as formal logic (truth tables) 3 Logical basis • Predicates/relations • broOf(john_kennedy, robert_kennedy) • Existential and universal quantification • In logic and X.broOf(X, robert_kennedy) 4 Logical basis • Syntax of well-formed formulas (and to a large extent, programs too), ideas of satisfiability and tautology • Axioms and (sound) proof rules modus ponens -given formulas a and a b, conclude b 5 Logical basis • Truth vs. proof truth -- dependence on unlimited values of logic variables makes direct determination impossible proof -- concludes truth from analysis of formula syntax, is independent of variable values, and requires no truth evaluation 6 OVERVIEW • Logical basis • • • • Logic/Prolog programming ideas Advantages for the first course Technical course details Experience and Conclusions 7 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 8 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 9 Prolog programming example cousin(X,Y) :grandFather(X,GF), grandFather(Y,GF), grandMother(X,GM), grandMother(Y,GM). ?- cousin(carolineKennedy, C). C= carolineKennedy 10 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 11 OVERVIEW • Logical basis • Logic/Prolog programming ideas • Advantages for the first course • Technical course details • Experience and Conclusions 12 Prolog programming advantages • Higher-level abstraction -- focus is on essential properties • Example -- list concatenation relation append([ ], Suf, Suf). append([X|Pre], Suf, [X|Join]) :- append(Pre, Suf, Join). 13 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 … 14 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 15 OVERVIEW • Logical basis • Logic/Prolog programming ideas • Advantages for the first course • Technical course details • Experience and Conclusions 16 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 17 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 18 Course details • Third phase - introduce Prolog's backtracking search strategy for proofs 1. Require substantial student use of the Prolog trace facility 2. Discuss e.g. success/failure loops and negation as failure with this procedural model 19 Course details • Fourth phase - gain experience with integration of multiple features 1. Compare the several means of expressing repetitive computation 2. Study the complication of deducing implications of negative assertions 3. Projects solving logic puzzles are sources requiring varied facilities 20 OVERVIEW • • • • Logical basis Logic/Prolog programming ideas Advantages for the first course Technical course details • Experience and Conclusions 21 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 22 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 23