2017-04-19 1 views
1

저는 하스켈에서 초보자입니다. 암호. main()의 모든 명령은 IO()가되어야하며, Graphics.Gloss.Interface.IO.Animate에서 사용한 함수 중 하나가 IO()를 반환하지 않아 오류가 발생했음을 이해합니다. 나는 광택 패키지를 사용하여 유전자 알고리즘의 결과를 표시하고 싶다. 여기 내 코드입니다 :예상 유형 'IO()'을 실제 유형 '(컨트롤러 -> IO()) -> IO()'와 일치시킬 수 없음

module Main where 

import Prelude as P 
import Control.Monad.Random as Rand 
import Data.Functor 
import Data.IORef 
import Graphics.Gloss.Interface.IO.Animate 
import Graphics.Solution 
import Graphics.Plot 

import Args 
import Task 
import Parsing 
import Genetic 

import Control.Concurrent.Async 
import Control.Concurrent.STM.TChan 
import Control.Monad.STM 
import Control.Arrow (first) 


main :: IO() 
main = do 
    args <- parseOptions 

    opts <- loadEvolOptions (evolOptionsFileName args) 
    gen <- newStdGen 
    [email protected](Task _ twrs _ _) <- loadTask (inputFileName args) (fitnessFuncFileName args) 

    chan <- newTChanIO 
    asolution <- async $ solve chan gen opts task 
    dataRef <- newIORef [] 
    finalSolutionRef <- newIORef Nothing 

    animateIO mode white $ const $ do 
     mfinsol <- readIORef finalSolutionRef 
     case mfinsol of 
     Just solution -> do 
      samples <- readIORef dataRef 
      return $ solutionPicture task solution (fitnessPlot samples) 
     Nothing -> do 
      msolution <- poll asolution  
      case msolution of 
      Nothing -> do 
       mv <- atomically $ tryReadTChan chan 
       case mv of 
       Nothing -> return() 
       Just v -> modifyIORef dataRef (++[v]) 
       samples <- readIORef dataRef 
       return $ fitnessPlot samples 
      Just esol -> case esol of 
       Left e -> fail $ show e 
       Right solution -> do 
       saveResult (outputFileName args) (filterTowers solution twrs) 
       writeIORef finalSolutionRef (Just solution) 
       samples <- readIORef dataRef 
       return $ solutionPicture task solution (fitnessPlot samples) 
     where mode = InWindow "test_genetic_al" (1280, 1024) (10, 10) 
      fitnessPlot ds = translate (-300) (-200) $ scale 600 600 $ plot "generation" "fitness" $ first fromIntegral <$> ds 

그리고 이것은 내가 가지고있는이다 : 나는 구글을 통해 내 문제를 검색하는 등 여러 번에 유래하지만 아직 이것에 대한 해결책을 찾을 수 있었어요

Couldn't match expected type ‘IO()’ 
       with actual type ‘(Controller -> IO()) -> IO()’ 
    In a stmt of a 'do' block: 
     animateIO mode white 
     $ const 
     $ do { mfinsol <- readIORef finalSolutionRef; 
       case mfinsol of { 
       Just solution -> do { ... } 
       Nothing -> do { ... } } } 
    In the expression: 
     do { args <- parseOptions; 
      opts <- loadEvolOptions (evolOptionsFileName args); 
      gen <- newStdGen; 
      [email protected](Task _ twrs _ _) <- loadTask 
             (inputFileName args) (fitnessFuncFileName args); 
      .... } 
    In an equation for ‘main’: 
     main 
      = do { args <- parseOptions; 
       opts <- loadEvolOptions (evolOptionsFileName args); 
       gen <- newStdGen; 
       .... } 
      where 
       mode = InWindow "test_genetic_al" (1280, 1024) (10, 10) 
       fitnessPlot ds 
       = translate (- 300) (- 200) 
        $ scale 600 600 
        $ plot "generation" "fitness" $ first fromIntegral <$> ds 

오류. 도와주세요.

P/S : 이것은 Graphics.Gloss위한 가이드 라인 : 나는 (당신이 코멘트 영역에서 아래 볼 수 있습니다) Lazersmoke의 제안을 준 후, 내 바보 같은 질문에 대한 다시 https://hackage.haskell.org/package/gloss-1.11.1.1/docs/Graphics-Gloss-Interface-IO-Animate.html

미안 해요, 다른있어 내가 라인 변경

: 내가 요청 오류와 매우 유사한 오류 animateIO 모드 흰색 $의 CONST $를 속으로

않습니다 animateIO 모드 화이트 (_ -> 리턴()) $의 CONST $를

Couldn't match type ‘Picture’ with ‘()’ 
    Expected type: Controller -> IO() 
     Actual type: Controller -> IO Picture 
    In the second argument of ‘($)’, namely 
     ‘const 
     $ do { mfinsol <- readIORef finalSolutionRef; 
       case mfinsol of { 
       Just solution -> do { ... } 
       Nothing -> do { ... } } }’ 
    In a stmt of a 'do' block: 
     animateIO mode white (\ _ -> return()) 
     $ const 
     $ do { mfinsol <- readIORef finalSolutionRef; 
       case mfinsol of { 
       Just solution -> do { ... } 
       Nothing -> do { ... } } } 
    In the expression: 
     do { args <- parseOptions; 
      opts <- loadEvolOptions (evolOptionsFileName args); 
      gen <- newStdGen; 
      [email protected](Task _ twrs _ _) <- loadTask 
             (inputFileName args) (fitnessFuncFileName args); 
      .... } 
+0

오류는 단지 하나의 매개 변수를 제공하는 것을 잊어 버린 것을 의미합니다.이 경우에는 'Controller -> IO()'함수입니다. – ocramz

+0

어떻게 해결할 수 있습니까? 제게 보여 주시겠습니까? –

+0

그가 말했듯이, 즉 "디스플레이 컨트롤러를 사용하기위한 콜백"입니다. 연결된 문서에서 볼 수 있듯이. 타입을 만족시키기 위해서는'animateIO mode white (\ _ -> return()) $ const $ do'과 같은 것을 필요로합니다.하지만 실제로 콜백을 할 필요가 있습니다. – Lazersmoke

답변

0

animateIO은 몇 개의 인수를 취합니까?

animateIO :: Display  
      -> Color 
      -> (Float -> IO Picture) 
      -> (Controller -> IO()) 
      -> IO() 

4 인. animateIO에 제공된 인수는 몇 개입니까?

animateIO mode white $ … 

3.

animateIO mode white $ … 

은 오류 메시지의 의미와 정확히 일치합니다. 당신이 Controller -> IO() 부분을 사용하지 않기 때문에, 당신은 animateIO 자신을 제공 할 수 있습니다 : 세 번째 인수가 Picture 아닌 IO() 생산하기 때문에 당신의 (\_ -> return()) 작동하지 않았다

animateIO' :: Display -> Color -> IO Picture -> IO() 
animateIO' m c a = animateIO m c (const a) (const $ return()) 

참고.

+0

나는 조언을 해주고 가능한 한 빨리 결과를 게시하는 것처럼 내 코드를 고칠 것이며, 저를 도와 주셔서 감사합니다. –

관련 문제