簡體   English   中英

Coq - 強制列表 nat

[英]Coq - Coercion of list nat

我想從類型list nat強制轉換為類型list bool 我認為這可以通過以下方式完成:

From Coq Require Import Lists.List.

Definition nat_to_bool (n : nat) : bool :=
  match n with
  | 0 => true
  | _ => false
  end.

Definition list_nat_to_list_bool (l : list nat) : list bool :=
  map (fun n => (nat_to_bool n)) l.

Coercion list_nat_to_list_bool : list nat >-> list bool.

但是,這不起作用。 在表單list nat上使用強制轉換似乎存在一個基本問題。 為什么這不起作用?

文檔指出 class 名稱必須是已定義的名稱。 list natlist bool都不是定義的名稱——它們都是應用程序。 您必須為要定義強制轉換的類型命名,如下所示:

From Coq Require Import Lists.List.

Definition nat_to_bool (n : nat) : bool :=
  match n with
  | 0 => true
  | _ => false
  end.

Definition list_nat := list nat.
Definition list_bool := list bool.

Definition list_nat_to_list_bool (l : list_nat) : list_bool :=
  map (fun n => (nat_to_bool n)) l.

Coercion list_nat_to_list_bool : list_nat >-> list_bool.

請注意強制 function 必須使用這些名稱 - 你不能寫list nat而不是list_nat 此外,強制的應用程序必須使用定義的名稱,如下所示:

Definition test : list_bool := (1 :: nil) : list_nat.

這是因為強制可能在類型統一很困難的句法級別上完成。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM