我希望把這種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上有更多的例子關注它?
我對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
是的,不帶'()'調用'PApplet.new'是大多數編譯器抱怨的原因!真的很傻我是 –
現在的問題只是關於如何擺脫'Process.fr:14:表達式中的類型錯誤fromList(「test」:[])type是:STMutable t1(JArray String)expected:ArrayOf RealWorld String' –