我已经看到一个数据类型定义如下,带有相应的Monoid实例:
data Foo where
FooEmpty :: String -> Foo
FooAppend :: Foo -> Foo -> Foo
-- | Create a 'Foo' with a specific 'String'.
foo :: String -> Foo
foo = FooEmpty
instance Monoid Foo where
mempty :: Foo
mempty = FooEmpty ""
mappend :: Foo -> Foo -> Foo
mappend = FooAppend
Run Code Online (Sandbox Code Playgroud)
你可以找到一个完整的代码要点 Github上.
这是怎么Foo用的:
exampleFoo :: Foo
exampleFoo =
(foo "hello" <> foo " reallylongstringthatislong") <>
(foo " world" <> mempty)
Run Code Online (Sandbox Code Playgroud)
exampleFoo …
在Haskell中,有两种主要方法可以查找函数信息.
像Hoogle和Stackage这样的网站.这些网站提供两种主要的搜索类型:
搜索函数的名称.例如,这里是在Hoogle上搜索一个名为的函数catMaybes.
此搜索返回catMaybes函数的类型,以及它定义的包和模块.
此类搜索的主要用例是当您看到某个地方使用的函数,并且您想知道它的类型以及它所定义的包时.
搜索函数的类型.例如,这里是在Hoogle上搜索类型的函数[Maybe a] -> [a].
此搜索返回多个具有相似类型的函数,第一个函数是catMaybes.它还返回包和模块catMaybes的定义.
在编写代码时会出现此类搜索的主要用例.你知道你需要的功能的类型,你想知道它是否已经在某处定义.例如,您有一个Maybes 列表,并且您希望返回一个列表,其中Nothing删除了所有s.你知道函数会有类型[Maybe a] -> [a].
直接来自ghci.在ghci,使用该:info命令可以很容易地获得有关函数的信息,只要该函数已经在您的环境中.
例如,这是一个ghci会话,显示如何获取有关该catMaybes功能的信息.请注意您必须首先导入Data.Maybes模块:
> import Data.Maybe
> :info catMaybes
catMaybes :: [Maybe a] -> [a] -- Defined in ‘Data.Maybe’
>
Run Code Online (Sandbox Code Playgroud)
:info显示它的类型catMaybes和定义的位置.
在OCaml中,可以使用哪些站点/工具按名称或类型搜索功能?
例如,我正在阅读 …
data Stream f m r
= Step !(f (Stream f m r))
| Effect (m (Stream f m r))
| Return r
Run Code Online (Sandbox Code Playgroud)
对Stream类型的评论说明如下:
的
Stream数据类型是等同于FreeT,并且可以表示的步骤,其中,通过所述第一(算符)参数指定的步骤或"命令"形式的任何effectful继承.
我想知道这种Stream类型是怎样的FreeT?
这是以下定义FreeT:
data FreeF f a b = Pure a | Free (f b)
newtype FreeT f m a = FreeT { runFreeT :: m (FreeF f a (FreeT f m a)) }
Run Code Online (Sandbox Code Playgroud)
看起来不可能在这两种类型之间创建同构.
具体来说,我没有找到一种方法来编写以下两个使它们成为同构的函数:
freeTToStream :: FreeT f …Run Code Online (Sandbox Code Playgroud) 当使用cabal来构建一个Haskell包,这似乎标志着一些包为legacy fallback:
$ cabal build
Resolving dependencies...
Build profile: -w ghc-9.0.1 -O1
In order, the following will be built (use -v for more details):
- appar-0.1.8 (lib:appar) (requires build)
- auto-update-0.1.6 (lib) (requires build)
- base-compat-0.11.2 (lib) (requires build)
...
Building base-orphans-0.8.4 (lib)
Building appar-0.1.8 (all, legacy fallback)
Downloaded memory-0.16.0
Downloading cryptonite-0.29
Installing base-orphans-0.8.4 (lib)
Downloaded cryptonite-0.29
Downloading some-1.0.3
...
Run Code Online (Sandbox Code Playgroud)
可以看到,对于某些库,它们被专门标记为(lib),而其他库则被标记为(all, legacy fallback)。
这些有什么区别?什么legacy fallback意思?
我使用的是cabal-install3.4.0.0 版:
$ …Run Code Online (Sandbox Code Playgroud) 使用Template Haskell时,我收到一个关于数据类型"不在范围内"的奇怪错误.
这是我的Main.hs文件:
{-# LANGUAGE TemplateHaskell #-}
module Main where
import Control.Lens
import Data.Aeson
import Data.Aeson.TH
type Foo = Bar
data Baz = Baz
$(deriveJSON defaultOptions ''Baz)
-- $(makeLenses ''Baz)
data Bar = Bar
main :: IO ()
main = print "hello"
Run Code Online (Sandbox Code Playgroud)
在尝试编译它时,我收到以下错误:
test-0.1.0.0: configure
Configuring test-0.1.0.0...
test-0.1.0.0: build
Building test-0.1.0.0...
Preprocessing executable 'test' for test-0.1.0.0...
[1 of 1] Compiling Main ( Main.hs, .stack-work/dist/x86_64-linux/Cabal-1.22.2.0/build/test/test-tmp/Main.o )
Main.hs:9:12:
Not in scope: type constructor or class ‘Bar’
-- While building package test-0.1.0.0 using: …Run Code Online (Sandbox Code Playgroud) 我想用Purescript的Halogen编写我的前端的特定组件.
例如,我想使用Halogen创建一个注册表.它看起来如下所示:
module RegistrationForm where
import Prelude
...
-- | The state of the application
newtype State = State { email :: String, password :: String }
derive instance genericState :: Generic State
instance showState :: Show State where show = gShow
instance eqState :: Eq State where eq = gEq
initialState :: State
initialState = State { email: "", password: "" }
-- | Inputs to the state machine
data Input a = FormSubmit a
| UpdateEmail String a …Run Code Online (Sandbox Code Playgroud) 在Monad.Reader第19期中的“协程管道”文章中,作者定义了一个泛型Coroutine类型:
newtype Coroutine f m a = Coroutine
{ resume :: m (Either (f (Coroutine f m a)) a)
}
Run Code Online (Sandbox Code Playgroud)
data FreeF f a b = Pure a | Free (f b)
newtype FreeT f m a = FreeT
{ runFreeT :: m (FreeF f a (FreeT f m a))
}
Run Code Online (Sandbox Code Playgroud)
看来FreeT和Coroutine是同构的。这是从一个映射到另一个的函数:
freeTToCoroutine
:: forall f m a. (Functor f, Functor m) => FreeT f m a …Run Code Online (Sandbox Code Playgroud) 当我尝试打电话#require "core"时utop,我收到一个错误:
utop # #require "core";;
Exception: Unix.Unix_error (Unix.ENOENT, "sysconf", "").
Raised by primitive operation at unknown location
Called from file "toplevel/topdirs.ml", line 144, characters 10-51
Run Code Online (Sandbox Code Playgroud)
我有一个.ocamlinit在我当前目录中看起来像这样的文件:
(* Added by OPAM. *)
let () =
try Topdirs.dir_directory (Sys.getenv "OCAML_TOPLEVEL_PATH")
with Not_found -> ()
;;
#use "topfind";;
#thread;;
#camlp4o;;
Run Code Online (Sandbox Code Playgroud)
可能导致此错误发生的原因是什么?它似乎是Findlib的一个错误?
我试图抹着我的~/.opam目录,重新运行opam init,opam switch set 4.05.0以及重新安装core和utop,但这个错误仍然存在.是否有可能我还需要重新安装opam?我目前正在使用opam-1.2.2的二进制版本.
我正在运行Arch Linux,我相信在更新系统上的所有软件包之后会出现这个错误,但我不确定.
我仍然可以用来jbuilder构建可执行文件,但是在运行可执行文件时,它们会因类似的错误而失败:
$ _build/install/default/bin/my_executable
Uncaught …Run Code Online (Sandbox Code Playgroud) Aeson提供FromJSON1并ToJSON1键入类。这些类似于模块中定义的Eq1和Show1类Data.Functor.Classes。
我对Eq1and Show1类的理解是,它们需要能够表达对转换器参数的约束,而无需使用诸如FlexibleContextsand的扩展名UndecidableInstances。
该Data.Functor.Classes模块文档中的示例如下:
假设我们有一个充当转换器的数据类型:T。举个例子,让我们与IdentityT:同构:
data T f a = T (f a)
Run Code Online (Sandbox Code Playgroud)
种类T如下:
T :: (* -> *) -> * -> *
Run Code Online (Sandbox Code Playgroud)
如果存在的Eq1实例f,则可以在Eq1为T f以下实例编写实例时使用它:
instance Eq1 f => Eq1 (T f) where
liftEq :: (a -> b -> Bool) -> T f a -> T f …Run Code Online (Sandbox Code Playgroud) 什么时候想要使用专门的存在类型与依赖对(也称为依赖和或西格玛类型)?
这是一个例子.
以下是长度索引列表和依赖类型的复制函数.请参阅另一个有关如何实施的问题replicateVect.以下是使用该singletons库:
data Vect :: Type -> Nat -> Type where
VNil :: Vect a 0
VCons :: a -> Vect a n -> Vect a (n + 1)
replicateVect :: forall n a. SNat n -> a -> Vect a n
Run Code Online (Sandbox Code Playgroud)
有(至少)两种可能的方法来创建一个采用普通Natural而不是单例的复制函数SNat.
一种方法是为其创建专门的存在类型Vect.我称之为SomeVect遵循以下惯例singletons:
data SomeVect :: Type -> Type where
SomeVect :: forall a n. Vect a n -> SomeVect a
replicateExistentialVect …Run Code Online (Sandbox Code Playgroud) singleton haskell existential-type dependent-type type-level-computation
haskell ×7
free-monad ×2
ocaml ×2
typeclass ×2
aeson ×1
build ×1
cabal ×1
conduit ×1
coroutine ×1
ghc ×1
halogen ×1
interpreter ×1
monoids ×1
ocaml-core ×1
ocaml-dune ×1
opam ×1
purescript ×1
singleton ×1
stream ×1
tooling ×1
utop ×1