Why3ML中的布尔模式匹配

时间:2017-11-17 17:13:14

标签: why3 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

如何在元组内正确进行基于值的模式匹配?

1 个答案:

答案 0 :(得分:1)

是的,有可能:

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:

与SML或OCaml相比,Why3的模式语言非常基础。在Why3中模式中允许的唯一值是构造函数(甚至不允许例如整数常量),并且只能对元组进行解构。这就是上述模式中使用TrueFalse的原因;它们实际上是bool - truefalse的正确构造函数,为方便起见,但它们不适用于模式。请参阅the grammar reference中的图7.2,并查看pattern的定义。