SWI-Prolog 1

Focal point

  • Knowledge base: A collection of Facts and Rules.
  • Syntax
  • Atoms
  • Numbers
  • Variables
  • Complex terms
--

Start SWI-Prolog in Windows

Prior Knowledge

  • Prolog 這個名稱由 Program + logic 組成,常被應用於人工智慧與計算語言學的邏輯程式語言。
  • swipl-win.exe: 執行 .pl 檔,讓使用者與 Prolog 互動的視窗程式。
  • Prolog Programs 的主要架構: Facts, Rules, Queries.
    • Facts: Things that unconditionally true of some situation.
    • Rules: Things that conditionally true of the situation.
    • Queries: Users ask questions to use Prolog program.
  • Prolog 和過去我們學的 C, Java, C# 等 Procedure-Oreinted Languages 不同。我們不需要一步步告訴 Prolog 要怎麼做,使用者只需要提問,Prolog 便會藉由 knowledge bases 來推論結果。
  • Prolog query knowledge bases from top to bottom, trying to match the expression you asking.
    • 比對第一條 rule 不符合,便會往下比對下一條 rule。
    • 補充:使用 Prolog query from top to bottom 的特性來寫 rules,盡量避免寫 if..else.. (... :- .... -> ... ; ...)。
  • After Prolog reports back to us, you could use semicolon ; (means or) to ask Prolog: are there any alternatives?
  • Using Prolog symbol (like semicolon) is more efficient as Prolog only has to deal with one rule but hard to read for human.
  • %: 註解符號。
  • ,: Prolog symbol for "AND", means conjunction.
  • ;: Prolog symbol for "OR" means disjunction.
  • :-: It means "if", or "is implied by". A :- B. means A is true if B is true.
    • The part on the left-hand side of the :- is called the head of the rule, the part on the right-hand side is called the body.
    • A fact could be view as a rule with an empty body. Which is means the fact as a condition that do not any antecedent (前情) condition.
  • Modus ponens (假說推論): Prolog 藉由 facts and rules 來推論結果的步驟。
--





A Knowledge Base of Prolog
happy(Abby).
playsAirGuitar(Taylor).
listen2Music(Abby):- happy(Abby).
happy(Taylor):- playsAirGuitat(Taylor).
singing(Abby):- listen2Music(Abby).
singing(Taylor):- happy(Taylor).

  • Facts: happy(Abby) and playsAirGuitar(Taylor).
  • Rules: listen2Music(Abby) :- happy(Abby), happy(Taylor):- playsAirGuitar(Taylor), singing(Abby):- listen2Music(Abby), and singing(Taylor):- happy(Taylor).
  • This knowledge base has six clouses and four predicates (happy, playsAirGuitar, listen2Music, singing).

Keywords

  • Clouses: All of the facts and rules contained in a knowledge base.
  • Predicates: The facts and rules consist knowledge base.
--

Variable Modus Ponens






Knowledge Base
woman(mia).
loves(marsellus,mia).
Query
% 請告訴我 marsellus 愛哪個女生?
?- loves(marsellus,X), woman(X).
Return value
X = mia

  • Prolog reports back the variable bindings to us if it could find out a bindings which satisfy all queries.
  • Modus ponens:
    • Search loves(marsellus,X), woman(X)..
    • Find out loves(marsellus,mia)..
    • Binding mia with X.
    • Find out woman(mia)..
    • Both loves(marsellus,mia) and woman(mia). queries are matched.
    • Reports back the variables bindings X = mia.

Keywords

  • Variable: A word beginning with an upper-case letter which is a placeholder for information.

Variable modus ponens 2






Knowledge Base
loves(vincent,mia).
loves(marsellus,mia).
loves(pumpkin,honey_bunny).
loves(honey_bunny,pumpkin).

jealous(X,Y):- loves(X,Z), loves(Y,Z).
Query
% 請告訴我 marsellus 會吃誰的醋?
?- jealous(marsellus,W).
Return value
W = vincent

  • Modus ponens:
    • Searching jealous(marsellus,W)..
    • Find out jealous(X,Y):- loves(X,Z), loves(Y,Z)..
    • Binging marsellus with X.
    • Searching loves(marsellus,Z)..
    • Find out loves(marsellus,mia)..
    • Binding mia with Z.
    • Searching loves(Y,mia)..
    • Find out loves(vincent,mia)..
    • Binding vincent with Y.
    • jealous(marsellus,vincent). is true
    • Binding vincent with W.
    • Report back W = vincent.

留言