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

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

singleLinkedStackSpec : {n m : Level } {t : Set m } {a : Set n} -> StackMethods {n} {m} a {t} (SingleLinkedStack a)
singleLinkedStackSpec = record {
push = pushSingleLinkedStack
; pop  = popSingleLinkedStack
; pop2 = pop2SingleLinkedStack
; get  = getSingleLinkedStack
; get2 = get2SingleLinkedStack
; clear = clearSingleLinkedStack
}

createSingleLinkedStack : {n m : Level } {t : Set m } {a : Set n} -> Stack {n} {m} a {t} (SingleLinkedStack a)
createSingleLinkedStack = record {
stack = emptySingleLinkedStack ;
stackMethods = singleLinkedStackSpec
}

-- Implementation

pushSingleLinkedStack : {n m : Level } {t : Set m } {Data : Set n} -> 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 : {n m : Level } {t : Set m } {a  : Set n} -> 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 : {n m : Level } {t : Set m } {a  : Set n} -> SingleLinkedStack a -> (Code : SingleLinkedStack a -> (Maybe a) -> (Maybe a) -> t) -> t
pop2SingleLinkedStack {n} {m} {t} {a} stack cs with (top stack)
...                                | Nothing = cs stack Nothing Nothing
...                                | Just d = pop2SingleLinkedStack' {n} {m} stack cs
  where
    pop2SingleLinkedStack' : {n m : Level } {t : Set m }  -> 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 d1)}) (Just (datum d)) (Just (datum d1))


getSingleLinkedStack : {n m : Level } {t : Set m } {a  : Set n} -> 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 : {n m : Level } {t : Set m } {a  : Set n} -> SingleLinkedStack a -> (Code : SingleLinkedStack a -> (Maybe a) -> (Maybe a) -> t) -> t
get2SingleLinkedStack {n} {m} {t} {a} stack cs with (top stack)
...                                | Nothing = cs stack Nothing Nothing
...                                | Just d = get2SingleLinkedStack' {n} {m} stack cs
  where
    get2SingleLinkedStack' : {n m : Level} {t : Set m } -> 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))

clearSingleLinkedStack : {n m : Level } {t : Set m } {a : Set n} -> SingleLinkedStack a -> (SingleLinkedStack a -> t) -> t
clearSingleLinkedStack stack next = next (record {top = Nothing})