23
Frontiers in Education 20 07 1 Logic (Prolog) as the First Programming Language Arthur Fleck Professor Emeritus University of Iowa

Logic (Prolog) as the First Programming Language

  • Upload
    gwyn

  • View
    56

  • Download
    2

Embed Size (px)

DESCRIPTION

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. - PowerPoint PPT Presentation

Citation preview

Page 1: Logic (Prolog) as the First Programming Language

Frontiers in Education 2007 1

Logic (Prolog) as the First Programming Language

Arthur Fleck

Professor Emeritus

University of Iowa

Page 2: Logic (Prolog) as the First Programming Language

2

OVERVIEW

• Logical basis

• Logic/Prolog programming ideas

• Advantages for the first course

• Technical course details

• Experience and Conclusions

Page 3: Logic (Prolog) as the First Programming Language

3

Logical basis

• 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)

Page 4: Logic (Prolog) as the First Programming Language

4

Logical basis

• Predicates/relations

• Existential and universal quantification

• broOf(john_kennedy,

robert_kennedy)

• In logic and - X.broOf(X, robert_kennedy)

Page 5: Logic (Prolog) as the First Programming Language

5

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 and ,conclude

Page 6: Logic (Prolog) as the First Programming Language

6

Logical basis• Truth vs. proof

truth -- dependence on unlimitedvalues of logic variables makesdirect determination impossible

proof -- concludes truth from analysisof formula syntax, is independent of variable values, and requires notruth evaluation

Page 7: Logic (Prolog) as the First Programming Language

7

OVERVIEWOVERVIEW

• Logical basis

• Logic/Prolog programming ideas • Advantages for the first course

• Technical course details

• Experience and Conclusions

Page 8: Logic (Prolog) as the First Programming Language

8

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

Page 9: Logic (Prolog) as the First Programming Language

9

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

Page 10: Logic (Prolog) as the First Programming Language

10

Prolog programming example

cousin(X,Y) :- grandFather(X,GF), grandFather(Y,GF), grandMother(X,GM), grandMother(Y,GM).

?- cousin(carolineKennedy, C).C= carolineKennedy

Page 11: Logic (Prolog) as the First Programming Language

11

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). % yieldsC=mariaSriver -- and many more

• ?- cousin(C,mariaSriver). % yieldsC= carolineKennedy -- and many more

Page 12: Logic (Prolog) as the First Programming Language

12

OVERVIEW

• Logical basis• Logic/Prolog programming ideas

• Advantages for the first course • Technical course details• Experience and Conclusions

Page 13: Logic (Prolog) as the First Programming Language

13

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).

Page 14: Logic (Prolog) as the First Programming Language

14

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 …

Page 15: Logic (Prolog) as the First Programming Language

15

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

Page 16: Logic (Prolog) as the First Programming Language

16

OVERVIEW

• Logical basis• Logic/Prolog programming ideas• Advantages for the first course

• Technical course details • Experience and Conclusions

Page 17: Logic (Prolog) as the First Programming Language

17

Course details• First phase - introduce logic

1. Discuss formula syntax and useof truth tables

2. Emphasize differences (e.g., domains, quantifiers) found inpredicate logic

3. Explain the relationship betweenproof and truth evaluation

Page 18: Logic (Prolog) as the First Programming Language

18

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

Page 19: Logic (Prolog) as the First Programming Language

19

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 thisprocedural model

Page 20: Logic (Prolog) as the First Programming Language

20

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

Page 21: Logic (Prolog) as the First Programming Language

21

OVERVIEW

• Logical basis• Logic/Prolog programming ideas• Advantages for the first course• Technical course details

• Experience and Conclusions

Page 22: Logic (Prolog) as the First Programming Language

22

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

Page 23: Logic (Prolog) as the First Programming Language

23

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