定义函数不使用辅助函数

时间:2017-03-20 21:34:12

标签: coq

我一直在阅读软件基础并解决其中的问题。这是我试图定义的定义之一:

Fixpoint split {X Y : Type} (l : list (X*Y)) : (list X) * (list Y)

基本上它是haskell的unzip版本。

我实现了这样:

Fixpoint split2 {X Y : Type} (l : list (X*Y)) (g :(list X) * (list Y))
               : (list X) * (list Y) :=
  match l with
    | [] => g
    | (x,y)::xs => split2 xs ((fst g) ++ [x],(snd g) ++ [y])
  end.

Fixpoint split {X Y : Type} (l : list (X*Y))
               : (list X) * (list Y) :=
split2 l ([],[]).

我有两个问题:

  • 是否可以在不使用split这样的帮助函数的情况下定义split2
  • 在Coq中是否有等效的where子句(如在Haskell中)?

1 个答案:

答案 0 :(得分:1)

Coq中有let。您可以而且应该只翻译标准的Haskell定义,避免使用++

的二次性能
Fixpoint split {A B} (l : list (A * B)) : list A * list B :=
  match l with
    [] => ([], [])
  | (x, y) :: xs => let (xs2, ys2) := split xs in (x::xs2, y::ys2)
  end.