Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maps: add MapKeys, MapValues, and related helpers #86

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
24 changes: 20 additions & 4 deletions src/Collections/Maps/Maps.dfy
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
// RUN: %dafny /compile:0 "%s"

/*******************************************************************************
* Original: Copyright 2018-2021 VMware, Inc., Microsoft Inc., Carnegie Mellon University,
* Original: Copyright 2018-2021 VMware, Inc., Microsoft Inc., Carnegie Mellon University,
* ETH Zurich, and University of Washington
* SPDX-License-Identifier: BSD-2-Clause
*
* SPDX-License-Identifier: BSD-2-Clause
*
* Modifications and Extensions: Copyright by the contributors to the Dafny Project
* SPDX-License-Identifier: MIT
* SPDX-License-Identifier: MIT
*******************************************************************************/

include "../../Wrappers.dfy"
include "../../Functions.dfy"
include "../Sets/Sets.dfy"

module {:options "-functionSyntax:4"} Maps {
import opened Wrappers
import Functions
import Sets

function Get<X, Y>(m: map<X, Y>, x: X): Option<Y>
{
Expand Down Expand Up @@ -127,4 +131,16 @@ module {:options "-functionSyntax:4"} Maps {
forall x, x' {:trigger m[x], m[x']} :: x in m && x' in m && start <= x <= x' ==> m[x] <= m[x']
}

/* Maps an injective function over the keys of a map, retaining the values. */
function {:opaque} MapKeys<X(!new), Y, X'>(m: map<X, Y>, f: X --> X'): (m': map<X', Y>)
alex-chew marked this conversation as resolved.
Show resolved Hide resolved
reads f.reads
requires forall x {:trigger f.requires(x)} :: f.requires(x)
alex-chew marked this conversation as resolved.
Show resolved Hide resolved
requires Functions.Injective(f)
ensures |m'| == |m|
ensures m'.Values == m.Values
alex-chew marked this conversation as resolved.
Show resolved Hide resolved
{
var m' := map k <- m :: f(k) := m[k];
Sets.LemmaMapSize(m.Keys, m'.Keys, f);
m'
}
}
alex-chew marked this conversation as resolved.
Show resolved Hide resolved