Upload
jaylen-lewis
View
217
Download
0
Tags:
Embed Size (px)
Citation preview
AI/ES(Artificial Intelligence / Expert System)
Visual Prolog: Part 2
2012. Fall.SME., Pukyong Nat’l Univ.
Kim, Minsoo
Contents• The IDE – In detail• Fundamental Prolog• PIE: Prolog Inference Engine• Program Control in Prolog
The IDE – In detail• Project Life Cycle
– Creation• properties of the project (dll/exe, gui/console,
…)– Building
• compiling & linking– Browsing
• IDE provide information about project– Development
• add/remove/edit project source files– Debugging
• follow program execution and exploit its state
The IDE – In detail• Open sample project ‘ch01p01’
– ‘Open Project’ or [Project/Open]
The IDE – In detail• Project Tree
– …/My Documents/Visual Prolog Projects/• ch01p01/TaskWindow/Toolbars
– *.cl / *.i / *.pro / *.dlg / *.frm / *.win / *.mnu / *.ico
– *.ctl / *.tb / *.cur / *.bmp / *.lib– #include directives: *.pack / *.ph
• $(ProDir) logical node
The IDE – In detail• Your ‘main’ program
– Open ‘main.pro’ file
The IDE – In detail• Add a package (folder)
– ‘ch01p01’ New In Existing Package
The IDE – In detail• Add a form in the package
– samplePackage New In Existing Package
The IDE – In detail– Edit ‘query’ From
• Add controls Edit Field, Push Button• Name (*_ctl), Text
The IDE – In detail– Connect ‘query’ form to File/Open menu
• TaskMenu.win [File/Open] Uncheck Disabled• Change code Save/Build/Execute
The IDE – In detail– Add ‘ClickResponder’ to a Push Button
• Open ‘query’ form Click ‘Ask!’ Button• Properties/Events ClickResponder
The IDE – In detail• Change ‘AboutDialog’
– AboutDialog.dlg Properties/Events– CloseResponder
Edit/Save/Build/Execute!
The IDE – In detail– Add ‘ClickResponder’ to a Push Button
• AboutDialog.dlg Click ‘OK’ Button• Properties/Events ClickResponder
The IDE – In detail• Save / Build / Execute
– Try Query Form, About Dialog
Fundamental Prolog• Prolog’s Formal System for Reasoning
– predicate (relation)• describes the relation between some objects.
– fact: ‘predicate(argument, … )’• describes a static relation between objects.
– rule: ‘conclusion :- premise1, …, premiseN.’• general relations between (groups of) objects.• IF premise1, …, premiseN THEN conclusion.
John is the father of Bill father(“Bill”, “John”)Thomas lives in Groningen lives_in(“Thomas”, “Groningen”)John is married to Claire is_married_to(“John”, “Claire”)…
grandFather(GrandChild, GrandFather) :- father(GrandChild, Father), father(Father, GrandFather).
Fundamental Prolog• In Prolog …
– Facts and rules are called clauses.– A theory is a set of clauses.– Questions to the inference engine are
goals.• Horn clauses (fact + rule + goal
clause)– Horn Clause Logic (Alfred Horn)
father(“Bill”, “John”).father(“Pam”, “Bill”).father(“Sue”, “Jim”).grandFather(GrandChild, GrandFather) :- father(GrandChild, Father), father(Father, GrandFather).
a theory with 3 facts and 1 rule.
Is Jim the father of Sue?Is John the father of Sue?Who is the father of Pam?Is John the father of Ann?Is John the grandfather of Pam?
YesNo (there is no such fact)BillNo (we cannot state to ‘yes’)Yes
Fundamental Prolog• A Prolog program …
– Consists of a theory and a goal.• When a program starts it tries to find a
solution to the goal in the theory.– Yes / No– No solution ~ Multiple solutions
father(“Bill”, “John”).father(“Pam”, “Bill”).father(“Sue”, “Jim”).grandFather(GrandChild, GrandFather) :- father(GrandChild, Father), father(Father, GrandFather).
a theory with 3 facts and 1 rule.
Is Jim the father of Sue?Is John the father of Sue?Who is the father of Pam?Is John the father of Ann?Is John the grandfather of Pam?
?- father(“Sue”, “Jim”).?- father(“Sue”, “Jonh”).?- father(“Pam”, X).?- father(“Ann”, “John”).?- grandFather(“Pam”, “John”).
PIE: Prolog Inference Engine• Running PIE Project
– Classical Prolog interpreter• Horn Clause Logic
– Install Examples• Run [Visual Prolog/Setup Examples]• Select [Help/Install Examples…]
– Open PIE project• …/My Documents/Visual Prolog Examples/pie• Build / Execute
– PIE Program• Dialog Window (for goals)• Program Window: [File/New] “…/FILE0.PRO”
– Place your theory (facts and rules)
PIE: Prolog Inference Engine
– Enter your theory in the Program Window• [Engine/Reconsult] load your theory to
engine• [File/Save] to save your theory as a file.
PIE: Prolog Inference Engine
– After [Engine/Reconsule]• Type a goal in the Dialog Window and
<Enter>.– Start with lower case letter and end with
period.
PIE: Prolog Inference Engine• Try following goals
– Logical AND(,) / OR(;)father(“Sue”, “Jim”).
father(“Sue”, “Jonh”).
father(“Pam”, X).
father(“Ann”, “John”).
grandFather(“Pam”, “John”).
father(X, Y).
father(“Sue”, “Jim”), father(“Bill”, “John”).
father(“Sue”, “Jim”); father(“Bill”, “John”).
PIE: Prolog Inference Engine• Extending the family theory
– Add facts/rules increase intellectual power• [Engine/Reconsult] Try sibling(X, Y).
PIE: Prolog Inference Engine
– Modify sibling & add fullBloodedSibling rule• Reconsult & Try again!
Program Control in Prolog• Rigid search for solving the goal
– Pattern matching: left to right, top to bottom
?- father(“Sue”, “Jim”).
1. father(“Bill”, “Jonh”) fail.2. father(“Pam”, “Bill”) fail.3. father(“Sue”, “Jim”) match success!4. Stop search even though there is a father(“Jack”, “Bill”) predicate. Answer is Yes.
Program Control in Prolog– Match while breaking down the goal
• Substitution(binding a variable), backtracking
?- grandFather(“Sue”, “Jim”).
1. grandFather(“Sue”, “Jim”) :- father(“Sue”, Father), father(Father, “Jim”).
1-A Subgoal: ?- father(“Sue”, Father). A.1. father(“Bill”, “John”) fail. A.2. father(“Pam”, “Bill”) fail. A.3. father(“Sue”, “Jim”) Bind Father = “Jim”. 1-B. Subgoal: ?- father(“Jim”, “Jim”). B.1. father(“Bill”, “John”) fail. B.2. father(“Pam”, “Bill”) fail. B.3. father(“Sue”, “Jim”) fail. B.4. father(“Jack”, “Bill”) fail. B.5. No more father predicate no solution backtrack! A.4. father(“Jack”, “Bill”) fail. A.5. No more father predicate no solution backtrack!2. No more grandFather rule fail No Solution.
Program Control in Prolog• Failing
– No fact or rule that satisfy current goal fail
– Go back to backtrack point
• Relentless search for solutions– Check all the possible predicates and
search all the possible solutions.
Program Control in Prolog• Cut: !
– Prevent backtracking• Used to prevent the search for more solutions
– Once the engine pass the cut(!), it is impossible to backtrack to predicates that precede the cut.• There is no way back to an alternative!
– Two main use of ‘cut’• Green cut: when you are sure that certain
clauses will never give rise to meaningful solutions, it’s a waste of time and storage space to use them in looking for alternative solutions.
• Red cut: when the logic of program demands the cut, to prevent consideration of alternative subgoals.
Program Control in Prolog– Green cut
• Try this theory and search this goal.
• And introduce a cut in the goal clause.
Program Control in Prolog– Red cut
• Switch-case statement like control
• Try this theory and search this goal.
r(X) :- X = 1, !, a, b, c.r(X) :- X = 2, !, d.r(X) :- X = 3, !, c, e.r(_) :- write(“This is a catch-all clause”).
Program Control in Prolog• Recursion
– A definition that is defined in terms of itself.• Recursion must make progress divide &
conquer!• Recursion must terminate no infinite-loop!
– Think of this recursive rule.ancestor(Person, Ancestor) :- parent(Person, Ancestor).ancestor(Person, Ancestor) :- parent(Person, P1), parent(P1, Ancestor).ancestor(Person, Ancestor) :- parent(Person, P1), parent(P1, P2), parent(P2, Ancestor)....
ancestor(Person, Ancestor) :- parent(Person, Ancestor).ancestor(Person, Ancestor) :- parent(Person, P1), ancestor(P1, Ancestor).
Program Control in Prolog• Side Effects
– A predicate can succeed or fail• Evaluated to be true of false.
– Always failing predicate: ‘fail’– Always succeeding predicates: reading/writing
– In Prolog predicates that … has side effects.• always succeed.• do more than simply evaluate to true or false.
– Think of this goal clause?- ancestor(“Pam”, AA), write(“Ancestor of Pam: ”, AA), nl().
?- ancestor(“Pam”, AA), write(“Ancestor of Pam: ”, AA), nl(), fail.