我开始做99个haskell问题,我在上,我的单元测试失败了。
显然,这是由于以下原因:
我只是想确保我理解正确,因为我有点困惑。
情况1: func a被定义为没有类型定义或非严格类型定义,然后使用一次,编译器在编译时推断类型没有问题。
情况2:相同的函数a在程序中被多次使用,编译器不能100%确定类型是什么,除非它为给定的参数重新计算函数。
为了避免计算损失,ghc向程序员抱怨说,它需要在a上定义严格的类型才能正常工作。
我认为在我的情况下,assertEqual的类型定义为
assertEqual :: (Eq a, Show a) => String -> a ->
是多态类型的Typeable的类比。但它的工作方式是相当不可预测的:
ghci> :t show
show :: Show a => a -> String
ghci> polyTypeOf show
a1 -> [Char]
ghci> :t fromEnum
fromEnum :: Enum a => a -> Int
ghci> polyTypeOf fromEnum
<interactive>:1:12:
Ambiguous type variable `a0' in the constraint:
寻找以下定义不能满足类型推断的解释:
-- | nub' - naive Data.List.nub implementation with extra constraint
nub' :: (Eq a, Show a) => [a] -> [a]
nub' = foldr (\e ac -> if e `elem` ac then ac else e:ac) []
main = do
print $ nub' []
-- Error: No instance for (Show a0) arising from a use of ‘p
这段代码来自于一本书“为伟大的美好而学习一个Haskell!”:
maximum' :: (Ord a) => [a] -> a
maximum' [] = error "maximum of empty list"
maximum' [x] = x
maximum' (x:xs)
| x > maxTail = x
| otherwise = maxTail
where maxTail = maximum' xs
它在非空列表上运行良好,但提供一个空列表:
main = p
考虑这些功能
{-# LANGUAGE TypeFamilies #-}
tryMe :: Maybe Int -> Int -> Int
tryMe (Just a) b = a
tryMe Nothing b = b
class Test a where
type TT a
doIt :: TT a -> a -> a
instance Test Int where
type TT Int = Maybe Int
doIt (Just a) b = a
doIt (Nothing) b = b
这行得通
main =
现在请看这段新写的代码,没有使用"read“,我仍然在不明确的”show“上得到错误:
data MyType0 a = Tong1 a | Tong2 a deriving Show
data MyType1 a = Cons1 a | Cons2 a | Cons3 | Cons4 deriving Show
data MyType2 a = MyType2 a deriving Show
fun ((Cons2 s):t:ts) (symseq, MyType2 msg) = (symseq, MyType2 (msg ++ ["You provided wrong
(受的启发,并基于我自己的答案的后续。)
考虑以下最小的示例:
test :: (Show a, Show b) => (a -> String, b -> String)
test = (show,show)
(resX, resY) = test
这将导致以下错误:
• Ambiguous type variable ‘a0’ arising from a use of ‘test’
prevents the constraint ‘(Show a0)’ from being solved.
Relevant bindings inclu
考虑以下职能:
foo :: Show a => Maybe a -> [Char]
foo (Just x) = show x
foo Nothing = "Nothing"
然后我尝试使用这个函数:
bar :: [Char]
bar = foo Nothing
传递给foo的参数是Maybe a类型的,其中没有指定a,实际上我们不关心a,因为我们只对Nothing使用foo的情况。但是GHC声称提供了具体的类型:
由于使用a0而产生的模糊类型变量foo
防止解决约束(Show a0)。可能修复:使用类型注释来指定a0应该是什么。
GHC提示指定类型
我正在试着写一个递归计算泰勒的函数:
mcloren x = log (1+x)/(1-x)
compareWithDelta' :: (Num a, Floating a, Integral a, Ord a) => a -> a -> Bool
compareWithDelta' x acc = abs (acc - mcloren x) < 1.0e-10
mcl :: (Floating a, Integral a, Num a) => a -> a
mcl x =
let
mcl' x acc y
| co
我正在尝试使用QuickCheck测试Haskell中有关长度索引列表(a.k.a向量)的属性。我的问题是,GHC抱怨Show函数的main约束上出现了一个不明确的变量。
我已经用标准的方式定义了向量
data Nat = Z | S Nat
data Vec :: Nat -> * -> * where
Nil :: Vec 'Z a
(:>) :: a -> Vec n a -> Vec ('S n) a
vlength :: Vec n a -> Int
vlength Nil = 0
vlength (_ :>
以下代码计算整数元组的列表:
import Data.List (sort)
import Data.Char (digitToInt)
import Text.Printf
-- Check if the given list of numbers is ascending by exactly one
isAscending :: (Eq a, Num a) => [a] -> Bool
isAscending [] = True
isAscending [x] = True
isAscending (x:y:xs) = (x+1 == y) && isAsc
我试图将Int定义为我的类型类Add的一个实例。我想要定义自己的操作符+++,它应该被重载在整数和字符串上。我的目标是能够使用同一个运算符添加整数和连接字符串。因此,我使用实例Add和[char]创建了类型类[char]。
class Add a where
(+++) :: a -> a -> a
instance Add Int where
x +++ y = x + y
instance Add [char] where
x +++ y = x ++ y
1 +++ 2**,问题:在计算表达式时, GHCi给出了以下错误消息:**
<inte
我遇到过这样一种情况:函数无法进行类型检查,除非我显式地将forall添加到其类型签名的开头。
有问题的函数是:
test :: (Typeable a) => a -> a
test x
| typeOf (undefined :: a) == typeOf (undefined :: a) = x
| otherwise = x
GHC对上述问题提出了以下警告:
Ambiguous type variable `a0' in the constraint:
(Typeable a0) arising from a use of `typeOf&
我试图在上限num下创建一个数字max的所有倍数的集合。我用Haskell编写了以下函数:
multiplesOf num max = [num*k | k <- [1..floor (max/num)]]
为什么这个函数在运行时抛出以下错误,以及如何修复它?
<interactive>:26:1: error:
• Ambiguous type variable ‘a0’ arising from a use of ‘print’
prevents the constraint ‘(Show a0)’ from being solved.
Probable fix:
我试图结合量化-约束派生来使用:
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE QuantifiedConstraints, StandaloneDeriving, UndecidableInstances #-}
module HKD2 where
import Control.Monad.Identity
type family HKD f a where
HKD Identity a = a
HKD f a = f a
data Result f = MkResult
{ foo :: HKD f Int
import Data.Monoid
times :: Monoid a => Int -> a -> a
times i = mconcat . replicate i
main =
print $ times 5 5
此代码显示以下错误:
Ambiguous type variable `a0' in the constraints:
(Monoid a0) arising from a use of `times'
at :7:11-15
(Show a0) arising from a use of `pri
我已经深入了解了haskell类型系统的本质,并试图了解类型类的精细点。我已经学到了一堆东西,但是我在下面的代码上遇到了麻烦。
使用以下类和实例定义:
class Show a => C a where
f :: Int -> a
instance C Integer where
f x = 1
instance C Char where
f x = if x < 10 then 'c' else 'd'
为什么它会通过类型检查器:
g :: C a => a -> Int -> a
g x y = f y
为什么即使我添加了Typeable边界以满足函数的要求,下面的代码也不编译?
import Data.Data
a :: Maybe Int
a = cast f
where
f :: Typeable a => a
f = undefined
app/Main.hs:6:5: error:
• No instance for (Typeable a0) arising from a use of ‘cast’
• In the expression: cast f
In an equation for ‘a’:
你好,我有一个带有类型参数的类型:
data A=A1| A2 deriving (Show)
data B=B1| B2 deriving(Show)
data C a=C{var::a,var2::Int}
getters=[show . var,show .var2]
在getters at show . var中,我得到了以下错误:
Ambiguous type variable `a0' arising from a use of `show'
prevents the constraint `(Show a0)' from being solv
我正在努力使以下代码工作(嗯,首先编译!):
module Orexio.Radix where
import Data.Data
import Data.Map (Map)
import qualified Data.Map as Map
import Data.Typeable
import Text.JSON.Generic
class Resource a where
type Representation a :: *
identifier :: Resource a => Identifier a
class Endpoint a where
call ::
如何理解Haskell "1.2 % 3.4“的错误消息?
Prelude> :m +Data.Ratio
Prelude Data.Ratio> 4.3 % 1.2
<interactive>:11:1:
No instance for (Show a0) arising from a use of ‘print’
The type variable ‘a0’ is ambiguous
Note: there are several potential instances:
instance Show Double --
我希望在MiniZinc中使用相同的项创建两个数组,而不一定是按照相同的顺序。在这里,A0中的每一项都应该在A1中
array[1..3] of var int:A0;
array[1..3] of var int:A1;
constraint forall(A2 in A0)(
(A2 in A1) /\ A2 < 5
);
但在这里,似乎存在一个类型错误:
MiniZinc: type error: type error in operator application for `'in''. No matching operator found wit
我想在参数的多态类型和返回类型中定义一组函数,如下所示。
class Poly a b where
poly :: a -> b
instance Poly Int Int where
poly :: Int -> Int
poly a = a
当我在ghci中测试它时,使用poly 3 :: Int,然后给出错误:
*Poly> poly 3 :: Int
<interactive>:486:1: error:
• Ambiguous type variable ‘a0’ arising from a use of ‘poly’
在ghci中:
λ> :t (pure 1)
(pure 1) :: (Applicative f, Num a) => f a
λ> show (pure 1)
<interactive>:1:1:
No instance for (Show (f0 a0))
arising from a use of `show'
Possible fix: add an instance declaration for (Show (f0 a0))
In the expression: show (pure 1)
In
:t error
展示,
error :: [Char] -> a
为了理解,a的类型是什么,我写了这个测试代码,
import Data.Typeable
custom = error "hello how are you"
main = do
let a = custom
putStrLn $ show (typeOf a)
它获取error函数的返回值,并尝试使用show函数打印它。它抛出错误,因为,
ab.hs:6:20:
No instance for (Typeable a0) arising from a use of ‘typeOf’
import Data.Dynamic
default(Integer,Double)
a :: Num a => a
a = 5
-- show :: Show a => a -> String
-- toDyn :: Typeable a => a -> Dynamic
main :: IO ()
-- main = print $ show a -- *** THIS LINE WORKS WELL
main = print $ toDyn a -- *** THIS LINE LEADS TO AN AMBIGUOUS TYPE ERROR
我
或者,用其他术语来说,是否有理由添加如下所述的约束?
工作代码,函数f的最小类型声明:
data ZeroPositive = Zero | Positive Int deriving Show
f :: [Int] -> [ZeroPositive]
f [] = []
f (0:xs) = Zero:(f xs)
f (x:xs) = (Positive x):(f xs)
main = putStrLn . show $ f [0,2]
结果:
[Zero,Positive 2]
具有无用约束的坏代码
data ZeroPositive = Zero | Positive Int
记者:我不知道如何命名我的问题,可以让我知道我应该如何命名它。
如果不指定具体类型,则会得到此错误,该错误非常清楚且易于解决:
由于使用a0而产生的不明确类型变量fct阻止了约束(Read a0)的解决。可能修复:使用类型注释来指定a0应该是什么。
我只需要更多的解释它为什么会起作用?Read将如何知道要返回的类型:
fct :: (Show a, Read b) => a -> b
fct = read . show
main = do
-- DOES NOT WORK: -- print (fct 4)
-- WORKS: -- print (fct 4
我正在尝试编写一个函数,以使嵌套列表变得平坦。
守则:
data NestedList a = Elem a | List [NestedList a]
flatten :: NestedList a -> [a]
flatten (Elem e) = [e]
flatten (List l) = foldMap flatten l
main = do
let c0 = List []
print $ flatten c0
当我试图从main提供一个空列表时,我会得到一个编译器错误:
Ambiguous type variable ‘a0’ arising from
我试图在MiniZinc中使用字符串连接运算符定义一个约束,解决变量a和b的问题。
include "disjunctive.mzn";
var string:a;
var string:b;
constraint("var1/var2" = (a ++ "/" ++ b));
solve satisfy;
output ["\nx=", show(a)];
尽管如此,这似乎是语法错误:
MiniZinc: type error: type error in operator application for `++'.
我已经开始用Haskell做实验了,有个问题。qqq是一个功能,如果用"Nothing“调用,应该打印一个字符串,如果用”Nothing“调用,则打印其他内容。
第一次尝试似乎奏效了:
qqq Nothing = print "There isn't anything to be printed."
qqq (Just x) = print "There is something to be printed." >> print x
main :: IO ()
main = qqq (Just 43)
但是:
当我试图使main
我想知道在GHCi表达式1 .+. 2中计算时,Haskell总是使用什么规则来决定Integer实例而不是其他实例
import Debug.Trace
class MyFuns a where
(.+.) :: a → a → a
instance MyFuns Double where
x .+. y = trace "Double " $ x + y
instance MyFuns Integer where
x .+. y = trace "Integer " $ x + y
instance MyFuns Int
import Data.Maybe
f :: (Show t) => Maybe t -> IO ()
f Nothing = putStrLn "Nothing!"
f (Just x) = putStrLn $ "The number is " ++ (show x)
main = do
f Nothing
这提供了:
foo.hs:7:3:
No instance for (Show a0) arising from a use of ‘f’
The type variable ‘a0’ is ambiguous
如果可以以多种方式输入表达式,Haskell如何选择使用哪一种?
激励示例
举个例子:
$ ghci
GHCi, version 8.8.4: https://www.haskell.org/ghc/ :? for help
Prelude> import Data.Ratio (Ratio)
Prelude Data.Ratio> f z s = show $ truncate $ z + (read s)
Prelude Data.Ratio> :type f
f :: (RealFrac a, Read a) => a -> String -> St