2016-02-23 59 views
4

我希望把這種Java代碼到弗雷格哈斯克爾:互操作性與Java弗雷格,尤其是在IO Monad的

PApplet pApplet = new PApplet(); 
    System.out.print(pApplet.toString()); 
    PApplet.runSketch(new String[]{"test"}, pApplet); 

我做了這麼遠:

data PApplet = mutable native processing.core.PApplet 
    where 
    native new ::() -> IO PApplet 
    native toString :: PApplet -> IO String 

native runSketch processing.core.PApplet.runSketch 
    :: ArrayOf RealWorld String -> PApplet -> IO() 

main _ = do p <- PApplet.new 
      pStr <- p.toString 
      putStrLn pStr 
      args = JArray.fromList ["test"] 
      runSketch args p 

部分高達main編譯但那麼我得到這些錯誤:

E Process.fr:14: type error in expression fromList ("test":[]) 
    type is : STMutable t1 (JArray String) 
    expected: ArrayOf RealWorld String 
E Process.fr:15: type error in expression p 
    type is : IO PApplet 
    expected: PApplet 
E Process.fr:12: type error in expression >>= p.toString (λpStr -> >> (putStrLn pStr) (runSketch (fromList ("test":[])) p)) 
    type is : IO() 
    expected:()→t1 
E Process.fr:11: type error in expression λp -> >>= p.toString (λpStr -> >> (putStrLn pStr) (runSketch (fromList ("test":[])) p)) 
    type is : IO() 
    expected:()→t1 
E Process.fr:11: type error in expression >>= new (λp -> >>= p.toString (λpStr -> >> (putStrLn pStr) (runSketch (fromList ("test":[])) p))) 
    type is :()→t1 
    expected: IO() 
E Process.fr:11: type error in expression λ_ -> >>= new (λp -> >>= p.toString (λpStr -> >> (putStrLn pStr) (runSketch (fromList ("test":[])) p))) 
    type is :()→t1 
    expected: IO() 
E Process.fr:12: can't find a type for p.toString `toString` 
    is neither an overloaded function nor a member of IO PApplet 

我努力達到編譯器標準,但沒有成功。經過無數的隨機組合,這段代碼對我來說似乎是最合理的。我需要do區塊的類型提示嗎?我不明白爲什麼p <- PApplet.new評估爲IO PApplet?以及如何使JArray.fromList返回ArrayOf RealWorld String?弗雷格是偉大的,但互操作性是相當艱鉅的。是否有可能在Frege github上有更多的例子關注它?

+2

我對frege不熟悉,所以我假設它與Haskell足夠相似。你聲明'PApplet.new'的類型是'() - > IO PApplet',但是你沒有使用參數:'p < - PApplet.new'應該是'p < - PApplet.new()'(和'pStr < - p.toString'應該是'pStr < - p.toString p')。 'p < - PApplet.new'行不是*類型錯誤,但是,因爲'( - >)()'是一個monad!編譯器認爲你正在使用這個monad,而不是IO。我認爲第一個錯誤(關於'fromList(「test」:[])')是不相關的,但抱怨'()→t1'的錯誤是由於這一點遺漏。 – user2407038

+0

是的,不帶'()'調用'PApplet.new'是大多數編譯器抱怨的原因!真的很傻我是 –

+0

現在的問題只是關於如何擺脫'Process.fr:14:表達式中的類型錯誤fromList(「test」:[])type是:STMutable t1(JArray String)expected:ArrayOf RealWorld String' –

回答

5

你有

ST s X 

,你想

X 

,你在IO,這不過是ST RealWorld

所以,最自然的解決辦法是replce的=<-在線

args = JArray.fromList ["test"] 

和你設置!

當然,整個故事是有點困難,因爲類型別名:

type ArrayOf a x = Mutable a (JArray x) 
type STMutable s a = ST s (Mutable s a) 

只好去aliaser選擇去翻譯

ST s (Mutable s (JArray String)) 

ST s (ArrayOf s String) 

你可能會看到它。

+0

'args < - JArray.fromList [「test」]'解決難題! –