如何为BNF生成代码?

时间:2017-10-03 15:37:25

标签: isabelle

我正在尝试定义自定义集类型:

notation bot ("⊥")

typedef 'a myset = "UNIV :: 'a fset option set" ..

definition myset :: "'a fset ⇒ 'a myset" where
  "myset b = Abs_myset (Some b)"

instantiation myset :: (type) bot
begin
definition "⊥ = Abs_myset None"
instance ..
end

free_constructors case_myset for
  myset
| "⊥ :: 'a myset"
  apply (simp_all add: bot_myset_def myset_def)
  apply (metis Rep_myset_inverse option.collapse)
  apply (metis Abs_myset_inverse iso_tuple_UNIV_I option.inject)
  apply (metis Abs_myset_inverse iso_tuple_UNIV_I option.distinct(1))
  done

copy_bnf 'a myset

value "map_myset (λx. x + 1) (myset {|1::int,2|})"

似乎没有为map_myset函数生成代码。因此value无法简化最后一行中的表达式。

我尝试定义map_myset的代码等式:

lemma map_myset_code [code]:
  "map_myset f xs = (case xs
    of myset fxs ⇒ myset (f |`| fxs)
     | ⊥ ⇒ ⊥)"
  apply (simp add: map_myset_def)
  apply (cases xs)
  apply (auto simp add: myset_def Abs_myset_inverse)

但我无法证明这是正确的,因为map_myset是使用MySetTest.myset.option.map_option定义的:

map_myset ≡ λf. Abs_myset ∘ MySetTest.myset.option.map_option f ∘ Rep_myset

如何找到此功能的定义?是否可以自动为BNF生成代码?

1 个答案:

答案 0 :(得分:1)

如果您对代码感兴趣,我建议使用提升/转移包而不是直接使用Abs / Rep等。然后在许多情况下,您可以免费获得代码。

notation bot ("⊥")

typedef 'a myset = "UNIV :: 'a fset option set" ..

setup_lifting type_definition_myset

lift_definition myset :: "'a fset ⇒ 'a myset" is Some .

instantiation myset :: (type) bot
begin
lift_definition bot_myset :: "'a myset" is None .
instance ..
end

lift_definition map_myset :: "('a ⇒ 'b) ⇒ 'a myset ⇒ 'b myset" 
  is "map_option o fimage" .

lift_definition of_fset :: "'a fset ⇒ 'a myset" is Some .

value "map_myset (λx. x + 1) (of_fset {|1::int,2|})"

我希望这有帮助, 勒

相关问题