这是一些简单的代码,需要-XRebindableSyntax.
{-# LANGUAGE RebindableSyntax, NoImplicitPrelude #-}
import NumericPrelude
import qualified Algebra.Additive (C)
import qualified Algebra.Ring (C)
newtype Foo = Foo Int deriving (Show)
instance Algebra.Additive.C Foo where
(Foo x) + (Foo y) = Foo (x+y)
instance Algebra.Ring.C Foo where
fromInteger = Foo . fromInteger
f :: Foo -> Foo -> Foo
f x y = x + y
g = f 3 5
Run Code Online (Sandbox Code Playgroud)
这是我的 GHCi 成绩单:
> ghci Foo.hs
GHCi, version 7.8.2
...
*Main> g
Foo 8
*Main> f 3 5
<interactive>:3:3:
No instance for (GHC.Num.Num Foo) arising from the literal ‘3’
In the first argument of ‘f’, namely ‘3’
In the expression: f 3 5
In an equation for ‘it’: it = f 3 5
*Main> :set -XRebindableSyntax
*Main> f 3 5
Foo 8
Run Code Online (Sandbox Code Playgroud)
我有 95% 的把握,当我在 GHCi 7.8 之前加载带有扩展名的文件时,我不必在 GHCi 中重置该扩展名。这是在某处记录的,还是一个错误?是否有一个简单的解决方法(例如,告诉 GHCi 总是使用-XRebindableSyntax,我通常会需要它?)
是否有一个简单的解决方法(例如,告诉 GHCi 始终使用我通常需要的 -XRebindableSyntax?)
您可以将.ghci文件放在与源文件相同的目录中。现在,如果您从该目录启动 ghci,该.ghci文件将被加载并执行其内容,就像您在 ghci 提示符下键入的命令一样。在这种情况下,你可以把:set -XRebindableSyntax它放在那里。
我认为这比放入你的主目录更好,:set -XRebindableSyntax因为.ghci你可能在其他目录中有其他需要不同扩展的 Haskell 项目。
有关将哪些内容放入特定于项目的 ghci 文件的更多想法,请参阅Neil Mitchell 的博客文章。