Я продолжаю свое исследование библиотеки Num
Ocaml по той причине, что с ее помощью была написана целая библиотека о логике.
Сегодня я хотел бы сделать отрицательное рациональное число. Получите -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), где другая функция ( 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
Что не дает мне никакого представления о том, как продолжать попытки. Любая помощь с этим тоже?
Буду признателен за любой ответ, будь то на первый вопрос или на второй!!
Здесь ниже я размещаю некоторые редакции, которые были добавлены к вопросу, в результате беседы ниже:
РЕДАКТИРОВАТЬ (решено добавлением необходимого #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'
#
РЕДАКТИРОВАТЬ 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>
#
РЕДАКТИРОВАТЬ 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 #