问题
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