在 Frege 中与 Java 的互操作性,尤其是在 IO Monad 中

Interoperability with Java in Frege, especially in IO Monad

我想将这段java代码翻译成弗雷格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 变为 return 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)

你可能已经看到了。