(Data.Functor.Classes.Show1 ExprF)没有实例(No instance for (Data.Functor.Classes.Show1 ExprF))

编程入门 行业动态 更新时间:2024-10-21 09:22:09
(Data.Functor.Classes.Show1 ExprF)没有实例(No instance for (Data.Functor.Classes.Show1 ExprF))

我写了一个小程序来找到一个数字的素数因子分解。 除了main函数之外,所有东西似乎都在编译, main函数抱怨无法找到Show1实例。

{-# LANGUAGE DeriveFunctor #-} module FactorAnamorphism where import Data.Functor.Foldable import Data.List nextPrimeFactor :: Integer -> Maybe Integer nextPrimeFactor n = find (\x -> n `mod` x /= 0) [2..(floor $ sqrt $ fromIntegral n)] data ExprF r = FactorF Integer | MultF r r deriving (Show, Functor) type Expr = Fix ExprF factor :: Integer -> Expr factor = ana coAlg where coAlg fac = case (nextPrimeFactor fac) of Just prime -> MultF prime (fac `div` prime) Nothing -> FactorF fac main :: IO () main = putStrLn $ show $ factor 10

日志:

% stack build haskell-playground-0.1.0.0: build (lib + exe) Preprocessing library haskell-playground-0.1.0.0... Preprocessing executable 'factor-anamorphism' for haskell-playground-0.1.0.0... [1 of 1] Compiling FactorAnamorphism ( app/FactorAnamorphism.hs, .stack-work/dist/x86_64-osx/Cabal-1.24.2.0/build/factor-anamorphism/factor-anamorphism-tmp/FactorAnamorphism.o ) /Users/ian/proj/macalinao/haskell-playground/app/FactorAnamorphism.hs:22:19: error: • No instance for (Data.Functor.Classes.Show1 ExprF) arising from a use of ‘show’ • In the second argument of ‘($)’, namely ‘show $ factor 10’ In the expression: putStrLn $ show $ factor 10 In an equation for ‘main’: main = putStrLn $ show $ factor 10 -- While building package haskell-playground-0.1.0.0 using: /Users/ian/.stack/setup-exe-cache/x86_64-osx/Cabal-simple_mPHDZzAJ_1.24.2.0_ghc-8.0.2 --builddir=.stack-work/dist/x86_64-osx/Cabal-1.24.2.0 build lib:haskell-playground exe:factor-anamorphism exe:haskell-playground-exe --ghc-options " -ddump-hi -ddump-to-file" Process exited with code: ExitFailure 1

I've written a small program to find the prime factorization of a number. Everything seems to compile except for the main function, which complains about not being able to find a Show1 instance.

{-# LANGUAGE DeriveFunctor #-} module FactorAnamorphism where import Data.Functor.Foldable import Data.List nextPrimeFactor :: Integer -> Maybe Integer nextPrimeFactor n = find (\x -> n `mod` x /= 0) [2..(floor $ sqrt $ fromIntegral n)] data ExprF r = FactorF Integer | MultF r r deriving (Show, Functor) type Expr = Fix ExprF factor :: Integer -> Expr factor = ana coAlg where coAlg fac = case (nextPrimeFactor fac) of Just prime -> MultF prime (fac `div` prime) Nothing -> FactorF fac main :: IO () main = putStrLn $ show $ factor 10

Logs:

% stack build haskell-playground-0.1.0.0: build (lib + exe) Preprocessing library haskell-playground-0.1.0.0... Preprocessing executable 'factor-anamorphism' for haskell-playground-0.1.0.0... [1 of 1] Compiling FactorAnamorphism ( app/FactorAnamorphism.hs, .stack-work/dist/x86_64-osx/Cabal-1.24.2.0/build/factor-anamorphism/factor-anamorphism-tmp/FactorAnamorphism.o ) /Users/ian/proj/macalinao/haskell-playground/app/FactorAnamorphism.hs:22:19: error: • No instance for (Data.Functor.Classes.Show1 ExprF) arising from a use of ‘show’ • In the second argument of ‘($)’, namely ‘show $ factor 10’ In the expression: putStrLn $ show $ factor 10 In an equation for ‘main’: main = putStrLn $ show $ factor 10 -- While building package haskell-playground-0.1.0.0 using: /Users/ian/.stack/setup-exe-cache/x86_64-osx/Cabal-simple_mPHDZzAJ_1.24.2.0_ghc-8.0.2 --builddir=.stack-work/dist/x86_64-osx/Cabal-1.24.2.0 build lib:haskell-playground exe:factor-anamorphism exe:haskell-playground-exe --ghc-options " -ddump-hi -ddump-to-file" Process exited with code: ExitFailure 1

最满意答案

Show for Fix的实例是: Show1 f => Show (Fix f) ,这就是编译器期望Show1 ExprF 。

Show1可以在Data.Functor.Classes下的Data.Functor.Classes找到,并且有一个TH脚本可以从Text.Show.Deriving -compat中的Text.Show.Deriving派生它。

The instance of Show for Fix is: Show1 f => Show (Fix f), which is why the compiler expects Show1 ExprF.

Show1 can be found in base under Data.Functor.Classes, and there is a TH script to derive it in Text.Show.Deriving from deriving-compat.

更多推荐

本文发布于:2023-08-01 15:37:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1359200.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实例   Classes   Functor   Data   instance

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!