/******************************************************************************* * Copyright by the contributors to the Dafny Project * SPDX-License-Identifier: MIT *******************************************************************************/ // RUN: %verify "%s" /** * Implements mutable maps in Dafny to guard against inconsistent specifications. * Only exists to verify feasability; not meant for actual usage. */ module {:options "-functionSyntax:4"} MutableMapDafny { /** * NOTE: Only here because of #2500; once resolved import "MutableMapTrait.dfy". */ trait {:termination false} MutableMapTrait { function content(): map reads this method Put(k: K, v: V) modifies this ensures this.content() == old(this.content())[k := v] ensures k in old(this.content()).Keys ==> this.content().Values + {old(this.content())[k]} == old(this.content()).Values + {v} ensures k !in old(this.content()).Keys ==> this.content().Values == old(this.content()).Values + {v} function Keys(): (keys: set) reads this ensures keys == this.content().Keys predicate HasKey(k: K) reads this ensures HasKey(k) <==> k in this.content().Keys function Values(): (values: set) reads this ensures values == this.content().Values function Items(): (items: set<(K,V)>) reads this ensures items == this.content().Items ensures items == set k | k in this.content().Keys :: (k, this.content()[k]) function Select(k: K): (v: V) reads this requires this.HasKey(k) ensures v in this.content().Values ensures this.content()[k] == v method Remove(k: K) modifies this ensures this.content() == old(this.content()) - {k} ensures k in old(this.content()).Keys ==> this.content().Values + {old(this.content())[k]} == old(this.content()).Values function Size(): (size: int) reads this ensures size == |this.content().Items| } class MutableMapDafny extends MutableMapTrait { var m: map function content(): map reads this { m } constructor () ensures this.content() == map[] { m := map[]; } method Put(k: K, v: V) modifies this ensures this.content() == old(this.content())[k := v] ensures k in old(this.content()).Keys ==> this.content().Values + {old(this.content())[k]} == old(this.content()).Values + {v} ensures k !in old(this.content()).Keys ==> this.content().Values == old(this.content()).Values + {v} { m := m[k := v]; if k in old(m).Keys { forall v' | v' in old(m).Values + {v} ensures v' in m.Values + {old(m)[k]} { if v' == v || v' == old(m)[k] { assert m[k] == v; } else { assert m.Keys == old(m).Keys + {k}; } } } if k !in old(m).Keys { forall v' | v' in old(m).Values + {v} ensures v' in m.Values { if v' == v { assert m[k] == v; assert m[k] == v'; assert v' in m.Values; } else { assert m.Keys == old(m).Keys + {k}; } } } } function Keys(): (keys: set) reads this ensures keys == this.content().Keys { m.Keys } predicate HasKey(k: K) reads this ensures HasKey(k) <==> k in this.content().Keys { k in m.Keys } function Values(): (values: set) reads this ensures values == this.content().Values { m.Values } function Items(): (items: set<(K,V)>) reads this ensures items == this.content().Items ensures items == set k | k in this.content().Keys :: (k, this.content()[k]) { var items := set k | k in m.Keys :: (k, m[k]); assert items == m.Items by { forall k | k in m.Keys ensures (k, m[k]) in m.Items { assert (k, m[k]) in m.Items; } assert items <= m.Items; forall x | x in m.Items ensures x in items { assert (x.0, m[x.0]) in items; } assert m.Items <= items; } items } function Select(k: K): (v: V) reads this requires this.HasKey(k) ensures v in this.content().Values ensures this.content()[k] == v { m[k] } method Remove(k: K) modifies this ensures this.content() == old(this.content()) - {k} ensures k in old(this.content()).Keys ==> this.content().Values + {old(this.content())[k]} == old(this.content()).Values { m := map k' | k' in m.Keys && k' != k :: m[k']; if k in old(m).Keys { var v := old(m)[k]; forall v' | v' in old(m).Values ensures v' in m.Values + {v} { if v' == v { } else { assert exists k' | k' in m.Keys :: old(m)[k'] == v'; } } } } function Size(): (size: int) reads this ensures size == |this.content().Items| { |m| } } }