如何解决为什么 Num 中的有理数打印为 <abstr>?
我继续探索 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 #
解决方法
使用 <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 无法从 nums 本身推导出如何打印比率,因此我们可以通过定义额外的打印机来帮助它,
# 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
回复:附言
对于 zarith,您需要安装系统依赖项。您可以为此使用 opam,例如,
opam depext --install zarith
它将使用您的操作系统包管理器安装系统依赖项(gmp
库),然后安装 zarith 库。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。