src/HOL/Matrix_LP/Compute_Oracle/compute.ML
changeset 60336 f0b2457bf68e
parent 59621 291934bac95e
child 60948 b710a5087116
equal deleted inserted replaced
60335:edac62cd7bde 60336:f0b2457bf68e
     1 (*  Title:      HOL/Matrix_LP/Compute_Oracle/compute.ML
     1 (*  Title:      HOL/Matrix_LP/Compute_Oracle/compute.ML
     2     Author:     Steven Obua
     2     Author:     Steven Obua
     3 *)
     3 *)
     4 
     4 
     5 signature COMPUTE = sig
     5 signature COMPUTE =
     6 
     6 sig
     7     type computer
     7     type computer
     8     type theorem
     8     type theorem
     9     type naming = int -> string
     9     type naming = int -> string
    10 
    10 
    11     datatype machine = BARRAS | BARRAS_COMPILED | HASKELL | SML
    11     datatype machine = BARRAS | BARRAS_COMPILED | HASKELL | SML
    30 
    30 
    31     val make_theorem : computer -> thm -> string list -> theorem
    31     val make_theorem : computer -> thm -> string list -> theorem
    32     (* ! *) val instantiate : computer -> (string * cterm) list -> theorem -> theorem
    32     (* ! *) val instantiate : computer -> (string * cterm) list -> theorem -> theorem
    33     (* ! *) val evaluate_prem : computer -> int -> theorem -> theorem
    33     (* ! *) val evaluate_prem : computer -> int -> theorem -> theorem
    34     (* ! *) val modus_ponens : computer -> int -> thm -> theorem -> theorem
    34     (* ! *) val modus_ponens : computer -> int -> thm -> theorem -> theorem
    35 
    35 end
    36 end
    36 
    37 
    37 structure Compute :> COMPUTE =
    38 structure Compute :> COMPUTE = struct
    38 struct
    39 
    39 
    40 open Report;
    40 open Report;
    41 
    41 
    42 datatype machine = BARRAS | BARRAS_COMPILED | HASKELL | SML      
    42 datatype machine = BARRAS | BARRAS_COMPILED | HASKELL | SML      
    43 
    43 
   185 fun set_naming (Computer (r as Unsynchronized.ref (SOME (p1,p2,p3,p4,p5,p6,_)))) naming'= 
   185 fun set_naming (Computer (r as Unsynchronized.ref (SOME (p1,p2,p3,p4,p5,p6,_)))) naming'= 
   186     (r := SOME (p1,p2,p3,p4,p5,p6,naming'))
   186     (r := SOME (p1,p2,p3,p4,p5,p6,naming'))
   187 
   187 
   188 fun ref_of (Computer r) = r
   188 fun ref_of (Computer r) = r
   189 
   189 
   190 fun super_theory thy1 thy2 =
       
   191   if Theory.subthy (thy1, thy2) then thy2
       
   192   else raise THEORY ("Not a super theory", [thy1, thy2]);
       
   193 
       
   194 
   190 
   195 datatype cthm = ComputeThm of term list * sort list * term
   191 datatype cthm = ComputeThm of term list * sort list * term
   196 
   192 
   197 fun thm2cthm th = 
   193 fun thm2cthm th = 
   198     let
   194     let
   373         fold (fn h => fn p => Thm.implies_elim p h) hyps th 
   369         fold (fn h => fn p => Thm.implies_elim p h) hyps th 
   374     end
   370     end
   375         
   371         
   376 (* --------- Rewrite ----------- *)
   372 (* --------- Rewrite ----------- *)
   377 
   373 
   378 fun rewrite computer ct =
   374 fun rewrite computer raw_ct =
   379     let
   375     let
   380         val thy = Thm.theory_of_cterm ct
   376         val thy = theory_of computer
       
   377         val ct = Thm.transfer_cterm thy raw_ct
   381         val t' = Thm.term_of ct
   378         val t' = Thm.term_of ct
   382         val ty = Thm.typ_of_cterm ct
   379         val ty = Thm.typ_of_cterm ct
   383         val _ = super_theory (theory_of computer) thy
       
   384         val naming = naming_of computer
   380         val naming = naming_of computer
   385         val (encoding, t) = remove_types (encoding_of computer) t'
   381         val (encoding, t) = remove_types (encoding_of computer) t'
   386         val t = runprog (prog_of computer) t
   382         val t = runprog (prog_of computer) t
   387         val t = infer_types naming encoding ty t
   383         val t = infer_types naming encoding ty t
   388         val eq = Logic.mk_equals (t', t)
   384         val eq = Logic.mk_equals (t', t)
   398                * prem list * AbstractMachine.term * term list * sort list
   394                * prem list * AbstractMachine.term * term list * sort list
   399 
   395 
   400 
   396 
   401 exception ParamSimplify of computer * theorem
   397 exception ParamSimplify of computer * theorem
   402 
   398 
   403 fun make_theorem computer th vars =
   399 fun make_theorem computer raw_th vars =
   404 let
   400 let
   405     val _ = super_theory (theory_of computer) (Thm.theory_of_thm th)
   401     val thy = theory_of computer
       
   402     val th = Thm.transfer thy raw_th
   406 
   403 
   407     val (ComputeThm (hyps, shyps, prop)) = thm2cthm th 
   404     val (ComputeThm (hyps, shyps, prop)) = thm2cthm th 
   408 
   405 
   409     val encoding = encoding_of computer
   406     val encoding = encoding_of computer
   410  
   407  
   451             case mk_prem encoding t  of 
   448             case mk_prem encoding t  of 
   452                 (encoding, t) => (encoding, t::l)) (Logic.strip_imp_prems prop) (encoding, []))
   449                 (encoding, t) => (encoding, t::l)) (Logic.strip_imp_prems prop) (encoding, []))
   453     val (encoding, concl) = remove_types encoding (Logic.strip_imp_concl prop)
   450     val (encoding, concl) = remove_types encoding (Logic.strip_imp_concl prop)
   454     val _ = set_encoding computer encoding
   451     val _ = set_encoding computer encoding
   455 in
   452 in
   456     Theorem (Thm.theory_of_thm th, stamp_of computer, vartab, varsubst, 
   453     Theorem (thy, stamp_of computer, vartab, varsubst, prems, concl, hyps, shyps)
   457              prems, concl, hyps, shyps)
       
   458 end
   454 end
   459     
   455     
   460 fun theory_of_theorem (Theorem (thy,_,_,_,_,_,_,_)) = thy
   456 fun theory_of_theorem (Theorem (thy,_,_,_,_,_,_,_)) = thy
   461 fun update_theory thy (Theorem (_,p0,p1,p2,p3,p4,p5,p6)) = Theorem (thy,p0,p1,p2,p3,p4,p5,p6)
   457 fun update_theory thy (Theorem (_,p0,p1,p2,p3,p4,p5,p6)) = Theorem (thy,p0,p1,p2,p3,p4,p5,p6)
   462 fun stamp_of_theorem (Theorem (_,s, _, _, _, _, _, _)) = s     
   458 fun stamp_of_theorem (Theorem (_,s, _, _, _, _, _, _)) = s     
   503         if AbstractMachine.closed t then
   499         if AbstractMachine.closed t then
   504             ()
   500             ()
   505         else 
   501         else 
   506             raise Compute "instantiate: not a closed term"
   502             raise Compute "instantiate: not a closed term"
   507 
   503 
   508     fun compute_inst (s, ct) vs =
   504     fun compute_inst (s, raw_ct) vs =
   509         let
   505         let
   510             val _ = super_theory (Thm.theory_of_cterm ct) thy
   506             val ct = Thm.transfer_cterm thy raw_ct
   511             val ty = Thm.typ_of_cterm ct
   507             val ty = Thm.typ_of_cterm ct
   512         in          
   508         in          
   513             (case Symtab.lookup vartab s of 
   509             (case Symtab.lookup vartab s of 
   514                  NONE => raise Compute ("instantiate: variable '"^s^"' not found in theorem")
   510                  NONE => raise Compute ("instantiate: variable '"^s^"' not found in theorem")
   515                | SOME (x, ty') => 
   511                | SOME (x, ty') => 
   604 
   600 
   605 fun prem2term (Prem t) = t
   601 fun prem2term (Prem t) = t
   606   | prem2term (EqPrem (a,b,_,eq)) = 
   602   | prem2term (EqPrem (a,b,_,eq)) = 
   607     AbstractMachine.App (AbstractMachine.App (AbstractMachine.Const eq, a), b)
   603     AbstractMachine.App (AbstractMachine.App (AbstractMachine.Const eq, a), b)
   608 
   604 
   609 fun modus_ponens computer prem_no th' th = 
   605 fun modus_ponens computer prem_no raw_th' th = 
   610 let
   606 let
       
   607     val thy = theory_of computer
   611     val _ = check_compatible computer th
   608     val _ = check_compatible computer th
   612     val thy = 
   609     val _ =
   613         let
   610       Theory.subthy (theory_of_theorem th, thy) orelse raise Compute "modus_ponens: bad theory"
   614             val thy1 = theory_of_theorem th
   611     val th' = make_theorem computer (Thm.transfer thy raw_th') []
   615             val thy2 = Thm.theory_of_thm th'
       
   616         in
       
   617             if Theory.subthy (thy1, thy2) then thy2 
       
   618             else if Theory.subthy (thy2, thy1) then thy1 else
       
   619             raise Compute "modus_ponens: theorems are not compatible with each other"
       
   620         end 
       
   621     val th' = make_theorem computer th' []
       
   622     val varsubst = varsubst_of_theorem th
   612     val varsubst = varsubst_of_theorem th
   623     fun run vars_allowed t =
   613     fun run vars_allowed t =
   624         runprog (prog_of computer) (apply_subst vars_allowed varsubst t)
   614         runprog (prog_of computer) (apply_subst vars_allowed varsubst t)
   625     val prems = prems_of_theorem th
   615     val prems = prems_of_theorem th
   626     val prem = run true (prem2term (nth prems prem_no))
   616     val prem = run true (prem2term (nth prems prem_no))