view src/parallel_execution/stack.agda @ 485:a7548f01f013

proof pop2 function in agda
author ryokka
date Fri, 29 Dec 2017 19:27:39 +0900
parents 8a22cfd174bf
children 8e133a3938c0
line wrap: on
line source

module stack where

open import Relation.Binary.PropositionalEquality
open import Relation.Binary.Core
open import Data.Nat
open import Level renaming (suc to succ ; zero to Zero)

ex : 1 + 2 ≡ 3
ex = refl

data Bool : Set where
  True  : Bool
  False : Bool

record _∧_ {a b : Set} : Set where
  field
    pi1 : a
    pi2 : b

data Maybe (a : Set) : Set  where
  Nothing : Maybe a
  Just    : a -> Maybe a

record Stack {a t : Set} (stackImpl : Set) : Set  where
  field
    stack : stackImpl
    push : stackImpl -> a -> (stackImpl -> t) -> t
    pop  : stackImpl -> (stackImpl -> Maybe a -> t) -> t
    pop2 : stackImpl -> (stackImpl -> Maybe a -> Maybe a -> t) -> t
    get  : stackImpl -> (stackImpl -> Maybe a -> t) -> t
    get2 : stackImpl -> (stackImpl -> Maybe a -> Maybe a -> t) -> t
open Stack

pushStack : {a t si : Set} -> Stack si -> a -> (Stack si -> t) -> t
pushStack {a} {t} s0 d next = push s0 (stack s0) d (\s1 -> next (record s0 {stack = s1} ))

popStack : {a t si : Set} -> Stack si -> (Stack si -> Maybe a  -> t) -> t
popStack {a} {t} s0  next = pop s0 (stack s0) (\s1 d1 -> next (record s0 {stack = s1}) d1 )

pop2Stack : {a t si : Set} -> Stack si -> (Stack si -> Maybe a -> Maybe a -> t) -> t
pop2Stack {a} {t} s0 next = pop2 s0 (stack s0) (\s1 d1 d2 -> next (record s0 {stack = s1}) d1 d2)

getStack : {a t si : Set} -> Stack si -> (Stack si -> Maybe a  -> t) -> t
getStack {a} {t} s0 next = get s0 (stack s0) (\s1 d1 -> next (record s0 {stack = s1}) d1 )

get2Stack : {a t si : Set} -> Stack si -> (Stack si -> Maybe a -> Maybe a -> t) -> t
get2Stack {a} {t} s0 next = get2 s0 (stack s0) (\s1 d1 d2 -> next (record s0 {stack = s1}) d1 d2)


data Element (a : Set) : Set where
  cons : a -> Maybe (Element a) -> Element a

datum : {a : Set} -> Element a -> a
datum (cons a _) = a

next : {a : Set} -> Element a -> Maybe (Element a)
next (cons _ n) = n


{-
-- cannot define recrusive record definition. so use linked list with maybe.
record Element {l : Level} (a : Set l) : Set (suc l) where
  field
    datum : a  -- `data` is reserved by Agda.
    next : Maybe (Element a)
-}



record SingleLinkedStack (a : Set) : Set where
  field
    top : Maybe (Element a)
open SingleLinkedStack

pushSingleLinkedStack : {Data t : Set} -> SingleLinkedStack Data -> Data -> (Code : SingleLinkedStack Data -> t) -> t
pushSingleLinkedStack stack datum next = next stack1
  where
    element = cons datum (top stack)
    stack1  = record {top = Just element}


popSingleLinkedStack : {a t : Set} -> SingleLinkedStack a -> (Code : SingleLinkedStack a -> (Maybe a) -> t) -> t
popSingleLinkedStack stack cs with (top stack)
...                                | Nothing = cs stack  Nothing
...                                | Just d  = cs stack1 (Just data1)
  where
    data1  = datum d
    stack1 = record { top = (next d) }

pop2SingleLinkedStack : {a t : Set} -> SingleLinkedStack a -> (Code : SingleLinkedStack a -> (Maybe a) -> (Maybe a) -> t) -> t
pop2SingleLinkedStack {a} stack cs with (top stack)
...                                | Nothing = cs stack Nothing Nothing
...                                | Just d = pop2SingleLinkedStack' stack cs
  where
    pop2SingleLinkedStack' : {t : Set} -> SingleLinkedStack a -> (Code : SingleLinkedStack a -> (Maybe a) -> (Maybe a) -> t) -> t
    pop2SingleLinkedStack' stack cs with (next d)
    ...              | Nothing = cs stack Nothing Nothing
    ...              | Just d1 = cs (record {top = (next d)}) (Just (datum d)) (Just (datum d1))
    

getSingleLinkedStack : {a t : Set} -> SingleLinkedStack a -> (Code : SingleLinkedStack a -> (Maybe a) -> t) -> t
getSingleLinkedStack stack cs with (top stack)
...                                | Nothing = cs stack  Nothing
...                                | Just d  = cs stack (Just data1)
  where
    data1  = datum d

get2SingleLinkedStack : {a t : Set} -> SingleLinkedStack a -> (Code : SingleLinkedStack a -> (Maybe a) -> (Maybe a) -> t) -> t
get2SingleLinkedStack {a} stack cs with (top stack)
...                                | Nothing = cs stack Nothing Nothing
...                                | Just d = get2SingleLinkedStack' stack cs
  where
    get2SingleLinkedStack' : {t : Set} -> SingleLinkedStack a -> (Code : SingleLinkedStack a -> (Maybe a) -> (Maybe a) -> t) -> t
    get2SingleLinkedStack' stack cs with (next d)
    ...              | Nothing = cs stack Nothing Nothing
    ...              | Just d1 = cs stack (Just (datum d)) (Just (datum d1))



emptySingleLinkedStack : {a : Set} -> SingleLinkedStack a
emptySingleLinkedStack = record {top = Nothing}

createSingleLinkedStack : {a b : Set} -> Stack {a} {b} (SingleLinkedStack a)
createSingleLinkedStack = record { stack = emptySingleLinkedStack
                                 ; push = pushSingleLinkedStack
                                 ; pop  = popSingleLinkedStack
                                 ; pop2 = pop2SingleLinkedStack
                                 ; get  = getSingleLinkedStack
                                 ; get2 = get2SingleLinkedStack
                                 }


test01 : {a : Set} -> SingleLinkedStack a -> Maybe a -> Bool
test01 stack _ with (top stack)
...                  | (Just _) = True
...                  | Nothing  = False


test02 : {a : Set} -> SingleLinkedStack a -> Bool
test02 stack = (popSingleLinkedStack stack) test01

test03 : {a : Set} -> a ->  Bool
test03 v = pushSingleLinkedStack emptySingleLinkedStack v test02

testStack01 : {a : Set} -> a -> Bool
testStack01 v = pushStack createSingleLinkedStack v (
   \s -> popStack s (\s1 d1 -> True))

testStack02 : (Stack (SingleLinkedStack ℕ) -> Bool) -> Bool
testStack02 cs = pushStack createSingleLinkedStack 1 (
   \s -> pushStack s 2 cs)


testStack031 : (d1 d2 : ℕ ) -> Bool
testStack031 1 2 = True
testStack031 _ _ = False

testStack032 : (d1 d2 : Maybe ℕ) -> Bool
testStack032  (Just d1) (Just d2) = testStack031 d1 d2
testStack032  _ _ = False

testStack03 : Stack (SingleLinkedStack ℕ) -> ((Maybe ℕ) -> (Maybe ℕ) -> Bool ) -> Bool
testStack03 s cs = pop2Stack s (
   \s d1 d2 -> cs d1 d2 )

testStack04 : Bool
testStack04 = testStack02 (\s -> testStack03 s testStack032)

testStack05 : Set
testStack05 = testStack04 ≡ True


lemma : {A : Set} {a : A} -> test03 a ≡ False
lemma = refl

id : {A : Set} -> A -> A
id a = a


n-push : {A : Set} {a : A} -> ℕ -> SingleLinkedStack A -> SingleLinkedStack A
n-push zero s            = s
n-push {A} {a} (suc n) s = pushSingleLinkedStack (n-push {A} {a} n s) a (\s -> s)

n-pop : {A : Set} {a : A} -> ℕ -> SingleLinkedStack A -> SingleLinkedStack A
n-pop zero    s         = s
n-pop {A} {a} (suc n) s = popSingleLinkedStack (n-pop {A} {a} n s) (\s _ -> s)

open ≡-Reasoning

push-pop-equiv : {A : Set} {a : A} (s : SingleLinkedStack A) -> popSingleLinkedStack (pushSingleLinkedStack s a (\s -> s)) (\s _ -> s) ≡ s
push-pop-equiv s = refl

push-and-n-pop : {A : Set} {a : A} (n : ℕ) (s : SingleLinkedStack A) -> n-pop {A} {a} (suc n) (pushSingleLinkedStack s a id) ≡ 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)
  ≡⟨ refl ⟩
  popSingleLinkedStack (n-pop {A} {a} (suc n) (pushSingleLinkedStack s a id)) (\s _ -> s)
  ≡⟨ cong (\s -> popSingleLinkedStack s (\s _ -> s)) (push-and-n-pop n s) ⟩ 
  popSingleLinkedStack (n-pop {A} {a} n s) (\s _ -> s)
  ≡⟨ refl ⟩
  n-pop {A} {a} (suc n) s

  

n-push-pop-equiv : {A : Set} {a : A} (n : ℕ) (s : SingleLinkedStack A) -> (n-pop {A} {a} n (n-push {A} {a} n s)) ≡ 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)
  ≡⟨ refl ⟩
  n-pop {A} {a} (suc n) (pushSingleLinkedStack (n-push n s) a (\s -> s))
  ≡⟨ push-and-n-pop n (n-push n s)  ⟩
  n-pop {A} {a} n (n-push n s)
  ≡⟨ n-push-pop-equiv n s ⟩
  s



n-push-pop-equiv-empty : {A : Set} {a : A} -> (n : ℕ) -> n-pop {A} {a} n (n-push {A} {a} n emptySingleLinkedStack)  ≡ emptySingleLinkedStack
n-push-pop-equiv-empty n = n-push-pop-equiv n emptySingleLinkedStack