Num
ロジックに関する 1 つのライブラリ全体が Ocaml を使用して作成されたという理由で、Ocamlのライブラリの探索を続けます。
今日は、有理数の負数を作りたいと思います。から を取得-1/2
し1/2
ます。
a
そうするために、 of 型が与えられた場合、次のようにRatio.ratio
その負の値を計算できる (そして aratio
ではなく aを返すnum
) ことができると思います。
ratio_of_num (minus_num (num_of_ratio a))
(関数: https://ocaml.org/releases/4.05/htmlman/libref/Num.html#TYPEnum )
今、私は結果を確認したいのですが、私はいつもこの解決策を得ます:Ratio.ratio = <abstr>
要点は、 を使用すると常にこの解決策が得られることに気付いたということratio_of_num
です。例えば:
ratio_of_num (Int 2);;
- : Ratio.ratio = <abstr>
少し検索したところ、別の関数 ( ) が使用されているこの質問 ( OCaml トップレベルの出力フォーマットratio_of_int 2
) が見つかりましたが、もはや可能ではないようです。多分それratio
は別のライブラリです。
何か助けはありますか?
PS:ちなみに、num
将来交換するために、でインストールしようとしてZarith
いopam
ますが、できません。
私の問題は私が行うことopam install zarith
であり、これが表示されます:
┌─ The following actions failed
│ λ build conf-gmp 3
└─
╶─ No changes have been performed
The packages you requested declare the following system dependencies. Please
make sure they are installed before retrying:
gmp
だから私はそうしますopam install gmp
、そして私は得ます:
┌─ The following actions failed
│ λ build gmp 6.2.1
└─
╶─ No changes have been performed
これは、試行を続ける方法についての手がかりを提供しません。これについても何か助けはありますか?
最初の質問でも2番目の質問でも、答えていただければ幸いです!!
以下に、以下の会話の結果として、質問に追加されたいくつかのエディションを投稿します。
編集 (必要な #require を追加して解決)
@ivg が提案したことを実行しましたが、まだ機能しません (別のopen Num
方法で要求されるため、最初の を実行します)。
─( 23:12:59 )─< command 0 >──────────────────────────────────────{ counter: 0 }─
utop # open Num;;
─( 23:13:00 )─< command 1 >──────────────────────────────────────{ counter: 0 }─
utop # let pp_num ppf x = Format.fprintf ppf "%s" (Num.string_of_num x);;
val pp_num : Format.formatter -> num -> unit = <fun>
─( 23:14:11 )─< command 2 >──────────────────────────────────────{ counter: 0 }─
utop # #install_printer pp_num;;
─( 23:14:16 )─< command 3 >──────────────────────────────────────{ counter: 0 }─
utop # ratio_of_num (Int 2);;
- : Ratio.ratio = <abstr>
編集 2 (#require も必要)
utop の代わりに Ocaml も試しましたが、エラーはさらに悪化しています。
OCaml version 4.10.2
Findlib has been successfully loaded. Additional directives:
#require "package";; to load a package
#list;; to list the available packages
#camlp4o;; to load camlp4 (standard syntax)
#camlp4r;; to load camlp4 (revised syntax)
#predicates "p,q,...";; to set these predicates
Topfind.reset();; to force that packages will be reloaded
#thread;; to enable threads
# open Num;;
# let pp_num ppf x = Format.fprintf ppf "%s" (Num.string_of_num x);;
Error: Reference to undefined global `Num'
#
編集 3 (utop の代わりに Ocaml で動作)
##require "num";;
# let pp_num ppf x = Format.fprintf ppf "%s" (Num.string_of_num x);;
val pp_num : Format.formatter -> Num.num -> unit = <fun>
# #install_printer pp_num;;
# ratio_of_num (Int 2);;
- : Ratio.ratio = <ratio 2/1>
#
編集 4 (utop で動作します。整数の場合、印刷すると結果が単純化されることに注意してください)
utop # let pp_ratio ppf r = Format.fprintf ppf "%a" pp_num (num_of_ratio r);;
val pp_ratio : Format.formatter -> Ratio.ratio -> unit = <fun>
─( 23:28:07 )─< command 6 >──────────────────────────────────────{ counter: 0 }─
utop # #install_printer pp_ratio;;
─( 23:28:22 )─< command 7 >──────────────────────────────────────{ counter: 0 }─
utop # ratio_of_num (Int 2);;
- : Ratio.ratio = 2
─( 23:28:29 )─< command 8 >──────────────────────────────────────{ counter: 0 }─
utop #