view whileTestGears.agda @ 6:28e80739eed6

fix whileTestGears
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Fri, 14 Dec 2018 22:06:24 +0900
parents 17e4f3b58148
children e7d6bdb6039d
line wrap: on
line source

module whileTestGears where

open import Function
open import Data.Nat
open import Data.Bool hiding ( _≟_ ; _∧_)
open import Level renaming ( suc to succ ; zero to Zero )
open import Relation.Nullary using (¬_; Dec; yes; no)
open import Relation.Binary.PropositionalEquality


record _∧_ {n : Level } (a : Set n) (b : Set n): Set n where
  field
    pi1 : a
    pi2 : b

open  _∧_

_-_ : ℕ -> ℕ -> ℕ 
x - zero  = x
zero - _  = zero
(suc x) - (suc y)  = x - y

sym1 : { y : ℕ } -> y + zero  ≡ y
sym1 {zero} = refl
sym1 {suc y} = cong ( λ x → suc x ) ( sym1 {y} )

+-sym : { x y : ℕ } -> x + y ≡ y + x
+-sym {zero} {zero} = refl
+-sym {zero} {suc y} = let open ≡-Reasoning  in
          begin
            zero + suc y 
          ≡⟨⟩
            suc y
          ≡⟨ sym sym1 ⟩
            suc y + zero

+-sym {suc x} {zero} =  let open ≡-Reasoning  in
          begin
            suc x + zero
          ≡⟨ sym1  ⟩
            suc x
          ≡⟨⟩
            zero + suc x

+-sym {suc x} {suc y} = cong ( λ z → suc z ) (  let open ≡-Reasoning  in
          begin
            x + suc y
          ≡⟨ +-sym {x} {suc y} ⟩
            suc (y + x)
          ≡⟨ cong ( λ z → suc z )  (+-sym {y} {x}) ⟩
            suc (x + y)
          ≡⟨ sym ( +-sym {y} {suc x}) ⟩
            y + suc x
          ∎ )

minus-plus : { x y : ℕ } -> (suc x - 1) + (y + 1) ≡ suc x + y
minus-plus {zero} {y} = +-sym {y} {1}
minus-plus {suc x} {y} =  cong ( λ z → suc z ) (minus-plus {x} {y})

lt : ℕ -> ℕ -> Bool
lt x y with (suc x ) ≤? y
lt x y | yes p = true
lt x y | no ¬p = false

Equal : ℕ -> ℕ -> Bool
Equal x y with x ≟ y
Equal x y | yes p = true
Equal x y | no ¬p = false

record Env  : Set where
  field
    varn : ℕ
    vari : ℕ
open Env

whileTest : {l : Level} {t : Set l} -> (Code : Env -> t) -> t
whileTest next = next (record {varn = 10 ; vari = 0} )

{-# TERMINATING #-}
whileLoop : {l : Level} {t : Set l} -> Env -> (Code : Env -> t) -> t
whileLoop env next with lt 0 (varn env)
whileLoop env next | false = next env
whileLoop env next | true =
    whileLoop (record {varn = (varn env) - 1 ; vari = (vari env) + 1}) next

test1 : Env
test1 = whileTest (λ env → whileLoop env (λ env1 → env1 ))


proof1 : whileTest (λ env → whileLoop env (λ e → (vari e) ≡ 10 ))
proof1 = refl



-- stmt2Cond : {l : Level} → EnvWithCond {l} → 
-- stmt2Cond env = (Equal (varn' env) 10) ∧ (Equal (vari' env) 0)

whileTest' : {l : Level} {t : Set l}  -> (Code : (env : Env)  -> ((vari env) ≡ 0) ∧ ((varn env) ≡ 10) -> t) -> t
whileTest' next = next env proof2
  where
    env : Env
    env = record {vari = 0 ; varn = 10}
    proof2 : ((vari env) ≡ 0) ∧ ((varn env) ≡ 10)
    proof2 = record {pi1 = refl ; pi2 = refl}
    
{-# TERMINATING #-}
whileLoop' : {l : Level} {t : Set l} -> (env : Env) -> ((varn env) + (vari env) ≡ 10) -> (Code : Env -> t) -> t
whileLoop' env proof next with lt 0 (varn  env)
whileLoop' env proof next | false = next env 
whileLoop' env proof next | true = whileLoop' env1 proof3 next
    where
      env1 = record {varn = (varn  env) - 1 ; vari = (vari env) + 1}
      proof3 : varn env1 + vari env1 ≡ 10
      proof3 = let open ≡-Reasoning  in
          begin 
            varn env1 + vari env1
          ≡⟨⟩
            (varn env - 1) + (vari env + 1)
          ≡⟨ {!!} ⟩
            10



conversion1 : {l : Level} {t : Set l } → (env : Env) -> ((vari env) ≡ 0) ∧ ((varn env) ≡ 10)
               -> (Code : (env1 : Env) -> (varn env1 + vari env1 ≡ 10) -> t) -> t
conversion1 env p1 next = next env proof4
   where
      proof4 : varn env + vari env ≡ 10
      proof4 = let open ≡-Reasoning  in
          begin
            varn env + vari env
          ≡⟨ cong ( λ n → n + vari env ) (pi2 p1 ) ⟩
            10 + vari env
          ≡⟨ cong ( λ n → 10 + n ) (pi1 p1 ) ⟩
            10 + 0
          ≡⟨⟩
            10



proofGears : Set
proofGears = whileTest' (λ n p1 →  conversion1 n p1 (λ n1 p2 → whileLoop' n1 p2 (λ n2 →  ( vari n2 ≡ 10 ))))