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