summaryrefslogtreecommitdiff
path: root/Test/dafny1/TreeDatatype.dfy
diff options
context:
space:
mode:
authorGravatar rustanleino <unknown>2011-03-30 14:57:41 +0000
committerGravatar rustanleino <unknown>2011-03-30 14:57:41 +0000
commit6e93acd001f5d23c08b02c162dc2111e531f3d5d (patch)
tree5ff62d24471a5447264d453ffef35a10f448afaf /Test/dafny1/TreeDatatype.dfy
parent5f953da880c83e1f3032c7ae2e76703418c498d4 (diff)
Dafny:
* Fixed handling of type parameters in automatic decreases clauses * Added ACL2s Rotate example
Diffstat (limited to 'Test/dafny1/TreeDatatype.dfy')
-rw-r--r--Test/dafny1/TreeDatatype.dfy36
1 files changed, 36 insertions, 0 deletions
diff --git a/Test/dafny1/TreeDatatype.dfy b/Test/dafny1/TreeDatatype.dfy
index 7fc62feb..f576850e 100644
--- a/Test/dafny1/TreeDatatype.dfy
+++ b/Test/dafny1/TreeDatatype.dfy
@@ -64,3 +64,39 @@ static function XForestInc(forest: TreeList): TreeList
case Nil => forest
case Cons(tree, tail) => #TreeList.Cons(XInc(tree), XForestInc(tail))
}
+
+// ------------------ fun with recursive functions
+
+function len<T>(l: List<T>): int
+{
+ match l
+ case Nil => 0
+ case Cons(h,t) => 1 + len(t)
+}
+
+function SingletonList<T>(h: T): List<T>
+ ensures len(SingletonList(h)) == 1;
+{
+ #List.Cons(h, #List.Nil)
+}
+
+function Append<T>(a: List<T>, b: List<T>): List<T>
+ ensures len(Append(a,b)) == len(a) + len(b);
+{
+ match a
+ case Nil => b
+ case Cons(h,t) => #List.Cons(h, Append(t, b))
+}
+
+function Rotate<T>(n: int, l: List<T>): List<T>
+ requires 0 <= n;
+ ensures len(Rotate(n, l)) == len(l);
+{
+ match l
+ case Nil => l
+ case Cons(h, t) =>
+ if n == 0 then l else
+ Rotate(n-1, Append(t, SingletonList(h)))
+}
+
+