caasih.net

程式語言學習心得 3

趁機複習一下這幾天學到的東西,我會看到什麼?

Implementing Functional Languages 一書不講 front-end ,也就是把目標語言 desugar 成 Core language 那部分,不像前身 the Implementation of Functional Languages 。另外一開始就當做讀者知道 supercombinator 是什麼東西。

書中範例是以 Miranda 寫成,似乎沒有 type classes ,學習起來也許會輕鬆些?現在讀到的程式都可以翻譯成 Haskell 。

雖然可以把值參數當成零個參數的函數,但是這東西有個名字叫做 constant applicative forms (CAFs) ,得另外處理。

Pack{7,2} (Pack{6,1} 3) (Pack{6,1} 4)

type 會被表示成 Pack{1, 0}, Pack{2, 2} ,前面的數字是 tag ,後面的是參數數量。STG 的 Tagless ,莫非指的就是這個 tag ?

之後想比較 Core 和 Haskell-基本語法與-lambda-程式的對應 的異同,願能幫我把實作拆得更合理。


今天才知道原來 Miranda 不是開源的,想用它寫也不成 XD

查 Miranda 和 type classes 時又遇到 A History of Haskell: Being Lazy With Class ,也許是時候讀完它了。


今天做完了 pretty print 中的 operator precedence ,正如書中所說:

The easiest way to achieve this is to give pprExpr an extra argument which indicates the precedence level of its context, and then use this to decide whether to add parentheses around the expression it produces.

pprExpr 的 type 改成 Int -> CoreExpr -> Iseq ,第一個參數表示這個 CoreExpr 外面的 operator precedence ,接著和裡面的 operator precedence 比較,決定該不該加括號就好了。

瞭解 pretty print 怎麼運作後,就不用像去年那樣手工幫 haskell-src-exts 的 data constructors 縮排了,照理說應該可以做自己的 pretty print ,不像它給的那樣印出真正的 Haskell code ,而是把 data constructors 漂亮地印出來。

接下來要面對 parser 。

Isaac Huang 發佈於