我对haskell有一点了解,但我总是不确定我应该使用什么样的pragma和优化以及在哪里.喜欢
SPECIALIZE
pragma以及它有什么性能提升一样.RULES
.我听说人们采取了一个不解雇的特定规则?我们如何检查?是否有任何关于高级优化(包括更高和更低级别)的消息来源,特别是对于haskell?
请考虑以下代码:
{-# LANGUAGE MultiParamTypeClasses,FlexibleInstances,FunctionalDependencies,UndecidableInstances,FlexibleContexts #-}
class Foo a c | a -> c
instance Foo Int Float
f :: (Foo Int a) => Int -> a
f = undefined
Run Code Online (Sandbox Code Playgroud)
现在,当我在ghci中看到f的推断类型时
> :t f
> f :: Int -> Float
Run Code Online (Sandbox Code Playgroud)
现在如果我添加以下代码
g :: Int -> Float
g = undefined
h :: (Foo Int a) => Int -> a
h = g
Run Code Online (Sandbox Code Playgroud)
我收到了错误
Could not deduce (a ~ Float)
Run Code Online (Sandbox Code Playgroud)
我无法理解这里发生了什么?限制Foo Int a
应该限制h
to 的类型,Int -> Float
如推断类型所示f …
haskell types functional-programming type-inference functional-dependencies
当我在加载文件之后尝试ghci中的某些内容时putStrLn $ showManyP "%d" 10
它会工作,但是为什么当我在文件中写入它时这不起作用
main = putStrLn $ showManyP "%d" 10
它给出了这个错误
printf.hs:37:19:
Ambiguous type variable `a0' in the constraints:
(Format a0) arising from a use of `showManyP' at printf.hs:37:19-27
(Num a0) arising from the literal `10' at printf.hs:37:34-35
Probable fix: add a type signature that fixes these type variable(s)
In the second argument of `($)', namely `showManyP "%d" 10'
In the expression: putStrLn $ showManyP "%d" 10
In an equation for `main': main = putStrLn $ …
Run Code Online (Sandbox Code Playgroud) 有没有办法使用DefaultSignatures
相关类型系列的扩展.
这是我需要它的一个例子.
class Foo p where
type Back p :: *
type Forward p :: *
customFunc :: p -> IO ()
newtype Bar a = Bar (Forward a)
data Bat = Bat
type family ForwardBar a :: *
type instance ForwardBar Bat = Int
instance Foo (Bar Bat) where
type Back (Bar Bat) = Bat
type Forward (Bar Bat) = ForwardBar Bat
customFunc _ = print "I am different customFunc and this is Bat Bat"
Run Code Online (Sandbox Code Playgroud)
现在我想,只要p …
我试图使用quickcheck生成给定函数的随机参数(假设它的所有类型都有Arbitrary实例和Show实例)以及对这些参数的函数的评估.我只需要打印参数的值并在之后评估答案.所以我期待一个具有以下类型的函数
randomEvaluate :: Testable a => a -> IO ( [String] -- arguments
, String ) -- Answer after evaluating
-- IO is just needed to get a new random number generator. If I pass a generator then I think probably I will not need IO here.
Run Code Online (Sandbox Code Playgroud)
我仍然不确定这里的类型,但我认为Testable a
会这样做.我仍然无法真正得到我需要的东西.我在快速检查数据类型的乱七八糟的所有困惑Rose
,Result
等等.
UPDATE
假设我有一个功能
add :: Int -> Int -> Int
add a b = a+b
Run Code Online (Sandbox Code Playgroud)
然后我假设一个行为
> randomEvaluate add
(["1","3"],"4")
Run Code Online (Sandbox Code Playgroud)
其中1和3是生成的随机值Int
,4是f 1 3 …