2010-06-11 117 views
5

有沒有辦法在OCaml中做C風格的前向聲明?OCaml前向聲明

我的問題是,我有兩個變體互相稱對方:

type path_formula = 
    [ `Next of state_formula 
    | `Until of (state_formula * state_formula) 
    | `UntilB of (state_formula * int * state_formula) 
    ] 

type state_formula = 
    [ `True | `False 
    | `Not of state_formula 
    | `And of (state_formula * state_formula) 
    | `Or of (state_formula * state_formula) 
    | `Imply of (state_formula * state_formula) 
    | `Label of string 
    | `Prob` of (boundf * path_formula) 
    | `Expc` of (boundi * formula) 
    ] 

所以這兩個類型必須知道對方的..我搜索了它在谷歌可惜OCaml的是不是這麼寬次使用的編程語言..

回答

13

使用

type T1 = ... 
and T2 = ... 

有遞歸類型。

+0

謝謝,我通常在發現語言特徵方面沒有問題,但OCaml與我迄今爲止所研究的完全不同,而且我只能找到幾百頁的「phat」參考文獻,這些參考文獻會讓我失去方向:) – Jack 2010-06-11 21:10:13

+0

請注意,您可以擲骰子並搜索F#語法/文檔以瞭解OCaml。核心語法是相同的,所以例如http://stackoverflow.com/questions/1378575/f-forward-type-declarations是一個勝利。 – Brian 2010-06-11 21:37:35

+1

這是一個很好的觀點。我不得不選擇OCaml,因爲他們強迫我。我認爲這是一種古老而強大的語言,但如果現代的功能語言(Scala,F#)保持相同的語法,那麼這將成爲一個不錯的選擇:) – Jack 2010-06-12 00:48:37