2017-04-23 15 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을 기대하는 이유입니다.

Show1Data.Functor.Classes 아래에서 찾을 수 있으며 deriving-compat에서 Text.Show.Deriving에 파생 된 TH 스크립트가 있습니다.

+0

감사합니다. 실례지만, 하스켈 스크립트 템플릿을 어떻게 호출할까요? 또한 deriving-compat은 compat 라이브러리 인 것 같습니다. 최신 버전의 GHCi를 사용하고 있다면이 라이브러리를 사용해야합니까? –

+0

파일 상단에'{- # LANGUAGE TemplateHaskell # -}'을 추가하고,'Text.Show.Deriving'을 가져 와서 데이터 선언 옆에'$ (deriveShow1 ''ExprF)'를 추가하십시오. 'deriveShow1'에서 제공하는 기능은 최신 GHC에서도 순간적으로 찾을 수 없기 때문에 'deriving-compat'은 단순한 compat 라이브러리 이상입니다. –

+0

감사합니다. –