我正在考慮編寫一個Coq程序來驗證relational algebra的某些屬性。我有一些基本的數據類型工作,但連接元組給我帶來一些麻煩。Coq中的異構列表
這裏是代碼的相關章節:
Require Import List.
Require Import String.
(* An enum representing SQL types *)
Inductive sqlType : Set := Nat | Bool.
(* Defines a map from SQL types (which are just enum values) to Coq types. *)
Fixpoint toType (t : sqlType) : Set :=
match t with
| Nat => nat
| Bool => bool
end.
(* A column consists of a name and a type. *)
Inductive column : Set :=
| Col : string -> sqlType -> column.
(* A schema is a list of columns. *)
Definition schema : Set := list column.
(* Concatenates two schema together. *)
Definition concatSchema (r : schema) (s : schema) : schema := app r s.
(* Sends a schema to the corresponding Coq type. *)
Fixpoint tuple (s : schema) : Set :=
match s with
| nil => unit
| cons (Col str t) sch => prod (toType t) (tuple sch)
end.
Fixpoint concatTuples {r : schema} {s : schema} (a : tuple r) (b : tuple s) : tuple (concatSchema r s) :=
match r with
| nil => b
| cons _ _ => (fst a , concatTuples (snd a) b)
end.
在功能concatTuples,在零的情況下,CoqIDE給我的錯誤:
"The term "b" has type "tuple s" while it is expected to have type "tuple (concatSchema ?8 s)"."
我想我明白髮生了什麼有;類型檢查器不能發現s
和concatSchema nil s
是相等的。但我發現離奇的是,當我添加以下行:
Definition stupid {s : schema} (b : tuple s) : tuple (concatSchema nil s) := b .
和的情況下更改爲nil => stupid b
,它的工作原理。 (當然,它仍然在抱怨的利弊情況,但我認爲,這意味着它接受零的情況下)。
我對這個三個問題:
- 是否有消除
stupid
的方法嗎? Coq似乎知道類型是平等的,它只是需要某種提示。 - 我怎麼可以做cons case呢?編寫類似
stupid
的函數時遇到了很多麻煩。 - 這甚至是異構列表的正確方法嗎?這對我來說似乎是最直接的一個,但我對科裏 - 霍華德的掌握非常寬鬆,以及Coq代碼的實際含義。
我會盡量避免這麼多,呃,「型變種」。 – Atsby 2015-04-02 02:54:44
你究竟是什麼意思? – 2015-04-02 02:56:54