2017-02-09 63 views
3

假設我的數據類型與存在量化組件存在一個小問題。這次我想定義何時兩個值類型ext相等。模式匹配的類型爲了實現Coq中存在類型的構造函數的相等性

Inductive ext (A: Set) := 
| ext_ : forall (X: Set), option X -> ext A. 

Fail Definition ext_eq (A: Set) (x y: ext A) : Prop := 
    match x with 
    | ext_ _ ox => match y with 
       | ext_ _ oy => (* only when they have the same types *) 
           ox = oy 
       end 
    end. 

我想要做的是以某種方式區分存在類型實際上相同和不存在的情況。這是JMeq的情況還是有其他方式來完成這種情況區分?

我google了很多,但不幸的是我大多偶然發現關於依賴模式匹配的帖子。

我也試着用Scheme Equality for ext生成一個(布爾型)方案,但是由於類型參數,這是不成功的。

回答

3

我想要做的是以某種方式區分存在類型實際上相同和不存在的情況。

這是不可能的,因爲Coq的邏輯與同構類型相等的單一公理相容。因此,即使(unit * unit)unit在句法上不同,它們也不能通過Coq的邏輯進行區分。

可能的解決方法是爲您感興趣的類型設置一個數據類型的代碼並將其存儲爲存在。事情是這樣的:

Inductive Code : Type := 
    | Nat : Code 
    | List : Code -> Code. 

Fixpoint meaning (c : Code) := match c with 
    | Nat  => nat 
    | List c' => list (meaning c') 
end. 

Inductive ext (A: Set) := 
    | ext_ : forall (c: Code), option (meaning c) -> ext A. 

Lemma Code_eq_dec : forall (c d : Code), { c = d } + { c <> d }. 
Proof. 
intros c; induction c; intros d; destruct d. 
- left ; reflexivity. 
- right ; inversion 1. 
- right ; inversion 1. 
- destruct (IHc d). 
    + left ; congruence. 
    + right; inversion 1; contradiction. 
Defined. 

Definition ext_eq (A: Set) (x y: ext A) : Prop. 
refine(
    match x with | @ext_ _ c ox => 
    match y with | @ext_ _ d oy => 
    match Code_eq_dec c d with 
    | left eq => _ 
    | right neq => False 
    end end end). 
subst; exact (ox = oy). 
Defined. 

然而,這顯然限制頗多的那種類型,你可以在ext包。其他功能更強大的語言(例如配備Induction-recursion)會給你更多的表現力。

+2

不錯的解決方法!讓我添加一些註釋:(1)'Code_eq_dec'可以* literally *使用'決定平等'的策略來定義,當我們開始在'Code'中添加更多的數據構造函數時,這真的會發光。 (2)可以使用'let'表達式來破壞'ext_'(因爲它只有一個構造函數),這使得代碼更加簡潔:'let(c,ox):= x in ...' –

+0

, 謝謝!我不記得那個「決定平等」戰術的名字,嘗試過我想到的各種關鍵詞組合,並最終放棄了。很高興知道'let'也可以打破任何單構造函數的數據類型。 – gallais

+0

我只想補充一點,不幸的是,這種解決方法對於我的設置來說太過於限制,但您的回答和評論畢竟是有幫助的。 – ichistmeinname

相關問題