我在ghc-users邮件列表上问了这个问题并得到了一些有用的回复,但仍然不明白这段代码中发生了什么.
本质上我试图理解如何捕获异常BlockedIndefinitelyOnMVar来恢复可能尚未返回的锁,并通常理解此异常.
以下是一些单线程代码:
-- This raises the exception only once and the lock is successfully restored:
main1 = do
lock <- newMVar ()
lockPrint "good1" lock
takeMVar lock
putStrLn "main: took lock but didn't return it!"
-- exception is raised and lock is restored here:
lockPrint "good2" lock
-- no exception raised:
lockPrint "good3" lock
readMVar lock
putStrLn "great success"
lockPrint :: String -> MVar () -> IO ()
lockPrint name v = takePrint `finally` put
where put …Run Code Online (Sandbox Code Playgroud) 我使用wl-pprint包,因为标准的 PrettyPrinter 缺乏功能。一切都很好,除了vcat函数中的空文档(与<$>组合器相同)。
正确行为:
import Text.PrettyPrint
> vcat[text "a", empty, text "b"]
a
b
Run Code Online (Sandbox Code Playgroud)
wl-pprint 显示一个额外的空行:
import Text.PrettyPrint.Leijen
> vcat[text "a", empty, text "b"]
a
b
Run Code Online (Sandbox Code Playgroud)
那我能做什么?这是IMPOSIBLE进行筛选VCAT列表中,因为没有Eq为实例Doc。
我想在库中做一些魔术,允许产品类型被多态地破坏.这是一个或多或少的工作模型,说明了我想做的事情:
{-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies, FlexibleInstances, UndecidableInstances #-}
newtype Wrapped a = Wrapped { unwrap :: a }
-- our example structure
ex :: (Int, (Int, Int))
ex = (1,(2,3))
class WrapDecomp x y | y -> x where
decomp :: x -> y
instance (WrapDecomp x x', WrapDecomp y y')=> WrapDecomp (x,y) (x',y') where
decomp (x,y) = (decomp x, decomp y)
instance WrapDecomp x (Wrapped x) where
decomp = Wrapped
example = let w = decomp ex
(w0, w1) …Run Code Online (Sandbox Code Playgroud) 我正在使用python和boto进行cloudwatch指标.我希望能够MetricName为所有其他维度激活警报.
比如我在度量sandbox命名空间MetricName中MemoryUsage和InstanceId的i-xxx.是否可以定义MemoryUsage针对所有 InstanceId尺寸触发的单个警报?
我研究过Haskell的标准.他们声明变量名称没有长度限制.如何在GHC中实施?变量名称是否有长度限制?
在我正在处理的库中,我有一个类似于以下的API:
data Collection a = Collection Seed {-etc...-}
type Seed = Int
newCollection :: Seed -> IO (Collection a)
newCollection = undefined
insert :: a -> Collection a -> IO () -- ...and other mutable set-like functions
insert = undefined
mergeCollections :: Collection a -> Collection a -> IO (Collection a)
mergeCollections (Collection s0 {-etc...-}) (Collection s1 {-etc...-})
| s0 /= s1 = error "This is invalid; how can we make it statically unreachable?"
| otherwise = undefined
Run Code Online (Sandbox Code Playgroud)
我希望能够强制用户不能打电话mergeCollections …
我有一个简单的C例程,它接受四个单词并返回四个单词,gcc可以优化并发出一些GHC不支持的初始化.我正在尝试以各种方式调用此过程,并且无法尝试使用此处描述的技术foreign import prim.
以下是为每个输入单词添加1,但是段错误.
Main.hs:
{-# LANGUAGE GHCForeignImportPrim #-}
{-# LANGUAGE ForeignFunctionInterface #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE UnboxedTuples #-}
{-# LANGUAGE UnliftedFFITypes #-}
import Foreign.C
import GHC.Prim
import GHC.Int
import GHC.Word
foreign import prim "sipRound"
sipRound_c# :: Word# -> Word# -> Word# -> Word# -> (# Word#, Word#, Word#, Word# #)
sipRound_c :: Word64 -> Word64 -> Word64 -> Word64 -> (Word64, Word64, Word64, Word64)
sipRound_c (W64# v0) (W64# v1) (W64# v2) (W64# v3) …Run Code Online (Sandbox Code Playgroud) 我想要类似以下内容:
constrName :: Data a=> a -> String
constrName = showConstr . toConstr
Run Code Online (Sandbox Code Playgroud)
但是对于GHC.Generics. 我看到了这个Constructor类,但在范围内没有看到任何实例。我正在使用base-4.8.1.0.
为什么这会通过Liquid Haskell验证?
{-@ sub :: Nat -> Nat -> Int @-}
sub :: Int -> Int -> Int
sub i j = i - j
Run Code Online (Sandbox Code Playgroud)
从LH的角度来看,这是否意味着Nat相同Int?
所以我试图准确理解Haskell do符号是如何工作的.据我所知,它与monad一起使用,它基本上扩展(因为它实际上是语法糖)到与bind(>>=)或then(>>)连接的匿名函数,如下所示https://en.wikibooks.org/wiki/Haskell/Syntactic_sugar #Do_notation.
但是我的问题是为什么以下命令
Prelude> do [1, 2, 3]; "hello"
Run Code Online (Sandbox Code Playgroud)
回报
"hellohellohello"
Run Code Online (Sandbox Code Playgroud)
我知道数组实际上是monad(并且这些字符串是字符数组)但是我没有看到它如何导致上面的行为.
haskell ×9
ghc ×6
types ×2
boto ×1
c ×1
concurrency ×1
do-notation ×1
ffi ×1
generics ×1
monads ×1
pretty-print ×1