在第44行它给了我一个类型错误类型自动化与类型formatter
不兼容即时尝试使用graphviz
绘制自动机
这是代码:
(*d'abord definissons le type automate*)
type automate = {
etat_initial : int;
ensemble_des_etats : int list;
alphabets : char list;
transitions :(int*char*int) list;
etats_finaux : int list
};;
(*prenons une variable a1 du type automate qu'on a definit precedemment
comme
exemple*)
let a1={
etat_initial=1;
ensemble_des_etats=[1;2];
alphabets=['a';'b'];
transitions=[(1,'b',2);(2,'c',3)];
etats_finaux=[2]
};;
let rec member a l =
match l with
| [] -> false
| x::rl -> x=a || member a rl;;
let fmt_transition auto fmt (inedge,by,outedge)=
if member outedge auto.etats_finaux=true then
Format.fprintf fmt "@[node [shape = doublecircle]%d;@]" outedge;
if inedge=auto.etat_initial then
Format.fprintf fmt "@[node [shape = point]start;node [shape = circle];start
-> %d ;@]" inedge;
Format.fprintf fmt "@[%d -> %d [label=\"%c\"];@]" inedge outedge by;;
let fmt_transitions auto fmt =
Format.fprintf fmt "@[<v 2>digraph output {@,%a@,@]}@,@."
(Format.pp_print_list (fmt_transition auto)) auto.transitions
;;
let call_dot auto =
let cmd = "dot -Tpng | display -" in
let (sout, sin, serr) as channels =
Unix.open_process_full cmd (Unix.environment ()) in
let fmt = Format.formatter_of_out_channel sin in
<b>Format.fprintf fmt "%a@." fmt_transitions auto;</b>
channels
let cleanup channels =
(* missing: flush channels, empty buffers *)
Unix.close_process_full channels;;
call_dot a1 ;;
答案 0 :(得分:1)
使用%a
时需要小心。
根据OCaml文档:
a:用户定义的打印机。取两个参数并将第一个参数应用于 outchan (当前输出通道)和第二个参数。 因此第一个参数必须具有类型out_channel - &gt; 'b - &gt;单位 ...
fmt_transitions auto fmt
函数的第一个参数参数必须是格式化程序,所以只需切换auto
和fmt
参数即可。
let fmt_transitions auto fmt = ...
let fmt_transitions fmt auto = ...