1

Numロジックに関する 1 つのライブラリ全体が Ocaml を使用して作成されたという理由で、Ocamlのライブラリの探索を続けます。

今日は、有理数の負数を作りたいと思います。から を取得-1/21/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将来交換するために、でインストールしようとしてZarithopamますが、できません。

私の問題は私が行うこと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 # 

4

1 に答える 1

3

実際の表現の代わりにある理由は、トップレベル (別名インタープリター) がオブジェクト<abstr>を印刷する方法を知らないためです。ディレクティブnumを使用して、トップレベルを簡単に教えることができます。#install_printer


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>
# 

そこで、プリティ印刷関数を定義しました。

let pp_num ppf x = Format.fprintf ppf "%s" (Num.string_of_num x)

そして、#install_printerディレクティブを使用して最上位にインストールし、

# #install_printer pp_num;;

そして今、私たちがそれを手に入れるたびにnum、私たちのために印刷されます.

この関数は、他のモジュール関数 (きれいな印刷に使用される) とpp_num一緒に使用することもできます。Format

Format.printf "my num = %a" pp_num (ratio_of_num (Int 2))

古いバージョンの OCaml では、数値自体から比率を出力する方法を導き出すことができない可能性があるため、追加のプリンターを定義することで解決できます。

# let pp_ratio ppf r = Format.fprintf ppf "%a" pp_num (num_of_ratio r);;
val pp_ratio : Format.formatter -> Ratio.ratio -> unit = <fun>
# #install_printer pp_ratio;;
# ratio_of_num (Int 2);;
- : Ratio.ratio = 2

Re: PS

zarith の場合、システムの依存関係をインストールする必要があります。そのために opam を使用できます。

opam depext --install zarith

オペレーティング システムのパッケージ マネージャーを使用してシステムの依存関係 (gmpライブラリ) をインストールしてから、zarith ライブラリをインストールします。

于 2021-05-12T19:47:31.520 に答える