Implementing vector addition in Coq

前端 未结 1 1202
南方客
南方客 2021-01-13 12:20

Implementing vector addition in some of the dependently typed languages (such as Idris) is fairly straightforward. As per the example on Wikipedia:



        
相关标签:
1条回答
  • 2021-01-13 12:53

    It's not possible to implement this function so easily in plain Coq: you need to rewrite your function using the convoy pattern. There was a similar question posted a while ago about this. The idea is that you need to make your match return a function in order to propagate the relation between the indices:

    Set Implicit Arguments.
    
    Inductive vector (X : Type) : nat -> Type :=
      | vnul : vector X 0
      | vcons {n : nat} (h : X) (v : vector X n) : vector X (S n).
       Arguments vnul [X].
    
    Definition vhd (X : Type) n (v : vector X (S n)) : X :=
      match v with
      | vcons _ h _ => h
      end.
    
    Definition vtl (X : Type) n (v : vector X (S n)) : vector X n :=
      match v with
      | vcons _ _ tl => tl
      end.
    
    Fixpoint vpadd {n : nat} (v1 v2 : vector nat n) : vector nat n :=
      match v1 in vector _ n return vector nat n -> vector nat n with
      | vnul =>           fun _  => vnul
      | vcons _ x1 v1' => fun v2 => vcons (x1 + vhd v2) (vpadd v1' (vtl v2))
      end v2.
    
    0 讨论(0)
提交回复
热议问题