简体   繁体   English

ghc如何执行类型推断

[英]How does ghc perform type inference in

I working through the Paul Hudaks highly recommended book Haskell School of Expression . 我通过Paul Hudaks高度推荐的书籍Haskell School of Expression In the 13:th chapter I stumbled across this definition 在第13章中,我偶然发现了这个定义

type Time = Float

newtype Behavior a = Beh (Time -> a)

The author has declared several instances of the newtype Behavior : Eq , Show , Num , Fractional and Floating , but among these it´s only one function in one of these instance declarations that is bugging me: 作者已经声明了newtype Behavior几个实例: EqShowNumFractionalFloating ,但其中只有一个函数在其中一个实例声明中让我烦恼:

instance Num a => Num (Behavior a) where
  (+) = lift2 (+)                                   -- This one!
  fromInteger = lift0 . fromInteger

lift0 :: a -> Behavior a
lift0 x = Beh (\t -> x)

lift2 :: (a -> b -> c) -> (Behavior a -> Behavior b -> Behavior c)
lift2 g (Beh a) (Beh b)
   = Beh (\t -> g (a t) (b t))                      -- Or actually this one.

time :: Behavior Time
time = Beh (\t -> t)

The author describes, after this, that with these new function declarations we can now write time + 5 and thus lift the (+) operator into the realm of Behavior, or something in that fashion. 在此之后,作者描述了使用这些新的函数声明,我们现在可以编写time + 5 ,从而将(+)运算符提升到行为领域,或者以这种方式提升。 That sounds all good to me, so I nod and smile as I read along. 这对我来说听起来很好,所以当我读书时,我点头微笑。 Suddenly, the author explains that: (time + 5) is equivalent to Beh (\\t -> t + 5) , which sounds totally whacked. 突然,作者解释说:( (time + 5)相当于Beh (\\t -> t + 5) ,这听起来完全不堪重负。 He actually even provides this unfolding of the expressions to prove it: 他实际上甚至提供了表达式的展开来证明它:

time + 5
==> { unfold overloadings for time, (+), and 5 }
(lift2 (+)) (Beh (\t -> t)) (Beh (\t -> 5))
==> { unfold lift2 }
(\ (Beh a) (Beh b) -> Beh (\t -> a t + b t)) (Beh (\t -> t)) (Beh (\t -> 5))
==> { unfold anonymous function }
Beh (\t -> (\t -> t) t + (\t -> 5) t )
==> { unfold two anonymous functions }
Beh (\t -> t + 5)

This is more specifically what I´m having trouble understanding. 更具体地说,这是我理解的问题。 To me the correct statement would be: time + (Beh 5) is equivalent to Beh (\\t -> t + 5) . 对我来说,正确的陈述是: time + (Beh 5)相当于Beh (\\t -> t + 5) But when I infer the type in ghci it tells me (of course) that the author is right and that I´m stupid in some formal way. 但是当我推断ghci中的类型时,它(当然)告诉我作者是正确的,并且我以某种正式方式愚蠢。 Can someone please explain it to me. 有人可以向我解释一下。

(+) has type Num a => a -> a -> a . (+)类型为Num a => a -> a -> a Here a is Behavior Float . a Behavior Float The literal 5 in your code is converted to Behavior Float with fromInteger , which should look like fromInteger n = Beh (\\t -> fromInteger n) . 您的代码中的文字5将使用fromInteger转换为Behavior Float ,它应该看起来像fromInteger n = Beh (\\t -> fromInteger n)

Beh 5 wouldn't typecheck since Beh wraps a function of type Float -> a , not a number. Beh 5不会进行类型检查,因为Beh包含了Float -> a类型的函数Float -> a ,而不是数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM