src/HOL/Import/import_rule.ML
author wenzelm
Fri, 17 Jan 2025 11:05:36 +0100
changeset 81841 c84c0c0e6907
parent 81840 345e592792fd
child 81844 f9d0d2ca2402
permissions -rw-r--r--
clarified pattern via antiquotations;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
     1
(*  Title:      HOL/Import/import_rule.ML
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
     2
    Author:     Cezary Kaliszyk, University of Innsbruck
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
     3
    Author:     Alexander Krauss, QAware GmbH
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
     4
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
     5
Importer proof rules and processing of lines and files.
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
     6
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
     7
Based on earlier code by Steven Obua and Sebastian Skalberg.
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
     8
*)
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
     9
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    10
signature IMPORT_RULE =
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    11
sig
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    12
  val beta : cterm -> thm
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    13
  val eq_mp : thm -> thm -> thm
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    14
  val comb : thm -> thm -> thm
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    15
  val trans : thm -> thm -> thm
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    16
  val deduct : thm -> thm -> thm
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    17
  val conj1 : thm -> thm
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    18
  val conj2 : thm -> thm
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    19
  val refl : cterm -> thm
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    20
  val abs : cterm -> thm -> thm
81835
35abb6dd8bd2 clarified signature: more standard Isabelle/ML;
wenzelm
parents: 81831
diff changeset
    21
  val mdef : theory -> string -> thm
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    22
  val def : string -> cterm -> theory -> thm * theory
81835
35abb6dd8bd2 clarified signature: more standard Isabelle/ML;
wenzelm
parents: 81831
diff changeset
    23
  val mtydef : theory -> string -> thm
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    24
  val tydef :
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    25
    string -> string -> string -> cterm -> cterm -> thm -> theory -> thm * theory
81835
35abb6dd8bd2 clarified signature: more standard Isabelle/ML;
wenzelm
parents: 81831
diff changeset
    26
  val inst_type : theory -> (ctyp * ctyp) list -> thm -> thm
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    27
  val inst : (cterm * cterm) list -> thm -> thm
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    28
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    29
  type state
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    30
  val init_state : state
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    31
  val process_line : string -> (theory * state) -> (theory * state)
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    32
  val process_file : Path.T -> theory -> theory
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    33
end
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    34
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    35
structure Import_Rule: IMPORT_RULE =
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    36
struct
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    37
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    38
val init_state = ((Inttab.empty, 0), (Inttab.empty, 0), (Inttab.empty, 0))
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    39
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    40
type state = (ctyp Inttab.table * int) * (cterm Inttab.table * int) * (thm Inttab.table * int)
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    41
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    42
fun implies_elim_all th = implies_elim_list th (map Thm.assume (cprems_of th))
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    43
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    44
fun meta_mp th1 th2 =
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    45
  let
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    46
    val th1a = implies_elim_all th1
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 58960
diff changeset
    47
    val th1b = Thm.implies_intr (strip_imp_concl (Thm.cprop_of th2)) th1a
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    48
    val th2a = implies_elim_all th2
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    49
    val th3 = Thm.implies_elim th1b th2a
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    50
  in
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    51
    implies_intr_hyps th3
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    52
  end
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    53
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    54
fun meta_eq_to_obj_eq th =
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    55
  let
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 58960
diff changeset
    56
    val (tml, tmr) = Thm.dest_binop (strip_imp_concl (Thm.cprop_of th))
59586
ddf6deaadfe8 clarified signature;
wenzelm
parents: 59582
diff changeset
    57
    val cty = Thm.ctyp_of_cterm tml
60801
7664e0916eec tuned signature;
wenzelm
parents: 60754
diff changeset
    58
    val i = Thm.instantiate' [SOME cty] [SOME tml, SOME tmr]
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    59
      @{thm meta_eq_to_obj_eq}
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    60
  in
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    61
    Thm.implies_elim i th
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    62
  end
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    63
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    64
fun beta ct = meta_eq_to_obj_eq (Thm.beta_conversion false ct)
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    65
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    66
fun eq_mp th1 th2 =
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    67
  let
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 58960
diff changeset
    68
    val (tm1l, tm1r) = Thm.dest_binop (Thm.dest_arg (strip_imp_concl (Thm.cprop_of th1)))
60801
7664e0916eec tuned signature;
wenzelm
parents: 60754
diff changeset
    69
    val i1 = Thm.instantiate' [] [SOME tm1l, SOME tm1r] @{thm iffD1}
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    70
    val i2 = meta_mp i1 th1
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    71
  in
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    72
    meta_mp i2 th2
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    73
  end
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    74
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    75
fun comb th1 th2 =
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    76
  let
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 58960
diff changeset
    77
    val t1c = Thm.dest_arg (strip_imp_concl (Thm.cprop_of th1))
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 58960
diff changeset
    78
    val t2c = Thm.dest_arg (strip_imp_concl (Thm.cprop_of th2))
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    79
    val (cf, cg) = Thm.dest_binop t1c
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    80
    val (cx, cy) = Thm.dest_binop t2c
59586
ddf6deaadfe8 clarified signature;
wenzelm
parents: 59582
diff changeset
    81
    val [fd, fr] = Thm.dest_ctyp (Thm.ctyp_of_cterm cf)
60801
7664e0916eec tuned signature;
wenzelm
parents: 60754
diff changeset
    82
    val i1 = Thm.instantiate' [SOME fd, SOME fr]
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    83
      [SOME cf, SOME cg, SOME cx, SOME cy] @{thm cong}
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    84
    val i2 = meta_mp i1 th1
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    85
  in
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    86
    meta_mp i2 th2
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    87
  end
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    88
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    89
fun trans th1 th2 =
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    90
  let
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 58960
diff changeset
    91
    val t1c = Thm.dest_arg (strip_imp_concl (Thm.cprop_of th1))
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 58960
diff changeset
    92
    val t2c = Thm.dest_arg (strip_imp_concl (Thm.cprop_of th2))
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    93
    val (r, s) = Thm.dest_binop t1c
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    94
    val (_, t) = Thm.dest_binop t2c
59586
ddf6deaadfe8 clarified signature;
wenzelm
parents: 59582
diff changeset
    95
    val ty = Thm.ctyp_of_cterm r
60801
7664e0916eec tuned signature;
wenzelm
parents: 60754
diff changeset
    96
    val i1 = Thm.instantiate' [SOME ty] [SOME r, SOME s, SOME t] @{thm trans}
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    97
    val i2 = meta_mp i1 th1
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    98
  in
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    99
    meta_mp i2 th2
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   100
  end
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   101
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   102
fun deduct th1 th2 =
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   103
  let
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 58960
diff changeset
   104
    val th1c = strip_imp_concl (Thm.cprop_of th1)
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 58960
diff changeset
   105
    val th2c = strip_imp_concl (Thm.cprop_of th2)
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   106
    val th1a = implies_elim_all th1
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   107
    val th2a = implies_elim_all th2
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   108
    val th1b = Thm.implies_intr th2c th1a
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   109
    val th2b = Thm.implies_intr th1c th2a
60801
7664e0916eec tuned signature;
wenzelm
parents: 60754
diff changeset
   110
    val i = Thm.instantiate' []
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   111
      [SOME (Thm.dest_arg th1c), SOME (Thm.dest_arg th2c)] @{thm iffI}
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 58960
diff changeset
   112
    val i1 = Thm.implies_elim i (Thm.assume (Thm.cprop_of th2b))
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   113
    val i2 = Thm.implies_elim i1 th1b
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 58960
diff changeset
   114
    val i3 = Thm.implies_intr (Thm.cprop_of th2b) i2
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   115
    val i4 = Thm.implies_elim i3 th2b
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   116
  in
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   117
    implies_intr_hyps i4
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   118
  end
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   119
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   120
fun conj1 th =
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   121
  let
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 58960
diff changeset
   122
    val (tml, tmr) = Thm.dest_binop (Thm.dest_arg (strip_imp_concl (Thm.cprop_of th)))
60801
7664e0916eec tuned signature;
wenzelm
parents: 60754
diff changeset
   123
    val i = Thm.instantiate' [] [SOME tml, SOME tmr] @{thm conjunct1}
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   124
  in
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   125
    meta_mp i th
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   126
  end
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   127
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   128
fun conj2 th =
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   129
  let
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 58960
diff changeset
   130
    val (tml, tmr) = Thm.dest_binop (Thm.dest_arg (strip_imp_concl (Thm.cprop_of th)))
60801
7664e0916eec tuned signature;
wenzelm
parents: 60754
diff changeset
   131
    val i = Thm.instantiate' [] [SOME tml, SOME tmr] @{thm conjunct2}
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   132
  in
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   133
    meta_mp i th
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   134
  end
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   135
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   136
fun refl ctm =
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   137
  let
59586
ddf6deaadfe8 clarified signature;
wenzelm
parents: 59582
diff changeset
   138
    val cty = Thm.ctyp_of_cterm ctm
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   139
  in
60801
7664e0916eec tuned signature;
wenzelm
parents: 60754
diff changeset
   140
    Thm.instantiate' [SOME cty] [SOME ctm] @{thm refl}
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   141
  end
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   142
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   143
fun abs cv th =
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   144
  let
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   145
    val th1 = implies_elim_all th
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 58960
diff changeset
   146
    val (tl, tr) = Thm.dest_binop (Thm.dest_arg (strip_imp_concl (Thm.cprop_of th1)))
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   147
    val (ll, lr) = (Thm.lambda cv tl, Thm.lambda cv tr)
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   148
    val (al, ar) = (Thm.apply ll cv, Thm.apply lr cv)
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   149
    val bl = beta al
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   150
    val br = meta_eq_to_obj_eq (Thm.symmetric (Thm.beta_conversion false ar))
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   151
    val th2 = trans (trans bl th1) br
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   152
    val th3 = implies_elim_all th2
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   153
    val th4 = Thm.forall_intr cv th3
60801
7664e0916eec tuned signature;
wenzelm
parents: 60754
diff changeset
   154
    val i = Thm.instantiate' [SOME (Thm.ctyp_of_cterm cv), SOME (Thm.ctyp_of_cterm tl)]
81829
ca1ad6660b4a tuned: prefer inlined thms;
wenzelm
parents: 81521
diff changeset
   155
      [SOME ll, SOME lr] @{lemma "(\<And>x. f x = g x) \<Longrightarrow> f = g" by (rule ext)}
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   156
  in
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   157
    meta_mp i th4
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   158
  end
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   159
60648
6c4550cd1b17 clarified context;
wenzelm
parents: 60642
diff changeset
   160
fun freezeT thy thm =
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   161
  let
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 58960
diff changeset
   162
    val tvars = Term.add_tvars (Thm.prop_of thm) []
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   163
    val tfrees = map (fn ((t, _), s) => TFree (t, s)) tvars
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   164
  in
74282
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 70303
diff changeset
   165
    Thm.instantiate (TVars.make (tvars ~~ map (Thm.global_ctyp_of thy) tfrees), Vars.empty) thm
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   166
  end
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   167
81840
345e592792fd misc tuning and clarification;
wenzelm
parents: 81839
diff changeset
   168
fun def' c rhs thy =
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   169
  let
81840
345e592792fd misc tuning and clarification;
wenzelm
parents: 81839
diff changeset
   170
    val b = Binding.name c
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   171
    val typ = type_of rhs
81840
345e592792fd misc tuning and clarification;
wenzelm
parents: 81839
diff changeset
   172
    val thy1 = Sign.add_consts [(b, typ, NoSyn)] thy
345e592792fd misc tuning and clarification;
wenzelm
parents: 81839
diff changeset
   173
    val eq = Logic.mk_equals (Const (Sign.full_name thy1 b, typ), rhs)
345e592792fd misc tuning and clarification;
wenzelm
parents: 81839
diff changeset
   174
    val (thm, thy2) = Global_Theory.add_def (Binding.suffix_name "_hldef" b, eq) thy1
79336
032a31db4c6f clarified signature: downgrade old-style Global_Theory.add_defs to Global_Theory.add_def without attributes;
wenzelm
parents: 75616
diff changeset
   175
    val def_thm = freezeT thy1 thm
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   176
  in
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   177
    (meta_eq_to_obj_eq def_thm, thy2)
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   178
  end
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   179
81835
35abb6dd8bd2 clarified signature: more standard Isabelle/ML;
wenzelm
parents: 81831
diff changeset
   180
fun mdef thy name =
35abb6dd8bd2 clarified signature: more standard Isabelle/ML;
wenzelm
parents: 81831
diff changeset
   181
  case Import_Data.get_const_def thy name of
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   182
    SOME th => th
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   183
  | NONE => error ("constant mapped but no definition: " ^ name)
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   184
81840
345e592792fd misc tuning and clarification;
wenzelm
parents: 81839
diff changeset
   185
fun def c rhs thy =
345e592792fd misc tuning and clarification;
wenzelm
parents: 81839
diff changeset
   186
  case Import_Data.get_const_def thy c of
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   187
    SOME _ =>
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   188
      let
81840
345e592792fd misc tuning and clarification;
wenzelm
parents: 81839
diff changeset
   189
        val () = warning ("Const mapped but def provided: " ^ c)
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   190
      in
81840
345e592792fd misc tuning and clarification;
wenzelm
parents: 81839
diff changeset
   191
        (mdef thy c, thy)
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   192
      end
81840
345e592792fd misc tuning and clarification;
wenzelm
parents: 81839
diff changeset
   193
  | NONE => def' c (Thm.term_of rhs) thy
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   194
81829
ca1ad6660b4a tuned: prefer inlined thms;
wenzelm
parents: 81521
diff changeset
   195
fun typedef_hol2hollight nty oty rep abs pred a r =
ca1ad6660b4a tuned: prefer inlined thms;
wenzelm
parents: 81521
diff changeset
   196
  Thm.instantiate' [SOME nty, SOME oty] [SOME rep, SOME abs, SOME pred, SOME a, SOME r]
ca1ad6660b4a tuned: prefer inlined thms;
wenzelm
parents: 81521
diff changeset
   197
    @{lemma "type_definition Rep Abs (Collect P) \<Longrightarrow> Abs (Rep a) = a \<and> P r = (Rep (Abs r) = r)"
ca1ad6660b4a tuned: prefer inlined thms;
wenzelm
parents: 81521
diff changeset
   198
        by (metis type_definition.Rep_inverse type_definition.Abs_inverse
ca1ad6660b4a tuned: prefer inlined thms;
wenzelm
parents: 81521
diff changeset
   199
              type_definition.Rep mem_Collect_eq)}
ca1ad6660b4a tuned: prefer inlined thms;
wenzelm
parents: 81521
diff changeset
   200
81835
35abb6dd8bd2 clarified signature: more standard Isabelle/ML;
wenzelm
parents: 81831
diff changeset
   201
fun typedef_hollight thy th =
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   202
  let
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 58960
diff changeset
   203
    val (th_s, cn) = Thm.dest_comb (Thm.dest_arg (Thm.cprop_of th))
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   204
    val (th_s, abst) = Thm.dest_comb th_s
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   205
    val rept = Thm.dest_arg th_s
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   206
    val P = Thm.dest_arg cn
59586
ddf6deaadfe8 clarified signature;
wenzelm
parents: 59582
diff changeset
   207
    val [nty, oty] = Thm.dest_ctyp (Thm.ctyp_of_cterm rept)
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   208
  in
81829
ca1ad6660b4a tuned: prefer inlined thms;
wenzelm
parents: 81521
diff changeset
   209
    typedef_hol2hollight nty oty rept abst P
ca1ad6660b4a tuned: prefer inlined thms;
wenzelm
parents: 81521
diff changeset
   210
      (Thm.global_cterm_of thy (Free ("a", Thm.typ_of nty)))
ca1ad6660b4a tuned: prefer inlined thms;
wenzelm
parents: 81521
diff changeset
   211
      (Thm.global_cterm_of thy (Free ("r", Thm.typ_of oty)))
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   212
  end
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   213
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   214
fun tydef' tycname abs_name rep_name cP ct td_th thy =
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   215
  let
59586
ddf6deaadfe8 clarified signature;
wenzelm
parents: 59582
diff changeset
   216
    val ctT = Thm.ctyp_of_cterm ct
81829
ca1ad6660b4a tuned: prefer inlined thms;
wenzelm
parents: 81521
diff changeset
   217
    val nonempty = Thm.instantiate' [SOME ctT] [SOME cP, SOME ct]
ca1ad6660b4a tuned: prefer inlined thms;
wenzelm
parents: 81521
diff changeset
   218
      @{lemma "P t \<Longrightarrow> \<exists>x. x \<in> Collect P" by auto}
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   219
    val th2 = meta_mp nonempty td_th
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 58960
diff changeset
   220
    val c =
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 58960
diff changeset
   221
      case Thm.concl_of th2 of
81841
c84c0c0e6907 clarified pattern via antiquotations;
wenzelm
parents: 81840
diff changeset
   222
        \<^Const_>\<open>Trueprop for \<^Const_>\<open>Ex _ for \<open>Abs (_, _, \<^Const_>\<open>Set.member _ for _ c\<close>)\<close>\<close>\<close> => c
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   223
      | _ => error "type_introduction: bad type definition theorem"
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   224
    val tfrees = Term.add_tfrees c []
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   225
    val tnames = sort_strings (map fst tfrees)
61110
6b7c2ecc6aea more general Typedef.bindings;
wenzelm
parents: 60801
diff changeset
   226
    val typedef_bindings =
62513
702085ca8564 take qualification of type name more seriously: derived consts and facts are qualified uniformly;
wenzelm
parents: 62436
diff changeset
   227
     {Rep_name = Binding.name rep_name,
702085ca8564 take qualification of type name more seriously: derived consts and facts are qualified uniformly;
wenzelm
parents: 62436
diff changeset
   228
      Abs_name = Binding.name abs_name,
702085ca8564 take qualification of type name more seriously: derived consts and facts are qualified uniformly;
wenzelm
parents: 62436
diff changeset
   229
      type_definition_name = Binding.name ("type_definition_" ^ tycname)}
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   230
    val ((_, typedef_info), thy') =
69829
3bfa28b3a5b2 streamlined specification interfaces
haftmann
parents: 69597
diff changeset
   231
     Named_Target.theory_map_result (apsnd o Typedef.transform_info)
3bfa28b3a5b2 streamlined specification interfaces
haftmann
parents: 69597
diff changeset
   232
     (Typedef.add_typedef {overloaded = false}
61260
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61110
diff changeset
   233
       (Binding.name tycname, map (rpair dummyS) tnames, NoSyn) c
69829
3bfa28b3a5b2 streamlined specification interfaces
haftmann
parents: 69597
diff changeset
   234
       (SOME typedef_bindings) (fn ctxt => resolve_tac ctxt [th2] 1)) thy
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   235
    val aty = #abs_type (#1 typedef_info)
60648
6c4550cd1b17 clarified context;
wenzelm
parents: 60642
diff changeset
   236
    val th = freezeT thy' (#type_definition (#2 typedef_info))
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 58960
diff changeset
   237
    val (th_s, _) = Thm.dest_comb (Thm.dest_arg (Thm.cprop_of th))
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   238
    val (th_s, abst) = Thm.dest_comb th_s
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   239
    val rept = Thm.dest_arg th_s
59586
ddf6deaadfe8 clarified signature;
wenzelm
parents: 59582
diff changeset
   240
    val [nty, oty] = Thm.dest_ctyp (Thm.ctyp_of_cterm rept)
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   241
    val typedef_th =
81829
ca1ad6660b4a tuned: prefer inlined thms;
wenzelm
parents: 81521
diff changeset
   242
      typedef_hol2hollight nty oty rept abst cP
ca1ad6660b4a tuned: prefer inlined thms;
wenzelm
parents: 81521
diff changeset
   243
        (Thm.global_cterm_of thy' (Free ("a", aty)))
ca1ad6660b4a tuned: prefer inlined thms;
wenzelm
parents: 81521
diff changeset
   244
        (Thm.global_cterm_of thy' (Free ("r", Thm.typ_of ctT)))
47363
c7fc95e722ff HOL/Import typed matches against Isabelle typedef result
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents: 47258
diff changeset
   245
    val th4 = typedef_th OF [#type_definition (#2 typedef_info)]
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   246
  in
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   247
    (th4, thy')
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   248
  end
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   249
81835
35abb6dd8bd2 clarified signature: more standard Isabelle/ML;
wenzelm
parents: 81831
diff changeset
   250
fun mtydef thy name =
35abb6dd8bd2 clarified signature: more standard Isabelle/ML;
wenzelm
parents: 81831
diff changeset
   251
  case Import_Data.get_typ_def thy name of
35abb6dd8bd2 clarified signature: more standard Isabelle/ML;
wenzelm
parents: 81831
diff changeset
   252
    SOME thn => meta_mp (typedef_hollight thy thn) thn
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   253
  | NONE => error ("type mapped but no tydef thm registered: " ^ name)
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   254
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   255
fun tydef tycname abs_name rep_name P t td_th thy =
81835
35abb6dd8bd2 clarified signature: more standard Isabelle/ML;
wenzelm
parents: 81831
diff changeset
   256
  case Import_Data.get_typ_def thy tycname of
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   257
    SOME _ =>
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   258
      let
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   259
        val () = warning ("Type mapped but proofs provided: " ^ tycname)
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   260
      in
81835
35abb6dd8bd2 clarified signature: more standard Isabelle/ML;
wenzelm
parents: 81831
diff changeset
   261
        (mtydef thy tycname, thy)
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   262
      end
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   263
  | NONE => tydef' tycname abs_name rep_name P t td_th thy
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   264
81835
35abb6dd8bd2 clarified signature: more standard Isabelle/ML;
wenzelm
parents: 81831
diff changeset
   265
fun inst_type thy lambda th =
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   266
  let
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   267
    fun assoc _ [] = error "assoc"
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   268
      | assoc x ((x',y)::rest) = if x = x' then y else assoc x rest
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 58960
diff changeset
   269
    val lambda = map (fn (a, b) => (Thm.typ_of a, b)) lambda
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 58960
diff changeset
   270
    val tys_before = Term.add_tfrees (Thm.prop_of th) []
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   271
    val th1 = Thm.varifyT_global th
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 58960
diff changeset
   272
    val tys_after = Term.add_tvars (Thm.prop_of th1) []
60642
48dd1cefb4ae simplified Thm.instantiate and derivatives: the LHS refers to non-certified variables -- this merely serves as index into already certified structures (or is ignored);
wenzelm
parents: 60367
diff changeset
   273
    val tyinst =
48dd1cefb4ae simplified Thm.instantiate and derivatives: the LHS refers to non-certified variables -- this merely serves as index into already certified structures (or is ignored);
wenzelm
parents: 60367
diff changeset
   274
      map2 (fn bef => fn iS =>
48dd1cefb4ae simplified Thm.instantiate and derivatives: the LHS refers to non-certified variables -- this merely serves as index into already certified structures (or is ignored);
wenzelm
parents: 60367
diff changeset
   275
        (case try (assoc (TFree bef)) lambda of
48dd1cefb4ae simplified Thm.instantiate and derivatives: the LHS refers to non-certified variables -- this merely serves as index into already certified structures (or is ignored);
wenzelm
parents: 60367
diff changeset
   276
          SOME cty => (iS, cty)
48dd1cefb4ae simplified Thm.instantiate and derivatives: the LHS refers to non-certified variables -- this merely serves as index into already certified structures (or is ignored);
wenzelm
parents: 60367
diff changeset
   277
        | NONE => (iS, Thm.global_ctyp_of thy (TFree bef))))
48dd1cefb4ae simplified Thm.instantiate and derivatives: the LHS refers to non-certified variables -- this merely serves as index into already certified structures (or is ignored);
wenzelm
parents: 60367
diff changeset
   278
      tys_before tys_after
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   279
  in
74282
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 70303
diff changeset
   280
    Thm.instantiate (TVars.make tyinst, Vars.empty) th1
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   281
  end
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   282
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   283
fun inst sigma th =
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   284
  let
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   285
    val (dom, rng) = ListPair.unzip (rev sigma)
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   286
  in
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   287
    th |> forall_intr_list dom
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   288
       |> forall_elim_list rng
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   289
  end
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   290
81831
4bb6c49ef791 clarified signature: more explicit operations;
wenzelm
parents: 81830
diff changeset
   291
val make_name = String.translate (fn #"." => "dot" | c => Char.toString c)
4bb6c49ef791 clarified signature: more explicit operations;
wenzelm
parents: 81830
diff changeset
   292
4bb6c49ef791 clarified signature: more explicit operations;
wenzelm
parents: 81830
diff changeset
   293
fun make_free (x, ty) = Free (make_name x, ty)
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   294
81831
4bb6c49ef791 clarified signature: more explicit operations;
wenzelm
parents: 81830
diff changeset
   295
fun make_tfree a =
4bb6c49ef791 clarified signature: more explicit operations;
wenzelm
parents: 81830
diff changeset
   296
  let val b = "'" ^ String.translate (fn #"?" => "t" | c => Char.toString c) a
4bb6c49ef791 clarified signature: more explicit operations;
wenzelm
parents: 81830
diff changeset
   297
  in TFree (b, \<^sort>\<open>type\<close>) end
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   298
81837
wenzelm
parents: 81835
diff changeset
   299
fun make_type thy (c, args) =
wenzelm
parents: 81835
diff changeset
   300
  let
wenzelm
parents: 81835
diff changeset
   301
    val d =
wenzelm
parents: 81835
diff changeset
   302
      (case Import_Data.get_typ_map thy c of
wenzelm
parents: 81835
diff changeset
   303
        SOME d => d
81839
wenzelm
parents: 81838
diff changeset
   304
      | NONE => Sign.full_bname thy (make_name c))
81837
wenzelm
parents: 81835
diff changeset
   305
  in Type (d, args) end
wenzelm
parents: 81835
diff changeset
   306
81831
4bb6c49ef791 clarified signature: more explicit operations;
wenzelm
parents: 81830
diff changeset
   307
fun make_const thy (c, ty) =
4bb6c49ef791 clarified signature: more explicit operations;
wenzelm
parents: 81830
diff changeset
   308
  let
4bb6c49ef791 clarified signature: more explicit operations;
wenzelm
parents: 81830
diff changeset
   309
    val d =
81835
35abb6dd8bd2 clarified signature: more standard Isabelle/ML;
wenzelm
parents: 81831
diff changeset
   310
      (case Import_Data.get_const_map thy c of
81831
4bb6c49ef791 clarified signature: more explicit operations;
wenzelm
parents: 81830
diff changeset
   311
        SOME d => d
81839
wenzelm
parents: 81838
diff changeset
   312
      | NONE => Sign.full_bname thy (make_name c))
81831
4bb6c49ef791 clarified signature: more explicit operations;
wenzelm
parents: 81830
diff changeset
   313
  in Const (d, ty) end
4bb6c49ef791 clarified signature: more explicit operations;
wenzelm
parents: 81830
diff changeset
   314
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   315
fun get (map, no) s =
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   316
  case Int.fromString s of
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   317
    NONE => error "Import_Rule.get: not a number"
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   318
  | SOME i => (case Inttab.lookup map (Int.abs i) of
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   319
      NONE => error "Import_Rule.get: lookup failed"
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   320
    | SOME res => (res, (if i < 0 then Inttab.delete (Int.abs i) map else map, no)))
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   321
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   322
fun getty i (thy, (tyi, tmi, thi)) = let val (i, tyi) = (get tyi i) in (i, (thy, (tyi, tmi, thi))) end
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   323
fun gettm i (thy, (tyi, tmi, thi)) = let val (i, tmi) = (get tmi i) in (i, (thy, (tyi, tmi, thi))) end
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   324
fun getth i (thy, (tyi, tmi, thi)) = let val (i, thi) = (get thi i) in (i, (thy, (tyi, tmi, thi))) end
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   325
fun set (map, no) v = (Inttab.update_new (no + 1, v) map, no + 1)
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   326
fun setty v (thy, (tyi, tmi, thi)) = (thy, (set tyi v, tmi, thi))
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   327
fun settm v (thy, (tyi, tmi, thi)) = (thy, (tyi, set tmi v, thi))
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   328
fun setth v (thy, (tyi, tmi, thi)) = (thy, (tyi, tmi, set thi v))
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   329
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   330
fun last_thm (_, _, (map, no)) =
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   331
  case Inttab.lookup map no of
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   332
    NONE => error "Import_Rule.last_thm: lookup failed"
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   333
  | SOME thm => thm
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   334
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   335
fun listLast (h1 :: (h2 :: t)) = apfst (fn t => h1 :: h2 :: t) (listLast t)
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   336
  | listLast [p] = ([], p)
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   337
  | listLast [] = error "listLast: empty"
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   338
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   339
fun pairList (h1 :: (h2 :: t)) = ((h1, h2) :: pairList t)
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   340
  | pairList [] = []
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   341
  | pairList _ = error "pairList: odd list length"
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   342
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   343
fun store_thm binding thm thy =
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   344
  let
60367
e201bd8973db clarified context;
wenzelm
parents: 59936
diff changeset
   345
    val ctxt = Proof_Context.init_global thy
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   346
    val thm = Drule.export_without_context_open thm
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 58960
diff changeset
   347
    val tvs = Term.add_tvars (Thm.prop_of thm) []
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   348
    val tns = map (fn (_, _) => "'") tvs
81521
1bfad73ab115 clarified signature: more operations;
wenzelm
parents: 79336
diff changeset
   349
    val nms = Name.variants (Variable.names_of ctxt) tns
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   350
    val vs = map TVar ((nms ~~ (map (snd o fst) tvs)) ~~ (map snd tvs))
74282
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 70303
diff changeset
   351
    val thm' = Thm.instantiate (TVars.make (tvs ~~ map (Thm.ctyp_of ctxt) vs), Vars.empty) thm
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   352
  in
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   353
    snd (Global_Theory.add_thm ((binding, thm'), []) thy)
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   354
  end
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   355
47371
4193098c4ec1 added timestamps to logging of named thms
krauss
parents: 47363
diff changeset
   356
fun log_timestamp () =
4193098c4ec1 added timestamps to logging of named thms
krauss
parents: 47363
diff changeset
   357
  let
4193098c4ec1 added timestamps to logging of named thms
krauss
parents: 47363
diff changeset
   358
    val time = Time.now ()
4193098c4ec1 added timestamps to logging of named thms
krauss
parents: 47363
diff changeset
   359
    val millis = nth (space_explode "." (Time.fmt 3 time)) 1
4193098c4ec1 added timestamps to logging of named thms
krauss
parents: 47363
diff changeset
   360
  in
4193098c4ec1 added timestamps to logging of named thms
krauss
parents: 47363
diff changeset
   361
    Date.fmt "%d.%m.%Y %H:%M:%S." (Date.fromTimeLocal time) ^ millis
4193098c4ec1 added timestamps to logging of named thms
krauss
parents: 47363
diff changeset
   362
  end
4193098c4ec1 added timestamps to logging of named thms
krauss
parents: 47363
diff changeset
   363
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   364
fun process_line str tstate =
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   365
  let
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   366
    fun process tstate (#"R", [t]) = gettm t tstate |>> refl |-> setth
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   367
      | process tstate (#"B", [t]) = gettm t tstate |>> beta |-> setth
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   368
      | process tstate (#"1", [th]) = getth th tstate |>> conj1 |-> setth
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   369
      | process tstate (#"2", [th]) = getth th tstate |>> conj2 |-> setth
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   370
      | process tstate (#"H", [t]) =
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 62513
diff changeset
   371
          gettm t tstate |>> Thm.apply \<^cterm>\<open>Trueprop\<close> |>> Thm.trivial |-> setth
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   372
      | process tstate (#"A", [_, t]) =
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 62513
diff changeset
   373
          gettm t tstate |>> Thm.apply \<^cterm>\<open>Trueprop\<close> |>> Skip_Proof.make_thm_cterm |-> setth
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   374
      | process tstate (#"C", [th1, th2]) =
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   375
          getth th1 tstate ||>> getth th2 |>> (fn (t1, t2) => comb t1 t2) |-> setth
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   376
      | process tstate (#"T", [th1, th2]) =
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   377
          getth th1 tstate ||>> getth th2 |>> (fn (t1, t2) => trans t1 t2) |-> setth
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   378
      | process tstate (#"E", [th1, th2]) =
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   379
          getth th1 tstate ||>> getth th2 |>> (fn (t1, t2) => eq_mp t1 t2) |-> setth
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   380
      | process tstate (#"D", [th1, th2]) =
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   381
          getth th1 tstate ||>> getth th2 |>> (fn (t1, t2) => deduct t1 t2) |-> setth
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   382
      | process tstate (#"L", [t, th]) =
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   383
          gettm t tstate ||>> (fn ti => getth th ti) |>> (fn (tm, th) => abs tm th) |-> setth
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   384
      | process (thy, state) (#"M", [s]) =
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   385
          let
70303
502749883f53 redundant: default is false;
wenzelm
parents: 69829
diff changeset
   386
            val ctxt = Proof_Context.init_global thy
60648
6c4550cd1b17 clarified context;
wenzelm
parents: 60642
diff changeset
   387
            val thm = freezeT thy (Global_Theory.get_thm thy s)
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   388
            val ((_, [th']), _) = Variable.import true [thm] ctxt
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   389
          in
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   390
            setth th' (thy, state)
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   391
          end
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   392
      | process (thy, state) (#"Q", l) =
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   393
          let
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   394
            val (tys, th) = listLast l
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   395
            val (th, tstate) = getth th (thy, state)
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   396
            val (tys, tstate) = fold_map getty tys tstate
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   397
          in
81835
35abb6dd8bd2 clarified signature: more standard Isabelle/ML;
wenzelm
parents: 81831
diff changeset
   398
            setth (inst_type thy (pairList tys) th) tstate
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   399
          end
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   400
      | process tstate (#"S", l) =
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   401
          let
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   402
            val (tms, th) = listLast l
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   403
            val (th, tstate) = getth th tstate
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   404
            val (tms, tstate) = fold_map gettm tms tstate
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   405
          in
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   406
            setth (inst (pairList tms) th) tstate
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   407
          end
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   408
      | process tstate (#"F", [name, t]) =
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   409
          let
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   410
            val (tm, (thy, state)) = gettm t tstate
81831
4bb6c49ef791 clarified signature: more explicit operations;
wenzelm
parents: 81830
diff changeset
   411
            val (th, thy) = def (make_name name) tm thy
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   412
          in
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   413
            setth th (thy, state)
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   414
          end
81835
35abb6dd8bd2 clarified signature: more standard Isabelle/ML;
wenzelm
parents: 81831
diff changeset
   415
      | process (thy, state) (#"F", [name]) = setth (mdef thy name) (thy, state)
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   416
      | process tstate (#"Y", [name, absname, repname, t1, t2, th]) =
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   417
          let
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   418
            val (th, tstate) = getth th tstate
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   419
            val (t1, tstate) = gettm t1 tstate
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   420
            val (t2, (thy, state)) = gettm t2 tstate
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   421
            val (th, thy) = tydef name absname repname t1 t2 th thy
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   422
          in
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   423
            setth th (thy, state)
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   424
          end
81835
35abb6dd8bd2 clarified signature: more standard Isabelle/ML;
wenzelm
parents: 81831
diff changeset
   425
      | process (thy, state) (#"Y", [name, _, _]) = setth (mtydef thy name) (thy, state)
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   426
      | process (thy, state) (#"t", [n]) =
81831
4bb6c49ef791 clarified signature: more explicit operations;
wenzelm
parents: 81830
diff changeset
   427
          setty (Thm.global_ctyp_of thy (make_tfree n)) (thy, state)
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   428
      | process (thy, state) (#"a", n :: l) =
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   429
          fold_map getty l (thy, state) |>>
81837
wenzelm
parents: 81835
diff changeset
   430
            (fn tys => Thm.global_ctyp_of thy (make_type thy (n, map Thm.typ_of tys))) |-> setty
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   431
      | process (thy, state) (#"v", [n, ty]) =
81831
4bb6c49ef791 clarified signature: more explicit operations;
wenzelm
parents: 81830
diff changeset
   432
          getty ty (thy, state) |>> (fn ty => Thm.global_cterm_of thy (make_free (n, Thm.typ_of ty))) |-> settm
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   433
      | process (thy, state) (#"c", [n, ty]) =
81831
4bb6c49ef791 clarified signature: more explicit operations;
wenzelm
parents: 81830
diff changeset
   434
          getty ty (thy, state) |>> (fn ty => Thm.global_cterm_of thy (make_const thy (n, Thm.typ_of ty))) |-> settm
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   435
      | process tstate (#"f", [t1, t2]) =
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   436
          gettm t1 tstate ||>> gettm t2 |>> (fn (t1, t2) => Thm.apply t1 t2) |-> settm
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   437
      | process tstate (#"l", [t1, t2]) =
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   438
          gettm t1 tstate ||>> gettm t2 |>> (fn (t1, t2) => Thm.lambda t1 t2) |-> settm
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   439
      | process (thy, state) (#"+", [s]) =
81831
4bb6c49ef791 clarified signature: more explicit operations;
wenzelm
parents: 81830
diff changeset
   440
          (store_thm (Binding.name (make_name s)) (last_thm state) thy, state)
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   441
      | process _ (c, _) = error ("process: unknown command: " ^ String.implode [c])
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   442
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   443
    fun parse_line s =
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   444
        case String.tokens (fn x => (x = #"\n" orelse x = #" ")) s of
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   445
          [] => error "parse_line: empty"
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   446
        | h :: t => (case String.explode h of
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   447
            [] => error "parse_line: empty command"
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   448
          | sh :: st => (sh, (String.implode st) :: t))
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   449
  in
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   450
    process tstate (parse_line str)
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   451
  end
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   452
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   453
fun process_file path thy =
75616
986506233812 clarified signature: File.read_lines is based on scalable Bytes.T;
wenzelm
parents: 75612
diff changeset
   454
  #1 (fold process_line (File.read_lines path) (thy, init_state))
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   455
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 62513
diff changeset
   456
val _ = Outer_Syntax.command \<^command_keyword>\<open>import_file\<close>
50214
67fb9a168d10 tuned command descriptions;
wenzelm
parents: 49835
diff changeset
   457
  "import a recorded proof file"
48881
46e053eda5dd clarified Parse.path vs. Parse.explode -- prefer errors in proper transaction context;
wenzelm
parents: 47371
diff changeset
   458
  (Parse.path >> (fn name => Toplevel.theory (fn thy => process_file (Path.explode name) thy)))
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   459
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   460
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   461
end