我試圖寫在OCaml的一個函數,得到一個邏輯運算作爲參數(這樣定義:)OCaml的satisfable邏輯運算
type oper = B of bool
| V of string
| Neg of oper
| And of oper * oper
| Or of oper * oper
| Impl of oper * oper
,並返回「真」如果操作satisfable(它在至少一個情況下返回true),如果不滿足則返回「false」(它在所有情況下都返回false)。爲此,我試圖用V true或B false替代V,直到我變爲true並引發Do_it異常。如果在任何情況下我都不是真的,我會拋出Not_found異常並評估在程序結束時我得到的異常。
let satisfable operation=
let switch x var tf=
let rec aux1 =function
|B b->B b
|V s when s=var ->aux1 (B tf)
|V s->V s
|And(e1,e2)->(match aux1 e1, aux1 e2 with
|(B false,_)|(_,B false)->B false
|(B true, x)|(x,B true)->aux1 x
|(x1,x2)->And(x1,x2))
|Or(e1,e2)->(match aux1 e1,aux1 e2 with
|(B true, _)|(_,B true)->B true
|(B false, x)|(x,B false)->aux1 x
|(x1,x2)->Or(x1,x2))
|Impl(e1,e2)->(match aux1 e1,aux1 e2 with
|(B false,_)->B false
|(B true,x)->aux1 x
|(x1,x2)->Impl(x1,x2))
|Neg b->(match b with
|B b-> B (not b)
|s->Neg (aux1 s))
in aux1 x
in let rec aux = function
|B b->B b
|V s->(match switch operation s true with
|B true->raise Do_it
|x-> aux (switch operation s true)
)
|V s->(match switch operation s false with
|B true->raise Do_it
|x-> aux (switch operation s false)
)
|V s->raise Not_found
|And(e1,e2)->(match aux e1, aux e2 with
|(B false,_)|(_,B false)->B false
|(B true, x)|(x,B true)->aux x
|(x1,x2)->And(x1,x2))
|Or(e1,e2)->(match aux e1,aux e2 with
|(B true, _)|(_,B true)->B true
|(B false, x)|(x,B false)->aux x
|(x1,x2)->Or(x1,x2))
|Impl(e1,e2)->(match aux e1,aux e2 with
|(B false,_)->B false
|(B true,x)->aux x
|(x1,x2)->Impl(x1,x2))
|Neg b->(match b with
|B b-> B (not b)
|s->Neg (aux s))
in if(try aux operation with Do_it->B true|Not_found->B false)=B false then false else true;;
但它不工作,我想要的方式,在某些情況下甚至會引發堆棧溢出異常。
函數「開關」返回其中字符串變量「VAR」被取代成TF(真/假)
你說這不符合你的要求。你期望什麼?發生了什麼? – PatJ
當我用 可以滿足(和(V「s」,Neg(V「s」)));; 它返回false,因爲(s &&!s)永遠不會成立。但是當我至少在一種情況下調用某個應該是真的東西時,它會拋出一個異常(堆棧溢出) – Stevie