天道酬勤,学无止境

haskell

How to simplify calling a field on a polymorphic field name into one typeclass

In a previous question I asked how a record field can be made polymorphic when using DuplicateRecordFields. I got an excellent answer for this from @user2407038. He answered the question to my initial spec providing one type class per field, but he mentioned that it could all be simplified into one typeclass. (Note: this too can be generalized to a single class with an additional parameter corresponding to the field name; this is probably outside the scope of this question). I'm not sure how to go about doing this generalization. Does anybody have any ideas on how this can accomplished?

2021-09-24 08:25:12    分类:问答    haskell   polymorphism   typeclass   haskell-lens

使用模板 Haskell 时键入同义词“不在范围内”(Type synonyms “not in scope” when using Template Haskell)

问题 使用 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" 尝试编译它时,出现以下错误: 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

2021-09-24 07:58:39    分类:技术分享    haskell   ghc   template-haskell   type-synonyms

元组的一元改变(Monadic alteration to tuple)

问题 我正在寻找一个类似于以下类型的函数: Monad m => (a, b) -> (b -> m c) -> m (a, c) 在我看来,它是 bind ( >>= ) 和镜头操作的某种组合。 我知道我可以在绑定后通过模式匹配来解决这个问题,但我的直觉告诉我,有一种“更简单”的方法可以通过利用镜头来写这个。 有没有这样的操作? 回答1 这绝对是镜头。 monad 实际上只是有点分散注意力,因为您只需要一个函子: changesecond (a, b) f = fmap (a,) (f b) 我很确定_2镜头可以用一个基本的镜头来满足你的要求,比如也许over但我对图书馆还不太熟悉。 编辑 真的不需要组合器。 你可以写 changesecond pair f = _2 f pair 您应该能够从Lens类型的一般定义中解决这个问题。 编辑 2 这个简单的例子展示了 Van Laarhoven 镜头结构的主题: 从上下文中提取焦点。 应用给定的函数来产生一个函子的结果。 使用fmap将上下文恢复到结果。 Ed Kmett 的lens库以各种方式阐述了这个主题。 有时它会加强函子约束。 有时它会将函数泛化为 profunctor。 在Equality的情况下,它删除了函子约束。 事实证明,相同的基本类型形状可以表达很多不同的想法。 回答2

2021-09-24 07:58:31    分类:技术分享    haskell   haskell-lens

Syntax confusion (do block)

Sorry for a poor title, feel free to edit. I can't understand what the problem is, so it might be altogether wrong. Below is the code (this is after I've done like a hundred of permutations and different sequences of let-do-if and tabulation, and I'm exhausted): -- The last statement in a 'do' construct must be an expression numberOfGoods :: IO String numberOfGoods = do putStrLn "Enter year (2000-2012):\n" let intYear = readYear in if (intYear < 2000 || intYear > 2012) then error "Year must be withing range: 2000-2012" else c <- readIORef connection [Only i] <- query_ c ("select count('*')" ++

2021-09-24 06:16:17    分类:问答    haskell   syntax   indentation

`stack build` failed due to `Undefined symbols for architecture x86_64` on Mac OS X using nix

First the error messages: $ stack build Linking /Users/yuzhao/.stack/setup-exe-cache/x86_64-osx/tmp-Cabal-simple_mPHDZzAJ_2.2.0.1_ghc-8.4.4 ... clang-5.0: warning: argument unused during compilation: '-nopie' [-Wunused-command-line-argument] clang-5.0: warning: argument unused during compilation: '-nopie' [-Wunused-command-line-argument] Undefined symbols for architecture x86_64: "_iconv", referenced from: _hs_iconv in libHSbase-4.11.1.0.a(iconv.o) (maybe you meant: _base_GHCziIOziEncodingziIconv_iconvEncoding13_info, _base_GHCziIOziEncodingziIconv_iconvEncoding1_info , _base

2021-09-24 05:11:20    分类:问答    haskell   haskell-stack

Function returns zero if parameter has more than 7 zeros

I wrote a haskell function which counts pi number with precision given as parameter: mojePi :: Integer -> Float mojePi x = sqrt $ 6 * mySum x where mySum 1 = 1 mySum x = sum $ map (1/) $ map (**2) [y,y-1..1] where y = fromInteger x If I run this function with 10000000(7 zeros) it works fine, but with 100000000(8 zeros) and more it immediately returns 0. Why does it happen?

2021-09-24 04:51:06    分类:问答    haskell

当前的 SYB 是否允许使用新类型扩展泛型函数?(Does the current SYB permit extension of generic functions with new types?)

问题 前两篇 Scrap Your Boilerplate 论文描述了一种编写通用函数的方法,这些函数适用于一般类型,但对特定类型有特殊情况。 例如,fromJSON从aeson包装,定义了一个通用功能从JSON转换,但提供了类型,如列表或特殊情况下Int : parseJSON :: (Data a) => Value -> Parser a parseJSON j = parseJSON_generic j `ext1R` list `ext1R` vector `ext2R'` mapAny `ext2R'` hashMapAny -- Use the standard encoding for all base types. `extR` (value :: F Integer) `extR` (value :: F Int) ... 然而,正如第三篇 SYB 论文所指出的,“当通用函数定义的递归结被捆绑时,所有类型特定的情况 [需要] 立即提供”。 然后论文提出了一种通过类型类机制解除这种限制的方法。 前两篇 SYB 论文(经过一些修改)是 syb 包的一部分,但第三篇不是。 在 Hackage 上实施 SYB 时,是否有其他方法可以解除所有特定于类型的情况需要立即指定的限制? 回答1 据我所知,没有办法绕过该系统内的限制。 事实上,没有必要在第三篇论文中引入一种新方法。

2021-09-24 04:06:02    分类:技术分享    haskell   generic-programming   scrap-your-boilerplate

了解 cabal 依赖消息(Understanding cabal dependency messages)

问题 目前,当我尝试安装 GOA 时,我收到以下消息: >sudo cabal install goa Resolving dependencies... In order, the following would be installed: directory-1.1.0.2 (reinstall) changes: filepath-1.3.0.0 -> 1.2.0.1 process-1.0.1.5 (new version) goa-3.1 (new package) cabal: The following packages are likely to be broken by the reinstalls: .... 我的问题是这是否意味着我目前默认使用“filepath-1.3.0.0”,并且 cabal(在 --force-reinstalls 上)将覆盖它并将“filepath-1.2.0.1”设为默认值? 我有点困惑的原因是,当我查看位置时: cabal/packages/hackage.haskell.org/filepath 我发现 1.2.0.1 和 1.3.0.0 都存在。 此外,命令“cabal list filepath”表示已安装两个软件包(请参阅下面的“已安装版本:1.2.0.1、1.3.0.0”): >cabal list filepath

2021-09-24 03:47:35    分类:技术分享    haskell   dependencies   dependency-management   cabal

Intuition in parsing Graph Dimacs format in Haskell

I'm a beginner in Haskell, and I'm trying to learn few concenpts so I would appretiate your help. I read few books and guides but I can't understand how can I use IO operations. I'm trying to parse file in this format and create graph object. So far I have this code: (nodes are identified by positive integer) data Edge = Edge Int Int deriving(Show) data Graph = Graph { nodeCount :: Int, edges :: [Edge] } deriving (Show) parseInput :: String -> IO () parseInput filePath = do handle <- openFile filePath ReadMode contents <- hGetContents handle putStrLn contents hClose handle I understand that

2021-09-24 02:00:39    分类:问答    haskell   io

Haskell:`Num [a] => a` 和 `Num a => [a]` 之间有什么区别(Haskell: What is the differrence between `Num [a] => a` and `Num a => [a]`)

问题 显然,我的类型签名已关闭。 我后来发现了原因。 现在,我有兴趣了解更多关于我的错别字上的 GHCI 推断签名的信息。 我试图让这段代码工作: elemNum :: (Eq a, Num b) => a -> [a] -> b elemNum e l = f e l where f _ [] = [] -- this was my typo, supposed to read 0 f e (x:xs) | x == e = 1 + f e xs | otherwise = f e xs 由于上述原因,它显然不起作用; 但是,如果我删除我的签名,它会编译(不知道为什么,请解释一下),然后我得到这个签名: elemNum :: (Num [a], Eq t) => t -> [t] -> [a] 我以前从未见过类型类Num [a] .. 这是什么意思,它与(Num a) => [a] 。 回答1 Num a表示可以将类型a视为数字; 例如。 您可以将两个a s 相加得到一个新的a或者您可以否定a并得到一个a 。 Integer和Double属于这一类。 相应地, Num [a]表示可以将类型[a]视为数字。 即你可以在两个列表加起来a获得的新列表a 。 这不太可能有意义,因为没有列表是数字(默认情况下)。 这意味着您将列表视为数字,从而导致 GHC 得出结论

2021-09-24 01:30:21    分类:技术分享    haskell   types   typeclass