src/HOL/Examples/Commands.thy
author wenzelm
Mon, 13 Jul 2020 17:08:45 +0200
changeset 72029 83456d9f0ed5
permissions -rw-r--r--
clarified examples;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
72029
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
     1
(*  Title:      HOL/Examples/Commands.thy
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
     3
*)
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
     4
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
     5
section \<open>Some Isar command definitions\<close>
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
     6
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
     7
theory Commands
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
     8
imports Main
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
     9
keywords
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    10
  "print_test" :: diag and
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    11
  "global_test" :: thy_decl and
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    12
  "local_test" :: thy_decl
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    13
begin
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    14
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    15
subsection \<open>Diagnostic command: no state change\<close>
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    16
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    17
ML \<open>
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    18
  Outer_Syntax.command \<^command_keyword>\<open>print_test\<close> "print term test"
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    19
    (Parse.term >> (fn s => Toplevel.keep (fn st =>
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    20
      let
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    21
        val ctxt = Toplevel.context_of st;
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    22
        val t = Syntax.read_term ctxt s;
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    23
        val ctxt' = Proof_Context.augment t ctxt;
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    24
      in Pretty.writeln (Syntax.pretty_term ctxt' t) end)));
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    25
\<close>
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    26
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    27
print_test x
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    28
print_test "\<lambda>x. x = a"
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    29
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    30
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    31
subsection \<open>Old-style global theory declaration\<close>
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    32
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    33
ML \<open>
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    34
  Outer_Syntax.command \<^command_keyword>\<open>global_test\<close> "test constant declaration"
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    35
    (Parse.binding >> (fn b => Toplevel.theory (fn thy =>
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    36
      let
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    37
        val thy' = Sign.add_consts [(b, \<^typ>\<open>'a\<close>, NoSyn)] thy;
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    38
      in thy' end)));
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    39
\<close>
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    40
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    41
global_test a
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    42
global_test b
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    43
print_test a
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    44
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    45
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    46
subsection \<open>Local theory specification\<close>
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    47
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    48
ML \<open>
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    49
  Outer_Syntax.local_theory \<^command_keyword>\<open>local_test\<close> "test local definition"
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    50
    (Parse.binding -- (\<^keyword>\<open>=\<close> |-- Parse.term) >> (fn (b, s) => fn lthy =>
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    51
      let
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    52
        val t = Syntax.read_term lthy s;
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    53
        val (def, lthy') = Local_Theory.define ((b, NoSyn), ((Thm.def_binding b, []), t)) lthy;
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    54
      in lthy' end));
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    55
\<close>
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    56
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    57
local_test true = True
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    58
print_test true
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    59
thm true_def
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    60
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    61
local_test identity = "\<lambda>x. x"
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    62
print_test "identity x"
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    63
thm identity_def
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    64
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    65
context fixes x y :: nat
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    66
begin
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    67
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    68
local_test test = "x + y"
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    69
print_test test
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    70
thm test_def
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    71
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    72
end
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    73
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    74
print_test "test 0 1"
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    75
thm test_def
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    76
83456d9f0ed5 clarified examples;
wenzelm
parents:
diff changeset
    77
end