summaryrefslogtreecommitdiff
path: root/tests/lean/misc/paper/Paper.lean
blob: adcd1eae4c6424b81bf7deaaac8bedb109c6e658 (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
-- THIS FILE WAS AUTOMATICALLY GENERATED BY AENEAS
-- [paper]
import Base.Primitives

structure OpaqueDefs where
  
  /- [paper::ref_incr] -/
  def ref_incr_fwd_back (x : Int32) : result Int32 :=
    Int32.checked_add x (Int32.ofNatCore 1 (by intlit))
  
  /- [paper::test_incr] -/
  def test_incr_fwd : result Unit :=
    do
      let x <- ref_incr_fwd_back (Int32.ofNatCore 0 (by intlit))
      if not (x = (Int32.ofNatCore 1 (by intlit)))
      then result.fail error.panic
      else result.ret ()
  
  /- Unit test for [paper::test_incr] -/
  #assert (test_incr_fwd = .ret ())
  
  /- [paper::choose] -/
  def choose_fwd (T : Type) (b : Bool) (x : T) (y : T) : result T :=
    if b
    then result.ret x
    else result.ret y
  
  /- [paper::choose] -/
  def choose_back
    (T : Type) (b : Bool) (x : T) (y : T) (ret0 : T) : result (T × T) :=
    if b
    then result.ret (ret0, y)
    else result.ret (x, ret0)
  
  /- [paper::test_choose] -/
  def test_choose_fwd : result Unit :=
    do
      let z <-
        choose_fwd Int32 true (Int32.ofNatCore 0 (by intlit))
          (Int32.ofNatCore 0 (by intlit))
      let z0 <- Int32.checked_add z (Int32.ofNatCore 1 (by intlit))
      if not (z0 = (Int32.ofNatCore 1 (by intlit)))
      then result.fail error.panic
      else
        do
          let (x, y) <-
            choose_back Int32 true (Int32.ofNatCore 0 (by intlit))
              (Int32.ofNatCore 0 (by intlit)) z0
          if not (x = (Int32.ofNatCore 1 (by intlit)))
          then result.fail error.panic
          else
            if not (y = (Int32.ofNatCore 0 (by intlit)))
            then result.fail error.panic
            else result.ret ()
  
  /- Unit test for [paper::test_choose] -/
  #assert (test_choose_fwd = .ret ())
  
  /- [paper::List] -/
  inductive list_t (T : Type) :=
  | ListCons : T -> list_t T -> list_t T
  | ListNil : list_t T
  
  /- [paper::list_nth_mut] -/
  def list_nth_mut_fwd (T : Type) (l : list_t T) (i : UInt32) : result T :=
    match l with
    | list_t.ListCons x tl =>
      if i = (UInt32.ofNatCore 0 (by intlit))
      then result.ret x
      else
        do
          let i0 <- UInt32.checked_sub i (UInt32.ofNatCore 1 (by intlit))
          list_nth_mut_fwd T tl i0
    | list_t.ListNil => result.fail error.panic
  
  /- [paper::list_nth_mut] -/
  def list_nth_mut_back
    (T : Type) (l : list_t T) (i : UInt32) (ret0 : T) : result (list_t T) :=
    match l with
    | list_t.ListCons x tl =>
      if i = (UInt32.ofNatCore 0 (by intlit))
      then result.ret (list_t.ListCons ret0 tl)
      else
        do
          let i0 <- UInt32.checked_sub i (UInt32.ofNatCore 1 (by intlit))
          let tl0 <- list_nth_mut_back T tl i0 ret0
          result.ret (list_t.ListCons x tl0)
    | list_t.ListNil => result.fail error.panic
  
  /- [paper::sum] -/
  def sum_fwd (l : list_t Int32) : result Int32 :=
    match l with
    | list_t.ListCons x tl => do
                                let i <- sum_fwd tl
                                Int32.checked_add x i
    | list_t.ListNil => result.ret (Int32.ofNatCore 0 (by intlit))
  
  /- [paper::test_nth] -/
  def test_nth_fwd : result Unit :=
    do
      let l := list_t.ListNil
      let l0 := list_t.ListCons (Int32.ofNatCore 3 (by intlit)) l
      let l1 := list_t.ListCons (Int32.ofNatCore 2 (by intlit)) l0
      let x <-
        list_nth_mut_fwd Int32 (list_t.ListCons (Int32.ofNatCore 1 (by intlit))
          l1) (UInt32.ofNatCore 2 (by intlit))
      let x0 <- Int32.checked_add x (Int32.ofNatCore 1 (by intlit))
      let l2 <-
        list_nth_mut_back Int32 (list_t.ListCons
          (Int32.ofNatCore 1 (by intlit)) l1) (UInt32.ofNatCore 2 (by intlit))
          x0
      let i <- sum_fwd l2
      if not (i = (Int32.ofNatCore 7 (by intlit)))
      then result.fail error.panic
      else result.ret ()
  
  /- Unit test for [paper::test_nth] -/
  #assert (test_nth_fwd = .ret ())
  
  /- [paper::call_choose] -/
  def call_choose_fwd (p : (UInt32 × UInt32)) : result UInt32 :=
    do
      let (px, py) := p
      let pz <- choose_fwd UInt32 true px py
      let pz0 <- UInt32.checked_add pz (UInt32.ofNatCore 1 (by intlit))
      let (px0, _) <- choose_back UInt32 true px py pz0
      result.ret px0