【发布时间】:2023-03-12 04:57:01
【问题描述】:
我预计以下代码会因违反 minBound 和 maxBound 而失败并出现类型错误。但是,正如您所看到的,它通过并没有标记错误。
{-# OPTIONS_GHC -XTypeSynonymInstances #-}
module Main where
type Probability = Float
instance Bounded Probability where
minBound = 0.0
maxBound = 1.0
testout :: Float -> Probability
testout xx = xx + 1.0
main = do
putStrLn $ show $ testout 0.5
putStrLn $ show $ testout (-1.5)
putStrLn $ show $ testout 1.5
在前奏曲中我明白了
*Main> :type (testout 0.5)
(testout 0.5) :: Probability
在提示符下我得到这个:
[~/test]$runhaskell demo.hs
1.5
-0.5
2.5
很明显,我没有正确声明 Bounded,而且我确定我在语法上做错了什么。 Google 上没有太多关于有界类型类的简单资料,因此我们将不胜感激。
【问题讨论】: