summaryrefslogtreecommitdiff
path: root/Test/hofs/TreeMapSimple.dfy
diff options
context:
space:
mode:
authorGravatar Dan Rosén <danr@chalmers.se>2014-08-11 14:57:27 -0700
committerGravatar Dan Rosén <danr@chalmers.se>2014-08-11 14:57:27 -0700
commit4cbe4583b329a39dee2b4b456758cafbe7e2fa79 (patch)
tree6bb2377f06036fd41d939d168365d4e47cc7a327 /Test/hofs/TreeMapSimple.dfy
parentc377658acba5472b6d0c1e1452ce4c4c8f1fc28e (diff)
Add higher-order-functions and some other goodies
* The reads clause now needs to be self framing. * The requires clause now needs to be framed by the reads clause. * There are one-shot lambdas, with a single arrow, but they will probably be removed. * There is a {:heapQuantifier} attribute to quantifiers, but they will probably be removed. * Add smart handling of type variables * Add < and > for datatype & type parameter
Diffstat (limited to 'Test/hofs/TreeMapSimple.dfy')
-rw-r--r--Test/hofs/TreeMapSimple.dfy49
1 files changed, 49 insertions, 0 deletions
diff --git a/Test/hofs/TreeMapSimple.dfy b/Test/hofs/TreeMapSimple.dfy
new file mode 100644
index 00000000..3ba4ffd6
--- /dev/null
+++ b/Test/hofs/TreeMapSimple.dfy
@@ -0,0 +1,49 @@
+// RUN: %dafny /compile:0 "%s" > "%t"
+// RUN: %diff "%s.expect" "%t"
+
+datatype List<A> = Nil | Cons(head: A,tail: List<A>);
+
+datatype Tree<A> = Branch(val: A,trees: List<Tree<A>>);
+
+function ListData(xs : List) : set
+ ensures forall x :: x in ListData(xs) ==> x < xs;
+{
+ match xs
+ case Nil => {}
+ case Cons(x,xs) => {x} + ListData(xs)
+}
+
+function TreeData(t0 : Tree) : set
+ ensures forall t :: t in TreeData(t0) ==> t < t0;
+{
+ var Branch(x,ts) := t0;
+ {x} + set t, y | t in ListData(ts) && y in TreeData(t) :: y
+}
+
+function Pre(f : A -> B, s : set<A>) : bool
+ reads (set x, y | x in s && y in f.reads(x) :: y);
+{
+ forall x :: x in s ==> f.reads(x) == {} && f.requires(x)
+}
+
+function method Map(xs : List<A>, f : A -> B): List<B>
+ reads (set x, y | x in ListData(xs) && y in f.reads(x) :: y);
+ requires Pre(f, ListData(xs));
+ decreases xs;
+{
+ match xs
+ case Nil => Nil
+ case Cons(x,xs) => Cons(f(x),Map(xs,f))
+}
+
+function method TMap(t0 : Tree<A>, f : A -> B) : Tree<B>
+ reads (set x, y | x in TreeData(t0) && y in f.reads(x) :: y);
+ requires Pre(f, TreeData(t0));
+ decreases t0;
+{
+ var Branch(x,ts) := t0;
+ Branch(f(x),Map(ts, t requires t in ListData(ts)
+ requires Pre(f, TreeData(t))
+ => TMap(t,f)))
+}
+