src/HOL/Import/import_rule.ML
author wenzelm
Fri Oct 12 21:22:35 2012 +0200 (2012-10-12)
changeset 49835 31f32ec4d766
parent 49833 1d80798e8d8a
child 50214 67fb9a168d10
permissions -rw-r--r--
discontinued typedef with alternative name;
     1 (*  Title:      HOL/Import/import_rule.ML
     2     Author:     Cezary Kaliszyk, University of Innsbruck
     3     Author:     Alexander Krauss, QAware GmbH
     4 
     5 Importer proof rules and processing of lines and files.
     6 
     7 Based on earlier code by Steven Obua and Sebastian Skalberg.
     8 *)
     9 
    10 signature IMPORT_RULE =
    11 sig
    12   val beta : cterm -> thm
    13   val eq_mp : thm -> thm -> thm
    14   val comb : thm -> thm -> thm
    15   val trans : thm -> thm -> thm
    16   val deduct : thm -> thm -> thm
    17   val conj1 : thm -> thm
    18   val conj2 : thm -> thm
    19   val refl : cterm -> thm
    20   val abs : cterm -> thm -> thm
    21   val mdef : string -> theory -> thm
    22   val def : string -> cterm -> theory -> thm * theory
    23   val mtydef : string -> theory -> thm
    24   val tydef :
    25     string -> string -> string -> cterm -> cterm -> thm -> theory -> thm * theory
    26   val inst_type : (ctyp * ctyp) list -> thm -> theory -> thm
    27   val inst : (cterm * cterm) list -> thm -> thm
    28 
    29   type state
    30   val init_state : state
    31   val process_line : string -> (theory * state) -> (theory * state)
    32   val process_file : Path.T -> theory -> theory
    33 end
    34 
    35 structure Import_Rule: IMPORT_RULE =
    36 struct
    37 
    38 val init_state = ((Inttab.empty, 0), (Inttab.empty, 0), (Inttab.empty, 0))
    39 
    40 type state = (ctyp Inttab.table * int) * (cterm Inttab.table * int) * (thm Inttab.table * int)
    41 
    42 fun implies_elim_all th = implies_elim_list th (map Thm.assume (cprems_of th))
    43 
    44 fun meta_mp th1 th2 =
    45   let
    46     val th1a = implies_elim_all th1
    47     val th1b = Thm.implies_intr (strip_imp_concl (cprop_of th2)) th1a
    48     val th2a = implies_elim_all th2
    49     val th3 = Thm.implies_elim th1b th2a
    50   in
    51     implies_intr_hyps th3
    52   end
    53 
    54 fun meta_eq_to_obj_eq th =
    55   let
    56     val (tml, tmr) = Thm.dest_binop (strip_imp_concl (cprop_of th))
    57     val cty = ctyp_of_term tml
    58     val i = Drule.instantiate' [SOME cty] [SOME tml, SOME tmr]
    59       @{thm meta_eq_to_obj_eq}
    60   in
    61     Thm.implies_elim i th
    62   end
    63 
    64 fun beta ct = meta_eq_to_obj_eq (Thm.beta_conversion false ct)
    65 
    66 fun eq_mp th1 th2 =
    67   let
    68     val (tm1l, tm1r) = Thm.dest_binop (Thm.dest_arg (strip_imp_concl (cprop_of th1)))
    69     val i1 = Drule.instantiate' [] [SOME tm1l, SOME tm1r] @{thm iffD1}
    70     val i2 = meta_mp i1 th1
    71   in
    72     meta_mp i2 th2
    73   end
    74 
    75 fun comb th1 th2 =
    76   let
    77     val t1c = Thm.dest_arg (strip_imp_concl (cprop_of th1))
    78     val t2c = Thm.dest_arg (strip_imp_concl (cprop_of th2))
    79     val (cf, cg) = Thm.dest_binop t1c
    80     val (cx, cy) = Thm.dest_binop t2c
    81     val [fd, fr] = Thm.dest_ctyp (ctyp_of_term cf)
    82     val i1 = Drule.instantiate' [SOME fd, SOME fr]
    83       [SOME cf, SOME cg, SOME cx, SOME cy] @{thm cong}
    84     val i2 = meta_mp i1 th1
    85   in
    86     meta_mp i2 th2
    87   end
    88 
    89 fun trans th1 th2 =
    90   let
    91     val t1c = Thm.dest_arg (strip_imp_concl (cprop_of th1))
    92     val t2c = Thm.dest_arg (strip_imp_concl (cprop_of th2))
    93     val (r, s) = Thm.dest_binop t1c
    94     val (_, t) = Thm.dest_binop t2c
    95     val ty = ctyp_of_term r
    96     val i1 = Drule.instantiate' [SOME ty] [SOME r, SOME s, SOME t] @{thm trans}
    97     val i2 = meta_mp i1 th1
    98   in
    99     meta_mp i2 th2
   100   end
   101 
   102 fun deduct th1 th2 =
   103   let
   104     val th1c = strip_imp_concl (cprop_of th1)
   105     val th2c = strip_imp_concl (cprop_of th2)
   106     val th1a = implies_elim_all th1
   107     val th2a = implies_elim_all th2
   108     val th1b = Thm.implies_intr th2c th1a
   109     val th2b = Thm.implies_intr th1c th2a
   110     val i = Drule.instantiate' []
   111       [SOME (Thm.dest_arg th1c), SOME (Thm.dest_arg th2c)] @{thm iffI}
   112     val i1 = Thm.implies_elim i (Thm.assume (cprop_of th2b))
   113     val i2 = Thm.implies_elim i1 th1b
   114     val i3 = Thm.implies_intr (cprop_of th2b) i2
   115     val i4 = Thm.implies_elim i3 th2b
   116   in
   117     implies_intr_hyps i4
   118   end
   119 
   120 fun conj1 th =
   121   let
   122     val (tml, tmr) = Thm.dest_binop (Thm.dest_arg (strip_imp_concl (cprop_of th)))
   123     val i = Drule.instantiate' [] [SOME tml, SOME tmr] @{thm conjunct1}
   124   in
   125     meta_mp i th
   126   end
   127 
   128 fun conj2 th =
   129   let
   130     val (tml, tmr) = Thm.dest_binop (Thm.dest_arg (strip_imp_concl (cprop_of th)))
   131     val i = Drule.instantiate' [] [SOME tml, SOME tmr] @{thm conjunct2}
   132   in
   133     meta_mp i th
   134   end
   135 
   136 fun refl ctm =
   137   let
   138     val cty = Thm.ctyp_of_term ctm
   139   in
   140     Drule.instantiate' [SOME cty] [SOME ctm] @{thm refl}
   141   end
   142 
   143 fun abs cv th =
   144   let
   145     val th1 = implies_elim_all th
   146     val (tl, tr) = Thm.dest_binop (Thm.dest_arg (strip_imp_concl (cprop_of th1)))
   147     val (ll, lr) = (Thm.lambda cv tl, Thm.lambda cv tr)
   148     val (al, ar) = (Thm.apply ll cv, Thm.apply lr cv)
   149     val bl = beta al
   150     val br = meta_eq_to_obj_eq (Thm.symmetric (Thm.beta_conversion false ar))
   151     val th2 = trans (trans bl th1) br
   152     val th3 = implies_elim_all th2
   153     val th4 = Thm.forall_intr cv th3
   154     val i = Drule.instantiate' [SOME (ctyp_of_term cv), SOME (ctyp_of_term tl)]
   155       [SOME ll, SOME lr] @{thm ext2}
   156   in
   157     meta_mp i th4
   158   end
   159 
   160 fun freezeT thm =
   161   let
   162     val tvars = Term.add_tvars (prop_of thm) []
   163     val tfrees = map (fn ((t, _), s) => TFree (t, s)) tvars
   164     val tvars = map TVar tvars
   165     val thy = Thm.theory_of_thm thm
   166     fun inst ty = ctyp_of thy ty
   167   in
   168     Thm.instantiate ((map inst tvars ~~ map inst tfrees), []) thm
   169   end
   170 
   171 fun def' constname rhs thy =
   172   let
   173     val rhs = term_of rhs
   174     val typ = type_of rhs
   175     val thy1 = Sign.add_consts_i [(Binding.name constname, typ, NoSyn)] thy
   176     val eq = Logic.mk_equals (Const (Sign.intern_const thy1 constname, typ), rhs)
   177     val (thms, thy2) = Global_Theory.add_defs false
   178       [((Binding.suffix_name "_hldef" (Binding.name constname), eq), [])] thy1
   179     val def_thm = freezeT (hd thms)
   180   in
   181     (meta_eq_to_obj_eq def_thm, thy2)
   182   end
   183 
   184 fun mdef name thy =
   185   case Import_Data.get_const_def name thy of
   186     SOME th => th
   187   | NONE => error ("constant mapped but no definition: " ^ name)
   188 
   189 fun def constname rhs thy =
   190   case Import_Data.get_const_def constname thy of
   191     SOME _ =>
   192       let
   193         val () = warning ("Const mapped but def provided: " ^ constname)
   194       in
   195         (mdef constname thy, thy)
   196       end
   197   | NONE => def' constname rhs thy
   198 
   199 fun typedef_hollight th thy =
   200   let
   201     val (th_s, cn) = Thm.dest_comb (Thm.dest_arg (cprop_of th))
   202     val (th_s, abst) = Thm.dest_comb th_s
   203     val rept = Thm.dest_arg th_s
   204     val P = Thm.dest_arg cn
   205     val [nty, oty] = Thm.dest_ctyp (ctyp_of_term rept)
   206   in
   207     Drule.instantiate' [SOME nty, SOME oty] [SOME rept, SOME abst, SOME P,
   208       SOME (cterm_of thy (Free ("a", typ_of nty))),
   209       SOME (cterm_of thy (Free ("r", typ_of oty)))] @{thm typedef_hol2hollight}
   210   end
   211 
   212 fun tydef' tycname abs_name rep_name cP ct td_th thy =
   213   let
   214     val ctT = ctyp_of_term ct
   215     val nonempty = Drule.instantiate' [SOME ctT] [SOME cP, SOME ct] @{thm light_ex_imp_nonempty}
   216     val th2 = meta_mp nonempty td_th
   217     val c = case concl_of th2 of
   218         _ $ (Const(@{const_name Ex},_) $ Abs(_,_,Const(@{const_name Set.member},_) $ _ $ c)) => c
   219       | _ => error "type_introduction: bad type definition theorem"
   220     val tfrees = Term.add_tfrees c []
   221     val tnames = sort_strings (map fst tfrees)
   222     val ((_, typedef_info), thy') =
   223      Typedef.add_typedef_global (Binding.name tycname, map (rpair dummyS) tnames, NoSyn) c
   224        (SOME (Binding.name rep_name, Binding.name abs_name)) (rtac th2 1) thy
   225     val aty = #abs_type (#1 typedef_info)
   226     val th = freezeT (#type_definition (#2 typedef_info))
   227     val (th_s, _) = Thm.dest_comb (Thm.dest_arg (cprop_of th))
   228     val (th_s, abst) = Thm.dest_comb th_s
   229     val rept = Thm.dest_arg th_s
   230     val [nty, oty] = Thm.dest_ctyp (ctyp_of_term rept)
   231     val typedef_th =
   232        Drule.instantiate'
   233           [SOME nty, SOME oty]
   234           [SOME rept, SOME abst, SOME cP, SOME (cterm_of thy' (Free ("a", aty))),
   235              SOME (cterm_of thy' (Free ("r", typ_of ctT)))]
   236           @{thm typedef_hol2hollight}
   237     val th4 = typedef_th OF [#type_definition (#2 typedef_info)]
   238   in
   239     (th4, thy')
   240   end
   241 
   242 fun mtydef name thy =
   243   case Import_Data.get_typ_def name thy of
   244     SOME thn => meta_mp (typedef_hollight thn thy) thn
   245   | NONE => error ("type mapped but no tydef thm registered: " ^ name)
   246 
   247 fun tydef tycname abs_name rep_name P t td_th thy =
   248   case Import_Data.get_typ_def tycname thy of
   249     SOME _ =>
   250       let
   251         val () = warning ("Type mapped but proofs provided: " ^ tycname)
   252       in
   253         (mtydef tycname thy, thy)
   254       end
   255   | NONE => tydef' tycname abs_name rep_name P t td_th thy
   256 
   257 fun inst_type lambda th thy =
   258   let
   259     fun assoc _ [] = error "assoc"
   260       | assoc x ((x',y)::rest) = if x = x' then y else assoc x rest
   261     val lambda = map (fn (a, b) => (typ_of a, b)) lambda
   262     val tys_before = Term.add_tfrees (prop_of th) []
   263     val th1 = Thm.varifyT_global th
   264     val tys_after = Term.add_tvars (prop_of th1) []
   265     val tyinst = map2 (fn bef => fn iS =>
   266        (case try (assoc (TFree bef)) lambda of
   267               SOME cty => (ctyp_of thy (TVar iS), cty)
   268             | NONE => (ctyp_of thy (TVar iS), ctyp_of thy (TFree bef))
   269        )) tys_before tys_after
   270   in
   271     Thm.instantiate (tyinst,[]) th1
   272   end
   273 
   274 fun inst sigma th =
   275   let
   276     val (dom, rng) = ListPair.unzip (rev sigma)
   277   in
   278     th |> forall_intr_list dom
   279        |> forall_elim_list rng
   280   end
   281 
   282 fun transl_dotc #"." = "dot"
   283   | transl_dotc c = Char.toString c
   284 val transl_dot = String.translate transl_dotc
   285 
   286 fun transl_qmc #"?" = "t"
   287   | transl_qmc c = Char.toString c
   288 val transl_qm = String.translate transl_qmc
   289 
   290 fun getconstname s thy =
   291   case Import_Data.get_const_map s thy of
   292       SOME s => s
   293     | NONE => Sign.full_name thy (Binding.name (transl_dot s))
   294 fun gettyname s thy =
   295   case Import_Data.get_typ_map s thy of
   296     SOME s => s
   297   | NONE => Sign.full_name thy (Binding.name s)
   298 
   299 fun get (map, no) s =
   300   case Int.fromString s of
   301     NONE => error "Import_Rule.get: not a number"
   302   | SOME i => (case Inttab.lookup map (Int.abs i) of
   303       NONE => error "Import_Rule.get: lookup failed"
   304     | SOME res => (res, (if i < 0 then Inttab.delete (Int.abs i) map else map, no)))
   305 
   306 fun getty i (thy, (tyi, tmi, thi)) = let val (i, tyi) = (get tyi i) in (i, (thy, (tyi, tmi, thi))) end
   307 fun gettm i (thy, (tyi, tmi, thi)) = let val (i, tmi) = (get tmi i) in (i, (thy, (tyi, tmi, thi))) end
   308 fun getth i (thy, (tyi, tmi, thi)) = let val (i, thi) = (get thi i) in (i, (thy, (tyi, tmi, thi))) end
   309 fun set (map, no) v = (Inttab.update_new (no + 1, v) map, no + 1)
   310 fun setty v (thy, (tyi, tmi, thi)) = (thy, (set tyi v, tmi, thi))
   311 fun settm v (thy, (tyi, tmi, thi)) = (thy, (tyi, set tmi v, thi))
   312 fun setth v (thy, (tyi, tmi, thi)) = (thy, (tyi, tmi, set thi v))
   313 
   314 fun last_thm (_, _, (map, no)) =
   315   case Inttab.lookup map no of
   316     NONE => error "Import_Rule.last_thm: lookup failed"
   317   | SOME thm => thm
   318 
   319 fun listLast (h1 :: (h2 :: t)) = apfst (fn t => h1 :: h2 :: t) (listLast t)
   320   | listLast [p] = ([], p)
   321   | listLast [] = error "listLast: empty"
   322 
   323 fun pairList (h1 :: (h2 :: t)) = ((h1, h2) :: pairList t)
   324   | pairList [] = []
   325   | pairList _ = error "pairList: odd list length"
   326 
   327 fun store_thm binding thm thy =
   328   let
   329     val thm = Drule.export_without_context_open thm
   330     val tvs = Term.add_tvars (prop_of thm) []
   331     val tns = map (fn (_, _) => "'") tvs
   332     val nms = fst (fold_map Name.variant tns (Variable.names_of (Proof_Context.init_global thy)))
   333     val vs = map TVar ((nms ~~ (map (snd o fst) tvs)) ~~ (map snd tvs))
   334     val cvs = map (ctyp_of thy) vs
   335     val ctvs = map (ctyp_of thy) (map TVar tvs)
   336     val thm' = Thm.instantiate ((ctvs ~~ cvs), []) thm
   337   in
   338     snd (Global_Theory.add_thm ((binding, thm'), []) thy)
   339   end
   340 
   341 fun log_timestamp () =
   342   let
   343     val time = Time.now ()
   344     val millis = nth (space_explode "." (Time.fmt 3 time)) 1
   345   in
   346     Date.fmt "%d.%m.%Y %H:%M:%S." (Date.fromTimeLocal time) ^ millis
   347   end
   348 
   349 fun process_line str tstate =
   350   let
   351     fun process tstate (#"R", [t]) = gettm t tstate |>> refl |-> setth
   352       | process tstate (#"B", [t]) = gettm t tstate |>> beta |-> setth
   353       | process tstate (#"1", [th]) = getth th tstate |>> conj1 |-> setth
   354       | process tstate (#"2", [th]) = getth th tstate |>> conj2 |-> setth
   355       | process tstate (#"H", [t]) =
   356           gettm t tstate |>> Thm.apply @{cterm Trueprop} |>> Thm.trivial |-> setth
   357       | process tstate (#"A", [_, t]) =
   358           gettm t tstate |>> Thm.apply @{cterm Trueprop} |>> Skip_Proof.make_thm_cterm |-> setth
   359       | process tstate (#"C", [th1, th2]) =
   360           getth th1 tstate ||>> getth th2 |>> (fn (t1, t2) => comb t1 t2) |-> setth
   361       | process tstate (#"T", [th1, th2]) =
   362           getth th1 tstate ||>> getth th2 |>> (fn (t1, t2) => trans t1 t2) |-> setth
   363       | process tstate (#"E", [th1, th2]) =
   364           getth th1 tstate ||>> getth th2 |>> (fn (t1, t2) => eq_mp t1 t2) |-> setth
   365       | process tstate (#"D", [th1, th2]) =
   366           getth th1 tstate ||>> getth th2 |>> (fn (t1, t2) => deduct t1 t2) |-> setth
   367       | process tstate (#"L", [t, th]) =
   368           gettm t tstate ||>> (fn ti => getth th ti) |>> (fn (tm, th) => abs tm th) |-> setth
   369       | process (thy, state) (#"M", [s]) =
   370           let
   371             val ctxt = Variable.set_body false (Proof_Context.init_global thy)
   372             val thm = freezeT (Global_Theory.get_thm thy s)
   373             val ((_, [th']), _) = Variable.import true [thm] ctxt
   374           in
   375             setth th' (thy, state)
   376           end
   377       | process (thy, state) (#"Q", l) =
   378           let
   379             val (tys, th) = listLast l
   380             val (th, tstate) = getth th (thy, state)
   381             val (tys, tstate) = fold_map getty tys tstate
   382           in
   383             setth (inst_type (pairList tys) th thy) tstate
   384           end
   385       | process tstate (#"S", l) =
   386           let
   387             val (tms, th) = listLast l
   388             val (th, tstate) = getth th tstate
   389             val (tms, tstate) = fold_map gettm tms tstate
   390           in
   391             setth (inst (pairList tms) th) tstate
   392           end
   393       | process tstate (#"F", [name, t]) =
   394           let
   395             val (tm, (thy, state)) = gettm t tstate
   396             val (th, thy) = def (transl_dot name) tm thy
   397           in
   398             setth th (thy, state)
   399           end
   400       | process (thy, state) (#"F", [name]) = setth (mdef name thy) (thy, state)
   401       | process tstate (#"Y", [name, absname, repname, t1, t2, th]) =
   402           let
   403             val (th, tstate) = getth th tstate
   404             val (t1, tstate) = gettm t1 tstate
   405             val (t2, (thy, state)) = gettm t2 tstate
   406             val (th, thy) = tydef name absname repname t1 t2 th thy
   407           in
   408             setth th (thy, state)
   409           end
   410       | process (thy, state) (#"Y", [name, _, _]) = setth (mtydef name thy) (thy, state)
   411       | process (thy, state) (#"t", [n]) =
   412           setty (ctyp_of thy (TFree ("'" ^ (transl_qm n), ["HOL.type"]))) (thy, state)
   413       | process (thy, state) (#"a", n :: l) =
   414           fold_map getty l (thy, state) |>>
   415             (fn tys => ctyp_of thy (Type (gettyname n thy, map typ_of tys))) |-> setty
   416       | process (thy, state) (#"v", [n, ty]) =
   417           getty ty (thy, state) |>> (fn ty => cterm_of thy (Free (transl_dot n, typ_of ty))) |-> settm
   418       | process (thy, state) (#"c", [n, ty]) =
   419           getty ty (thy, state) |>> (fn ty => cterm_of thy (Const (getconstname n thy, typ_of ty))) |-> settm
   420       | process tstate (#"f", [t1, t2]) =
   421           gettm t1 tstate ||>> gettm t2 |>> (fn (t1, t2) => Thm.apply t1 t2) |-> settm
   422       | process tstate (#"l", [t1, t2]) =
   423           gettm t1 tstate ||>> gettm t2 |>> (fn (t1, t2) => Thm.lambda t1 t2) |-> settm
   424       | process (thy, state) (#"+", [s]) =
   425           let
   426             val _ = tracing ("NOTING " ^ log_timestamp () ^ ": " ^ s)
   427           in
   428             (store_thm (Binding.name (transl_dot s)) (last_thm state) thy, state)
   429           end
   430       | process _ (c, _) = error ("process: unknown command: " ^ String.implode [c])
   431 
   432     fun parse_line s =
   433         case String.tokens (fn x => (x = #"\n" orelse x = #" ")) s of
   434           [] => error "parse_line: empty"
   435         | h :: t => (case String.explode h of
   436             [] => error "parse_line: empty command"
   437           | sh :: st => (sh, (String.implode st) :: t))
   438   in
   439     process tstate (parse_line str)
   440   end
   441 
   442 fun process_file path thy =
   443   (thy, init_state) |> File.fold_lines process_line path |> fst
   444 
   445 val _ = Outer_Syntax.command @{command_spec "import_file"}
   446   "Import a recorded proof file"
   447   (Parse.path >> (fn name => Toplevel.theory (fn thy => process_file (Path.explode name) thy)))
   448 
   449 
   450 end