src/HOL/Import/import_rule.ML
author wenzelm
Fri, 17 Jan 2025 13:44:45 +0100
changeset 81853 f06281e21df9
parent 81852 c693485575a9
child 81854 2a5cbd329241
permissions -rw-r--r--
tuned names;
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
81847
c163ad6d18a5 clarified signature;
wenzelm
parents: 81846
diff changeset
    28
  val import_file : Path.T -> theory -> theory
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    29
end
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    30
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    31
structure Import_Rule: IMPORT_RULE =
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    32
struct
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    33
81848
5093dac27c14 clarified signature: more standard Isabelle/ML;
wenzelm
parents: 81847
diff changeset
    34
type state = (ctyp Inttab.table * int) * (cterm Inttab.table * int) * (thm Inttab.table * int)
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    35
81848
5093dac27c14 clarified signature: more standard Isabelle/ML;
wenzelm
parents: 81847
diff changeset
    36
val init_state: state = ((Inttab.empty, 0), (Inttab.empty, 0), (Inttab.empty, 0))
47258
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
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
    39
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    40
fun meta_mp th1 th2 =
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    41
  let
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    42
    val th1a = implies_elim_all th1
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 58960
diff changeset
    43
    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
    44
    val th2a = implies_elim_all th2
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    45
    val th3 = Thm.implies_elim th1b th2a
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    46
  in
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    47
    implies_intr_hyps th3
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    48
  end
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    49
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    50
fun meta_eq_to_obj_eq th =
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    51
  let
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 58960
diff changeset
    52
    val (tml, tmr) = Thm.dest_binop (strip_imp_concl (Thm.cprop_of th))
59586
ddf6deaadfe8 clarified signature;
wenzelm
parents: 59582
diff changeset
    53
    val cty = Thm.ctyp_of_cterm tml
60801
7664e0916eec tuned signature;
wenzelm
parents: 60754
diff changeset
    54
    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
    55
      @{thm meta_eq_to_obj_eq}
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    56
  in
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    57
    Thm.implies_elim i th
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    58
  end
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    59
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    60
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
    61
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    62
fun eq_mp th1 th2 =
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    63
  let
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 58960
diff changeset
    64
    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
    65
    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
    66
    val i2 = meta_mp i1 th1
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    67
  in
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    68
    meta_mp i2 th2
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    69
  end
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    70
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    71
fun comb th1 th2 =
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    72
  let
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 58960
diff changeset
    73
    val t1c = Thm.dest_arg (strip_imp_concl (Thm.cprop_of th1))
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 58960
diff changeset
    74
    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
    75
    val (cf, cg) = Thm.dest_binop t1c
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    76
    val (cx, cy) = Thm.dest_binop t2c
59586
ddf6deaadfe8 clarified signature;
wenzelm
parents: 59582
diff changeset
    77
    val [fd, fr] = Thm.dest_ctyp (Thm.ctyp_of_cterm cf)
60801
7664e0916eec tuned signature;
wenzelm
parents: 60754
diff changeset
    78
    val i1 = Thm.instantiate' [SOME fd, SOME fr]
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    79
      [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
    80
    val i2 = meta_mp i1 th1
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    81
  in
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    82
    meta_mp i2 th2
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    83
  end
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    84
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    85
fun trans th1 th2 =
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    86
  let
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 58960
diff changeset
    87
    val t1c = Thm.dest_arg (strip_imp_concl (Thm.cprop_of th1))
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 58960
diff changeset
    88
    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
    89
    val (r, s) = Thm.dest_binop t1c
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    90
    val (_, t) = Thm.dest_binop t2c
59586
ddf6deaadfe8 clarified signature;
wenzelm
parents: 59582
diff changeset
    91
    val ty = Thm.ctyp_of_cterm r
60801
7664e0916eec tuned signature;
wenzelm
parents: 60754
diff changeset
    92
    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
    93
    val i2 = meta_mp i1 th1
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    94
  in
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    95
    meta_mp i2 th2
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    96
  end
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    97
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    98
fun deduct th1 th2 =
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
    99
  let
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 58960
diff changeset
   100
    val th1c = strip_imp_concl (Thm.cprop_of th1)
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 58960
diff changeset
   101
    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
   102
    val th1a = implies_elim_all th1
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   103
    val th2a = implies_elim_all th2
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   104
    val th1b = Thm.implies_intr th2c th1a
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   105
    val th2b = Thm.implies_intr th1c th2a
60801
7664e0916eec tuned signature;
wenzelm
parents: 60754
diff changeset
   106
    val i = Thm.instantiate' []
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   107
      [SOME (Thm.dest_arg th1c), SOME (Thm.dest_arg th2c)] @{thm iffI}
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 58960
diff changeset
   108
    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
   109
    val i2 = Thm.implies_elim i1 th1b
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 58960
diff changeset
   110
    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
   111
    val i4 = Thm.implies_elim i3 th2b
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   112
  in
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   113
    implies_intr_hyps i4
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   114
  end
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   115
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   116
fun conj1 th =
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   117
  let
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 58960
diff changeset
   118
    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
   119
    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
   120
  in
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   121
    meta_mp i th
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   122
  end
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   123
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   124
fun conj2 th =
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   125
  let
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 58960
diff changeset
   126
    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
   127
    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
   128
  in
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   129
    meta_mp i th
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   130
  end
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   131
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   132
fun refl ctm =
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   133
  let
59586
ddf6deaadfe8 clarified signature;
wenzelm
parents: 59582
diff changeset
   134
    val cty = Thm.ctyp_of_cterm ctm
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   135
  in
60801
7664e0916eec tuned signature;
wenzelm
parents: 60754
diff changeset
   136
    Thm.instantiate' [SOME cty] [SOME ctm] @{thm refl}
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   137
  end
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   138
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   139
fun abs cv th =
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   140
  let
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   141
    val th1 = implies_elim_all th
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 58960
diff changeset
   142
    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
   143
    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
   144
    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
   145
    val bl = beta al
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   146
    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
   147
    val th2 = trans (trans bl th1) br
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   148
    val th3 = implies_elim_all th2
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   149
    val th4 = Thm.forall_intr cv th3
60801
7664e0916eec tuned signature;
wenzelm
parents: 60754
diff changeset
   150
    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
   151
      [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
   152
  in
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   153
    meta_mp i th4
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   154
  end
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   155
81852
c693485575a9 tuned names;
wenzelm
parents: 81851
diff changeset
   156
fun freezeT thy th =
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   157
  let
81852
c693485575a9 tuned names;
wenzelm
parents: 81851
diff changeset
   158
    val tvars = Term.add_tvars (Thm.prop_of th) []
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   159
    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
   160
  in
81852
c693485575a9 tuned names;
wenzelm
parents: 81851
diff changeset
   161
    Thm.instantiate (TVars.make (tvars ~~ map (Thm.global_ctyp_of thy) tfrees), Vars.empty) th
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   162
  end
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   163
81840
345e592792fd misc tuning and clarification;
wenzelm
parents: 81839
diff changeset
   164
fun def' c rhs thy =
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   165
  let
81840
345e592792fd misc tuning and clarification;
wenzelm
parents: 81839
diff changeset
   166
    val b = Binding.name c
81852
c693485575a9 tuned names;
wenzelm
parents: 81851
diff changeset
   167
    val ty = type_of rhs
c693485575a9 tuned names;
wenzelm
parents: 81851
diff changeset
   168
    val thy1 = Sign.add_consts [(b, ty, NoSyn)] thy
c693485575a9 tuned names;
wenzelm
parents: 81851
diff changeset
   169
    val eq = Logic.mk_equals (Const (Sign.full_name thy1 b, ty), rhs)
c693485575a9 tuned names;
wenzelm
parents: 81851
diff changeset
   170
    val (th, thy2) = Global_Theory.add_def (Binding.suffix_name "_hldef" b, eq) thy1
c693485575a9 tuned names;
wenzelm
parents: 81851
diff changeset
   171
    val def_thm = freezeT thy1 th
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   172
  in
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   173
    (meta_eq_to_obj_eq def_thm, thy2)
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   174
  end
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   175
81835
35abb6dd8bd2 clarified signature: more standard Isabelle/ML;
wenzelm
parents: 81831
diff changeset
   176
fun mdef thy name =
35abb6dd8bd2 clarified signature: more standard Isabelle/ML;
wenzelm
parents: 81831
diff changeset
   177
  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
   178
    SOME th => th
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   179
  | NONE => error ("constant mapped but no definition: " ^ name)
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   180
81840
345e592792fd misc tuning and clarification;
wenzelm
parents: 81839
diff changeset
   181
fun def c rhs thy =
345e592792fd misc tuning and clarification;
wenzelm
parents: 81839
diff changeset
   182
  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
   183
    SOME _ =>
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   184
      let
81840
345e592792fd misc tuning and clarification;
wenzelm
parents: 81839
diff changeset
   185
        val () = warning ("Const mapped but def provided: " ^ c)
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   186
      in
81840
345e592792fd misc tuning and clarification;
wenzelm
parents: 81839
diff changeset
   187
        (mdef thy c, thy)
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   188
      end
81840
345e592792fd misc tuning and clarification;
wenzelm
parents: 81839
diff changeset
   189
  | NONE => def' c (Thm.term_of rhs) thy
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   190
81829
ca1ad6660b4a tuned: prefer inlined thms;
wenzelm
parents: 81521
diff changeset
   191
fun typedef_hol2hollight nty oty rep abs pred a r =
ca1ad6660b4a tuned: prefer inlined thms;
wenzelm
parents: 81521
diff changeset
   192
  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
   193
    @{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
   194
        by (metis type_definition.Rep_inverse type_definition.Abs_inverse
ca1ad6660b4a tuned: prefer inlined thms;
wenzelm
parents: 81521
diff changeset
   195
              type_definition.Rep mem_Collect_eq)}
ca1ad6660b4a tuned: prefer inlined thms;
wenzelm
parents: 81521
diff changeset
   196
81835
35abb6dd8bd2 clarified signature: more standard Isabelle/ML;
wenzelm
parents: 81831
diff changeset
   197
fun typedef_hollight thy th =
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   198
  let
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 58960
diff changeset
   199
    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
   200
    val (th_s, abst) = Thm.dest_comb th_s
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   201
    val rept = Thm.dest_arg th_s
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   202
    val P = Thm.dest_arg cn
59586
ddf6deaadfe8 clarified signature;
wenzelm
parents: 59582
diff changeset
   203
    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
   204
  in
81829
ca1ad6660b4a tuned: prefer inlined thms;
wenzelm
parents: 81521
diff changeset
   205
    typedef_hol2hollight nty oty rept abst P
ca1ad6660b4a tuned: prefer inlined thms;
wenzelm
parents: 81521
diff changeset
   206
      (Thm.global_cterm_of thy (Free ("a", Thm.typ_of nty)))
ca1ad6660b4a tuned: prefer inlined thms;
wenzelm
parents: 81521
diff changeset
   207
      (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
   208
  end
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   209
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   210
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
   211
  let
59586
ddf6deaadfe8 clarified signature;
wenzelm
parents: 59582
diff changeset
   212
    val ctT = Thm.ctyp_of_cterm ct
81829
ca1ad6660b4a tuned: prefer inlined thms;
wenzelm
parents: 81521
diff changeset
   213
    val nonempty = Thm.instantiate' [SOME ctT] [SOME cP, SOME ct]
ca1ad6660b4a tuned: prefer inlined thms;
wenzelm
parents: 81521
diff changeset
   214
      @{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
   215
    val th2 = meta_mp nonempty td_th
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 58960
diff changeset
   216
    val c =
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 58960
diff changeset
   217
      case Thm.concl_of th2 of
81841
c84c0c0e6907 clarified pattern via antiquotations;
wenzelm
parents: 81840
diff changeset
   218
        \<^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
   219
      | _ => error "type_introduction: bad type definition theorem"
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   220
    val tfrees = Term.add_tfrees c []
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   221
    val tnames = sort_strings (map fst tfrees)
61110
6b7c2ecc6aea more general Typedef.bindings;
wenzelm
parents: 60801
diff changeset
   222
    val typedef_bindings =
62513
702085ca8564 take qualification of type name more seriously: derived consts and facts are qualified uniformly;
wenzelm
parents: 62436
diff changeset
   223
     {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
   224
      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
   225
      type_definition_name = Binding.name ("type_definition_" ^ tycname)}
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   226
    val ((_, typedef_info), thy') =
69829
3bfa28b3a5b2 streamlined specification interfaces
haftmann
parents: 69597
diff changeset
   227
     Named_Target.theory_map_result (apsnd o Typedef.transform_info)
3bfa28b3a5b2 streamlined specification interfaces
haftmann
parents: 69597
diff changeset
   228
     (Typedef.add_typedef {overloaded = false}
61260
e6f03fae14d5 explicit indication of overloaded typedefs;
wenzelm
parents: 61110
diff changeset
   229
       (Binding.name tycname, map (rpair dummyS) tnames, NoSyn) c
69829
3bfa28b3a5b2 streamlined specification interfaces
haftmann
parents: 69597
diff changeset
   230
       (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
   231
    val aty = #abs_type (#1 typedef_info)
60648
6c4550cd1b17 clarified context;
wenzelm
parents: 60642
diff changeset
   232
    val th = freezeT thy' (#type_definition (#2 typedef_info))
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 58960
diff changeset
   233
    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
   234
    val (th_s, abst) = Thm.dest_comb th_s
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   235
    val rept = Thm.dest_arg th_s
59586
ddf6deaadfe8 clarified signature;
wenzelm
parents: 59582
diff changeset
   236
    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
   237
    val typedef_th =
81829
ca1ad6660b4a tuned: prefer inlined thms;
wenzelm
parents: 81521
diff changeset
   238
      typedef_hol2hollight nty oty rept abst cP
ca1ad6660b4a tuned: prefer inlined thms;
wenzelm
parents: 81521
diff changeset
   239
        (Thm.global_cterm_of thy' (Free ("a", aty)))
ca1ad6660b4a tuned: prefer inlined thms;
wenzelm
parents: 81521
diff changeset
   240
        (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
   241
    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
   242
  in
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   243
    (th4, thy')
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   244
  end
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   245
81835
35abb6dd8bd2 clarified signature: more standard Isabelle/ML;
wenzelm
parents: 81831
diff changeset
   246
fun mtydef thy name =
35abb6dd8bd2 clarified signature: more standard Isabelle/ML;
wenzelm
parents: 81831
diff changeset
   247
  case Import_Data.get_typ_def thy name of
35abb6dd8bd2 clarified signature: more standard Isabelle/ML;
wenzelm
parents: 81831
diff changeset
   248
    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
   249
  | 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
   250
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   251
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
   252
  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
   253
    SOME _ =>
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   254
      let
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   255
        val () = warning ("Type mapped but proofs provided: " ^ tycname)
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   256
      in
81835
35abb6dd8bd2 clarified signature: more standard Isabelle/ML;
wenzelm
parents: 81831
diff changeset
   257
        (mtydef thy tycname, thy)
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   258
      end
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   259
  | 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
   260
81835
35abb6dd8bd2 clarified signature: more standard Isabelle/ML;
wenzelm
parents: 81831
diff changeset
   261
fun inst_type thy lambda th =
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   262
  let
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   263
    fun assoc _ [] = error "assoc"
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   264
      | 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
   265
    val lambda = map (fn (a, b) => (Thm.typ_of a, b)) lambda
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 58960
diff changeset
   266
    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
   267
    val th1 = Thm.varifyT_global th
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 58960
diff changeset
   268
    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
   269
    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
   270
      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
   271
        (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
   272
          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
   273
        | 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
   274
      tys_before tys_after
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   275
  in
74282
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 70303
diff changeset
   276
    Thm.instantiate (TVars.make tyinst, Vars.empty) th1
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   277
  end
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   278
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   279
fun inst sigma th =
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   280
  let
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   281
    val (dom, rng) = ListPair.unzip (rev sigma)
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   282
  in
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   283
    th |> forall_intr_list dom
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   284
       |> forall_elim_list rng
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   285
  end
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   286
81831
4bb6c49ef791 clarified signature: more explicit operations;
wenzelm
parents: 81830
diff changeset
   287
val make_name = String.translate (fn #"." => "dot" | c => Char.toString c)
4bb6c49ef791 clarified signature: more explicit operations;
wenzelm
parents: 81830
diff changeset
   288
4bb6c49ef791 clarified signature: more explicit operations;
wenzelm
parents: 81830
diff changeset
   289
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
   290
81831
4bb6c49ef791 clarified signature: more explicit operations;
wenzelm
parents: 81830
diff changeset
   291
fun make_tfree a =
4bb6c49ef791 clarified signature: more explicit operations;
wenzelm
parents: 81830
diff changeset
   292
  let val b = "'" ^ String.translate (fn #"?" => "t" | c => Char.toString c) a
4bb6c49ef791 clarified signature: more explicit operations;
wenzelm
parents: 81830
diff changeset
   293
  in TFree (b, \<^sort>\<open>type\<close>) end
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   294
81837
wenzelm
parents: 81835
diff changeset
   295
fun make_type thy (c, args) =
wenzelm
parents: 81835
diff changeset
   296
  let
wenzelm
parents: 81835
diff changeset
   297
    val d =
wenzelm
parents: 81835
diff changeset
   298
      (case Import_Data.get_typ_map thy c of
wenzelm
parents: 81835
diff changeset
   299
        SOME d => d
81839
wenzelm
parents: 81838
diff changeset
   300
      | NONE => Sign.full_bname thy (make_name c))
81837
wenzelm
parents: 81835
diff changeset
   301
  in Type (d, args) end
wenzelm
parents: 81835
diff changeset
   302
81831
4bb6c49ef791 clarified signature: more explicit operations;
wenzelm
parents: 81830
diff changeset
   303
fun make_const thy (c, ty) =
4bb6c49ef791 clarified signature: more explicit operations;
wenzelm
parents: 81830
diff changeset
   304
  let
4bb6c49ef791 clarified signature: more explicit operations;
wenzelm
parents: 81830
diff changeset
   305
    val d =
81835
35abb6dd8bd2 clarified signature: more standard Isabelle/ML;
wenzelm
parents: 81831
diff changeset
   306
      (case Import_Data.get_const_map thy c of
81831
4bb6c49ef791 clarified signature: more explicit operations;
wenzelm
parents: 81830
diff changeset
   307
        SOME d => d
81839
wenzelm
parents: 81838
diff changeset
   308
      | NONE => Sign.full_bname thy (make_name c))
81831
4bb6c49ef791 clarified signature: more explicit operations;
wenzelm
parents: 81830
diff changeset
   309
  in Const (d, ty) end
4bb6c49ef791 clarified signature: more explicit operations;
wenzelm
parents: 81830
diff changeset
   310
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   311
fun get (map, no) s =
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   312
  case Int.fromString s of
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   313
    NONE => error "Import_Rule.get: not a number"
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   314
  | 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
   315
      NONE => error "Import_Rule.get: lookup failed"
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   316
    | 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
   317
81853
f06281e21df9 tuned names;
wenzelm
parents: 81852
diff changeset
   318
fun typ i (thy, (tyi, tmi, thi)) = let val (i, tyi) = (get tyi i) in (i, (thy, (tyi, tmi, thi))) end
f06281e21df9 tuned names;
wenzelm
parents: 81852
diff changeset
   319
fun term i (thy, (tyi, tmi, thi)) = let val (i, tmi) = (get tmi i) in (i, (thy, (tyi, tmi, thi))) end
f06281e21df9 tuned names;
wenzelm
parents: 81852
diff changeset
   320
fun thm i (thy, (tyi, tmi, thi)) = let val (i, thi) = (get thi i) in (i, (thy, (tyi, tmi, thi))) end
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   321
fun set (map, no) v = (Inttab.update_new (no + 1, v) map, no + 1)
81853
f06281e21df9 tuned names;
wenzelm
parents: 81852
diff changeset
   322
fun set_typ v (thy, (tyi, tmi, thi)) = (thy, (set tyi v, tmi, thi))
f06281e21df9 tuned names;
wenzelm
parents: 81852
diff changeset
   323
fun set_term v (thy, (tyi, tmi, thi)) = (thy, (tyi, set tmi v, thi))
f06281e21df9 tuned names;
wenzelm
parents: 81852
diff changeset
   324
fun set_thm v (thy, (tyi, tmi, thi)) = (thy, (tyi, tmi, set thi v))
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   325
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   326
fun last_thm (_, _, (map, no)) =
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   327
  case Inttab.lookup map no of
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   328
    NONE => error "Import_Rule.last_thm: lookup failed"
81852
c693485575a9 tuned names;
wenzelm
parents: 81851
diff changeset
   329
  | SOME th => th
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   330
81849
1f46f6f7dec4 tuned signature;
wenzelm
parents: 81848
diff changeset
   331
fun list_last (x :: y :: zs) = apfst (fn t => x :: y :: t) (list_last zs)
1f46f6f7dec4 tuned signature;
wenzelm
parents: 81848
diff changeset
   332
  | list_last [x] = ([], x)
1f46f6f7dec4 tuned signature;
wenzelm
parents: 81848
diff changeset
   333
  | list_last [] = error "list_last: empty"
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   334
81849
1f46f6f7dec4 tuned signature;
wenzelm
parents: 81848
diff changeset
   335
fun pair_list (x :: y :: zs) = ((x, y) :: pair_list zs)
1f46f6f7dec4 tuned signature;
wenzelm
parents: 81848
diff changeset
   336
  | pair_list [] = []
1f46f6f7dec4 tuned signature;
wenzelm
parents: 81848
diff changeset
   337
  | pair_list _ = error "pair_list: odd list length"
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   338
81852
c693485575a9 tuned names;
wenzelm
parents: 81851
diff changeset
   339
fun store_thm binding th0 thy =
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   340
  let
60367
e201bd8973db clarified context;
wenzelm
parents: 59936
diff changeset
   341
    val ctxt = Proof_Context.init_global thy
81852
c693485575a9 tuned names;
wenzelm
parents: 81851
diff changeset
   342
    val th = Drule.export_without_context_open th0
c693485575a9 tuned names;
wenzelm
parents: 81851
diff changeset
   343
    val tvs = Term.add_tvars (Thm.prop_of th) []
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   344
    val tns = map (fn (_, _) => "'") tvs
81521
1bfad73ab115 clarified signature: more operations;
wenzelm
parents: 79336
diff changeset
   345
    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
   346
    val vs = map TVar ((nms ~~ (map (snd o fst) tvs)) ~~ (map snd tvs))
81852
c693485575a9 tuned names;
wenzelm
parents: 81851
diff changeset
   347
    val th' = Thm.instantiate (TVars.make (tvs ~~ map (Thm.ctyp_of ctxt) vs), Vars.empty) th
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   348
  in
81852
c693485575a9 tuned names;
wenzelm
parents: 81851
diff changeset
   349
    snd (Global_Theory.add_thm ((binding, th'), []) thy)
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   350
  end
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   351
81847
c163ad6d18a5 clarified signature;
wenzelm
parents: 81846
diff changeset
   352
fun parse_line s =
c163ad6d18a5 clarified signature;
wenzelm
parents: 81846
diff changeset
   353
  (case String.tokens (fn x => x = #"\n" orelse x = #" ") s of
c163ad6d18a5 clarified signature;
wenzelm
parents: 81846
diff changeset
   354
    [] => error "parse_line: empty"
c163ad6d18a5 clarified signature;
wenzelm
parents: 81846
diff changeset
   355
  | cmd :: args =>
c163ad6d18a5 clarified signature;
wenzelm
parents: 81846
diff changeset
   356
      (case String.explode cmd of
c163ad6d18a5 clarified signature;
wenzelm
parents: 81846
diff changeset
   357
        [] => error "parse_line: empty command"
c163ad6d18a5 clarified signature;
wenzelm
parents: 81846
diff changeset
   358
      | c :: cs => (c, String.implode cs :: args)))
c163ad6d18a5 clarified signature;
wenzelm
parents: 81846
diff changeset
   359
c163ad6d18a5 clarified signature;
wenzelm
parents: 81846
diff changeset
   360
fun process_line str =
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   361
  let
81853
f06281e21df9 tuned names;
wenzelm
parents: 81852
diff changeset
   362
    fun process (#"R", [t]) = term t #>> refl #-> set_thm
f06281e21df9 tuned names;
wenzelm
parents: 81852
diff changeset
   363
      | process (#"B", [t]) = term t #>> beta #-> set_thm
f06281e21df9 tuned names;
wenzelm
parents: 81852
diff changeset
   364
      | process (#"1", [th]) = thm th #>> conj1 #-> set_thm
f06281e21df9 tuned names;
wenzelm
parents: 81852
diff changeset
   365
      | process (#"2", [th]) = thm th #>> conj2 #-> set_thm
f06281e21df9 tuned names;
wenzelm
parents: 81852
diff changeset
   366
      | process (#"H", [t]) = term t #>> Thm.apply \<^cterm>\<open>Trueprop\<close> #>> Thm.trivial #-> set_thm
81851
10fc1cff7bbb tuned signature;
wenzelm
parents: 81850
diff changeset
   367
      | process (#"A", [_, t]) =
81853
f06281e21df9 tuned names;
wenzelm
parents: 81852
diff changeset
   368
          term t #>> Thm.apply \<^cterm>\<open>Trueprop\<close> #>> Skip_Proof.make_thm_cterm #-> set_thm
f06281e21df9 tuned names;
wenzelm
parents: 81852
diff changeset
   369
      | process (#"C", [th1, th2]) = thm th1 ##>> thm th2 #>> uncurry comb #-> set_thm
f06281e21df9 tuned names;
wenzelm
parents: 81852
diff changeset
   370
      | process (#"T", [th1, th2]) = thm th1 ##>> thm th2 #>> uncurry trans #-> set_thm
f06281e21df9 tuned names;
wenzelm
parents: 81852
diff changeset
   371
      | process (#"E", [th1, th2]) = thm th1 ##>> thm th2 #>> uncurry eq_mp #-> set_thm
f06281e21df9 tuned names;
wenzelm
parents: 81852
diff changeset
   372
      | process (#"D", [th1, th2]) = thm th1 ##>> thm th2 #>> uncurry deduct #-> set_thm
f06281e21df9 tuned names;
wenzelm
parents: 81852
diff changeset
   373
      | process (#"L", [t, th]) = term t ##>> (fn ti => thm th ti) #>> uncurry abs #-> set_thm
81851
10fc1cff7bbb tuned signature;
wenzelm
parents: 81850
diff changeset
   374
      | process (#"M", [s]) = (fn (thy, state) =>
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   375
          let
70303
502749883f53 redundant: default is false;
wenzelm
parents: 69829
diff changeset
   376
            val ctxt = Proof_Context.init_global thy
81852
c693485575a9 tuned names;
wenzelm
parents: 81851
diff changeset
   377
            val th = freezeT thy (Global_Theory.get_thm thy s)
c693485575a9 tuned names;
wenzelm
parents: 81851
diff changeset
   378
            val ((_, [th']), _) = Variable.import true [th] ctxt
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   379
          in
81853
f06281e21df9 tuned names;
wenzelm
parents: 81852
diff changeset
   380
            set_thm th' (thy, state)
81851
10fc1cff7bbb tuned signature;
wenzelm
parents: 81850
diff changeset
   381
          end)
10fc1cff7bbb tuned signature;
wenzelm
parents: 81850
diff changeset
   382
      | process (#"Q", l) = (fn (thy, state) =>
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   383
          let
81849
1f46f6f7dec4 tuned signature;
wenzelm
parents: 81848
diff changeset
   384
            val (tys, th) = list_last l
81853
f06281e21df9 tuned names;
wenzelm
parents: 81852
diff changeset
   385
            val (th, tstate) = thm th (thy, state)
f06281e21df9 tuned names;
wenzelm
parents: 81852
diff changeset
   386
            val (tys, tstate) = fold_map typ tys tstate
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   387
          in
81853
f06281e21df9 tuned names;
wenzelm
parents: 81852
diff changeset
   388
            set_thm (inst_type thy (pair_list tys) th) tstate
81851
10fc1cff7bbb tuned signature;
wenzelm
parents: 81850
diff changeset
   389
          end)
10fc1cff7bbb tuned signature;
wenzelm
parents: 81850
diff changeset
   390
      | process (#"S", l) = (fn tstate =>
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   391
          let
81849
1f46f6f7dec4 tuned signature;
wenzelm
parents: 81848
diff changeset
   392
            val (tms, th) = list_last l
81853
f06281e21df9 tuned names;
wenzelm
parents: 81852
diff changeset
   393
            val (th, tstate) = thm th tstate
f06281e21df9 tuned names;
wenzelm
parents: 81852
diff changeset
   394
            val (tms, tstate) = fold_map term tms tstate
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   395
          in
81853
f06281e21df9 tuned names;
wenzelm
parents: 81852
diff changeset
   396
            set_thm (inst (pair_list tms) th) tstate
81851
10fc1cff7bbb tuned signature;
wenzelm
parents: 81850
diff changeset
   397
          end)
10fc1cff7bbb tuned signature;
wenzelm
parents: 81850
diff changeset
   398
      | process (#"F", [name, t]) = (fn tstate =>
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   399
          let
81853
f06281e21df9 tuned names;
wenzelm
parents: 81852
diff changeset
   400
            val (tm, (thy, state)) = term t tstate
81831
4bb6c49ef791 clarified signature: more explicit operations;
wenzelm
parents: 81830
diff changeset
   401
            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
   402
          in
81853
f06281e21df9 tuned names;
wenzelm
parents: 81852
diff changeset
   403
            set_thm th (thy, state)
81851
10fc1cff7bbb tuned signature;
wenzelm
parents: 81850
diff changeset
   404
          end)
81853
f06281e21df9 tuned names;
wenzelm
parents: 81852
diff changeset
   405
      | process (#"F", [name]) = (fn (thy, state) => set_thm (mdef thy name) (thy, state))
81851
10fc1cff7bbb tuned signature;
wenzelm
parents: 81850
diff changeset
   406
      | process (#"Y", [name, absname, repname, t1, t2, th]) = (fn tstate =>
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   407
          let
81853
f06281e21df9 tuned names;
wenzelm
parents: 81852
diff changeset
   408
            val (th, tstate) = thm th tstate
f06281e21df9 tuned names;
wenzelm
parents: 81852
diff changeset
   409
            val (t1, tstate) = term t1 tstate
f06281e21df9 tuned names;
wenzelm
parents: 81852
diff changeset
   410
            val (t2, (thy, state)) = term t2 tstate
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   411
            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
   412
          in
81853
f06281e21df9 tuned names;
wenzelm
parents: 81852
diff changeset
   413
            set_thm th (thy, state)
81851
10fc1cff7bbb tuned signature;
wenzelm
parents: 81850
diff changeset
   414
          end)
81853
f06281e21df9 tuned names;
wenzelm
parents: 81852
diff changeset
   415
      | process (#"Y", [name, _, _]) = (fn (thy, state) => set_thm (mtydef thy name) (thy, state))
81851
10fc1cff7bbb tuned signature;
wenzelm
parents: 81850
diff changeset
   416
      | process (#"t", [n]) = (fn (thy, state) =>
81853
f06281e21df9 tuned names;
wenzelm
parents: 81852
diff changeset
   417
          set_typ (Thm.global_ctyp_of thy (make_tfree n)) (thy, state))
81851
10fc1cff7bbb tuned signature;
wenzelm
parents: 81850
diff changeset
   418
      | process (#"a", n :: l) = (fn (thy, state) =>
81853
f06281e21df9 tuned names;
wenzelm
parents: 81852
diff changeset
   419
          fold_map typ l (thy, state) |>>
f06281e21df9 tuned names;
wenzelm
parents: 81852
diff changeset
   420
            (fn tys => Thm.global_ctyp_of thy (make_type thy (n, map Thm.typ_of tys))) |-> set_typ)
81851
10fc1cff7bbb tuned signature;
wenzelm
parents: 81850
diff changeset
   421
      | process (#"v", [n, ty]) = (fn (thy, state) =>
81853
f06281e21df9 tuned names;
wenzelm
parents: 81852
diff changeset
   422
          typ ty (thy, state) |>> (fn ty => Thm.global_cterm_of thy (make_free (n, Thm.typ_of ty))) |-> set_term)
81851
10fc1cff7bbb tuned signature;
wenzelm
parents: 81850
diff changeset
   423
      | process (#"c", [n, ty]) = (fn (thy, state) =>
81853
f06281e21df9 tuned names;
wenzelm
parents: 81852
diff changeset
   424
          typ ty (thy, state) |>> (fn ty => Thm.global_cterm_of thy (make_const thy (n, Thm.typ_of ty))) |-> set_term)
f06281e21df9 tuned names;
wenzelm
parents: 81852
diff changeset
   425
      | process (#"f", [t1, t2]) = term t1 ##>> term t2 #>> uncurry Thm.apply #-> set_term
f06281e21df9 tuned names;
wenzelm
parents: 81852
diff changeset
   426
      | process (#"l", [t1, t2]) = term t1 ##>> term t2 #>> uncurry Thm.lambda #-> set_term
81851
10fc1cff7bbb tuned signature;
wenzelm
parents: 81850
diff changeset
   427
      | process (#"+", [s]) = (fn (thy, state) =>
10fc1cff7bbb tuned signature;
wenzelm
parents: 81850
diff changeset
   428
          (store_thm (Binding.name (make_name s)) (last_thm state) thy, state))
10fc1cff7bbb tuned signature;
wenzelm
parents: 81850
diff changeset
   429
      | process (c, _) = error ("process: unknown command: " ^ String.implode [c])
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   430
  in
81848
5093dac27c14 clarified signature: more standard Isabelle/ML;
wenzelm
parents: 81847
diff changeset
   431
    process (parse_line str)
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   432
  end
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   433
81847
c163ad6d18a5 clarified signature;
wenzelm
parents: 81846
diff changeset
   434
fun import_file path0 thy =
81846
5a7bf0f038e2 more robust import_file path: proper master_directory;
wenzelm
parents: 81844
diff changeset
   435
  let
5a7bf0f038e2 more robust import_file path: proper master_directory;
wenzelm
parents: 81844
diff changeset
   436
    val path = File.absolute_path (Resources.master_directory thy + path0)
5a7bf0f038e2 more robust import_file path: proper master_directory;
wenzelm
parents: 81844
diff changeset
   437
    val lines =
5a7bf0f038e2 more robust import_file path: proper master_directory;
wenzelm
parents: 81844
diff changeset
   438
      if Path.is_zst path then Bytes.read path |> Zstd.uncompress |> Bytes.trim_split_lines
5a7bf0f038e2 more robust import_file path: proper master_directory;
wenzelm
parents: 81844
diff changeset
   439
      else File.read_lines path
5a7bf0f038e2 more robust import_file path: proper master_directory;
wenzelm
parents: 81844
diff changeset
   440
  in #1 (fold process_line lines (thy, init_state)) end
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   441
81847
c163ad6d18a5 clarified signature;
wenzelm
parents: 81846
diff changeset
   442
val _ =
c163ad6d18a5 clarified signature;
wenzelm
parents: 81846
diff changeset
   443
  Outer_Syntax.command \<^command_keyword>\<open>import_file\<close> "import recorded proofs from HOL Light"
c163ad6d18a5 clarified signature;
wenzelm
parents: 81846
diff changeset
   444
    (Parse.path >> (fn name => Toplevel.theory (fn thy => import_file (Path.explode name) thy)))
47258
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   445
880e587eee9f Modernized HOL-Import for HOL Light
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
diff changeset
   446
end