view paper/src/stackTest.agda.replaced @ 19:046b2b20d6c7 default tip

fix
author ryokka
date Mon, 09 Mar 2020 11:25:49 +0900
parents c7acb9211784
children
line wrap: on
line source

open import Level renaming (suc to succ ; zero to Zero )
module stackTest where

open import stack

open import Relation.Binary.PropositionalEquality
open import Relation.Binary.Core
open import Data.Nat
open import Function


open SingleLinkedStack
open Stack

----
--
-- proof of properties ( concrete cases )
--

test01 : {n : Level } {a : Set n} @$\rightarrow$@ SingleLinkedStack a @$\rightarrow$@ Maybe a @$\rightarrow$@ Bool {n}
test01 stack _ with (top stack)
...                  | (Just _) = True
...                  | Nothing  = False


test02 : {n : Level } {a : Set n} @$\rightarrow$@ SingleLinkedStack a @$\rightarrow$@ Bool
test02 stack = popSingleLinkedStack stack test01

test03 : {n : Level } {a : Set n} @$\rightarrow$@ a @$\rightarrow$@  Bool
test03 v = pushSingleLinkedStack emptySingleLinkedStack v test02

-- after a push and a pop, the stack is empty
lemma : {n : Level} {A : Set n} {a : A} @$\rightarrow$@ test03 a @$\equiv$@ False
lemma = refl

testStack01 : {n m : Level } {a : Set n} @$\rightarrow$@ a @$\rightarrow$@ Bool {m}
testStack01 v = pushStack createSingleLinkedStack v (
   \s @$\rightarrow$@ popStack s (\s1 d1 @$\rightarrow$@ True))

-- after push 1 and 2, pop2 get 1 and 2

testStack02 : {m : Level } @$\rightarrow$@  ( Stack  @$\mathbb{N}$@ (SingleLinkedStack @$\mathbb{N}$@) @$\rightarrow$@ Bool {m} ) @$\rightarrow$@ Bool {m}
testStack02 cs = pushStack createSingleLinkedStack 1 (
   \s @$\rightarrow$@ pushStack s 2 cs)


testStack031 : (d1 d2 : @$\mathbb{N}$@ ) @$\rightarrow$@ Bool {Zero}
testStack031 2 1 = True
testStack031 _ _ = False

testStack032 : (d1 d2 : Maybe @$\mathbb{N}$@) @$\rightarrow$@ Bool {Zero}
testStack032  (Just d1) (Just d2) = testStack031 d1 d2
testStack032  _ _ = False

testStack03 : {m : Level } @$\rightarrow$@ Stack  @$\mathbb{N}$@ (SingleLinkedStack @$\mathbb{N}$@) @$\rightarrow$@ ((Maybe @$\mathbb{N}$@) @$\rightarrow$@ (Maybe @$\mathbb{N}$@) @$\rightarrow$@ Bool {m} ) @$\rightarrow$@ Bool {m}
testStack03 s cs = pop2Stack s (
   \s d1 d2 @$\rightarrow$@ cs d1 d2 )

testStack04 : Bool
testStack04 = testStack02 (\s @$\rightarrow$@ testStack03 s testStack032)

testStack05 : testStack04 @$\equiv$@ True
testStack05 = refl

testStack06 : {m : Level } @$\rightarrow$@ Maybe (Element @$\mathbb{N}$@)
testStack06 = pushStack createSingleLinkedStack 1 (
   \s @$\rightarrow$@ pushStack s 2 (\s @$\rightarrow$@ top (stack s)))

testStack07 : {m : Level } @$\rightarrow$@ Maybe (Element @$\mathbb{N}$@)
testStack07 = pushSingleLinkedStack emptySingleLinkedStack 1 (
   \s @$\rightarrow$@ pushSingleLinkedStack s 2 (\s @$\rightarrow$@ top s))

testStack08 = pushSingleLinkedStack emptySingleLinkedStack 1 
   $ \s @$\rightarrow$@ pushSingleLinkedStack s 2 
   $ \s @$\rightarrow$@ pushSingleLinkedStack s 3 
   $ \s @$\rightarrow$@ pushSingleLinkedStack s 4 
   $ \s @$\rightarrow$@ pushSingleLinkedStack s 5 
   $ \s @$\rightarrow$@ top s

------
--
-- proof of properties with indefinite state of stack
--
-- this should be proved by properties of the stack inteface, not only by the implementation,
--    and the implementation have to provides the properties.
--
--    we cannot write "s @$\equiv$@ s3", since level of the Set does not fit , but use stack s @$\equiv$@ stack s3 is ok.
--    anyway some implementations may result s != s3
--  

stackInSomeState : {l m : Level } {D : Set l} {t : Set m } (s : SingleLinkedStack D ) @$\rightarrow$@ Stack {l} {m} D {t}  ( SingleLinkedStack  D )
stackInSomeState s =  record { stack = s ; stackMethods = singleLinkedStackSpec }

push@$\rightarrow$@push@$\rightarrow$@pop2 : {l : Level } {D : Set l} (x y : D ) (s : SingleLinkedStack D ) @$\rightarrow$@
    pushStack ( stackInSomeState s )  x ( \s1 @$\rightarrow$@ pushStack s1 y ( \s2 @$\rightarrow$@ pop2Stack s2 ( \s3 y1 x1 @$\rightarrow$@ (Just x @$\equiv$@ x1 ) @$\wedge$@ (Just y @$\equiv$@ y1 ) ) ))
push@$\rightarrow$@push@$\rightarrow$@pop2 {l} {D} x y s = record { pi1 = refl ; pi2 = refl }


-- id : {n : Level} {A : Set n} @$\rightarrow$@ A @$\rightarrow$@ A
-- id a = a

-- push a, n times

n-push : {n : Level} {A : Set n} {a : A} @$\rightarrow$@ @$\mathbb{N}$@ @$\rightarrow$@ SingleLinkedStack A @$\rightarrow$@ SingleLinkedStack A
n-push zero s            = s
n-push {l} {A} {a} (suc n) s = pushSingleLinkedStack (n-push {l} {A} {a} n s) a (\s @$\rightarrow$@ s ) 

n-pop :  {n : Level}{A : Set n} {a : A} @$\rightarrow$@ @$\mathbb{N}$@ @$\rightarrow$@ SingleLinkedStack A @$\rightarrow$@ SingleLinkedStack A
n-pop zero    s         = s
n-pop  {_} {A} {a} (suc n) s = popSingleLinkedStack (n-pop {_} {A} {a} n s) (\s _ @$\rightarrow$@ s )

open @$\equiv$@-Reasoning

push-pop-equiv : {n : Level} {A : Set n} {a : A} (s : SingleLinkedStack A) @$\rightarrow$@ (popSingleLinkedStack (pushSingleLinkedStack s a (\s @$\rightarrow$@ s)) (\s _ @$\rightarrow$@ s) ) @$\equiv$@ s
push-pop-equiv s = refl

push-and-n-pop : {n : Level} {A : Set n} {a : A} (n : @$\mathbb{N}$@) (s : SingleLinkedStack A) @$\rightarrow$@ n-pop {_} {A} {a} (suc n) (pushSingleLinkedStack s a id) @$\equiv$@ n-pop {_} {A} {a} n s
push-and-n-pop zero s            = refl
push-and-n-pop {_} {A} {a} (suc n) s = begin
   n-pop {_} {A} {a} (suc (suc n)) (pushSingleLinkedStack s a id)
  @$\equiv$@@$\langle$@ refl @$\rangle$@
   popSingleLinkedStack (n-pop {_} {A} {a} (suc n) (pushSingleLinkedStack s a id)) (\s _ @$\rightarrow$@ s)
  @$\equiv$@@$\langle$@ cong (\s @$\rightarrow$@ popSingleLinkedStack s (\s _ @$\rightarrow$@ s )) (push-and-n-pop n s) @$\rangle$@ 
   popSingleLinkedStack (n-pop {_} {A} {a} n s) (\s _ @$\rightarrow$@ s)
  @$\equiv$@@$\langle$@ refl @$\rangle$@
    n-pop {_} {A} {a} (suc n) s
  @$\blacksquare$@
  

n-push-pop-equiv : {n : Level} {A : Set n} {a : A} (n : @$\mathbb{N}$@) (s : SingleLinkedStack A) @$\rightarrow$@ (n-pop {_} {A} {a} n (n-push {_} {A} {a} n s)) @$\equiv$@ s
n-push-pop-equiv zero s            = refl
n-push-pop-equiv {_} {A} {a} (suc n) s = begin
    n-pop {_} {A} {a} (suc n) (n-push (suc n) s)
  @$\equiv$@@$\langle$@ refl @$\rangle$@
    n-pop {_} {A} {a} (suc n) (pushSingleLinkedStack (n-push n s) a (\s @$\rightarrow$@ s))
  @$\equiv$@@$\langle$@ push-and-n-pop n (n-push n s)  @$\rangle$@
    n-pop {_} {A} {a} n (n-push n s)
  @$\equiv$@@$\langle$@ n-push-pop-equiv n s @$\rangle$@
    s
  @$\blacksquare$@


n-push-pop-equiv-empty : {n : Level} {A : Set n} {a : A} @$\rightarrow$@ (n : @$\mathbb{N}$@) @$\rightarrow$@ n-pop {_} {A} {a} n (n-push {_} {A} {a} n emptySingleLinkedStack)  @$\equiv$@ emptySingleLinkedStack
n-push-pop-equiv-empty n = n-push-pop-equiv n emptySingleLinkedStack