该Debug.Trace模块具有traceEvent向事件日志发出消息的功能。最近,traceMarker添加了一个功能似乎相同的功能。例如,使用以下程序:
module Main where
import Debug.Trace
main :: IO ()
main = do
traceEventIO "This is an event"
traceMarkerIO "This is a marker"
Run Code Online (Sandbox Code Playgroud)
使用编译-debug -w -eventlog -rtsopts并执行+RTS -vu,它在事件日志中显示以下内容:
...
cap 0: running thread 1 (ThreadRunGHC)
cap 0: This is an event
cap 0: User marker: This is a marker
cap 0: thread 1 stopped (finished)
cap 0: created thread 2
...
Run Code Online (Sandbox Code Playgroud)
什么traceEvent时候使用,traceMarker什么时候使用?
我知道我可以通过在文件的 部分中列出本地包来将其添加到 cabal 项目中。最简单的示例是.packages:cabal.projectpackages: .
我还cabal.project可以添加这两件事:
source-repository-package
type: git
location: git@github.com:haskell-streaming/streaming.git
tag: eb3073e6ada51b2bae82c15a9ef3a21ffa5f5529
Run Code Online (Sandbox Code Playgroud)
本地无索引存储库的定义
repository my-local-repository
url: file+noindex:///home/zzz/no-index-repo-dir
Run Code Online (Sandbox Code Playgroud)
看来他们都让我将包添加到项目中。两者有什么区别?
(注意:这个问题与类似名称的source-repository:字段无关,该字段只是说明哪个是与包的源代码关联的存储库。)
我正在使用GHC 9.0.1。我有这个类型类:
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneKindSignatures #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE ViewPatterns #-}
import Data.Kind
type Has :: ((Type -> Type) …Run Code Online (Sandbox Code Playgroud) 我想定义应用函子的深层嵌套组合。例如这样的事情:
{-# LANGUAGE TypeOperators #-}
import Control.Monad.Trans.Cont
import Control.Arrow (Kleisli (..))
import Data.Aeson
import Data.Aeson.Types
import Data.Functor.Compose
import Data.Functor
type Configurator = Kleisli Parser Value
type Allocator = ContT () IO
type Validator = Either String
someConfigurator :: Configurator Int
someConfigurator = undefined
someAllocator :: Allocator Char
someAllocator = undefined
-- the nested functor composition. left-associated
type Phases = Configurator `Compose` Allocator `Compose` Validator
data Foo = Foo Int Char
-- I want to streamline writing this, without …Run Code Online (Sandbox Code Playgroud) 它们都允许您连接到正在运行的容器的stdout/stderr.特别是docker logs --follow似乎与...类似docker attach.
一个命令是另一个命令的过时版本,还是存在显着差异?
我有这种类型,基本上是Kleisli箭头:
{-# language DeriveFunctor #-}
data Plan m i o = Plan (i -> m o) deriving Functor
instance (Monad m) => Applicative (Plan m i) where
pure x = Plan (\_ -> pure x)
Plan f <*> Plan x = Plan (\i -> f i <*> x i)
Run Code Online (Sandbox Code Playgroud)
由于它有一个Applicative实例,我打开ApplicativeDo并尝试使用do-notation构建一个值:
{-# language ApplicativeDo #-}
myplan :: Plan IO () ()
myplan = do
pure ()
pure ()
Run Code Online (Sandbox Code Playgroud)
它不起作用:
No instance for (Monad (Plan IO ())) arising …Run Code Online (Sandbox Code Playgroud) 我想要一个函数,给定任何元组,返回一个形状相同的元组,其中每个组件x都变成了Just x。
例如,maybeizeProduct ('a', 'b')应该返回(Just 'a',Just 'b').
maybeizeProduct 应该适用于任何(合理)数量的组件的元组。
我有这样的记录:
import Data.Functor.Identity
import Control.Monad.Trans.Identity
import Data.Coerce
data Env m = Env {
logger :: String -> m ()
}
env :: Env IO
env = undefined
Run Code Online (Sandbox Code Playgroud)
和这个强制函数
decorate
:: Coercible (r_ m) (r_ (IdentityT m))
=> r_ m -> r_ (IdentityT m)
decorate = coerce
Run Code Online (Sandbox Code Playgroud)
适用于没有问题的记录值:
decoratedEnv :: Env (IdentityT IO)
decoratedEnv = decorate env
Run Code Online (Sandbox Code Playgroud)
但是,如果我定义唯一稍微复杂的记录
data Env' h m = Env' {
logger' :: h (String -> m ())
}
env' :: Env' Identity IO
env' = undefined …Run Code Online (Sandbox Code Playgroud) 我正在使用 GHC 9.2.2 并使用OverloadedRecordDotgeneric -lens。作为一个实验,我想使用重载点作为通用镜头功能(包括类型更改更新)的“前端”。
我有这些辅助定义:
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE OverloadedRecordDot #-}
import Control.Lens ( (&), (.~), Lens )
import Data.Generics.Product.Fields qualified as G
import GHC.Records (HasField (..))
import GHC.TypeLits (Symbol)
import GHC.Generics (Generic)
-- Basically a 'Control.Lens.Reified.ReifiedLens'.
newtype Lensy s t a b = Lensy (Lens s t a b)
pry :: Lensy s t a b -> Lens s t a b
pry (Lensy l) = l
-- Just …Run Code Online (Sandbox Code Playgroud) 我构建了一个黑胶唱片的简单例子.首先,一些语言编译指示和导入:
{-# LANGUAGE DataKinds, TypeOperators #-}
import Data.Vinyl
import Data.Vinyl.Functor
import Control.Applicative
Run Code Online (Sandbox Code Playgroud)
实际示例(为简单起见,它使用HList类型同义词):
mytuple :: HList [Integer,Bool]
mytuple = Identity 4 :& Identity True :& RNil
Run Code Online (Sandbox Code Playgroud)
编译好了.但现在我想使用rtraverse打印黑胶唱片:
printi :: Show a => Identity a -> IO (Identity a)
printi (Identity x) = print x *> pure (Identity x)
main :: IO ()
main = rtraverse printi mytuple *> pure ()
Run Code Online (Sandbox Code Playgroud)
这会出现以下错误:No instance for (Show x) arising from a use of ‘printi’.我想这是预期的,因为rtraverse …
我基于ReaderT 设计模式构建了一个项目。我选择使用简单的处理程序注入作为函数参数,而不是使用类型类方法进行依赖项注入。这部分工作得很好,因为我们能够静态构建依赖树并动态定义环境。
环境可能包含配置以及日志记录效果:: String -> IO ()、时间效果:: IO UTCDate等。考虑以下简化示例
import Control.Monad.Reader (runReaderT, liftIO, reader, MonadReader, MonadIO)
data SomeEnv
= SomeEnv
{ a :: Int
, logger :: String -> IO ()
}
class HasLogger a where
getLogger :: a -> (String -> IO())
instance HasLogger SomeEnv where
getLogger = logger
myFun :: (MonadIO m, MonadReader e m, HasLogger e) => Int -> m Int
myFun x = do
logger <- reader getLogger
liftIO $ …Run Code Online (Sandbox Code Playgroud) 使用函数getLine.
它有一种类型
getLine :: IO String
Run Code Online (Sandbox Code Playgroud)
如何从此IO值中提取String.更一般地说,我该如何转换它.
IO a
Run Code Online (Sandbox Code Playgroud)
对此:
a
Run Code Online (Sandbox Code Playgroud)
如果这是不可能的.为什么我不能这样做?
我正在尝试使用库的长度函数streaming-bytestring Data.ByteString.Streaming.Char8。
我看到返回值的类型为Of,但不清楚如何检查它。我试过使用case,但Not in scope: data constructor ‘O.Of’即使我对进行了合格的导入,编译器也会说Data.Functor.Of。
如何检查价值?
代码示例:
ghci> let bs = BSSC.string "tiger"
ghci> bs
Chunk "tiger" (Empty (()))
ghci> BSSC.length bs
6 :> ()
ghci> let len = BSSC.length bs
ghci> :t len
len :: Monad m => m (OO.Of Int ())
Run Code Online (Sandbox Code Playgroud) haskell ×12
applicative ×2
do-notation ×2
cabal ×1
coerce ×1
docker ×1
ghc ×1
haskell-lens ×1
monads ×1
profiling ×1
vinyl ×1