【发布时间】:2017-05-01 15:59:04
【问题描述】:
我正在尝试使用newtypes 并尝试向Lib 的用户公开newtype F。我在下面编写了一个简单的用例示例,我想在应用someFunc 后检查newtype F。
但是编译器指出我认为F Double 实际上是F (Double -> Double),因此没有 Show 实例,即,没有出现 (Show (Double -> Double)) 的实例来自“show”的使用(也许您没有将函数应用于足够的参数?)
我明白了,但我很难考虑如何为此构建一个显示实例。谁能想到一种方法可以保留我的newtype F,同时对这个库的用户隐藏F' 和someFunc'?
-- Lib.hs
module Lib
( someFunc
, F(..)
) where
newtype F a = F (a -> Double)
type F' a = a -> Double
someFunc :: Double -> Double -> F Double
someFunc a b = F (\x -> someFunc' a b x)
someFunc' :: Double -> Double -> F' Double
someFunc' a b x = a * x + b
-- Main.hs
module Main where
import Prelude
import Data.Monoid ((<>))
import Lib
foo :: Double -> F Double
foo x = someFunc 1.0 2.0
inspectFoo :: F Double -> String
inspectFoo (F x) = "F(" <> show x <> ")"
main :: IO ()
main = print . inspectFoo $ foo 2.0
【问题讨论】:
-
您的
someFunc'类型错误。应该是someFunc' :: Double -> Double -> Double -> F' Double -
不,该类型在 OP 的代码@Shersh 中是正确的
-
或者,确实如此。
F与F'不匹配,因为赶时间 :(
标签: haskell