Function to double a list in Isabelle

社会主义新天地 提交于 2019-12-11 09:11:55

问题


I would like to define a function in Isabelle/HOL that doubles a list

fun double :: "'a list => 'a list" where
...

such that double [x1, x2, ...] = [x1, x1, x2, x2, ...]

I have tried the following:

fun double :: " 'a list ⇒ 'a list" where
"double [] = []" |
"double [x#[l]] = x # x # double [l]"

as well as some other definitions. I get the error

Type unification failed

Type error in application: incompatible operand type

What is wrong with my function?


回答1:


Actually the error message contains some further information. Namely

Operator:  double :: 'a list ⇒ 'a list
Operand:   [[x, l]] :: ??'a list list

which tells us that [[x, l]] is of type ?'a list list, i.e., a list of lists. As you want to give it as argument to double, which expects an argument of type 'a list, you get the type error.

The origin of the term [[x, l]] in the error message is the second line of your definition

`double [x # [l]]`

where x#[l] is printed as the equivalent [x, l].

There are several superfluous brackets in your input. Note that in contrast to informal mathematical text (with informal meaning as on paper ;)) in Isabelle you cannot use brackets [, ] for explicit nesting. Try the following instead.

fun double :: " 'a list ⇒ 'a list"
where
  "double [] = []" |
  "double (x#xs) = x # x # double xs"


来源:https://stackoverflow.com/questions/26451510/function-to-double-a-list-in-isabelle

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