src/HOL/Import/import_rule.ML
changeset 81913 5b9aca9b073b
parent 81912 ec2143e688b1
child 81926 402660d4558e
equal deleted inserted replaced
81912:ec2143e688b1 81913:5b9aca9b073b
   321 datatype state =
   321 datatype state =
   322   State of theory * (ctyp Inttab.table * int) * (cterm Inttab.table * int) * (thm Inttab.table * int)
   322   State of theory * (ctyp Inttab.table * int) * (cterm Inttab.table * int) * (thm Inttab.table * int)
   323 
   323 
   324 fun init_state thy = State (thy, (Inttab.empty, 0), (Inttab.empty, 0), (Inttab.empty, 0))
   324 fun init_state thy = State (thy, (Inttab.empty, 0), (Inttab.empty, 0), (Inttab.empty, 0))
   325 
   325 
   326 fun get (tab, no) s =
   326 fun get (tab, reg) s =
   327   (case Int.fromString s of
   327   (case Int.fromString s of
   328     NONE => raise Fail "get: not a number"
   328     NONE => raise Fail "get: not a number"
   329   | SOME i =>
   329   | SOME i =>
   330       (case Inttab.lookup tab (Int.abs i) of
   330       (case Inttab.lookup tab (Int.abs i) of
   331         NONE => raise Fail "get: lookup failed"
   331         NONE => raise Fail "get: lookup failed"
   332       | SOME res => (res, (if i < 0 then Inttab.delete (Int.abs i) tab else tab, no))))
   332       | SOME res => (res, (if i < 0 then Inttab.delete (Int.abs i) tab else tab, reg))))
   333 
   333 
   334 fun get_theory (State (thy, _, _, _)) = thy;
   334 fun get_theory (State (thy, _, _, _)) = thy;
   335 val theory = `get_theory;
   335 val theory = `get_theory;
   336 fun theory_op f (State (thy, a, b, c)) =
   336 fun theory_op f (State (thy, a, b, c)) = let val (y, thy') = f thy in (y, State (thy', a, b, c)) end;
   337   let val (res, thy') = f thy in (res, State (thy', a, b, c)) end;
       
   338 
   337 
   339 fun typ i (State (thy, a, b, c)) = let val (i, a') = get a i in (i, State (thy, a', b, c)) end
   338 fun typ i (State (thy, a, b, c)) = let val (i, a') = get a i in (i, State (thy, a', b, c)) end
   340 fun term i (State (thy, a, b, c)) = let val (i, b') = get b i in (i, State (thy, a, b', c)) end
   339 fun term i (State (thy, a, b, c)) = let val (i, b') = get b i in (i, State (thy, a, b', c)) end
   341 fun thm i (State (thy, a, b, c)) = let val (i, c') = get c i in (i, State (thy, a, b, c')) end
   340 fun thm i (State (thy, a, b, c)) = let val (i, c') = get c i in (i, State (thy, a, b, c')) end
   342 
   341 
   343 val typs = fold_map typ
   342 val typs = fold_map typ
   344 val terms = fold_map term
   343 val terms = fold_map term
   345 
   344 
   346 fun set (tab, no) v = (Inttab.update_new (no + 1, v) tab, no + 1)
   345 fun set (tab, reg) res = (Inttab.update_new (reg + 1, res) tab, reg + 1)
   347 fun set_typ ty (State (thy, a, b, c)) = State (thy, set a ty, b, c)
   346 fun set_typ ty (State (thy, a, b, c)) = State (thy, set a ty, b, c)
   348 fun set_term tm (State (thy, a, b, c)) = State (thy, a, set b tm, c)
   347 fun set_term tm (State (thy, a, b, c)) = State (thy, a, set b tm, c)
   349 fun set_thm th (State (thy, a, b, c)) = State (thy, a, b, set c th)
   348 fun set_thm th (State (thy, a, b, c)) = State (thy, a, b, set c th)
   350 
   349 
   351 fun stored_thm name (State (thy, a, b, c)) =
   350 fun stored_thm name (State (thy, a, b, c)) =
   352   let val th = freeze thy (Global_Theory.get_thm thy name)
   351   let val th = freeze thy (Global_Theory.get_thm thy name)
   353   in State (thy, a, b, set c th) end
   352   in State (thy, a, b, set c th) end
   354 
   353 
   355 fun store_thm name (State (thy, a, b, c as (tab, no))) =
   354 fun store_thm name (State (thy, a, b, c as (tab, reg))) =
   356   let
   355   let
   357     val th =
   356     val th =
   358       (case Inttab.lookup tab no of
   357       (case Inttab.lookup tab reg of
   359         NONE => raise Fail ("No result thm " ^ string_of_int no)
   358         NONE => raise Fail "store_thm: lookup failed"
   360       | SOME th0 => Drule.export_without_context_open th0)
   359       | SOME th0 => Drule.export_without_context_open th0)
   361 
   360 
   362     val tvars = TVars.build (Thm.fold_terms {hyps = false} TVars.add_tvars th);
   361     val tvars = TVars.build (Thm.fold_terms {hyps = false} TVars.add_tvars th);
   363     val names = Name.invent_global_types (TVars.size tvars)
   362     val names = Name.invent_global_types (TVars.size tvars)
   364     val tyinst =
   363     val tyinst =
   405   | command (#"F", [name]) = theory #-> (fn thy => set_thm (mdef thy name))
   404   | command (#"F", [name]) = theory #-> (fn thy => set_thm (mdef thy name))
   406   | command (#"Y", [name, abs, rep, t1, t2, th]) =
   405   | command (#"Y", [name, abs, rep, t1, t2, th]) =
   407       thm th #-> (fn th => term t1 #-> (fn t1 => term t2 #-> (fn t2 =>
   406       thm th #-> (fn th => term t1 #-> (fn t1 => term t2 #-> (fn t2 =>
   408         theory_op (tydef name abs rep t1 t2 th) #-> set_thm)))
   407         theory_op (tydef name abs rep t1 t2 th) #-> set_thm)))
   409   | command (#"Y", [name, _, _]) = theory #-> (fn thy => set_thm (mtydef thy name))
   408   | command (#"Y", [name, _, _]) = theory #-> (fn thy => set_thm (mtydef thy name))
   410   | command (#"t", [n]) = theory #-> (fn thy => set_typ (make_tfree thy n))
   409   | command (#"t", [a]) = theory #-> (fn thy => set_typ (make_tfree thy a))
   411   | command (#"a", c :: tys) = theory #-> (fn thy => typs tys #>> make_type thy c #-> set_typ)
   410   | command (#"a", c :: tys) = theory #-> (fn thy => typs tys #>> make_type thy c #-> set_typ)
   412   | command (#"v", [x, ty]) = typ ty #>> make_free x #-> set_term
   411   | command (#"v", [x, ty]) = typ ty #>> make_free x #-> set_term
   413   | command (#"c", [c, ty]) = theory #-> (fn thy => typ ty #>> make_const thy c #-> set_term)
   412   | command (#"c", [c, ty]) = theory #-> (fn thy => typ ty #>> make_const thy c #-> set_term)
   414   | command (#"f", [t, u]) = term t #-> (fn t => term u #-> (fn u => set_term (Thm.apply t u)))
   413   | command (#"f", [t, u]) = term t #-> (fn t => term u #-> (fn u => set_term (Thm.apply t u)))
   415   | command (#"l", [x, t]) = term x #-> (fn x => term t #-> (fn t => set_term (Thm.lambda x t)))
   414   | command (#"l", [x, t]) = term x #-> (fn x => term t #-> (fn t => set_term (Thm.lambda x t)))