summaryrefslogtreecommitdiff
path: root/Test/dafny0/NestedMatch.dfy
blob: 81319b4aeb488c3fb16982b3d63eae114aefdb79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// RUN: %dafny /compile:0  "%s" > "%t"
// RUN: %diff "%s.expect" "%t"

datatype Nat = Zero | Suc(Nat)

predicate Even(n: Nat)
{
  match n
  case Zero => true
  case Suc(Zero) => false
  case Suc(Suc(p)) => Even(p)
}


method checkEven(n: Nat) {
  assert Even(Zero) == true;
  assert Even(Suc(Zero)) == false;
  assert Even(Suc(Suc(n))) == Even(n);
}

datatype List<T> = Nil | Cons(T, List<T>)

function last<T>(xs: List<T>): T
  requires xs != Nil
{
  match xs
  case Cons(y, Nil) => y
  case Cons(y, Cons(z, zs)) => last(Cons(z, zs))
}

method checkLast<T>(y: T) {
  assert last(Cons(y, Nil)) == y;
  assert last(Cons(y, Cons(y, Nil))) == last(Cons(y, Nil));
}


function minus(x: Nat, y: Nat): Nat
{
  match (x, y)
  case (Zero, _) => Zero
  case (Suc(_), Zero) => x
  case (Suc(a), Suc(b)) => minus(a, b)
}

method checkMinus(x:Nat, y: Nat) {
  assert minus(Suc(x), Suc(y)) == minus(x,y);
}


// nested match statement
method Last<T>(xs: List<T>) returns (x: T)
  requires xs != Nil
{
 
  match xs {
  	case Cons(y, Nil) => x:= y;
  	case Cons(y, Cons(z, zs)) => x:=Last(Cons(z, zs));
  }
}