作者godfat (godfat 真常)
看板PLT
標題Re: [問題] pattern matching in Haskell
時間Thu Jul 8 19:00:02 2010
希望你不介意我寫中文,也不介意可能會有不少講錯的地方... XD
也就麻煩大家更正了,感謝
※ 引述《etwas (i'm only dust)》之銘言:
: hi,
: i'm a novice, so better to say sorry first for anything stupid i'll ask.
: and for some reason i can only type in english.
: in chapter 6. of the book "Real World Haskell", the author explained some
: features of "newtype".
: data DataInt = D Int
: deriving (Eq, Ord, Show)
: newtype NewtypeInt = N Int
: deriving (Eq, Ord, Show)
: =====
: 1.
: ghci> case undefined of D _ -> 1
: *** Exception: Prelude.undefined
: the author wrote:
: "the unprotected undefined is evaluated when the pattern match occurs".
: i realise that matching is based on value constructors.
: but under what circumstances does it have to evaluate them?
: in this case, does pattern matching run into a divergence?
: ("divergence", or diverge as verb is introduced in many documents. but i
: can't make out.)
: pattern matching, does it happen in runtime, compile time or both?
: if it's a runtime process, then some sort of bookkeeping data about
: constructors should be retained during programme execution, right?
我想 pattern matching 是發生在 runtime, 不過這樣講或許並不適當,
或許比較正確的說法是在 evaluation 時發生,而 evaluation 通常是在
runtime 時才發生。如果連 evaluation 都是 compile time 就發生了,
那好像 runtime 也沒什麼事情要做了。
而什麼時候要 evaluate, 則是看需不需要做 pattern matching,
而需不需要 pattern matching, 則是看需不需要決定一個 function
需要用哪一個 branch. 例如:
data Nat = Zero | Succ Nat
num :: Nat -> Int
num Zero = 0
num (Succ n) = 1 + num n
而當我們需要 evaluate `num (Succ (Succ (Zero)))` 時,
需要知道這裡應該用哪一個 branch, 此時就需要做 pattern matching.
同樣在 let in expression 中也可能會需要碰到,case expression 亦然。
: =====
: 2.
: ghci> case undefined of N _ -> 1
: 1
: the author said:
: "there's no constructor present at runtime, matching against N _ is
: in fact equivalent to matching against the plain wild card (_)"
: but something like
: ghci> case 10.77 of N _ -> 1
: ghci> case "a string" of N _ -> 1
: justifiably fail
: regarding to the author's explanation, why the two cases fail while
: the "undefined" case succeeds?
: aren't they also equivalent to matching against wild card (_)?
也許你不應該用 ghci 來測試,因為這樣混淆了 compile time 和 runtime,
所以讓你搞不清楚 type check 和 runtime exception 的差異。
這邊 undefined 會過的原因,就是作者所說的,而 undefined 可以放在
任何的 context 下都能 type check, 我不知道該不該稱呼他為一種 bottom.
而 10.77 的 type 是 Fractional, "a string" 是 [Char],
很明顯不是 Int, 因此無法 type check, 也不能被 compile.
: sorry to ask so many questions, i checked few documents but got stuck
: every so often.
: little complaint: "a gentle introduction to Haskell" is not gentle but
: prickly to me :(
我不記得我有沒有看過這篇,我個人是看 Yet Another Haskell Tutorial 入門的
--
Hear me exalted spirits. Hear me, be you gods or devils, ye who hold
dominion here:
I am a wizard without a home. I am a wonderer seeking refuge.
Sacrifice
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 220.135.160.129
1F:推 brianhsu:我個人是覺得 Learn A Haskell for Great Good 寫得比較 07/09 13:50
2F:→ brianhsu:淺顯。:p 07/09 13:50
3F:推 etwas:謝謝兩位~原來undefined always type checks 07/09 18:09
4F:推 jaiyalas:[Yet Another Haskell Tutorial]+1 07/10 10:16