summaryrefslogtreecommitdiff
path: root/Test/hofs/Examples.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/Examples.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/Examples.dfy')
-rw-r--r--Test/hofs/Examples.dfy59
1 files changed, 59 insertions, 0 deletions
diff --git a/Test/hofs/Examples.dfy b/Test/hofs/Examples.dfy
new file mode 100644
index 00000000..c31b68da
--- /dev/null
+++ b/Test/hofs/Examples.dfy
@@ -0,0 +1,59 @@
+// RUN: %dafny /compile:0 /print:"%t.print" "%s" > "%t"
+// RUN: %diff "%s.expect" "%t"
+
+function Apply(f: A -> B, x: A): B
+ reads f.reads(x);
+ requires f.requires(x);
+{
+ f(x)
+}
+
+function Apply'(f: A -> B) : A -> B
+{
+ x reads f.reads(x)
+ requires f.requires(x)
+ => f(x)
+}
+
+
+function Compose(f: B -> C, g:A -> B): A -> C
+{
+ x reads g.reads(x)
+ reads if g.requires(x) then f.reads(g(x)) else {}
+ requires g.requires(x)
+ requires f.requires(g(x))
+ => f(g(x))
+}
+
+function W(f : (A,A) -> A): A -> A
+{
+ x requires f.requires(x,x)
+ reads f.reads(x,x)
+ => f(x,x)
+}
+
+function Curry(f : (A,B) -> C) : A -> B -> C
+{
+ x => y requires f.requires(x,y)
+ reads f.reads(x,y)
+ => f(x,y)
+}
+
+function Uncurry(f : A -> B -> C) : (A,B) -> C
+{
+ (x,y) requires f.requires(x)
+ requires f(x).requires(y)
+ reads f.reads(x)
+ reads if f.requires(x) then f(x).reads(y) else {}
+ => f(x)(y)
+}
+
+function S(f : (A,B) -> C, g : A -> B): A -> C
+{
+ x requires g.requires(x)
+ requires f.requires(x,g(x))
+ reads g.reads(x)
+ reads if g.requires(x) then f.reads(x,g(x)) else {}
+ => f(x,g(x))
+}
+