2011-04-27 6 views
10

저는 haskell에서 Ubigraph를 사용하려고 노력하고 있습니다.하지만 제 문제는 좀 더 일반적이라고 생각합니다. 내가 컴파일하려고 해요 :하스켈에서 모나드 혼합하기

import Graphics.Ubigraph 
import Control.Monad 
import System.Posix.Unistd 

main = do 
    h <- initHubigraph "http://127.0.0.1:20738/RPC2" 
    runHubigraph op h 

op = do 
    clear 
    vs <- mapM (const newVertex) [0..200] 
    mapM_ (setVAttr (VShape Sphere)) vs 
    putStrLn "something" 
    let bind i = zipWithM (\a b -> newEdge (a,b)) vs (drop i vs ++ take i vs) 
    mapM_ bind [1..15] 
    mapM_ (removeVertex) vs 
    return() 

을 내가 얻고

내가 연산의 종류가 putStrLn의 반환 형식과 다른 무언가로 암시되는 방법을 볼 수 있습니다

Couldn't match expected type `Control.Monad.Trans.Reader.ReaderT 
           Ubigraph IO a0' 
      with actual type `IO()' 
In the return type of a call of `putStrLn' 
In a stmt of a 'do' expression: putStrLn "something" 
In the expression: 
    do { clear; 
     vs <- mapM (const newVertex) [0 .. 200]; 
     mapM_ (setVAttr (VShape Sphere)) vs; 
     putStrLn "something"; 
     .... } 
, 그러나 나는이 코드를 어떻게 적절하게 컴파일하기 위해 리엔지니어링을 할 것인지 잘 모르겠습니다. op 함수의 반환 유형을 간단히 변경할 수 있습니까?

감사

답변

12

op에서 아래로 묻혀 putStrLn에 대한 귀하의 호출은 IO 모나드이다. 당신은

liftIO $ putStrLn "foo" 

이러한 리프팅 함수를 사용하면 스택의 낮은 모나드 기능에 액세스 할 수 있도록 사용하여 Ubigraph 모나드로 들어 올리해야합니다. 이 경우 Ubigraph은 IO 모나드로 구성된 리더 모나드이며 IO 모나드는 맨 아래에 있습니다. 따라서 IO의 작업을 해제해야합니다.

liftIOMonadIO 클래스, in the transformers package입니다.

+0

멋진 작품. 빠른 답변 감사합니다 –