Skip to main content

ostd/specs/mm/frame/linked_list/
linked_list_specs.rs

1use vstd::prelude::*;
2use vstd_extra::{cast_ptr::*, ownership::*, prelude::*};
3
4use super::linked_list_owners::*;
5use crate::mm::{
6    Paddr, PagingLevel, Vaddr,
7    frame::{
8        linked_list::{CursorMut, Link, LinkedList},
9        meta::AnyFrameMeta,
10        *,
11    },
12};
13
14verus! {
15
16impl CursorModel {
17    pub open spec fn move_next_spec(self) -> Self {
18        if self.list_model.list.len() > 0 {
19            if self.rear.len() > 0 {
20                let cur = self.rear[0];
21                Self {
22                    fore: self.fore.insert(self.fore.len() as int, cur),
23                    rear: self.rear.remove(0),
24                    list_model: self.list_model,
25                }
26            } else {
27                Self {
28                    fore: Seq::<LinkModel>::empty(),
29                    rear: self.fore,
30                    list_model: self.list_model,
31                }
32            }
33        } else {
34            self
35        }
36    }
37
38    pub open spec fn move_prev_spec(self) -> Self {
39        if self.list_model.list.len() > 0 {
40            if self.fore.len() > 0 {
41                let cur = self.fore[self.fore.len() - 1];
42                Self {
43                    fore: self.fore.remove(self.fore.len() - 1),
44                    rear: self.rear.insert(0, cur),
45                    list_model: self.list_model,
46                }
47            } else {
48                Self {
49                    fore: self.rear,
50                    rear: Seq::<LinkModel>::empty(),
51                    list_model: self.list_model,
52                }
53            }
54        } else {
55            self
56        }
57    }
58
59    pub open spec fn remove(self) -> Self {
60        let rear = self.rear.remove(0);
61
62        Self {
63            fore: self.fore,
64            rear: rear,
65            list_model: LinkedListModel { list: self.fore.add(rear) },
66        }
67    }
68
69    pub open spec fn insert(self, link: LinkModel) -> Self {
70        let fore = self.fore.insert(self.fore.len() as int, link);
71
72        Self {
73            fore: fore,
74            rear: self.rear,
75            list_model: LinkedListModel { list: fore.add(self.rear) },
76        }
77    }
78}
79
80impl<M: AnyFrameMeta + Repr<MetaSlotSmall>> CursorOwner<M> {
81    pub open spec fn remove_owner_spec(self, post: Self) -> bool {
82        &&& post.list_own.list == self.list_own.list.remove(self.index)
83        &&& post.index == self.index
84    }
85
86    pub proof fn remove_owner_spec_implies_model_spec(self, post: Self)
87        requires
88            self.remove_owner_spec(post),
89            0 <= self.index < self.length(),
90        ensures
91            post@ == self@.remove(),
92    {
93        let idx = self.index;
94        let s = self.list_own.list;
95        let ps = post.list_own.list;
96
97        let vh_s = LinkedListOwner::<M>::view_helper(s);
98        let vh_ps = LinkedListOwner::<M>::view_helper(ps);
99
100        assert(post@.fore == self@.fore) by {
101            assert forall|j: int| 0 <= j < vh_ps[..idx].len() implies #[trigger] vh_ps[..idx][j]
102                == vh_s[..idx][j] by {};
103        };
104
105        assert(post@.rear == self@.remove().rear) by {
106            assert forall|j: int| 0 <= j < vh_ps[idx..].len() implies #[trigger] vh_ps[idx..][j]
107                == vh_s[idx..].remove(0)[j] by {};
108        };
109
110        assert forall|j: int| 0 <= j < vh_ps.len() implies #[trigger] vh_ps[j] == vh_s[..idx].add(
111            vh_s[idx..].remove(0),
112        )[j] by {};
113        assert(vh_ps == vh_s[..idx].add(vh_s[idx..].remove(0)));
114        assert(post@.list_model == self@.remove().list_model);
115    }
116
117    pub open spec fn insert_owner_spec(self, link: LinkOwner, post: Self) -> bool {
118        // The list model (`view_helper`) depends only on the element sequence,
119        // not on `list_id`, so this carries no `list_id` clause — letting it
120        // hold even when the insert mints a fresh id for a previously-empty list.
121        &&& post.list_own.list == self.list_own.list.insert(self.index, link)
122        &&& post.index == self.index + 1
123    }
124
125    pub proof fn insert_owner_spec_implies_model_spec(self, link: LinkOwner, post: Self)
126        requires
127            self.insert_owner_spec(link, post),
128            0 <= self.index <= self.length(),
129        ensures
130            post@ == self@.insert(link@),
131    {
132        let idx = self.index;
133        let s = self.list_own.list;
134        let ps = post.list_own.list;
135        LinkedListOwner::<M>::lemma_view_helper_insert(s, idx, link);
136
137        let vh_s = LinkedListOwner::<M>::view_helper(s);
138        let vh_ps = LinkedListOwner::<M>::view_helper(ps);
139
140        assert(post@.fore == self@.insert(link@).fore) by {
141            assert forall|j: int| 0 <= j < vh_ps[..idx + 1].len() implies #[trigger] vh_ps[..idx
142                + 1][j] == vh_s[..idx].insert(idx as int, link@)[j] by {};
143        };
144
145        assert(post@.rear == self@.insert(link@).rear) by {
146            assert forall|j: int| 0 <= j < vh_ps[idx + 1..].len() implies #[trigger] vh_ps[idx
147                + 1..][j] == vh_s[idx..][j] by {};
148        };
149
150        let new_fore = vh_s[..idx].insert(idx as int, link@);
151        assert forall|j: int| 0 <= j < vh_ps.len() implies #[trigger] vh_ps[j] == new_fore.add(
152            vh_s[idx..],
153        )[j] by {};
154        assert(vh_ps == new_fore.add(vh_s[idx..]));
155        assert(post@.list_model == self@.insert(link@).list_model);
156    }
157
158    pub open spec fn move_next_owner_spec(self) -> Self {
159        if self.length() == 0 {
160            self
161        } else if self.index == self.length() {
162            Self { list_own: self.list_own, index: 0 }
163        } else {
164            Self { list_own: self.list_own, index: self.index + 1 }
165        }
166    }
167
168    pub open spec fn move_prev_owner_spec(self) -> Self {
169        if self.length() == 0 {
170            self
171        } else if self.index == 0 {
172            Self { list_own: self.list_own, index: self.length() }
173        } else {
174            Self { list_own: self.list_own, index: self.index - 1 }
175        }
176    }
177}
178
179} // verus!