From 5e7d03fba113642aa1d6b181a854c684c740979e Mon Sep 17 00:00:00 2001 From: Dan Rosén Date: Mon, 18 Aug 2014 13:24:57 -0700 Subject: Consider lambdas literals + create literal axioms when an argument is a function --- Test/hofs/Fold.dfy | 30 ++++++++++++++++++++++++++++++ Test/hofs/Fold.dfy.expect | 6 ++++++ Test/hofs/TreeMapSimple.dfy | 8 +++++++- Test/hofs/TreeMapSimple.dfy.expect | 7 +++++-- 4 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 Test/hofs/Fold.dfy create mode 100644 Test/hofs/Fold.dfy.expect (limited to 'Test/hofs') diff --git a/Test/hofs/Fold.dfy b/Test/hofs/Fold.dfy new file mode 100644 index 00000000..df7d0126 --- /dev/null +++ b/Test/hofs/Fold.dfy @@ -0,0 +1,30 @@ +// RUN: %dafny /compile:3 "%s" > "%t" +// RUN: %diff "%s.expect" "%t" + +datatype List = Nil | Cons(A,List); + +datatype Expr = Add(List) | Mul(List) | Lit(int); + +function method Eval(e : Expr): int +{ + match e + case Add(es) => Fold(es,0,(e,v) requires e < es => Eval(e) + v) + case Mul(es) => Fold(es,1,(e,v) requires e < es => Eval(e) * v) + case Lit(i) => i +} + +function method Fold(xs : List, unit : B, f : (A,B) -> B): B + reads f.reads; + requires forall x : A, y: B :: x < xs ==> f.requires(x,y); +{ + match xs + case Nil => unit + case Cons(x,xs) => f(x,Fold(xs,unit,f)) +} + +method Main() { + var two := Lit(2); + var add := (x,y) => Add(Cons(x,Cons(y,Nil))); + assert Eval(add(two,two)) == 4; + print "3 = ", Eval(add(Lit(1),two)), "\n"; +} diff --git a/Test/hofs/Fold.dfy.expect b/Test/hofs/Fold.dfy.expect new file mode 100644 index 00000000..d76e85f9 --- /dev/null +++ b/Test/hofs/Fold.dfy.expect @@ -0,0 +1,6 @@ + +Dafny program verifier finished with 4 verified, 0 errors +Program compiled successfully +Running... + +3 = 3 diff --git a/Test/hofs/TreeMapSimple.dfy b/Test/hofs/TreeMapSimple.dfy index e717b096..3c70840e 100644 --- a/Test/hofs/TreeMapSimple.dfy +++ b/Test/hofs/TreeMapSimple.dfy @@ -1,4 +1,4 @@ -// RUN: %dafny "%s" > "%t" +// RUN: %dafny /compile:3 "%s" > "%t" // RUN: %diff "%s.expect" "%t" datatype List = Nil | Cons(head: A,tail: List); @@ -47,3 +47,9 @@ function method TMap(t0 : Tree, f : A -> B) : Tree => TMap(t,f))) } +method Main() +{ + var t := TMap(Branch(1,Cons(Branch(2,Nil),Nil)), x requires x != 0 => 100 / x); + assert t == Branch(100,Cons(Branch(50,Nil),Nil)); + print t, "\n"; +} diff --git a/Test/hofs/TreeMapSimple.dfy.expect b/Test/hofs/TreeMapSimple.dfy.expect index f523ef5d..3b59a2e3 100644 --- a/Test/hofs/TreeMapSimple.dfy.expect +++ b/Test/hofs/TreeMapSimple.dfy.expect @@ -1,3 +1,6 @@ -Dafny program verifier finished with 5 verified, 0 errors -Compiled assembly into TreeMapSimple.dll +Dafny program verifier finished with 7 verified, 0 errors +Program compiled successfully +Running... + +Tree.Branch(100, List.Cons(Tree.Branch(50, List.Nil), List.Nil)) -- cgit v1.2.3