问题描述
我继续探索 Ocaml 的 Num
库,原因是整个逻辑库都是使用它编写的。
今天,我想做一个有理数的负数。从 -1/2
获得 1/2
。
为此,我认为,给定 a
类型的 Ratio.ratio
,我可以计算它的负数(并返回 ratio
,而不是 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 toplevel output formatting) 使用了不同的函数 (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
这让我不知道如何继续尝试。对此也有帮助吗?
无论是第一个问题还是第二个问题,我都将不胜感激!
在下面,根据以下对话,我发布了一些已添加到问题中的版本:
EDIT(解决添加所需的#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)
我也试过用 Ocaml 代替 utop,但错误更严重:
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'
#
EDIT 3(适用于 Ocaml,而不是 utop)
##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>
#
EDIT 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 #
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)