Quantcast
Channel: How to do theorems that require mutual recursion? - Stack Overflow
Viewing all articles
Browse latest Browse all 2

How to do theorems that require mutual recursion?

$
0
0

I ran into a problem that required mutual recursion to solve. I could not do it in Coq, but I suspected that it might be possible in Agda and proved that is the case using two mutually recursive functions. I am not sure what exactly I should do to bring that solution back into Coq and Googling this problem has not turned up any solutions.

What are my options here?

To further motivate the question here is the Agda proof that I want to translate into Coq. It proves the functional equality between a tree walking and a flattened calculator.

The proof requires that remove-from-stack and add-to-stack call each other in a mutually recursive manner.

open import Data.Productopen import Data.Natopen import Data.Listopen import Data.List.Propertiesopen import Functionimport Relation.Binary.PropositionalEquality as Eqopen Eq using (_≡_; refl; cong; subst)open Eq.≡-Reasoningdata Sinstr : Set where  SPush : ℕ→ Sinstr  SPlus : Sinstrdata Aexp : Set where  ANum : (n : ℕ) → Aexp  APlus : Aexp → Aexp → Aexps-execute : List Sinstr → List ℕ→ List ℕs-execute [] stack = stacks-execute (SPush x ∷ prog) stack = s-execute prog (x ∷ stack)s-execute (SPlus ∷ prog) (x1 ∷ x2 ∷ stack) = s-execute prog (x2 + x1 ∷ stack)s-execute _ stack = stackaeval : Aexp →ℕaeval (ANum n) = naeval (APlus a a₁) = aeval a + aeval a₁s-compile : Aexp → List Sinstrs-compile (ANum n) = [ SPush n ]s-compile (APlus a a₁) = s-compile a ++ s-compile a₁++ [ SPlus ]++-assoc⁴ : ∀ {T : Set} (a b c d : List T) → (a ++ b ++ c) ++ d ≡ a ++ b ++ c ++ d++-assoc⁴ a b c d =  begin    ((a ++ b ++ c) ++ d)≡⟨++-assoc a (b ++ c) d ⟩    (a ++ (b ++ c) ++ d)≡⟨ cong (a ++_) (++-assoc b c d) ⟩    (a ++ b ++ c ++ d)∎remove-from-stack : ∀ {e2 stack x} e1 →  s-execute (s-compile e1 ++ e2) stack ≡ [ x ] →∃[ a ] (s-execute e2 (a ∷ stack) ≡ [ x ] × s-execute (s-compile e1) [] ≡ [ a ])add-to-stack : ∀ {e2 stack x} e1 →  s-execute (s-compile e1) [] ≡ [ x ] →  s-execute (s-compile e1 ++ e2) stack ≡ s-execute e2 (x ∷ stack)remove-from-stack (ANum n) prf = n , (prf , refl)remove-from-stack {rest} {stack} (APlus e1 e2) prf with subst (λ l → s-execute l stack ≡ _) (++-assoc⁴ (s-compile e1) (s-compile e2) [ _ ]  rest) prf... | []∷stack with remove-from-stack e1 []∷stackremove-from-stack {rest} {stack} (APlus e1 e2) _ | []∷stack | a , a∷stack , e1≡a with remove-from-stack e2 a∷stackremove-from-stack {rest} {stack} (APlus e1 e2) _ | []∷stack | a , a∷stack , e1≡a | b , b∷a∷stack , e2≡b = a + b , b∷a∷stack , e1+e1≡a+b where  e1+e1≡a+b : _  e1+e1≡a+b =    begin      s-execute (s-compile e1 ++ s-compile e2 ++ SPlus ∷ []) []≡⟨ add-to-stack e1 e1≡a ⟩      s-execute (s-compile e2 ++ SPlus ∷ []) [ a ]≡⟨ add-to-stack e2 e2≡b ⟩      s-execute (SPlus ∷ []) (b ∷ [ a ])≡⟨⟩      (a + b ∷ [])∎add-to-stack (ANum n) refl = refladd-to-stack (APlus e1 e2) []∷[] with remove-from-stack e1 []∷[]add-to-stack (APlus e1 e2) []∷[] | a , a∷[] , e1≡a with remove-from-stack e2 a∷[]add-to-stack {rest} {stack} (APlus e1 e2) []∷[] | a , a∷[] , e1≡a | b , refl , e2≡b =  begin    s-execute ((s-compile e1 ++ s-compile e2 ++ SPlus ∷ []) ++ rest) stack≡⟨ cong (λ l → s-execute l stack) (++-assoc⁴ (s-compile e1) (s-compile e2) [ _ ]  rest) ⟩    s-execute (s-compile e1 ++ s-compile e2 ++ SPlus ∷ [] ++ rest) stack≡⟨ add-to-stack e1 e1≡a ⟩    s-execute (s-compile e2 ++ SPlus ∷ [] ++ rest) (a ∷ stack)≡⟨ add-to-stack e2 e2≡b ⟩    s-execute rest (a + b ∷ stack)∎s-compile-correct : (e : Aexp) → s-execute (s-compile e) [] ≡ [ aeval e ]s-compile-correct (ANum n) = refls-compile-correct (APlus l r) =  begin    (s-execute (s-compile l ++ s-compile r ++ SPlus ∷ []) [])≡⟨ add-to-stack l (s-compile-correct l) ⟩    (s-execute (s-compile r ++ SPlus ∷ []) (aeval l ∷ []))≡⟨ add-to-stack r (s-compile-correct r) ⟩    (s-execute (SPlus ∷ []) (aeval r ∷ aeval l ∷ []))≡⟨⟩    (aeval l + aeval r ∷ [])∎

Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images