如何通过限制或扩展另一个函数来定义函数?

时间:2017-09-25 12:35:17

标签: isabelle

我的理论中有3种布尔类型:

  • bool = {True,False}
  • bool3 = {True,False,⊥}
  • bool4 = {True,False,⊥,ε}

bool3基于bool

typedef bool3 = "UNIV :: bool option set" ..

definition bool3 :: "bool ⇒ bool3" where
  "bool3 b = Abs_bool3 (Some b)"

notation bot ("⊥")

instantiation bool3 :: bot
begin
definition "⊥ ≡ Abs_bool3 None"
instance ..
end

free_constructors case_bool3 for
  bool3
| "⊥ :: bool3"
  apply (metis Rep_bool3_inverse bot_bool3_def bool3_def not_Some_eq)
  apply (smt Abs_bool3_inverse bool3_def iso_tuple_UNIV_I option.inject)
  by (simp add: Abs_bool3_inject bot_bool3_def bool3_def)

bool4基于bool3

typedef bool4 = "UNIV :: bool3 option set" ..

definition bool4 :: "bool ⇒ bool4" where
  "bool4 b = Abs_bool4 (Some (bool3 b))"

class opt = bot +
  fixes void :: "'a" ("ε")

instantiation bool4 :: opt
begin
definition "⊥ = Abs_bool4 (Some ⊥)"
definition "ε = Abs_bool4 None"
instance ..
end

free_constructors case_bool4 for
  bool4
| "⊥ :: bool4"
| "ε :: bool4"
  apply (metis Rep_bool4_inverse bool3.exhaust bool4_def bot_bool4_def not_Some_eq void_bool4_def)
  apply (metis Abs_bool4_inverse UNIV_I bool3.inject bool4_def option.sel)
  apply (metis Abs_bool4_inverse UNIV_I bot_bool4_def bool3.distinct(1) bool4_def option.sel)
  apply (metis Abs_bool4_inverse UNIV_I bool4_def option.distinct(1) void_bool4_def)
  by (metis Abs_bool4_inverse UNIV_I bot_bool4_def option.distinct(1) void_bool4_def)

以下是一些类型转换:

fun bool3_to_bool4 :: "bool3 ⇒ bool4" where
  "bool3_to_bool4 ⊥ = ⊥"
| "bool3_to_bool4 (bool3 b) = bool4 b"

declare [[coercion_enabled]]
declare [[coercion "bool3 :: bool ⇒ bool3"]]
declare [[coercion "bool4 :: bool ⇒ bool4"]]
declare [[coercion "bool3_to_bool4 :: bool3 ⇒ bool4"]]

我为bool3bool4定义了逻辑连词:

fun bool3_and :: "bool3 ⇒ bool3 ⇒ bool3" where
  "bool3_and (bool3 a) (bool3 b) = bool3 (a ∧ b)"
| "bool3_and (bool3 False) _ = bool3 False"
| "bool3_and _ (bool3 False) = bool3 False"
| "bool3_and ⊥ _ = ⊥"
| "bool3_and _ ⊥ = ⊥"

fun bool4_and :: "bool4 ⇒ bool4 ⇒ bool4" where
  "bool4_and (bool4 a) (bool4 b) = bool4 (a ∧ b)"
| "bool4_and (bool4 False) _ = bool4 False"
| "bool4_and _ (bool4 False) = bool4 False"
| "bool4_and ⊥ _ = ⊥"
| "bool4_and _ ⊥ = ⊥"
| "bool4_and ε _ = ε"
| "bool4_and _ ε = ε"

并证明它们是等价的:

lemma bool3_and_eq_bool4_and:
  "(bool3_and a b = c) =
   (bool4_and a b = c)"
  apply (cases a; cases b; cases c; simp)
  apply (metis (full_types) bool3.distinct(1) bool3_and.simps(2) bool3_and.simps(6) bool4.distinct(1) bool4_and.simps(2) bool4_and.simps(9))
  apply (metis (full_types) bool3.distinct(1) bool3_and.simps(2) bool3_and.simps(6) bool4.distinct(1) bool4_and.simps(2) bool4_and.simps(9))
  apply (metis (full_types) bool3.distinct(1) bool3_and.simps(3) bool3_and.simps(4) bool4.distinct(1) bool4_and.simps(4) bool4_and.simps(6))
  apply (metis (full_types) bool3.distinct(1) bool3_and.simps(3) bool3_and.simps(4) bool4.distinct(1) bool4_and.simps(4) bool4_and.simps(6))
  done

问题是bool3_andbool4_and非常相似。我的理论中有很多这样的功能。而且我不想两次复制相同的逻辑。我也不想证明类似功能的等价性。是否可以通过限制bool3_and来定义bool4_and?或者通过[{1}}的扩展名定义bool4_and

1 个答案:

答案 0 :(得分:0)

答案很简单。我应该按照以下方式定义bool4构造函数:

definition bool4 :: "bool3 ⇒ bool4" where
  "bool4 b = Abs_bool4 (Some b)"

free_constructors case_bool4 for
  bool4
| "ε :: bool4"
  apply (metis Abs_bool4_cases bool4_def not_None_eq void_bool4_def)
  apply (metis Abs_bool4_inverse UNIV_I bool4_def option.inject)
  by (simp add: Abs_bool4_inject bool4_def void_bool4_def)

declare [[coercion "bool4 :: bool3 ⇒ bool4"]]

bool3_and可以通过以下方式简化:

fun bool3_and :: "bool3 ⇒ bool3 ⇒ bool3" where
  "bool3_and a b = (a ∧ b)"
| "bool3_and False _ = False"
| "bool3_and _ False = False"
| "bool3_and ⊥ _ = ⊥"
| "bool3_and _ ⊥ = ⊥"

这是bool4_and扩展bool3_and的地方:

fun bool4_and :: "bool4 ⇒ bool4 ⇒ bool4" where
  "bool4_and a b = bool3_and a b"
| "bool4_and ε _ = ε"
| "bool4_and _ ε = ε"

通过简化可以证明bool3_and域上bool4_andbool3的等效性:

lemma bool3_and_eq_bool4_and:
  "(bool3_and a b = c) =
   (bool4_and a b = c)"
  by simp