2

Frege の Java バインディングに関するドキュメントがどこにあるか知っていますか? Haskell 出身の私は、Frege の最も興味深い側面を見つけました。残念ながら、私が見つけたドキュメントはあまり詳しくありません。

これが私のテスト例です。基本的に、次の 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
4

2 に答える 2

1

見つけた。コンストラクターは new と呼ばれます。

pure native jBigDecimalS new :: String -> JBigDecimal RealWorld
于 2013-02-21T23:06:17.433 に答える
1

ところで、RealWorldどこでも必要はありません。純粋なネイティブ データ型があり、純粋なネイティブ関数のみを適用します。

さらに、ここで使用するファントム型規則は、Java Generics をサポートする場合にうまく機能しないことが判明しました。次に、次のようなものがあります

data List a = native java.util.LinkedList

ここでawith kind*マップをジェネリック型パラメーターにマップします。ただし、これはファントムタイプを示す状態スレッドとうまく混ざりません。

したがって、(近日公開予定です!) 変更可能な値にタグを付けるための型が用意されます。

abstract data Mutable s a = Mutable a

そのため、Mutable を実際に構築/分解することはできません。これは、ネイティブ関数のみが (IO モナドまたは ST モナドで) 型の値を作成できるように機能するはずであり、不変であると想定される場所でMutable s a安全なコピーを作成することが可能になります。しかし、これを要求する不純な関数に渡すことはできません。freezeaMutable s a

しかし、繰り返しますが、不変データを扱う場合は、それだけBigDecimalで十分です (そして、これは変わることはありません)。

于 2013-02-22T00:33:40.127 に答える