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

时间:2016-02-23 15:18:57

标签: java haskell frege

我想把这个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上有更多关注它的例子?

1 个答案:

答案 0 :(得分:5)

你有

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)

de-aliaser选择翻译

ST s (Mutable s (JArray String))

回到

ST s (ArrayOf s String)
你可能会看到它。