勇闖新世界︰ 《 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 的呢??

 

 

 

 

 

 

 

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

《 Simply Logical
Intelligent Reasoning by Example 》

之第一部份起頭處, Peter Flach 開宗明義的講︰

Logic and Logic Programming

Logic Programming is the name of a programming paradigm which was developed in the 70s. Rather than viewing a computer program as a step-by-step description of an algorithm, the program is conceived as a logical theory, and a procedure call is viewed as a theorem of which the truth needs to be established. 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. Readers familiar with imperative programming will find that Logic Programming requires quite a different way of thinking. Indeed, their knowledge of the imperative paradigm will be partly incompatible with the logic paradigm.

This is certainly true with regard to the concept of a program variable. In imperative languages, a variable is a name for a memory location which can store data of certain types. While the contents of the location may vary over time, the variable always points to the
same location. In fact, the term ‘variable’ is a bit of a misnomer here, since it refers to a value that is well-defined at every moment. In contrast, a variable in a logic program is a variable in the mathematical sense, i.e. a placeholder that can take on any value. In this respect, Logic Programming is therefore much closer to mathematical intuition than imperative programming.

Imperative programming and Logic Programming also differ with respect to the machine model they assume. A machine model is an abstraction of the computer on which programs are executed. The imperative paradigm assumes a dynamic, state-based machine model, where the state of the computer is given by the contents of its memory. The effect of a program statement is a transition from one state to another. Logic Programming does not assume such a dynamic machine model. Computer plus program represent a certain amount
of knowledge about the world, which is used to answer queries.

,指出『邏輯編程』和『 Von Neumann 程式語言』──

摘自《 CPU 機器語言的『解譯器』》︰

事實上 Von Neumann 的計算機架構,對於電腦程式語言的發展,有著極為深遠的影響,產生了現在叫做 Von Neumann 程式語言,與Von Neumann 的計算機架構,同形 isomorphism 同構

program variables ↔ computer storage cells
程式變數 對映  計算機的儲存單元

control statements ↔ computer test-and-jump instructions
控制陳述  計算機的『測試.跳至』指令

assignment statements ↔ fetching, storing instructions
賦值陳述  計算機的取得、儲存指令

expressions ↔ memory reference and arithmetic instructions.
表達式  記憶體參照和算術指令

── 之『觀點』有極大的不同。通常這造成了學過一般程式語言的人『理解』上的困難。也可以說『邏輯編程』之『推導歸結』得到『證明』的想法遠離『圖靈機』的『狀態轉移』到達『接受』狀態 。反倒是比較接近『 λ運算』與『 Thue 字串改寫系統』抽象建構。讀者可以試著參考讀讀那些文章, 看看能否將『思考』方式的

Logic Programming requires quite a different way of thinking. Indeed, their knowledge of the imperative paradigm will be partly incompatible with the logic paradigm.

『不相容性』鎔鑄成整體思維之『結晶』,嫻熟用於解決各式各樣的『問題』!

也許『學習』到了一定的時候,或早或遲人們需要想想『學習方法 』,經由對自己的了解,找到『事半功倍』的有效作法。雖說人人都是獨特的,然而在『學習』上確實有很多類似的『難處』。比方說一般寫程式的思維,有所謂『從上往下』 Top-Down 以及『由底向頂』 Button-Up 思路取向不同。一者類似『歐式幾何』,從公理公設出發,逐步推演定理定律,邏輯清楚明白。然而一旦要自己去證明 □□ 定理時,有時總覺的無處下手。這是因為由『公理』通往『定理』的『推導歸結』之道路遙遠,常常看不出 ○□ 兩個『概念 』間竟然有如此的『聯繫』。另一彷彿『學會做菜』,由觀察親朋做菜開始,知道什麼菜要怎麼洗?怎麼切?用什麼方法調理?久而久之,學會了一道二道三道很多很多的菜的作法。也可以講,這樣的『學法』知道了許多『如何作』 Know-How ,很少思考『為什麼 』 Know-Why 這麼作,也很少形成一般『這一類』 Know-What 的菜,多半這樣作的『通則』。我們將要如何回答別人,自己『想都沒想過』,這菜『卻得這麼作』的『問題』?或許異地嫁娶之人,更能體會,這種『不同』就是『不同』的意思。

既然打算『勇闖』,並非『暴虎馮河』,而是『有勇有謀』,想那『新世界』的『智能機器』,難到不會『自我學習』?何不就假借此時『野人獻曝』,用著談『學習之法』來學此法!體會『遞迴』之『遞迴』也許正是『邏輯』的『原點』??『方法』的『方法』或者恰是『智慧』之『菩提』!!

就讓我們效法『科學家』的精神,用著探索『大自然』的方法,弄明白他到底如何研究『未知』。假設已知『 pyDatalog 』 源於

Datalog ,也知道一般『邏輯編程』是什麼

Logic programming ,還知道關鍵句式是『霍恩子句

Horn clause

。同時據該作者講︰

pyDatalog is derived from previous work by John D. Ramsdell

,那麼在閱讀

Datalog User Manual

5 Syntax

In Datalog input, whitespace characters are ignored except when they separate adjacent tokens or when they occur in strings. Comments are also considered to be whitespace. The character ‘%’ introduces a comment, which extends to the next line break. Comments do not occur inside strings.

The characters in Datalog input are collected into tokens according to the rules that follow. There are four classes of tokens: punctuations, variables, identifiers, and strings.

The punctuation tokens are: ‘(’, ‘,’, ‘)’, ‘=’, ‘:-’, ‘.’, ‘~’, ‘?’, and ‘”’.

A variable is a sequence of Latin capital and small letters, digits, and the underscore character. A variable must begin with a Latin capital letter.

An identifier is a sequence of printing characters that does not contain any of the following characters: ‘(’, ‘,’, ‘)’, ‘=’, ‘:’, ‘.’, ‘~’, ‘?’, ‘”’, ‘%’, and space. An identifier must not begin with a Latin capital letter. Note that the characters that start punctuation are forbidden in identifiers, but the hyphen character is allowed.

A string is a sequence of characters enclosed in double quotes. Characters other than double quote, newline, and backslash can be directly included in a string. The remaining characters can be specified using escape characters, ‘\”’, ‘\n’, and ‘\\’ respectively.

Other escape characters can be used to improve the readability of the input. If a string is too long to fit conveniently on one line, all but the final line containing the string can be ended with a backslash character, and each backslash newline pair is ignored. The character escape codes from the C programming language are allowed—‘\a’, ‘\b’, ‘\f’, ‘\n’, ‘\r’, ‘\t’, ‘\v’, ‘\’’, and ‘\?’. The numeric escape codes consist of one, two, or three octal digits. Thus the ASCII character newline is ‘\012’, and zero is ‘\000’. A printed Datalog string is also a string constant in C.

5.1 Literals

A literal, is a predicate symbol followed by an optional parenthesized list of comma separated terms. A predicate symbol is either an identifier or a string. A term is either a variable or a constant. As with predicate symbols, a constant is either an identifier or a string.

The following are literals:

parent(john, douglas)
zero-arity-literal
aBcD(-0, “\n\377”)
“=”(3,3)
“”(-0-0-0,&&&,***,”\0”)

5.2 Clauses

A clause is a head literal followed by an optional body. A body is a comma separated list of literals. A clause without a body is called a fact, and a rule when it has one. The punctuation ‘:-’ separates the head of a rule from its body. A clause is safe if every variable in its head occurs in some literal in its body.

The following are safe clauses:

parent(john, douglas)
ancestor(A, B) :-
parent(A, B)
ancestor(A, B) :-
parent(A, C),
ancestor(C, B)

5.3 Programs

A Datalog reader consumes a Datalog program. A program is a sequence of zero or more statements, followed by an optional query. A statement is an assertion or a retraction. An assertion is a clause followed by a period, and it adds the clause to the database if it is safe. A retraction is a clause followed by a tilde, and it removes the clause from the database. A query is a literal followed by a question mark. The effect of reading a Datalog program is to modify the database as directed by its statements, and then to return the literal designated as the query. If no query is specified, a reader returns a literal know to have no answers.

The following is a program:

edge(a, b). edge(b, c). edge(c, d). edge(d, a).
path(X, Y) :- edge(X, Y).
path(X, Y) :- edge(X, Z), path(Z, Y).
path(X, Y)?

,同時參考

Datalog tutorial

pyDatalog is a powerful language with very few syntactic elements, mostly coming from Python : this makes it easy to learn ! In this tutorial, we’ll review:

  • Variables and expressions
  • Loops
  • Logic Functions and dictionaries
  • Aggregate functions
  • Literals and sets
  • Tree, graphs and recursive algorithms
  • 8-queen problem

We’ll see that pyDatalog statements are declarative : they describe the result we want, leaving to the computer the task of finding the appropriate solutions. We’ll start with trivial problems to show the basics of the language, and progressively address more complex problems, to show how simply they can be expressed. We’ll finish with an efficient solution to the 8-queen problem.

……

,以及了解安裝方法

#
sudo pip-3.2 install pyDatalog
sudo pip-3.2 install sqlalchemy 
#

後,我們是否就已學會且能詮釋

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

這個程式的『意指』的呢?

 

 

 

 

 

 

 

勇闖新世界︰《邏輯編程》

就讓我們從《人工智慧!!》一文中所講的『哲學爭論』︰

一九八零年希爾勒寫了一篇論文《心靈、大腦、與程式》Minds, Brains, and Programs,講到一個稱作『中文房間』Chinese room 的思想實驗︰

220px-Salachinesa2

一 位中文一竅不通,以英文為母語的人,關閉在一間只有一個開口的密閉房間中。房間裡有一本英文手冊以及伴隨著的電腦程式,還有充足的紙、筆、橡皮擦與檔案 櫃。門外的人從那個開口向房間內輸入中文之字詞,門裡的人便按照程式指令處理,再由那個開口輸出中文字詞。假使該程式已然通過中文的圖零測試,當然那人自 可以作剛才所說的這件事情。他只需簡單『用手』manually 來跑程式就好了。

希爾勒認為,儘管門裡的人很能夠以假亂真,讓門外的人以為他本就是華語使用者,然而他卻是壓根不懂中文的啊!假使門裡的人是『不可能』透過英文手冊來『理解』中文的話,那麼電腦也是不可能借著程式來獲得這個『理解力』的吧!!

有 批評者這麼說︰人們都認為人是擁有智慧的,然而人的智慧卻是來自於腦細胞的物理訊息轉換作用,單一個腦細胞並不能理解字詞的意義,難道不是大量的腦細胞造 就了人 類的認知與理解能力的嗎?這跟有眾多指令組成的整體程式又能有什麼實質的差異呢?所以為什麼電腦就不可能會有『理解力』的呢??

,展開『新世界』科技的探索。首先讀讀

年代 20世紀40年代 20世紀50年代 20世紀60年代 20世紀70年代 20世紀80年代 20世紀90年代
計算機 1945 計算機(ENIAC) 1957 FORTRAN語言
人工智慧研究 1953 博弈論
1956 達特矛斯會議
1977 知識工程宣言 1982 第五代電腦計劃開始 1991 人工神經網路
人工智慧語言 1960 LISP語言 1973 PROLOG語言
知識表達 1973 生產系統
1976 框架理論
專家系統 1965 DENDRAL 1975 MYCIN 1980 Xcon

,可以對於『人工智慧』的進程、主張以及批評,有個初步了解。知道此一語詞早在一九五五年,就由約翰‧麥卡錫  John McCarthy 所鑄造︰

Artificial intelligence (AI) is the intelligence exhibited by machines or software. It is also the name of the academic field of study which studies how to create computers and computer software that are capable of intelligent behavior. Major AI researchers and textbooks define this field as “the study and design of intelligent agents”,[1] in which an intelligent agent is a system that perceives its environment and takes actions that maximize its chances of success.[2] John McCarthy, who coined the term in 1955,[3] defines it as “the science and engineering of making intelligent machines”.[4]

,至今也已經有了一甲子了。這樣看來,也許人類尚未能理解如何創造科學怪人的奧秘。

英國著名小說家瑪莉‧雪萊 Mary Shelley 因一八八一年所創作的《弗蘭肯斯坦》 Frankenstein’s monster ── 科學怪人 ── 被譽為科幻小說之母。她曾經提到了伽伐尼的研究報告︰這份研究報告是夏季閱讀書目的一部分,這一書目引發了在瑞士的一次下雨天所進行的一場即席鬼故事大賽,繼而導致了小說《弗蘭肯斯坦》和其中的復活情節的誕生。

180px-Frankenstein.1831.inside-cover

那位 LISP 之父,約翰‧麥卡錫,也於二零一一年十月辭世。──

樹莓 λ 者 Lisps》︰

220px-Lisplogo_alien_256

“Lisp has jokingly been called “the most intelligent way to misuse a computer”. I think that description is a great compliment because it transmits the full flavor of liberation: it has assisted a number of our most gifted fellow humans in thinking previously impossible thoughts.” — Edsger Dijkstra, CACM, 15:10

一九五四年美國艾倫‧紐厄爾 Allen Newell、克里夫‧蕭 Cliff Shaw 和赫伯特‧西蒙 Herbert Alexander Simon 等人於蘭德公司卡內基技術學院研發了資訊處理語言 IPL Information Processing Language 。它以『圖靈測試』為目標,被認為是史上第一個用於『人工智慧』── 符號演算』系統可以衍生出智慧 ── 領域的語言,首先使用了『串列』 Link List 結構與『遞迴』,啟發了之後『Lisp』的發展。一九五八年約翰‧麥卡錫 John McCarthy ── 在一九五五年的達特矛斯會議上提出了『人工智慧』這個概念── 於麻省理工學院發明了 Lisp 這個程式語言。其中採用了 IPL 語言的特徵。一九六零年麥卡錫在《ACM 通訊》上發表了一篇名為《遞迴函式的符號表達式以及由機器運算的方式,第一部》 Recursive Functions of Symbolic Expressions and Their Computation by Machine, Part I 之論文。在這篇論文中,他展示了只要透過一些簡單的運算子以及函式的記號法,也就可以建立一個具有『圖靈完備性』的 λ 演算法語言。約翰‧麥卡錫的學生史帝芬‧羅素 Steve Russell ── 著名程式設計師與計算機科學家,曾在 PDP-1 上創造了第一個電子遊戲 Spacewar ! ── 在閱讀完此論文後,認為 Lisp 程式語言當中的『eval 函式』可以用『機器碼』來實做。他於是在IBM 704 機器上,完成了第一個 LISP 『解譯器』 Interpreter。Lisp 的名稱來自於『串列處理器』 List Processor 之縮寫,最初拼寫為『LI SP』,是一個僅次於 Fortran 語言之歷史悠久的泛函式計算機程式語言,以『波蘭表示法』編程,擁有多種方言家族。

── 。

據聞,

「簡約派(the neats)」:邏輯,Prolog語言和專家系統

早在 1958 年,John McCarthy 就提出了名為「納諫者(Advice Taker)」的一個程序構想,將邏輯學引入了 AI 研究界。[94] 1963年,J. Alan Robinson發現了在計算機上實現推理的簡單方法:歸結(resolution)與合一(unification)算法。然而,根據 60 年代末McCarthy 和他的學生們的工作,對這一想法的直接實現具有極高的計算複雜度:即使是證明很簡單的定理也需要天文數字的步驟。[95] 70 年代 Robert Kowalsky 在Edinburgh大學的工作則更具成效:法國學者 Alain Colmerauer 和 Phillipe Roussel 在他的合作下開發出成功的邏輯程式語言 Prolog[96]

Dreyfus等人針對邏輯方法的批評觀點認為,人類在解決問題時並沒有使用邏輯運算。心理學家 Peter Wason,Eleanor Rosch,阿摩司·特沃斯基,Daniel Kahneman 等人的實驗證明了這一點。[97] McCarthy 則回應說,人類怎麼思考是無關緊要的:真正想要的是解題機器,而不是模仿人類進行思考的機器。

,是目前這場革命中的皎皎者。

由於實務上的學習考量,並不打算詳細介紹『Prolog』這個語言。有興趣深入的讀者可以下載閱讀

Simply Logical

Intelligent Reasoning by Example

by Peter Flach

這本教科書。

我們將把『輕量級』的『邏輯編程』帶入『派生』 Python 世界裡,探一探這個『典範』的主旨,嚐一嚐『人工智慧』的味道,談一談 pyDatalog 的一些應用。

 

from pyDatalog import pyDatalog
pyDatalog.create_terms('factorial, N')
factorial[N] = N*factorial[N-1] 
factorial[1] = 1
print (factorial[3==N) # prints N=6
  pyDatalog extends Datalog with logic functions
 Easy to learn
Based on programmer-friendly Python 

 Be productive
Write short, declarative programs
Batteries included
With full access to the extensive Python library 
Be readable
Order your statements to be literate 
Query 11 SQL databases 
and noSQL, and Python classes 
Use AI
to implement expert systems and complex algorithms
pyDatalog adds the logic programming paradigm to Python‘s extensive toolbox, in a pythonic way.  
 
Logic programmers can now use the extensive standard library of Python, and Python programmers can now express complex algorithms quickly.
Datalog is a truly declarative language derived from Prolog, with strong academic foundations.  Datalog excels at managing complexity.  Datalog programs are shorter than their Python equivalent, and Datalog statements can be specified in any order, as simply as formula in a spreadsheet. 
pyDatalog can be used for:
  • simulating intelligent behavior (for games or expert systems), 
  • querying complex sets of related information (e.g. in data integration or Natural Language Processing),
  • performing recursive algorithms (e.g. on hierarchical data structure)
pyDatalog is derived from previous work by John D. Ramsdell.  It is an open-source project (LGPL) lead by Pierre Carbonnelle (in Belgium).  It is inspired by LogicBlox.

Core technology

 Core technology in pyDatalog  Benefits  Sample applications
The resolution engine determines the best sequence of clauses to use to reach a goal Spreadsheet-style programming : faster development; fewer bugs, easier to read afterwards Rule-based models with many input and output, e.g. expert system for price calculation or tax planningaccess right managementrobot controlintelligent agent in games or automated assistants
The resolution engine can resolve recursive algorithm Easy to write queries on hierarchical structure Reporting with hierarchical reference data such as organisational structure
The same clause can solve various mixes of known and unknown parameters Maximize code reuse : shorter program, simpler query language than SQL Cross-database queries, data integration
Intermediate results are memoized. Improved speed by avoiding duplicate work. Business logic in 3-tier architecture.
 

 

 

 

 

 

 

勇闖新世界︰《序》

假使閱讀維基百科『工業 4.0』詞條︰

『工業 4.0』  Industry 4.0 是一個德國政府提出的高科技戰略計劃,由德國聯邦教育及研究部和聯邦經濟技術部聯合資助,投資預計達 2 億歐元,用來提昇製造業的電腦化、數位化、與智能化。目標是建立具有適應性、資源效率、及人因工程學的『智慧工廠』 Smart Factory ,在商業流程及價值流程中整合客戶以及商業夥伴。其技術基礎是『智慧整合感控系統』 CPS Cyber-Physical System ,  及『物聯網』 IoT Internet of Things 。如果架構陸續成真,最終將能建構出一個有智能意識的工業世界。

『工業 4.0』一詞是指第四次工業革命,第一次工業革命是利用水力及蒸汽的力量作為動力源,第二次工業革命則使用電力為大量生產提供動力,第三次工業革命則是使用電子設備及資訊技術 IT 來增進工業製造的自動化。

,也許該看看一九三一年,英國作家阿道斯‧雷歐那德‧赫胥黎 Aldous Leonard Huxley 所寫作的

美麗新世界 Brave New World

這本書。顯然他對『科技』能夠產生『烏托邦』這件事很不看好。他是那時代之知識精英,認為人類終將進入莎士比亞的《暴風雨》當中,喊著︰

How beauteous mankind is!
O brave new world,
that has such people in it.

。所以自己最好在思考過後,再做出抉擇︰

當『工具觀』已經對上了『道德觀』之時

,尚不知熟對熟錯?!且也不知誰是誰非!?

僅憑對未來相信,勇敢踏上這條《美麗新世界》科技探索的道路。

 

 

 

 

 

 

 

推背圖??

推背圖》金聖歎序

謂數可知乎?可知而不可知也。謂數不可知乎?不可知而可知也。可知者數,不可知者亦數也。可知其所不可知者數,不可知其所可知者亦數也。 吾嘗仰觀於天,日月星辰猶是也;俯察於地,山川草木猶是也。我所親見之天地,非猶我所未親見之天地耶。然不得謂我所未親見之天地,即為我所親見之天地。天地自天地,而我異矣。我自我,兩天地異矣。我生以前之天地可知也,可知者數也。我生以後之天地不可知也,不可知者亦數也。有生我以前之天地,然後有我生以後之天地,此可知其所不可知者數也。我生以後之天地,豈不同於我生以前之天地,此不可知其所可知者亦數也。數之時義大矣哉。

唐臣袁天罡、李淳風著有「推背圖」,父老相傳,迄未寓目。壬戌之夏,得一抄本,展而讀之,其經過之事若合符節,其數耶,其數之可知者耶,其數之可知而不可知而可知者耶。

玩其詞,參其意,胡運不長,可立而待,毋以天之驕子自處也。

癸亥人日 金喟識

推背圖

 

第五四象 丁巳 乾下兌上 夬

讖曰:

磊磊落落 殘棋一局

啄息茍安 雖笑亦哭

頌曰:

不分牛鼠與牛羊
去毛存鞟尚稱強
寰中自有真龍出
九曲黃河水不黃

金聖歎:「此象有實去名存之兆 ,或為周末時,號令不行,尚頒止朔:亦久合必分之徵也 。

Tbt-54

根據維基百科『推背圖』詞條上講︰

現在坊間常見的版本是託名明末清初金聖嘆作序並加以評註字樣的六十圖版本,共一卷,六十象,按天干、地支排序。每象附有圖一幅、讖語四句、「頌曰」四句、金聖嘆評註一段。起「自從盤古迄希夷」,終「不如推背去歸休」。

,疑為清末民初人的偽作。既然都說是偽作了,為什麼還要談它?因為它曾經『流行』過,正可以探討人們對於『預言』、『算命』 、『預測』、『趨勢』、……的通俗看法。

為了避免誤解爭議,我們將『預言』定義為︰現在對未來將發生的事所作的斷言。比方說,假設有一天科技能夠讓人回到過去,那麼那個來自未來的人,他知道 □□ 事件曾經發生,因為他回到過去,他斷言未來 □□ 事件將會發生,這也是一種『預言』。對於這樣的『預言』來講,能否符合目前已知的科學?在

【Sonic π】電聲學之電路學《四》之《 V!》‧中下》文中,我們談到︰

一九四九年,『庫爾特‧哥德爾』 Kurt Gödel 開啟一條『封閉類時曲線』 CTC closed timelike curve 的研究風潮,這是一種『特殊的世界線』,它『首尾相環』,於是乎,『未來』可以通向『過去』。據聞這種『可能性』最早是在一九三七時,Willem Jacob van Stockum 先發現的。假使『愛因斯坦場方程式』可以『產生』這樣的『數學解』,我們應當認為那個『方程式』可能是『錯了』的嗎?或者認為『時間機器』理論上是『可行』的呢??再加之以『連接不同時空』之『蟲洞解』 ── 愛因斯坦‧羅森橋 ── 的存在︰

可穿越之蟲洞

ds^2= - c^2 dt^2 + dl^2 + (k^2 + l^2)(d \theta^2 + \sin^2 \theta \, d\phi^2)

不可穿越之蟲洞

ds^2= - c^2 \left(1 - \frac{2GM}{rc^2}\right)dt^2 + \frac{dr^2}{1 - \frac{2GM}{rc^2}} + r^2(d \theta^2 + \sin^2 \theta \, d\phi^2)

因此『時間旅行』成了『熱門話題』,它到底是『科學』或是『幻想』的呢?也許它根本是推動『科幻小說』、『科幻電影』和『科幻影集』的『無形之手』,因此我們就與『祖父悖論』為伍的了!一九四三年,法國科幻小說作家『赫內‧巴赫札維勒』 René Barjavel 寫了一本《不小心的旅遊者》 Le Voyageur Imprudent ,其中一段情景如是︰

假設你回到過去,在自己父親出生前把自己的祖父母殺死;因為你祖父母死了,就不會有你的父親;沒有了你的父親,你就不會出生;你沒出生,就沒有人會把你祖父母殺死;但若是沒有人把你的祖父母殺死,你是否會存在並回到過去且把你的祖父母殺死?

這就是知名的『祖父悖論』,牽動著為數五車之『大哉論辯』!!引發了一籮筐新的『平行世界因果律』、『未來者道德律』、『宇宙自恰律』……等等『因應方式』,作者無意於此議論著那些『是非真假』,只想於此處指出『時間旅行』之『自然律』,宇宙中若是『』,那麼就能夠『發生』;『物理方程式』是『觀測自然』之後才有的,因此『它的解』卻未必是『自然解』的吧!即使只以『地球』四五十億年的『歷史』,過去了的『分分秒秒』之『時刻』之『無窮無盡』的『地球光景』全然存在於『時流』中,所需的『能量之大』怕是『無量之不可思議』之『無限大』的吧!就算果真有如是之大的能量,又將我們帶回了『動矢不動』的『矛盾』上,若是『過去者』存在,『接續者』也存在,『將來者』又存在,那『運動』的是『何物』的呢?將怎麼談論『時間』能『變慢』的呢?於是那『同一』個『開天闢地』的『大霹靂』會『發生』、會『再發生』、會『無窮盡發生』的嗎??

……

。由於可能引發的因果矛盾,因此對於『預言』真實的存在性,就現今人類所知,或許抱持兩疑之間,最多只是存而不論。若講言之鑿鑿之『命定』論者,怕更是不可信的了吧!假使說『未來』不能『被改變』,算命到底所謂何來?反倒是多此一舉!要是『未來』是『未定』之天,算命到底能有何用??不如認識自己,認真作為 ,行有不得,反求諸己的好!!

然而今天打著『科技』的旗號,用『理化』的術語,行使詐騙之實的人,卻是比『預言』危害更大。說什麼是『能量水』?它還分成『正能量』與『負能量』!卻是『科學』也無法知道的『能量』。就像有人講□□好壞看『磁場』!不曉得『這種磁場』的『量測』滿不滿足『電磁學』?它會不會發生『電磁感應』?又為什麼能用『非物理』的方式加以『增強減弱』!於是有某些『科技巫術』在人世間流竄,它的骨子裡是『偽科學』,它的目的是『詐騙』。如果問它為什麼能遂行的呢?有人說︰因為人性的弱點。也許更由於缺少磨練『正確』的『思維』。試想兩人擲一個『公正』的骰子,比大小賭輸贏,一人已經連贏了十二次,另一人想︰沒聽說人能夠連擲這麼多次六的,接下來,我必定贏,他勢將輸。事實卻是兩人輸贏機會一樣,擲六的概率還是 \frac{1}{6} 沒變。

『統計的』與『遭遇的』有著根本性的差異,『樂透』可遇不可求 ,正如『雷擊』是無妄之災。有人計算中『樂透』的『機率』可是比『雷擊』還要小,卻無法勸阻有人『豪賭』,還別說,也還真有『豪賭』的人『中獎』!這和『統計』與『機率』的概念,又能有什麼關係的呢?『大數』統計與『少量』事實,『長期』平均和『當下』個例,能是相同的事情嗎??