annotate src/stack-subtype.agda.replaced @ 3:b124f02ea3f1

post agda code
author soto@cr.ie.u-ryukyu.ac.jp
date Wed, 09 Sep 2020 22:07:32 +0900
parents 73127e0ab57c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
1 open import Level hiding (lift)
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
2 open import Data.Maybe
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
3 open import Data.Product
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
4 open import Data.Nat hiding (suc)
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
5 open import Function
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
6
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
7 module stack-subtype (A : Set) where
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
8
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
9 -- data definitions
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
10
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
11 data Element (a : Set) : Set where
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
12 cons : a @$\rightarrow$@ Maybe (Element a) @$\rightarrow$@ Element a
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
13
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
14 datum : {a : Set} @$\rightarrow$@ Element a @$\rightarrow$@ a
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
15 datum (cons a _) = a
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
16
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
17 next : {a : Set} @$\rightarrow$@ Element a @$\rightarrow$@ Maybe (Element a)
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
18 next (cons _ n) = n
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
19
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
20 record SingleLinkedStack (a : Set) : Set where
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
21 field
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
22 top : Maybe (Element a)
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
23 open SingleLinkedStack
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
24
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
25 record Context : Set where
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
26 field
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
27 -- fields for concrete data segments
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
28 n : @$\mathbb{N}$@
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
29 -- fields for stack
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
30 element : Maybe A
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
31
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
32
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
33
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
34
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
35
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
36 open import subtype Context as N
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
37
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
38 instance
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
39 ContextIsDataSegment : N.DataSegment Context
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
40 ContextIsDataSegment = record {get = (\c @$\rightarrow$@ c) ; set = (\_ c @$\rightarrow$@ c)}
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
41
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
42
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
43 record Meta : Set@$\_{1}$@ where
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
44 field
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
45 -- context as set of data segments
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
46 context : Context
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
47 stack : SingleLinkedStack A
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
48 nextCS : N.CodeSegment Context Context
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
49
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
50
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
51
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
52
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
53 open import subtype Meta as M
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
54
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
55 instance
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
56 MetaIncludeContext : M.DataSegment Context
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
57 MetaIncludeContext = record { get = Meta.context
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
58 ; set = (\m c @$\rightarrow$@ record m {context = c}) }
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
59
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
60 MetaIsMetaDataSegment : M.DataSegment Meta
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
61 MetaIsMetaDataSegment = record { get = (\m @$\rightarrow$@ m) ; set = (\_ m @$\rightarrow$@ m) }
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
62
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
63
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
64 liftMeta : {X Y : Set} {{_ : M.DataSegment X}} {{_ : M.DataSegment Y}} @$\rightarrow$@ N.CodeSegment X Y @$\rightarrow$@ M.CodeSegment X Y
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
65 liftMeta (N.cs f) = M.cs f
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
66
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
67 liftContext : {X Y : Set} {{_ : N.DataSegment X}} {{_ : N.DataSegment Y}} @$\rightarrow$@ N.CodeSegment X Y @$\rightarrow$@ N.CodeSegment Context Context
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
68 liftContext {{x}} {{y}} (N.cs f) = N.cs (\c @$\rightarrow$@ N.DataSegment.set y c (f (N.DataSegment.get x c)))
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
69
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
70 -- definition based from Gears(209:5708390a9d88) src/parallel_execution
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
71
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
72 emptySingleLinkedStack : SingleLinkedStack A
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
73 emptySingleLinkedStack = record {top = nothing}
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
74
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
75
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
76 pushSingleLinkedStack : Meta @$\rightarrow$@ Meta
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
77 pushSingleLinkedStack m = M.exec (liftMeta n) (record m {stack = (push s e) })
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
78 where
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
79 n = Meta.nextCS m
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
80 s = Meta.stack m
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
81 e = Context.element (Meta.context m)
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
82 push : SingleLinkedStack A @$\rightarrow$@ Maybe A @$\rightarrow$@ SingleLinkedStack A
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
83 push s nothing = s
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
84 push s (just x) = record {top = just (cons x (top s))}
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
85
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
86
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
87
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
88 popSingleLinkedStack : Meta @$\rightarrow$@ Meta
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
89 popSingleLinkedStack m = M.exec (liftMeta n) (record m {stack = (st m) ; context = record con {element = (elem m)}})
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
90 where
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
91 n = Meta.nextCS m
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
92 con = Meta.context m
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
93 elem : Meta @$\rightarrow$@ Maybe A
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
94 elem record {stack = record { top = (just (cons x _)) }} = just x
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
95 elem record {stack = record { top = nothing }} = nothing
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
96 st : Meta @$\rightarrow$@ SingleLinkedStack A
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
97 st record {stack = record { top = (just (cons _ s)) }} = record {top = s}
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
98 st record {stack = record { top = nothing }} = record {top = nothing}
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
99
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
100
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
101
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
102
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
103 pushSingleLinkedStackCS : M.CodeSegment Meta Meta
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
104 pushSingleLinkedStackCS = M.cs pushSingleLinkedStack
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
105
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
106 popSingleLinkedStackCS : M.CodeSegment Meta Meta
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
107 popSingleLinkedStackCS = M.cs popSingleLinkedStack
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
108
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
109
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
110 -- for sample
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
111
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
112 firstContext : Context
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
113 firstContext = record {element = nothing ; n = 0}
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
114
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
115
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
116 firstMeta : Meta
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
117 firstMeta = record { context = firstContext
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
118 ; stack = emptySingleLinkedStack
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
119 ; nextCS = (N.cs (\m @$\rightarrow$@ m))
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
120 }
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
121
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
122
soto@cr.ie.u-ryukyu.ac.jp
parents:
diff changeset
123