Fixing type variables in locale extensions

≡放荡痞女 提交于 2021-02-07 20:14:36

问题


Given this code

locale A =
  fixes foo :: "'a"

locale B = A +
  fixes bar :: "'a × 'a"

locale C' = A +
  fixes baz :: "'a"
begin
  sublocale B foo "(foo, baz)".
end

I get

Type unification failed

Failed to meet type constraint:

Term:  (foo, baz) :: 'b × 'a
Type:  'b × 'b

so it seems that Isabelle does not understand that baz and foo should be of the same type. Is there a way to fix this?


回答1:


The problem is with your declaration of locales B and C. The declaration for B is equivalent to the following

locale B = A foo for foo +
  fixes bar :: "'a * 'a"

Locales imports only remember the names of the parameters, but not the names of type variables. Thus, as you have not specified the type for foo, the most general type for B's parameter is the following:

 foo :: 'b
 bar :: 'a * 'a

You can see this using the command print_locale B. The same happens in the declaration of locale C.

If you want to have the same type for foo and bar, you have to make the connection explicit in the locale declarations. There are two ways to do this.

locale B = A foo
  for foo :: 'a
  +
  fixes bar :: "'a * 'a"

and

locale B = A +
  constrains foo :: 'a
  fixes bar :: "'a * 'a"


来源:https://stackoverflow.com/questions/26253538/fixing-type-variables-in-locale-extensions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!