[英]Boolean pattern matching in Why3ML
在其他ML變體(例如SML)中,可以執行以下操作:
case l of
(true, _) => false
| (false,true) => false
| (false,false) => true
但是,使用Why3ML match
聲明執行類似的操作會引發語法錯誤:
match l with
| (true, _) -> false
| (false,true) -> false
| (false,false) -> true
end
如何在元組中正確進行基於值的模式匹配?
是的,有可能:
module Test
let unpack_demo () =
let tup = (true, false) in (* parens required here! *)
match tup with
| True, False -> true (* pattern must use bool's constructor tags *)
| _ -> false
end
let ex2 () = match true, false with (* parens not required here *)
| True, x -> x
| False, True -> false
| False, False -> true
end
end
hayai[cygwin64][1155]:~/prog/why3$ why3 execute test.mlw Test.unpack_demo
Execution of Test.unpack_demo ():
type: bool
result: true
globals:
hayai[cygwin64][1156]:~/prog/why3$ why3 execute test.mlw Test.ex2
Execution of Test.ex2 ():
type: bool
result: false
globals:
Why3的模式語言與SML或OCaml相比是非常基礎的。 Why3模式中唯一允許使用的值是構造函數(甚至不允許使用整數常量),並且只能對元組進行解構。 這就是在上述模式中使用True
和False
原因; 實際上,它們是bool
的正確構造函數-為方便起見,存在true
和false
,但它們不能在模式中工作。 請參見語法參考中的圖7.2,並了解pattern
的定義。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.