Debian Codegraph @

doc/README/Debug/Trace.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- An example showing how the Debug.Trace module can be used
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --rewriting --guardedness #-}
8
9 module README.Debug.Trace where
10
11 ------------------------------------------------------------------------
12 -- Sometimes compiled code can contain bugs.
13
14 -- Whether caused by the compiler or present in the source code already, they
15 -- can be hard to track. A primitive debugging technique is to strategically
16 -- insert calls to tracing functions which will display their String argument
17 -- upon evaluation.
18
19 open import Data.String.Base using (_++_)
20 open import Debug.Trace
21
22 -- We can for instance add tracing messages to make sure an invariant is
23 -- respected or check in which order evaluation takes place in the backend
24 -- (which can inform our decision to use, or not, strictness primitives).
25
26 -- In the following example, we define a division operation on natural numbers
27 -- using the original dividend as the termination measure. We:
28
29 -- 1. check in the base case that when the fuel runs out then the updated dividend
30 -- is already zero.
31
32 -- 2. wrap the calls to _∸_ and go in respective calls to trace to see when all
33 -- of these thunks are forced: are we building a big thunk in go's second
34 -- argument or evaluating it as we go?
35
36 open import Data.Maybe.Base
37 open import Data.Nat.Base
38 open import Data.Nat.Show using (show)
39
40 div : ℕ → ℕ → Maybe ℕ
41 div m zero = nothing
42 div m n = just (go m m) where
43
44 -- invariants: m ≤ fuel
45 -- result : m / n
46 go : (fuel : ℕ) (m : ℕ) → ℕ
47 go zero m = trace ("Invariant: " ++ show m ++ " should be zero.") zero
48 go (suc fuel) m =
49 let m′ = trace ("Thunk for step " ++ show fuel ++ " forced") (m ∸ n) in
50 trace ("Recursive call for step " ++ show fuel) (suc (go fuel m′))
51
52 -- To observe the behaviour of this code, we need to compile it and run it.
53 -- To run it, we need a main function. We define a very basic one: run div,
54 -- and display its result if the run was successful.
55
56 -- We add two calls to trace to see when div is evaluated and when the returned
57 -- number is forced (by a call to show).
58
59 open import Level using (0ℓ)
60 open import IO
61
62 main : Main
63 main =
64 let r = trace "Call to div" (div 4 2)
65 j = λ n → trace "Forcing the result wrapped in just." (putStrLn (show n)) in
66 run (maybe′ j (pure _) r)
67
68 -- We get the following trace where we can see that checking that the
69 -- maybe-solution is just-headed does not force the natural number. Once forced,
70 -- we observe that we indeed build a big thunk on go's second argument (all the
71 -- recursive calls happen first and then we force the thunks one by one).
72
73 -- Call to div
74 -- Forcing the result wrapped in just.
75 -- Recursive call for step 3
76 -- Recursive call for step 2
77 -- Recursive call for step 1
78 -- Recursive call for step 0
79 -- Thunk for step 0 forced
80 -- Thunk for step 1 forced
81 -- Thunk for step 2 forced
82 -- Thunk for step 3 forced
83 -- Invariant: 0 should be zero.
84 -- 4
85
86 -- We also notice that the result is incorrect: 4/2 is 2 and not 4. We quickly
87 -- notice that (div m (suc n)) will perform m recursive calls no matter what.
88 -- And at each call it will put add 1. We can fix this bug by adding a new first
89 -- equation to go:
90
91 -- go fuel zero = zero
92
93 -- Running the example again we observe that because we now need to check
94 -- whether go's second argument is zero, the function is more strict: we see
95 -- that recursive calls and thunk forcings are interleaved.
96
97 -- Call to div
98 -- Forcing the result wrapped in just.
99 -- Recursive call for step 3
100 -- Thunk for step 3 forced
101 -- Recursive call for step 2
102 -- Thunk for step 2 forced
103 -- 2