我想将这段 java 代码翻译成 Frege Haskell:
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 上有更多关于它的例子?
最佳答案
你有
ST s X
你想要
X
你在 IO
, 这只不过是 ST RealWorld
因此,最自然的解决方案是替换 =
与 <-
在行中
args = JArray.fromList ["test"]
一切就绪!
诚然,由于类型别名,整个故事有点困难:
type ArrayOf a x = Mutable a (JArray x)
type STMutable s a = ST s (Mutable s a)
是否选择了去锯齿进行翻译
ST s (Mutable s (JArray String))
回到
ST s (ArrayOf s String)
你可能已经看到了。
关于java - Frege 中与 Java 的互操作性,尤其是在 IO Monad 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35581300/