Ich bin ein Neuling in Haskell, nahm ich diesen Fehler, wenn ich versuche, meine konfigurieren Code. Ich verstehe, dass alle Anweisungen in main() ein IO() sein müssen, und der Fehler ist aufgetreten, weil eine der Funktionen, die ich verwendete (in Graphics.Gloss.Interface.IO.Animate) IO() nicht zurückgegeben hat. Ich möchte das Ergebnis eines genetischen Algorithmus anzeigen, indem ich ein Glanzpaket verwende. Hier ist mein Code:Konnte nicht erwarteten Typ 'IO()' mit tatsächlichen Typ '(Controller -> 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
Und das ist, was ich habe:
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
Ich habe mein Problem über Google gesucht und für so viele Male Stackoverflow, aber immer noch keine Lösung für diese finden Error. Bitte hilf mir.
P/S: Dies ist eine Führungslinie für Graphics.Gloss: https://hackage.haskell.org/package/gloss-1.11.1.1/docs/Graphics-Gloss-Interface-IO-Animate.html
Leider wieder für meine dumme Frage, nachdem ich Lazersmoke Vorschlag gab (die Sie unten im Kommentarbereich sehen), bekam ich ein anderes Fehler, die sehr ähnlich mit dem Fehler, den ich für die Frage:
ich die Zeile geändert: weiß $ const $ animateIO Modus do
In: animateIO Modus weiß (_ -> return()) $ const $ tun
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);
.... }
Der Fehler bedeutet nur, dass Sie vergessen haben, einen Parameter zu liefern, in diesem Fall die 'Controller -> IO()' Funktion. – ocramz
Also wie kann ich es reparieren, können Sie mir bitte den Weg zeigen? –
Was er sagte, nämlich "Callback, um den Display-Controller zu nehmen." wie Sie in der verknüpften Dokumentation sehen können. Sie benötigen etwas wie 'animateIO-Modus weiß (\ _ -> return()) $ const $ do ', um den Typ zu erfüllen, obwohl Sie wahrscheinlich dort einen tatsächlichen Rückruf geben müssen, basierend auf Ihren Absichten, damit es richtig funktioniert. – Lazersmoke