相关疑难解决方法(0)

如何在Haskell中使用Bounded类型类定义具有浮点范围的类型?

由于违反了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
Run Code Online (Sandbox Code Playgroud)

在序曲中我得到了这个

*Main> :type (testout 0.5)
(testout 0.5) :: Probability
Run Code Online (Sandbox Code Playgroud)

在提示我得到这个:

[~/test]$runhaskell demo.hs
1.5
-0.5
2.5
Run Code Online (Sandbox Code Playgroud)

很明显,我没有正确宣布有限,我确信我在语法上做错了.谷歌关于有界类型类没有太多简单的东西,所以任何帮助都会非常感激.

haskell types typeclass

16
推荐指数
1
解决办法
4504
查看次数

标签 统计

haskell ×1

typeclass ×1

types ×1