datatype alpha = a | b
inductive S :: "alpha list ⇒ bool" where
empty [simp]: "S []" |
step1 [simp]: "S w ⟹ S (a # w @ [b])" |
step2 [simp]: "⟦S w1; S w2⟧ ⟹ S (w1 @ w2)"
fun balanced :: "nat ⇒ alpha list ⇒ bool" where
"balanced 0 [] = True "|
"balanced n (a # w) = balanced (Suc n) w" |
"balanced (Suc n) (b # w) = balanced n w " |
"balanced n w = False"
The function balanced
is defined such that balanced n w = S (replicate n a @ w)
where replicate n a
returns list [a,a,...,a]
of length n.
I have to prove balanced n w ⟹ S (replicate n a @ w)
and S (replicate n a @ w) ⟹ balanced n w
and am having trouble doing both.
For balanced n w ⟹ S (replicate n a @ w)
I tried to proof as below.
proof (induct n w rule : balanced.induct)
case 1
then show ?case by auto
next
case (2 n w)
then show ?case by (metis Cons_eq_appendI ex5_7.b2 replicate_Suc replicate_app_Cons_same)
next
case (3 n w)
then show ?case sorry
next
case ("4_1" v)
then show ?case by auto
next
case ("4_2" va)
then show ?case by auto
qed
For the third case I get the subgoal (balanced n w ⟹ S (replicate n a @ w)) ⟹ balanced (Suc n) (b # w) ⟹ S (replicate (Suc n) a @ b # w)
and failed to proof it even with try
. Assuming S (x @ y) ⟹ S (x @ [a,b] @ y)
will solve the problem but I could not find a way to proof it either. Are there any other way to proof it or is it possible to proof S (x @ y) ⟹ S (x @ [a,b] @ y)
?
For S (replicate n a @ w) ⟹ balanced n w
I tried to proof as below.
proof (induct n w rule : balanced.induct)
case 1
then show ?case by simp
next
case (2 n w)
then show ?case try
by (metis Cons_eq_appendI ex5_7.b2 replicate_Suc replicate_app_Cons_same)
next
case (3 n w)
then show ?case sorry
next
case ("4_1" v)
then show ?case sorry
next
case ("4_2" va)
then show ?case sorry
qed
The reason that I could not proof case 3, 4_1 and 4_2 is apparently because the assumptions of all 3 subgoals are always false. For example, the subgoal I got for case 4_2 is S (replicate 0 a @ b # va) ⟹ balanced 0 (b # va)
. Since balanced 0 (b # va)
is always false. I will need to show that S (replicate 0 a @ b # va)
is always false too. Is there a way to do this without changing the definition of S
?