ProgTutorial/Recipes/Timing.thy
author Norbert Schirmer <norbert.schirmer@web.de>
Tue, 14 May 2019 17:10:47 +0200
changeset 565 cecd7a941885
parent 544 501491d56798
child 569 f875a25aa72d
permissions -rw-r--r--
isabelle update_cartouches -t
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
154
e81ebb37aa83 updated to repository version; added a section about timing
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
theory Timing
346
0fea8b7a14a1 tuned the ML-output mechanism; tuned slightly the text
Christian Urban <urbanc@in.tum.de>
parents: 239
diff changeset
     2
imports "../Appendix"
154
e81ebb37aa83 updated to repository version; added a section about timing
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
begin
e81ebb37aa83 updated to repository version; added a section about timing
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 544
diff changeset
     5
section \<open>Measuring Time\label{rec:timing}\<close> 
154
e81ebb37aa83 updated to repository version; added a section about timing
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 544
diff changeset
     7
text \<open>
154
e81ebb37aa83 updated to repository version; added a section about timing
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
 {\bf Problem:}
e81ebb37aa83 updated to repository version; added a section about timing
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
  You want to measure the running time of a tactic or function.\smallskip
e81ebb37aa83 updated to repository version; added a section about timing
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
e81ebb37aa83 updated to repository version; added a section about timing
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
  {\bf Solution:} Time can be measured using the function 
460
5c33c4b52ad7 Updated to changes in timing structure
Christian Urban <urbanc@in.tum.de>
parents: 456
diff changeset
    12
  @{ML start in Timing} and @{ML result in Timing}.\smallskip
154
e81ebb37aa83 updated to repository version; added a section about timing
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
191
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
    14
  Suppose you defined the Ackermann function on the Isabelle level. 
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 544
diff changeset
    15
\<close>
154
e81ebb37aa83 updated to repository version; added a section about timing
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
155
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    17
fun 
168
009ca4807baa polished somewhat the recipes and solutions
Christian Urban <urbanc@in.tum.de>
parents: 167
diff changeset
    18
 ackermann:: "(nat \<times> nat) \<Rightarrow> nat"
155
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    19
where
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    20
    "ackermann (0, n) = n + 1"
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    21
  | "ackermann (m, 0) = ackermann (m - 1, 1)"
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    22
  | "ackermann (m, n) = ackermann (m - 1, ackermann (m, n - 1))"
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    23
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 544
diff changeset
    24
text \<open>
168
009ca4807baa polished somewhat the recipes and solutions
Christian Urban <urbanc@in.tum.de>
parents: 167
diff changeset
    25
  You can measure how long the simplifier takes to verify a datapoint
191
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
    26
  of this function. The actual timing is done inside the
175
7c09bd3227c5 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 168
diff changeset
    27
  wrapper function:
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 544
diff changeset
    28
\<close>
167
3e30ea95c7aa added temporarily some timing test about conversions and simprocs
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
    29
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 544
diff changeset
    30
ML %linenosgray\<open>fun timing_wrapper tac st =
167
3e30ea95c7aa added temporarily some timing test about conversions and simprocs
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
    31
let 
460
5c33c4b52ad7 Updated to changes in timing structure
Christian Urban <urbanc@in.tum.de>
parents: 456
diff changeset
    32
  val t_start = Timing.start ();
167
3e30ea95c7aa added temporarily some timing test about conversions and simprocs
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
    33
  val res = tac st;
460
5c33c4b52ad7 Updated to changes in timing structure
Christian Urban <urbanc@in.tum.de>
parents: 456
diff changeset
    34
  val t_end = Timing.result t_start;
167
3e30ea95c7aa added temporarily some timing test about conversions and simprocs
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
    35
in
460
5c33c4b52ad7 Updated to changes in timing structure
Christian Urban <urbanc@in.tum.de>
parents: 456
diff changeset
    36
  (writeln (Timing.message t_end); res)
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 544
diff changeset
    37
end\<close>
167
3e30ea95c7aa added temporarily some timing test about conversions and simprocs
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
    38
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 544
diff changeset
    39
text \<open>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 544
diff changeset
    40
  Note that this function, in addition to a tactic, also takes a state \<open>st\<close> as argument and applies this state to the tactic (Line 4). The reason is that
175
7c09bd3227c5 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 168
diff changeset
    41
  tactics are lazy functions and you need to force them to run, otherwise the
191
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
    42
  timing will be meaningless. The simplifier tactic, amongst others,  can be 
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
    43
  forced to run by just applying the state to it. But ``fully'' lazy tactics,
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
    44
  such as @{ML "resolve_tac"}, need even more ``standing-on-ones-head'' to force
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
    45
  them to run. 
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
    46
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
    47
  The time between start and finish of the simplifier will be calculated 
0150cf5982ae polished some recipies
Christian Urban <urbanc@in.tum.de>
parents: 189
diff changeset
    48
  as the end time minus the start time.  An example of the
175
7c09bd3227c5 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 168
diff changeset
    49
  wrapper is the proof
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 544
diff changeset
    50
\<close>
154
e81ebb37aa83 updated to repository version; added a section about timing
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
155
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    52
lemma "ackermann (3, 4) = 125"
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 544
diff changeset
    53
apply(tactic \<open>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 544
diff changeset
    54
  timing_wrapper (simp_tac (@{context} addsimps @{thms "eval_nat_numeral"}) 1)\<close>)
155
Christian Urban <urbanc@in.tum.de>
parents: 154
diff changeset
    55
done
154
e81ebb37aa83 updated to repository version; added a section about timing
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 544
diff changeset
    57
text \<open>
185
043ef82000b4 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 175
diff changeset
    58
  where it returns something on the scale of 3 seconds. We chose to return
168
009ca4807baa polished somewhat the recipes and solutions
Christian Urban <urbanc@in.tum.de>
parents: 167
diff changeset
    59
  this information as a string, but the timing information is also accessible
009ca4807baa polished somewhat the recipes and solutions
Christian Urban <urbanc@in.tum.de>
parents: 167
diff changeset
    60
  in number format.
167
3e30ea95c7aa added temporarily some timing test about conversions and simprocs
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
    61
3e30ea95c7aa added temporarily some timing test about conversions and simprocs
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
    62
  \begin{readmore}
3e30ea95c7aa added temporarily some timing test about conversions and simprocs
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
    63
  Basic functions regarding timing are defined in @{ML_file 
460
5c33c4b52ad7 Updated to changes in timing structure
Christian Urban <urbanc@in.tum.de>
parents: 456
diff changeset
    64
  "Pure/General/timing.ML"} (for the PolyML compiler). Some more
167
3e30ea95c7aa added temporarily some timing test about conversions and simprocs
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
    65
  advanced functions are defined in @{ML_file "Pure/General/output.ML"}.
3e30ea95c7aa added temporarily some timing test about conversions and simprocs
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
    66
  \end{readmore}
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 544
diff changeset
    67
\<close>
167
3e30ea95c7aa added temporarily some timing test about conversions and simprocs
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
    68
3e30ea95c7aa added temporarily some timing test about conversions and simprocs
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
    69
3e30ea95c7aa added temporarily some timing test about conversions and simprocs
Christian Urban <urbanc@in.tum.de>
parents: 157
diff changeset
    70
456
89fccd3d5055 'nat_number' was renamed to 'eval_nat_numeral'
griff
parents: 346
diff changeset
    71
end