From 57a7fa3aa01ced1943dde6fe3270c62f1b6923c3 Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 31 May 2012 17:13:35 -0700 Subject: Dafny: Added map comprehensions and updated display syntax --- Binaries/DafnyPrelude.bpl | 9 +++++++++ Binaries/DafnyRuntime.cs | 36 ++++++++++++++++++++++++++++++++++-- 2 files changed, 43 insertions(+), 2 deletions(-) (limited to 'Binaries') diff --git a/Binaries/DafnyPrelude.bpl b/Binaries/DafnyPrelude.bpl index 09b6efcf..742fd4e4 100644 --- a/Binaries/DafnyPrelude.bpl +++ b/Binaries/DafnyPrelude.bpl @@ -371,6 +371,15 @@ axiom (forall u: U :: { Map#Domain(Map#Empty(): Map U V)[u] } !Map#Domain(Map#Empty(): Map U V)[u]); +function Map#Glue([U] bool, [U]V): Map U V; +axiom (forall a: [U] bool, b:[U]V :: + { Map#Domain(Map#Glue(a, b)) } + Map#Domain(Map#Glue(a, b)) == a); +axiom (forall a: [U] bool, b:[U]V :: + { Map#Elements(Map#Glue(a, b)) } + Map#Elements(Map#Glue(a, b)) == b); + + //Build is used in displays, and for map updates function Map#Build(Map U V, U, V): Map U V; /*axiom (forall m: Map U V, u: U, v: V :: diff --git a/Binaries/DafnyRuntime.cs b/Binaries/DafnyRuntime.cs index 67669245..f7eecfc5 100644 --- a/Binaries/DafnyRuntime.cs +++ b/Binaries/DafnyRuntime.cs @@ -274,6 +274,19 @@ namespace Dafny } return new MultiSet(r); } + public IEnumerable Elements { + get { + List l = new List(); + foreach (T t in dict.Keys) { + int n; + dict.TryGetValue(t, out n); + for (int i = 0; i < n; i ++) { + l.Add(t); + } + } + return l; + } + } } public class Map @@ -288,9 +301,16 @@ namespace Dafny return new Map(new Dictionary()); } } - public static Map FromElements(params Pair[] values) { + public static Map FromElements(params Pair[] values) { Dictionary d = new Dictionary(values.Length); - foreach (Pair p in values) { + foreach (Pair p in values) { + d[p.Car] = p.Cdr; + } + return new Map(d); + } + public static Map FromCollection(List> values) { + Dictionary d = new Dictionary(values.Count); + foreach (Pair p in values) { d[p.Car] = p.Cdr; } return new Map(d); @@ -343,6 +363,11 @@ namespace Dafny d[index] = val; return new Map(d); } + public IEnumerable Domain { + get { + return dict.Keys; + } + } } public class Sequence { @@ -471,6 +496,12 @@ namespace Dafny } return frall; } + public static bool QuantMap(Dafny.Map map, bool frall, System.Predicate pred) { + foreach (var u in map.Domain) { + if (pred(u) != frall) { return !frall; } + } + return frall; + } public static bool QuantSeq(Dafny.Sequence seq, bool frall, System.Predicate pred) { foreach (var u in seq.Elements) { if (pred(u) != frall) { return !frall; } @@ -479,6 +510,7 @@ namespace Dafny } // Enumerating other collections public delegate Dafny.Set ComprehensionDelegate(); + public delegate Dafny.Map MapComprehensionDelegate(); public static IEnumerable AllBooleans { get { yield return false; -- cgit v1.2.3