2017-04-23 10 views
2

数値の素因数分解を見つけるための小さなプログラムを作成しました。 main関数を除いてすべてコンパイルされているように見えますが、Show1インスタンスが見つからないという不満があります。(Data.Functor.Classes.Show1 ExprF)のインスタンスがありません

{-# 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 

ログ:Fixため

% 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 

答えて

6

Showのインスタンスである:Show1 f => Show (Fix f)、コンパイラはShow1 ExprFを期待する理由です。

Show1は、Data.Functor.Classesの下に存在し、deriving-compatからText.Show.Derivingに派生するTHスクリプトがあります。

+0

ありがとうございました。私の無知を許すが、テンプレートHaskellスクリプトをどのように呼び出すのだろうか?また、deriving-compatはcompatライブラリであると思われます.GHCiの最新バージョンを使用している場合はこれを使用する必要がありますか? –

+0

ファイルの先頭に '{ - #LANGUAGE TemplateHaskell# - }'を追加し、 'Text.Show.Deriving'をインポートし、データ宣言の隣に' $(deriveShow1 "'ExprF)を追加します。 'deriveShow1'によって提供される機能は、現時点では最新のGHCであっても他のどこにも見つけることができないため、' deriving-compat'は単純なcompatライブラリ以上のものです。 –

+0

ありがとうございました。 –

関連する問題