勇闖新世界︰ 《 pyDatalog 》 導引《二》

要是我們對『declarative 』的了解僅止於『陳述的』、『說明的』或『宣告的』,一般來說並無助於理解

Thus, executing a program means searching for a proof. In traditional (imperative) programming languages, the program is a procedural specification of how a problem needs to be solved. In contrast, a logic program concentrates on a declarative specification of what the problem is.

這一段話的內容。這是因為我們還不知道『declarative 』語詞在『邏輯編程』語境中的『意指』,將之對比於『procedural』程序的,也只能帶來模糊的印象而已。如果用昨兒的互動程式為例,

 

pi@raspberrypi ~ $ python3
Python 3.2.3 (default, Mar  1 2013, 11:53:50) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from pyDatalog import pyDatalog
>>> pyDatalog.create_terms('X單位, Y單位, Z單位, V數值, 單位轉換, 重量轉換')
>>> 單位轉換['公斤', '公克'] = 1000
>>> 單位轉換['台斤', '公克'] = 600
>>> 單位轉換['市斤', '公克'] = 500
>>> 單位轉換['英磅', '公克'] = 453.59237
>>> 單位轉換[X單位, Y單位] = 單位轉換[X單位, Z單位] * 單位轉換[Z單位, Y單位]
>>> 單位轉換[X單位, Y單位] = 1 / 單位轉換[Y單位, X單位]
>>> print(單位轉換['公斤', '台斤'] == V數值)
V數值               
------------------
1.6666666666666667
>>> print(單位轉換['英磅', '台斤'] == V數值)
V數值               
------------------
0.7559872833333334
>>> 單位轉換['台斤', '台兩'] = 16
>>> 重量轉換[V數值, X單位, Y單位] = V數值 * 單位轉換[X單位, Y單位]
>>> print(重量轉換[1, '公斤', '台兩'] == V數值)
V數值               
------------------
26.666666666666668
>>> 

 

,是否會令人懷疑,讀了那篇文章所說的種種,真的足以『寫作』且『詮釋』那個『程式』的嗎?假使有一人宣稱他已經仔細閱讀過那些種種,他將發現自己處於『術語叢林』,面對『理解 A 』得先『理解 B 』…… ,需要『理解 X 』,它卻又依賴於『理解 A 』。舉例講嬰兒學會母語,是那麼的天經地義,然而這個『能力』是因為『天生的』── 本能 ──?『環境的』── 學習 ── ?總在爭論中!彼此誰也不能完全說服了誰!!

雖話說如此,那些『術語』還是有『親疏遠近』,或許不該一開始就『捨近求遠』,如是我們會認為『知識表示』與『推理』

Knowledge representation and reasoning

The starting point for knowledge representation is the knowledge representation hypothesis first formalized by Brian C. Smith in 1985:[7]

Any mechanically embodied intelligent process will be comprised of structural ingredients that a) we as external observers naturally take to represent a propositional account of the knowledge that the overall process exhibits, and b) independent of such external semantic attribution, play a formal but causal and essential role in engendering the behavior that manifests that knowledge.

和『邏輯編程』的關係較為緊密。而且能夠設想『 □□ 知識』就是『 ○○ 理論』裡,關於『 ☆☆ 世界』中的『事實』或是『定律』 。這樣我們可以揣測

單位轉換[□, ○]= ??

說的就是某個世界中的約定『事實』。更由

單位轉換[, ○]=單位轉換[,☆]   乘   單位轉換[☆,○]

單位轉換[□,○]=1   除以  單位轉換[○,□]

知道符合一般單位轉換運算『性質』。

當這些『具足』時,『pyDatalog』就能『自動推導』作單位轉換,而且可以『添加』新事實以及新規則。於是從『作中學』Learning by doing ,我們可以了解,這跟用一般的程式語言寫作方式不同,這時『宣告的』一詞或許就有了『操作』 Operational 上的『意義』 ,它與『程序的』一詞的『對比』,也將更為『鮮明』。至於那個程式『中文化』的目的,大概只能始於『臆想』?用著『試誤法』作初步『嘗試』,閱讀『pyDatalog』原始碼意圖『確認』。藉著對『派生三』 Python3 萬國碼的了解,做出的『待證明』假設吧!?看過《M♪o 之學習筆記本》系列文本 TPG Toy Parser Generator 的讀者,大概對此『中文表現』並不陌生,主要不過是希望程式能夠『易讀』,寫來比較精簡清楚罷了。

所以學會

如何閱讀□○??

金文大篆智
蛛網

其實『知識』如同『蛛網』,經由各種『學習渠道』與『自身經驗』,逐步建立起來。果真能像『蜘蛛』一樣,牽一髮而動全身的去『捕捉』,又何止是談『閱讀』一本書?智慧不只可以『閱讀』一個『時代』! 更能夠『閱讀』整個『自然』與『社會』的啊!!

,往往是學習者在『反思學習』時,所應作的第一件事。

如果想要了解『邏輯編程』的理論旨趣,掌握『pyDatalog』語言的『符號』、『語法』以及『語意』,可以多遍的重複閱讀

《 Simply Logical
Intelligent Reasoning by Example 》

的第二章

2 Clausal logic and resolution: theoretical backgrounds

In this chapter we develop a more formal view of Logic Programming by means of a rigorous treatment of clausal logic and resolution theorem proving. Any such treatment has three parts: syntax,   semantics, and proof theory. Syntax defines the logical language we are using, i.e. the alphabet, different kinds of ‘words’, and the allowed ‘sentences’. Semantics defines, in some formal way, the meaning of words and sentences in the language. As with most logics, semantics for clausal logic is truth-functional, i.e. the meaning of a sentence is defined by specifying the conditions under which it is assigned certain truth values (in our case: true or false). Finally, proof theory specifies how we can obtain new sentences (theorems) from assumed ones (axioms) by means of pure symbol manipulation (inference rules).

Of these three, proof theory is most closely related to Logic Programming, because answering queries is in fact no different from proving theorems. In addition to proof theory, we need semantics for deciding whether the things we prove actually make sense. For instance, we need to be sure that the truth of the theorems is assured by the truth of the axioms. If our inference rules guarantee this, they are said to be sound. But this will not be enough, because sound inference rules can be actually very weak, and unable to prove anything of interest. We also need to be sure that the inference rules are powerful enough to eventually prove any possible theorem: they should be complete.

Concepts like soundness and completeness are called meta-theoretical, since they are not expressed in the logic under discussion, but rather belong to a theory about that logic (‘meta’ means above). Their significance is not merely theoretical, but extends to logic programming languages like Prolog. For example, if a logic programming language is unsound, it will give wrong answers to some queries; if it is incomplete, it will give no answer to some other queries. Ideally, a logic programming language should be sound and
complete; in practice, this will not be the case. For instance, in the next chapter we will see that Prolog is both unsound and incomplete. This has been a deliberate design choice: a sound and complete Prolog would be much less efficient. Nevertheless, any Prolog
programmer should know exactly the circumstances under which Prolog is unsound or incomplete, and avoid these circumstances in her programs.

……

,直到熟悉內容為止。在此『熟悉』並不是說『理解』,而是對『全盤』有個鳥瞰,知道有哪些『概念』彼此相關,認識一些重要的『術語』,明白某些『理論』需要先行了解,……如此才能開始敲門叩關。或許可想想『為什麼』

Prolog 故意的設計成既不『健全』 Sound 又不『完備』 Complete 的呢??