2017-02-09 58 views
1

第44行它給了我一個類型的錯誤鍵入自動不兼容formatter型IM試圖繪製使用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 {@,%[email protected],@]}@,@." 
(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 "%[email protected]" fmt_transitions auto;</b> 
channels 

let cleanup channels = 
(* missing: flush channels, empty buffers *) 
Unix.close_process_full channels;; 

call_dot a1 ;; 
+0

也涉及到了問題[我怎麼能代表圖形的自動機從較不使用循環過渡INT *字符*爲int的列表(http://stackoverflow.com/questions/41780982/how- can-i-represent-an-automaton-graphics-from-a-list-of-intcharint-represe)和[如何繪製自動機圖?](http://stackoverflow.com/questions/41901071/how-to -draw-AN-自動機-圖)。 –

回答

1

您需要的時候使用%a要小心。 根據OCaml文檔:

a:用戶定義的打印機。取兩個參數並將第一個參數應用於outchan(當前輸出通道)和第二個參數。 第一個參數,因此必須有類型out_channel - >「B - >單元 ...

fmt_transitions auto fmt函數的第一個參數一個參數必須是格式化,所以只需切換autofmt參數和它應該是可以的。

let fmt_transitions auto fmt = ... 

let fmt_transitions fmt auto = ... 
+0

非常感謝! –

相關問題