Re: [问题] pattern matching in Haskell

楼主: noctem (noctem)   2010-07-10 14:33:13
※ 引述《etwas (i'm only dust)》之铭言:
: data DataInt = D Int
: deriving (Eq, Ord, Show)
: newtype NewtypeInt = N Int
: deriving (Eq, Ord, Show)
: =====
: 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?
大致上,如果一个值去 match 一个 data constructor 的时候
就必须被 evaluate 成 weak head normal form (WHNF). 在这个
例子中,undefined 要和 D _ 配对,于是电脑试着把 undefined
evalulate 成 WHNF,但当然做不到。于是就开始 loop 了。
: in this case, does pattern matching run into a divergence?
是的。通常 divergence 指的是无法“聚合”成一个值。
: pattern matching, does it happen in runtime, compile time or both?
在 Haskell 中应该是只有 runtime 才会发生的。
: if it's a runtime process, then some sort of bookkeeping data about
: constructors should be retained during programme execution, right?
是的。基本上 runtime 时的值都有某些方式让我们可测出它
是什么东西。例如一个 list, 我们当然得知道它是或不是空
的 (是 [] 或是 _:_ ).
: ghci> case undefined of N _ -> 1
: 1
NewtypeInt 是用 newtype 定义的, 因此和 DataInt 有些不同。
如果我们宣告 newtype T = C U, 定出的 T 其实“就是”U.
newtype 的使用时机是我们想把某些 U 当作不一样的 type
(例如定义成某些 class 的 instance),但不想要多耗费一个
data constructor 去包它。
所以 NewtypeInt 的那个 N 其实是不存在的。去 match N _
和 match _ 其实是一样的事情。因此上面的式子不会 loop.
: 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?
以上这两个情形都会出现 type error. 那又是另一个层次的问题了... :)

Links booklink

Contact Us: admin [ a t ] ucptt.com