相关疑难解决方法(0)

Haskell中函数的序列化

有没有办法在Haskell中序列化(读/显示)函数?

举例来说:

:t (+1) 
(+1) :: Num a => a -> a
Run Code Online (Sandbox Code Playgroud)

我希望能够有类似的东西:

read "(+1)" :: Num a => a -> a
Run Code Online (Sandbox Code Playgroud)

不幸的是,这会引发错误:

Could not deduce (Read (a -> a)) arising from a use of `read'
from the context (Num a)
  bound by an expression type signature: Num a => a -> a
  at <interactive>:1:1-30
Possible fix:
  add (Read (a -> a)) to the context of
    an expression type signature: Num a => a -> a
  or add an instance …
Run Code Online (Sandbox Code Playgroud)

haskell

11
推荐指数
2
解决办法
1001
查看次数

是否有可能将haskell"可操作"或"免费monad"存储到磁盘?

我有一些简单的原始操作,例如:

在的情况下,操作单子:

import Control.Monad.Operational
type Process a = Program ProcessI a
data ProcessI a where
    GetInput :: ProcessI String
    Dump :: String -> ProcessI ()
getInput :: Process String
getInput = singleton GetInput
dump :: String -> Process ()
dump = singleton . Dump
Run Code Online (Sandbox Code Playgroud)

或者在免费 monad的情况下:

import Control.Monad.Free
type Process = Free ProcessF
data ProcessF a
    = GetInput (String -> a)
    | Dump String a
    deriving (Functor)
getInput :: Process String
getInput = liftF $ GetInput id …
Run Code Online (Sandbox Code Playgroud)

haskell operational continuation

10
推荐指数
1
解决办法
240
查看次数

如何序列化或保存文件Thunk?

在Haskell中,您可以拥有无​​限列表,因为它不完全计算它们,它使用thunk.我想知道是否有一种方法可以序列化或以其他方式将一个数据保存到文件中.例如,假设您有一个列表[0..].然后,你做一些处理就可以了(我最感兴趣tail(:),但它应该支持这样做filtermap为好.)这里是有点什么,我找的一个例子.

serial::(SerialThunk a)=>a->serThunk
serialized = serial ([0..] :: [Int])
main=writeToFile "foo.txt" serialized
Run Code Online (Sandbox Code Playgroud)

deserial::(SerialThunk a)=>serThunk->a
main=do
    deserialized <- readFromFile "foo.txt" :: IO [Int]
    print $ take 10 deserialized
Run Code Online (Sandbox Code Playgroud)

serialization haskell thunk

6
推荐指数
2
解决办法
304
查看次数