gpt4 book ai didi

haskell - 为什么产品有界?

转载 作者:行者123 更新时间:2023-12-02 13:11:32 25 4
gpt4 key购买 nike

http://learnyouahaskell.com/functors-applicative-functors-and-monoids :

Product is defined like this:

newtype Product a =  Product { getProduct :: a }  
deriving (Eq, Ord, Read, Show, Bounded)

为什么Product被强制Bounded

来自同一本书,以下几段:

Its instance for Monoid goes a little something like this:

instance Num a => Monoid (Product a) where  
mempty = Product 1
Product x `mappend` Product y = Product (x * y)

呃?唯一的限制是Num a!但Num 特别表示Integer,并且这是没有界限的(与Int 不同),据我所知。

让我们测试一下:

import Data.Monoid

numbers :: [Integer]
numbers = [1..100]

main = print (getProduct . mconcat . map Product $ numbers)

让我们看看这段代码的实际效果:

m@m-X555LJ:~$ runhaskell wtf.hs
93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
m@m-X555LJ:~$

有效。不会失败。

那么Product的边界是什么? Product 是否有任何限制?

我们再玩一次:

m@m-X555LJ:~$ ghci
GHCi, version 8.0.2: http://www.haskell.org/ghc/ :? for help
Prelude> minBound :: Int
-9223372036854775808
Prelude> maxBound :: Int
9223372036854775807
Prelude> import Data.Monoid
Prelude Data.Monoid> maxBound :: (Product Integer)

<interactive>:4:1: error:
• No instance for (Bounded Integer)
arising from a use of ‘maxBound’
• In the expression: maxBound :: Product Integer
In an equation for ‘it’: it = maxBound :: Product Integer
Prelude Data.Monoid> maxBound :: Product

<interactive>:5:13: error:
• Expecting one more argument to ‘Product’
Expected a type, but ‘Product’ has kind ‘* -> *’
• In an expression type signature: Product
In the expression: maxBound :: Product
In an equation for ‘it’: it = maxBound :: Product
Prelude Data.Monoid> maxBound :: (Product Int)
Product {getProduct = 9223372036854775807}
Prelude Data.Monoid>
Leaving GHCi.
m@m-X555LJ:~$

Product 本身似乎并不是有界Int 是;但是 maxBound::(Product Integer) 会抛出异常! IIUC Product deriving BoundedmaxBoundProduct 上明确定义的 promise 。显然,并非总是如此。

那么为什么 ProductBounded 的实例?

最佳答案

Product不是 Bounded 的实例(无论如何,这将是一个错误)。你错过了什么deriving事实上确实如此。

a formal specification of derived instances在 Haskell 报告中:

If T is an algebraic datatype declared by:

data <em>cx</em> => <em>T</em> <em>u<sub>1</sub></em> … <em>u<sub>k</sub></em> = <em>K<sub>1</sub></em> <em>t<sub>11</sub></em> … <em>t<sub>1k<sub>1</sub></sub></em> | ⋅⋅⋅ | <em>K<sub>n</sub></em> <em>t<sub>n1</sub></em> … <em>t<sub>nk<sub>n</sub></sub></em>
deriving (<em>C<sub>1</sub></em>, …, <em>C<sub>m</sub></em>)

(where m ≥ 0 and the parentheses may be omitted if m = 1) then a derived instance declaration is possible for a class C if these conditions hold:

  1. C is one of Eq, Ord, Enum, Bounded, Show, or Read.
  2. There is a context cx′ such that cx′ ⇒ C tij holds for each of the constituent types tij.

[...]

Each derived instance declaration will have the form: instance (<em>cx</em>, <em>cx′</em>) => <em>C<sub>i</sub></em> (<em>T</em> <em>u<sub>1</sub></em> … <em>u<sub>k</sub></em>) where { <em>d</em> } [...]

这意味着类约束会自动分配给类型参数。

the section on Bounded 中所述:

The Bounded class introduces the class methods minBound and maxBound, which define the minimal and maximal elements of the type. For an enumeration, the first and last constructors listed in the data declaration are the bounds. For a type with a single constructor, the constructor is applied to the bounds for the constituent types. For example, the following datatype:

data  Pair a b = Pair a b deriving Bounded

would generate the following Bounded instance:

instance (Bounded a,Bounded b) => Bounded (Pair a b) where  
minBound = Pair minBound minBound
maxBound = Pair maxBound maxBound

对于Product ,这意味着Product a只是 Bounded 的一个实例如果a是。

关于haskell - 为什么产品有界?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55772069/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com