L4K ︰ 通往 Python 的道路 ── RUR-PLE 觀點

如果從『GvR』之『結論』︰

Where to Go from Here…

Conclusion

Guido is starting to realize that there are some things he cannot do. In the projects where Guido traveled, he had no memory of how big the world was. He has no way to keep track of a count. He tried to tell his parents about the journey but when they asked How far did you go? he didn’t know. In the rain project where Guido had to close the windows, he had no way to remember where he started, at the door. He had to leave a beeper there to know when he had gone all around the house.

What Guido would like is a way to remember things. He read in a computer programming book about a part of a program called a variable that could be used to store numbers or letters or even words. Variables can hold a number value and that value can be changed. If he had a variable, he could increase the value in his variable by one for each step and know how many steps he had taken. If he had a two variables, he could store the street and the avenue where he stood at the door in the rain project and wouldn’t have needed to drop a beeper there.

Alas, Guido does not have variables. Sadly, he knows he never will. He has heard rumors about other programming languages, such as Python, which have all of his capabilities and much more, including variables and the ability to listen and speak (input and output instructions) and even the ability to create whole new types of robots (object oriented programming and inheritance).

Its time to say goodbye to Guido and his world. He will wait patiently for the next class of students while you move on and learn more about programming as you continue your journey in Computer Science.

 

到『RUR-PLE』的『觀點』︰

Learning Python: Child’s Play with RUR-PLE!

André Roberge, © 2005

What is RUR-PLE
Summary of instructions
Acknowledgements
About the browser

What is RUR-PLE

Simply put, RUR-PLE is a Python Learning Environment. With the assistance of a robot named Reeborg , we will explore the fun of programming in the Python language. But first, a little bit of background:

The word ‘robot’ entered our language through the 1920 play R.U.R. (Rossum’s Universal Robots) by Czech writer Karel Capek. Little did Capek know how widespread the word would become!

Karel Capek’s name cropped up in 1981, when Richard Pattis wrote a delightful little book titled Karel the Robot, a Gentle Introduction to the Art of Programming. In this book, Pattis introduces the main concepts of sequential programming (including loops and decisions, but not variable assignments) using the paradigm of instructing a robot capable of only four basic actions (turning left, moving one step forward, picking up and putting down beepers). Through the magic of programming, the robot learns to combine those four basic actions in order to accomplish tasks of increasing complexity. Pattis used Pascal, the preferred language of the day, as a means of “teaching” the robot new tricks. Since then, many new versions of Karel the Robot have appeared, used to introducing various computer languages, notably Java and C++, which are both based on the modern Object-Oriented Programming (OOP) approach.

However, since the complexity of Java and C++ contrasts with the simplicity of the robot world, these languages seem at odds with the idea of providing a Gentle Introduction to the Art of Programming.

Enter Python!… Named after the famous Monty Python’s Flying Circus by its creator, Guido van Rossum. Python, like Java and C++, is an OOP language. However Python also allows a non-OOP programming style more suitable for interacting with Pattis’s robot. A first implementation of Karel the Robot in Python was called PyKarel. The current implementation is called Guido van Robot (GvR for short), and is available at sourceforge.net.

RUR: a Python Learning Environment (rur-ple) is a “new and improved” version of GvR that extends Pattis’ ideas and allows a smooth transition to the use of variables as well as functions/methods, classes and objects. Rur-ple is a complete environment, its lessons cover the use of all of Python’s keywords. A Python interpreter and simple editor are included to explore Python outside the robot’s world.

Reeborg is a robot built on the 1981 Pattis’ model … and it is starting to show its age. It has an oil leak, which allows us to follow its trail. Its compass is broken; it only knows if it is facing North or not, unlike Karel or Guido who could determine their orientation with respect to all four cardinal points.

Pattis’ Karel the Robot was named after the author Karel Capek, who popularized the word robot in his play Rossum’s Universal Robots (RUR). While rur-ple shares the basic RUR acronym, in this case it stands for Roberge’s Used Robot. However, through the magic of Guido van Rossum’s Python, you can learn how to fix it and design a better one, worthy of the name Rossum’s Universal Robot.

 

來看,核心概念落在藉著規劃之『如何寫程式』的環境,透過課程了解『程式是什麼』,促進『程式語言』之學習。

以簡馭繁固然容易開始,課程緊湊有趣可以輕鬆上手。然而………果真不能在『派生體系』裡創造『微環境』直攻 Python 的乎!!

於是常若有所思夫??

要如何看待這些『建置』  implementation 的呢?也許好好『履行』 Implementation 其一,將自可及於『其它』,或若有『所思』,也可讀讀『淮南子』之《說林訓》︰

以一世之度制治天下,譬猶客之乘舟,中流遺其劍,遽契其舟桅,暮薄而求之,其不知物類亦甚矣!夫 隨一隅之跡,而不知因天地以游,惑莫大焉。雖時有所合,然而不足貴也。譬若旱歲之土龍,疾疫之芻狗,是時為帝者也。曹氏之裂布,蛷者貴之,然非夏后氏之 璜。無古無今,無始無終,未有天地而生天地,至深微廣大矣。足以蹍者淺矣,然待所不蹍而後行;智所知者偏矣,然待所不知而後明。遊者以足蹶,以手㧊,不得 其數,愈蹶愈敗。及其能遊者,非手足者矣。鳥飛反鄉,兔走歸窟,狐死首丘,寒將翔水,各哀其所生。毋貽盲者鏡,毋予躄者履,毋賞越人章甫,非其用也。

椎固有柄,不能自椓;目見百步之外,不能自見其眥。狗彘不擇甂甌而食,偷肥其體而顧近其死。鳳皇高翔千仞之上,故莫之能致。月照天下,蝕于詹諸。騰蛇遊霧,而殆於蝍蛆。鳥力勝日,而服於鵻禮,能有修短也。莫壽於殤子,而彭祖為夭矣。短綆不可以汲深,器小不可以盛大,非其任也。怒出於不怒,為出於不為。視於無形,則得其所見矣;聽於無聲,則得其所聞矣。至味不慊,至言不文,至樂不笑,至音不叫,大匠不斫,大豆不具,大勇不鬥,得道而德從之矣。譬若黃鍾之比宮,太簇之比商,無更調焉。以瓦鉒者全,以金鉒者跋,以玉鉒者發,是故所重者在外,則內為之掘。逐獸者目不見太山,嗜欲在外,則明所蔽矣。聽有音之音者聾,聽無音之音者聰;不聾不聰,與神明通。

卜者操龜,筮者端策,以問于數,安所問之哉!舞者舉節,坐者不期而拌皆如一,所極同也。日 出暘谷,入于虞淵,莫知其動,須臾之間,俛人之頸。人莫欲學御龍,而皆欲學御馬;莫欲學治鬼,而欲學治人。急所用也。解門以為薪,塞井以為臼,人之從事, 或時相似,水火相憎,鏏在其間,五味以和。骨肉相愛,讒賊間之,而父子相危。夫所以養而害所養,譬猶削足而適履,殺頭而便冠。昌羊去蚤虱而來蚙窮,除小害 而致大賊,欲小快而害大利。牆之壞也,不若無也,然逾屋之覆。璧瑗成器,礛諸之功;鏌邪斷割,砥礪之力。狡兔得而獵犬烹,高鳥盡而強弩藏。虻與驥,致千里 而不飛,無糗糧之資而不饑。失火而遇雨,失火則不幸,遇雨則幸也。故禍中有福也。

鬻棺者,欲民之疾病也;畜粟者,欲歲之荒饑也。水靜則平,平則清,清則見物之形,弗能匿也。故可以為正。川 竭而穀虛,丘夷而淵塞,唇竭而齒寒,河水之深,其壤在山。鈞之縞也,一端以為冠,一端以為袜,冠則戴致之,袜則蹍履之。知己者不可誘以物,明於死生者,不 可卻以危。故善遊者不可懼以涉。親莫親於骨肉,節族之屬連也。心失其制,乃反自喜,況疏遠乎!聖人之一道,猶葵之與日也。雖不能與終始哉,其向之誠也。宮 池涔則溢,旱則涸。江水之原,淵泉不能竭。蓋非橑不能蔽日,輪非輻不能追疾,然而橑輻未足恃也。金勝木者,非以一刃殘林也;土勝水者,非以一墣塞江也。躄 者見虎而不走,非勇,勢不便也。傾者易覆也。倚者易軵也。幾易助也,濕易雨也。設鼠者機動,釣魚者泛杭,任動者車鳴也。

芻狗能立而不能行,蛇床似麋蕪而不能芳。謂 許由無德,烏獲無力,莫不醜於色。人莫不奮於其所不足。以兔之走,使犬如馬,則逮日歸風;及其為馬,則又不能走矣。冬有雷電,夏有霜雪,然而寒暑之勢不 易,小變不足以妨大節。黃帝生陰陽,上駢生耳目,桑林生臂手,此女媧所以七十化也。終日之言,必有聖之事;百發之中,必有羿、逢蒙之巧。然而世不與也,其 守節非也。牛蹄彘顱亦骨也,而世弗灼,必問吉凶於龜者,以其曆歲久矣。近敖倉者,不為之多飯;臨江河者,不為之多飲;期滿腹而已。蘭芝以芳,未嘗見霜;鼓 造辟兵,壽盡五月之望。舌之與齒,孰先砻也?錞之與刃,孰先弊也?繩之與矢,孰先直也?今鱔之與蛇,蠶之與蠋,狀相類而愛憎異。晉以垂棘之璧得虞、虢,驪 戎以美女亡晉國。聾者不歌,無以自樂;盲者不觀,無以接物。觀射者遺其執,觀書者忘其愛。意有所在,則忘其所守。

……

然後大膽『解讀』《 TPG 》 Toy Parser Generator 『原始碼』,『遊藝』於『文法』『語意』物件之中,『採擷』 TPG 之『自生自成』的『精華』︰

Toy Parser Generator is a lexical and syntactic parser generator for Python. This generator was born from a simple statement: YACC is too complex to use in simple cases (calculators, configuration files, small programming languages, …).

TPG can very simply write parsers that are usefull for most every day needs (even if it can’t make your coffee). With a very clear and simple syntax, you can write an attributed grammar that is translated into a recursive descendant parser. TPG generated code is very closed to the original grammar. This means that the parser works like the grammar. A grammar rule can be seen as a method of the parser class, symbols as method calls, attributes as method parameters and semantic values as return values. You can also add Python code directly into grammar rules and build abstract syntax trees while parsing.

The first application of TPG is TPG itself. The first (not released) version of TPG has been written by hand then was used to generate next versions. Now TPG can generate itself.

For an uptodate documentation, please read tpg.pdf.

─── 摘自《W!o 的派生‧十日談之《九》