你知道我在哪里可以找到关于Frege的Java绑定的文档吗?来自Haskell,我发现Frege最有趣的方面。不幸的是,我找到的documentation并没有太多的细节。
这是我的测试示例。基本上,我想翻译以下Java代码:
BigDecimal x = BigDecimal.valueOf(7);
BogDecimal y = new BigDecimal("5.13");
System.out.println(x.add(y));这是我目前的Frege代码:
module Main where
data JBigDecimal s = pure native java.math.BigDecimal
where
pure native jAdd add :: JBigDecimal RealWorld -> JBigDecimal RealWorld -> JBigDecimal RealWorld
pure native jShow toString :: JBigDecimal RealWorld -> String
pure native jBigDecimalI java.math.BigDecimal.valueOf :: Int -> JBigDecimal RealWorld
-- ERROR: Here, I don't know what I should write.
-- I want to bind to the BigDecimal(String) constructor.
-- I tried several versions but none of them was successful, e.g.:
pure native jBigDecimalS java.math.BigDecimal.BigDecimal :: String -> JBigDecimal RealWorld
main :: [String] -> IO ()
main args = let x = jBigDecimalI 7
y = jBigDecimalS "5.13"
z = JBigDecimal.jAdd x y
in printStrLn $ (JBigDecimal.jShow z)
-- (BTW, why `printStrLn` and not `putStrLn` as it is called in Haskell?)为了完整起见,错误消息是:
calling: javac -cp fregec-3.21.jar:. -d . -encoding UTF-8 ./Main.java
./Main.java:258: error: cannot find symbol
return java.math.BigDecimal.BigDecimal(
^
symbol: method BigDecimal(String)
location: class BigDecimal
1 error
E frege-repl/example.fr:15: java compiler errors are most likely caused by
erronous native definitions发布于 2013-02-22 07:06:17
我找到了。构造函数名为new:
pure native jBigDecimalS new :: String -> JBigDecimal RealWorld发布于 2013-02-22 08:33:40
顺便说一句,你不需要到处都有RealWorld。您具有纯本机数据类型,并且仅应用纯本机函数。
此外,事实证明,当我们有时支持Java泛型时,您在这里使用的幻影类型约定将不能很好地发挥作用。然后我们就会有类似这样的东西
data List a = native java.util.LinkedList我们希望带有kind *的a映射到泛型类型参数的位置。然而,这不会很好地与指示幻影类型的状态线程混合。
因此(很快就会出现!)我们将有一个类型来标记可变的值,它类似于
abstract data Mutable s a = Mutable a因此,我们永远不能真正构造/解构可变变量。这应该是可行的,因此只有本机函数可以创建Mutable s a类型的值(在IO或ST monad中),然后就可以使用freeze进行安全复制,其中可以得到一个假定不可变的a。但是这个函数不能传递给需要Mutable s a的不纯函数。
但同样,在处理不可变数据时,仅使用BigDecimal就足够了(这一点不会改变)。
https://stackoverflow.com/questions/15013824
复制相似问题