1+ // Any copyright is dedicated to the Public Domain.
2+ // http://creativecommons.org/publicdomain/zero/1.0/
3+
4+
5+ domain Val {}
6+
7+ domain WellFoundedOrder[T] {
8+
9+ function decreasing(arg1: T, arg2: T): Bool
10+
11+ function bounded(arg1: T): Bool
12+ }
13+
14+ domain List[V] {
15+
16+ function Nil(): List[V]
17+
18+ function Cons(value: V, tail: List[V]): List[V]
19+
20+ function get_List_value(t: List[V]): V
21+
22+ function get_List_tail(t: List[V]): List[V]
23+
24+ function List_tag(t: List[V]): Int
25+
26+ axiom {
27+ (forall value: V, tail: List[V] ::
28+ { (Cons(value, tail): List[V]) }
29+ value == (get_List_value((Cons(value, tail): List[V])): V))
30+ }
31+
32+ axiom {
33+ (forall value: V, tail: List[V] ::
34+ { (Cons(value, tail): List[V]) }
35+ tail == (get_List_tail((Cons(value, tail): List[V])): List[V]))
36+ }
37+
38+ axiom {
39+ (List_tag((Nil(): List[V])): Int) == 1
40+ }
41+
42+ axiom {
43+ (forall value: V, tail: List[V] ::
44+ { (Cons(value, tail): List[V]) }
45+ (List_tag((Cons(value, tail): List[V])): Int) == 0)
46+ }
47+
48+ axiom {
49+ (forall t: List[V] ::
50+ { (List_tag(t): Int) }
51+ { (get_List_value(t): V) }
52+ { (get_List_tail(t): List[V]) }
53+ (List_tag(t) == 1 && t == (Nil(): List[V])) ||
54+ (List_tag(t) == 0 && exists v: V, l: List[V] :: t == Cons(v, l))
55+ //(t == (Cons((get_List_value(t): V), (get_List_tail(t): List[V])): List[V]))
56+ )
57+ }
58+ }
59+
60+ domain ListWellFoundedOrder[V] {
61+
62+ axiom {
63+ (bounded((Nil(): List[V])): Bool)
64+ }
65+
66+ axiom {
67+ (forall value: V, tail: List[V] ::
68+ { (Cons(value, tail): List[V]) }
69+ (bounded((Cons(value, tail): List[V])): Bool) &&
70+ (decreasing(tail, (Cons(value, tail): List[V])): Bool))
71+ }
72+ }
73+
74+ // decreases l
75+ function len(l: List[Val]): Int
76+ ensures result >= 0
77+ {
78+ ((List_tag(l): Int) == 1 ? 0 : 1 + len((get_List_tail(l): List[Val])))
79+ }
80+
81+ // decreases l
82+ method len_termination_proof(l: List[Val])
83+ {
84+ if ((List_tag(l): Int) == 1) {
85+ } else {
86+ assert (decreasing((get_List_tail(l): List[Val]), old(l)): Bool) &&
87+ (bounded(old(l)): Bool)}
88+ }
0 commit comments