| author | wenzelm | 
| Fri, 17 Mar 2023 12:10:14 +0100 | |
| changeset 77683 | 3e8e749935fc | 
| parent 69597 | ff784d5a5bfb | 
| child 77730 | 4a174bea55e2 | 
| permissions | -rw-r--r-- | 
| 47455 | 1 | (* Title: HOL/Matrix_LP/Compute_Oracle/compute.ML | 
| 23174 | 2 | Author: Steven Obua | 
| 3 | *) | |
| 4 | ||
| 60336 | 5 | signature COMPUTE = | 
| 6 | sig | |
| 23174 | 7 | type computer | 
| 25217 | 8 | type theorem | 
| 9 | type naming = int -> string | |
| 23174 | 10 | |
| 23663 | 11 | datatype machine = BARRAS | BARRAS_COMPILED | HASKELL | SML | 
| 23174 | 12 | |
| 25217 | 13 | (* Functions designated with a ! in front of them actually update the computer parameter *) | 
| 14 | ||
| 23663 | 15 | exception Make of string | 
| 16 | val make : machine -> theory -> thm list -> computer | |
| 25520 | 17 | val make_with_cache : machine -> theory -> term list -> thm list -> computer | 
| 23174 | 18 | val theory_of : computer -> theory | 
| 23663 | 19 | val hyps_of : computer -> term list | 
| 20 | val shyps_of : computer -> sort list | |
| 25217 | 21 | (* ! *) val update : computer -> thm list -> unit | 
| 25520 | 22 | (* ! *) val update_with_cache : computer -> term list -> thm list -> unit | 
| 25217 | 23 | |
| 24 | (* ! *) val set_naming : computer -> naming -> unit | |
| 25 | val naming_of : computer -> naming | |
| 26 | ||
| 27 | exception Compute of string | |
| 28 | val simplify : computer -> theorem -> thm | |
| 29 | val rewrite : computer -> cterm -> thm | |
| 23663 | 30 | |
| 25217 | 31 | val make_theorem : computer -> thm -> string list -> theorem | 
| 32 | (* ! *) val instantiate : computer -> (string * cterm) list -> theorem -> theorem | |
| 33 | (* ! *) val evaluate_prem : computer -> int -> theorem -> theorem | |
| 34 | (* ! *) val modus_ponens : computer -> int -> thm -> theorem -> theorem | |
| 23174 | 35 | end | 
| 36 | ||
| 60336 | 37 | structure Compute :> COMPUTE = | 
| 38 | struct | |
| 23663 | 39 | |
| 25217 | 40 | open Report; | 
| 24654 | 41 | |
| 26626 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 42 | datatype machine = BARRAS | BARRAS_COMPILED | HASKELL | SML | 
| 23663 | 43 | |
| 44 | (* Terms are mapped to integer codes *) | |
| 45 | structure Encode :> | |
| 46 | sig | |
| 47 | type encoding | |
| 48 | val empty : encoding | |
| 49 | val insert : term -> encoding -> int * encoding | |
| 50 | val lookup_code : term -> encoding -> int option | |
| 46536 | 51 | val lookup_term : int -> encoding -> term option | 
| 23663 | 52 | val remove_code : int -> encoding -> encoding | 
| 53 | val remove_term : term -> encoding -> encoding | |
| 54 | end | |
| 55 | = | |
| 56 | struct | |
| 57 | ||
| 58 | type encoding = int * (int Termtab.table) * (term Inttab.table) | |
| 59 | ||
| 60 | val empty = (0, Termtab.empty, Inttab.empty) | |
| 61 | ||
| 62 | fun insert t (e as (count, term2int, int2term)) = | |
| 63 | (case Termtab.lookup term2int t of | |
| 26626 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 64 | NONE => (count, (count+1, Termtab.update_new (t, count) term2int, Inttab.update_new (count, t) int2term)) | 
| 23663 | 65 | | SOME code => (code, e)) | 
| 66 | ||
| 67 | fun lookup_code t (_, term2int, _) = Termtab.lookup term2int t | |
| 68 | ||
| 69 | fun lookup_term c (_, _, int2term) = Inttab.lookup int2term c | |
| 70 | ||
| 71 | fun remove_code c (e as (count, term2int, int2term)) = | |
| 72 | (case lookup_term c e of NONE => e | SOME t => (count, Termtab.delete t term2int, Inttab.delete c int2term)) | |
| 73 | ||
| 74 | fun remove_term t (e as (count, term2int, int2term)) = | |
| 75 | (case lookup_code t e of NONE => e | SOME c => (count, Termtab.delete t term2int, Inttab.delete c int2term)) | |
| 76 | ||
| 77 | end | |
| 78 | ||
| 23174 | 79 | exception Make of string; | 
| 23663 | 80 | exception Compute of string; | 
| 23174 | 81 | |
| 23663 | 82 | local | 
| 46531 | 83 | fun make_constant t encoding = | 
| 26626 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 84 | let | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 85 | val (code, encoding) = Encode.insert t encoding | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 86 | in | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 87 | (encoding, AbstractMachine.Const code) | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 88 | end | 
| 23663 | 89 | in | 
| 23174 | 90 | |
| 23663 | 91 | fun remove_types encoding t = | 
| 92 | case t of | |
| 46531 | 93 | Var _ => make_constant t encoding | 
| 94 | | Free _ => make_constant t encoding | |
| 95 | | Const _ => make_constant t encoding | |
| 96 | | Abs (_, _, t') => | |
| 26626 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 97 | let val (encoding, t'') = remove_types encoding t' in | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 98 | (encoding, AbstractMachine.Abs t'') | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 99 | end | 
| 23663 | 100 | | a $ b => | 
| 26626 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 101 | let | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 102 | val (encoding, a) = remove_types encoding a | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 103 | val (encoding, b) = remove_types encoding b | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 104 | in | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 105 | (encoding, AbstractMachine.App (a,b)) | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 106 | end | 
| 23663 | 107 | | Bound b => (encoding, AbstractMachine.Var b) | 
| 108 | end | |
| 109 | ||
| 110 | local | |
| 111 | fun type_of (Free (_, ty)) = ty | |
| 112 | | type_of (Const (_, ty)) = ty | |
| 113 | | type_of (Var (_, ty)) = ty | |
| 40314 
b5ec88d9ac03
replaced ancient sys_error by raise Fail, assuming that the latter is not handled specifically by the environment;
 wenzelm parents: 
38808diff
changeset | 114 | | type_of _ = raise Fail "infer_types: type_of error" | 
| 23663 | 115 | in | 
| 116 | fun infer_types naming encoding = | |
| 23174 | 117 | let | 
| 42364 | 118 | fun infer_types _ bounds _ (AbstractMachine.Var v) = (Bound v, nth bounds v) | 
| 26626 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 119 | | infer_types _ bounds _ (AbstractMachine.Const code) = | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 120 | let | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 121 | val c = the (Encode.lookup_term code encoding) | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 122 | in | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 123 | (c, type_of c) | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 124 | end | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 125 | | infer_types level bounds _ (AbstractMachine.App (a, b)) = | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 126 | let | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 127 | val (a, aty) = infer_types level bounds NONE a | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 128 | val (adom, arange) = | 
| 23174 | 129 | case aty of | 
| 130 |                         Type ("fun", [dom, range]) => (dom, range)
 | |
| 40314 
b5ec88d9ac03
replaced ancient sys_error by raise Fail, assuming that the latter is not handled specifically by the environment;
 wenzelm parents: 
38808diff
changeset | 131 | | _ => raise Fail "infer_types: function type expected" | 
| 46531 | 132 | val (b, _) = infer_types level bounds (SOME adom) b | 
| 26626 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 133 | in | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 134 | (a $ b, arange) | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 135 | end | 
| 23663 | 136 |           | infer_types level bounds (SOME (ty as Type ("fun", [dom, range]))) (AbstractMachine.Abs m) =
 | 
| 23174 | 137 | let | 
| 23663 | 138 | val (m, _) = infer_types (level+1) (dom::bounds) (SOME range) m | 
| 23174 | 139 | in | 
| 23663 | 140 | (Abs (naming level, dom, m), ty) | 
| 23174 | 141 | end | 
| 46531 | 142 | | infer_types _ _ NONE (AbstractMachine.Abs _) = | 
| 40314 
b5ec88d9ac03
replaced ancient sys_error by raise Fail, assuming that the latter is not handled specifically by the environment;
 wenzelm parents: 
38808diff
changeset | 143 | raise Fail "infer_types: cannot infer type of abstraction" | 
| 23174 | 144 | |
| 23663 | 145 | fun infer ty term = | 
| 23174 | 146 | let | 
| 23663 | 147 | val (term', _) = infer_types 0 [] (SOME ty) term | 
| 23174 | 148 | in | 
| 149 | term' | |
| 150 | end | |
| 151 | in | |
| 152 | infer | |
| 153 | end | |
| 23663 | 154 | end | 
| 23174 | 155 | |
| 23663 | 156 | datatype prog = | 
| 26626 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 157 | ProgBarras of AM_Interpreter.program | 
| 23663 | 158 | | ProgBarrasC of AM_Compiler.program | 
| 159 | | ProgHaskell of AM_GHC.program | |
| 160 | | ProgSML of AM_SML.program | |
| 23174 | 161 | |
| 25217 | 162 | fun machine_of_prog (ProgBarras _) = BARRAS | 
| 163 | | machine_of_prog (ProgBarrasC _) = BARRAS_COMPILED | |
| 164 | | machine_of_prog (ProgHaskell _) = HASKELL | |
| 165 | | machine_of_prog (ProgSML _) = SML | |
| 166 | ||
| 167 | type naming = int -> string | |
| 168 | ||
| 41491 | 169 | fun default_naming i = "v_" ^ string_of_int i | 
| 25217 | 170 | |
| 32740 | 171 | datatype computer = Computer of | 
| 52788 | 172 | (theory * Encode.encoding * term list * unit Sorttab.table * prog * unit Unsynchronized.ref * naming) | 
| 32740 | 173 | option Unsynchronized.ref | 
| 25217 | 174 | |
| 52788 | 175 | fun theory_of (Computer (Unsynchronized.ref (SOME (thy,_,_,_,_,_,_)))) = thy | 
| 32740 | 176 | fun hyps_of (Computer (Unsynchronized.ref (SOME (_,_,hyps,_,_,_,_)))) = hyps | 
| 177 | fun shyps_of (Computer (Unsynchronized.ref (SOME (_,_,_,shyptable,_,_,_)))) = Sorttab.keys (shyptable) | |
| 178 | fun shyptab_of (Computer (Unsynchronized.ref (SOME (_,_,_,shyptable,_,_,_)))) = shyptable | |
| 179 | fun stamp_of (Computer (Unsynchronized.ref (SOME (_,_,_,_,_,stamp,_)))) = stamp | |
| 180 | fun prog_of (Computer (Unsynchronized.ref (SOME (_,_,_,_,prog,_,_)))) = prog | |
| 181 | fun encoding_of (Computer (Unsynchronized.ref (SOME (_,encoding,_,_,_,_,_)))) = encoding | |
| 46531 | 182 | fun set_encoding (Computer (r as Unsynchronized.ref (SOME (p1,_,p2,p3,p4,p5,p6)))) encoding' = | 
| 25217 | 183 | (r := SOME (p1,encoding',p2,p3,p4,p5,p6)) | 
| 32740 | 184 | fun naming_of (Computer (Unsynchronized.ref (SOME (_,_,_,_,_,_,n)))) = n | 
| 46531 | 185 | fun set_naming (Computer (r as Unsynchronized.ref (SOME (p1,p2,p3,p4,p5,p6,_)))) naming'= | 
| 25217 | 186 | (r := SOME (p1,p2,p3,p4,p5,p6,naming')) | 
| 187 | ||
| 188 | fun ref_of (Computer r) = r | |
| 23663 | 189 | |
| 58669 | 190 | |
| 23663 | 191 | datatype cthm = ComputeThm of term list * sort list * term | 
| 192 | ||
| 193 | fun thm2cthm th = | |
| 61039 | 194 | (if not (null (Thm.tpairs_of th)) then raise Make "theorems may not contain tpairs" else (); | 
| 195 | ComputeThm (Thm.hyps_of th, Thm.shyps_of th, Thm.prop_of th)) | |
| 23174 | 196 | |
| 25520 | 197 | fun make_internal machine thy stamp encoding cache_pattern_terms raw_ths = | 
| 23663 | 198 | let | 
| 26626 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 199 | fun transfer (x:thm) = Thm.transfer thy x | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 200 | val ths = map (thm2cthm o Thm.strip_shyps o transfer) raw_ths | 
| 23174 | 201 | |
| 46531 | 202 | fun make_pattern encoding n vars (AbstractMachine.Abs _) = | 
| 26626 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 203 | raise (Make "no lambda abstractions allowed in pattern") | 
| 46531 | 204 | | make_pattern encoding n vars (AbstractMachine.Var _) = | 
| 26626 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 205 | raise (Make "no bound variables allowed in pattern") | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 206 | | make_pattern encoding n vars (AbstractMachine.Const code) = | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 207 | (case the (Encode.lookup_term code encoding) of | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 208 | Var _ => ((n+1, Inttab.update_new (code, n) vars, AbstractMachine.PVar) | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 209 | handle Inttab.DUP _ => raise (Make "no duplicate variable in pattern allowed")) | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 210 | | _ => (n, vars, AbstractMachine.PConst (code, []))) | 
| 25520 | 211 | | make_pattern encoding n vars (AbstractMachine.App (a, b)) = | 
| 212 | let | |
| 213 | val (n, vars, pa) = make_pattern encoding n vars a | |
| 214 | val (n, vars, pb) = make_pattern encoding n vars b | |
| 215 | in | |
| 216 | case pa of | |
| 217 | AbstractMachine.PVar => | |
| 218 | raise (Make "patterns may not start with a variable") | |
| 219 | | AbstractMachine.PConst (c, args) => | |
| 220 | (n, vars, AbstractMachine.PConst (c, args@[pb])) | |
| 221 | end | |
| 222 | ||
| 23663 | 223 | fun thm2rule (encoding, hyptable, shyptable) th = | 
| 224 | let | |
| 26626 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 225 | val (ComputeThm (hyps, shyps, prop)) = th | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 226 | val hyptable = fold (fn h => Termtab.update (h, ())) hyps hyptable | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 227 | val shyptable = fold (fn sh => Sorttab.update (sh, ())) shyps shyptable | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 228 | val (prems, prop) = (Logic.strip_imp_prems prop, Logic.strip_imp_concl prop) | 
| 23663 | 229 | val (a, b) = Logic.dest_equals prop | 
| 230 | handle TERM _ => raise (Make "theorems must be meta-level equations (with optional guards)") | |
| 26626 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 231 | val a = Envir.eta_contract a | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 232 | val b = Envir.eta_contract b | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 233 | val prems = map Envir.eta_contract prems | 
| 23174 | 234 | |
| 23663 | 235 | val (encoding, left) = remove_types encoding a | 
| 26626 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 236 | val (encoding, right) = remove_types encoding b | 
| 23663 | 237 | fun remove_types_of_guard encoding g = | 
| 26626 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 238 | (let | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 239 | val (t1, t2) = Logic.dest_equals g | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 240 | val (encoding, t1) = remove_types encoding t1 | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 241 | val (encoding, t2) = remove_types encoding t2 | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 242 | in | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 243 | (encoding, AbstractMachine.Guard (t1, t2)) | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 244 | end handle TERM _ => raise (Make "guards must be meta-level equations")) | 
| 23663 | 245 | val (encoding, prems) = fold_rev (fn p => fn (encoding, ps) => let val (e, p) = remove_types_of_guard encoding p in (e, p::ps) end) prems (encoding, []) | 
| 23174 | 246 | |
| 23663 | 247 | (* Principally, a check should be made here to see if the (meta-) hyps contain any of the variables of the rule. | 
| 248 | As it is, all variables of the rule are schematic, and there are no schematic variables in meta-hyps, therefore | |
| 249 | this check can be left out. *) | |
| 250 | ||
| 251 | val (vcount, vars, pattern) = make_pattern encoding 0 Inttab.empty left | |
| 23174 | 252 | val _ = (case pattern of | 
| 23663 | 253 | AbstractMachine.PVar => | 
| 23174 | 254 | raise (Make "patterns may not start with a variable") | 
| 26626 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 255 | | _ => ()) | 
| 23174 | 256 | |
| 257 | (* finally, provide a function for renaming the | |
| 23663 | 258 | pattern bound variables on the right hand side *) | 
| 23174 | 259 | |
| 23663 | 260 | fun rename level vars (var as AbstractMachine.Var _) = var | 
| 26626 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 261 | | rename level vars (c as AbstractMachine.Const code) = | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 262 | (case Inttab.lookup vars code of | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 263 | NONE => c | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 264 | | SOME n => AbstractMachine.Var (vcount-n-1+level)) | 
| 23663 | 265 | | rename level vars (AbstractMachine.App (a, b)) = | 
| 266 | AbstractMachine.App (rename level vars a, rename level vars b) | |
| 267 | | rename level vars (AbstractMachine.Abs m) = | |
| 268 | AbstractMachine.Abs (rename (level+1) vars m) | |
| 26626 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 269 | |
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 270 | fun rename_guard (AbstractMachine.Guard (a,b)) = | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 271 | AbstractMachine.Guard (rename 0 vars a, rename 0 vars b) | 
| 23174 | 272 | in | 
| 23663 | 273 | ((encoding, hyptable, shyptable), (map rename_guard prems, pattern, rename 0 vars right)) | 
| 23174 | 274 | end | 
| 275 | ||
| 23663 | 276 | val ((encoding, hyptable, shyptable), rules) = | 
| 277 | fold_rev (fn th => fn (encoding_hyptable, rules) => | |
| 23174 | 278 | let | 
| 23663 | 279 | val (encoding_hyptable, rule) = thm2rule encoding_hyptable th | 
| 280 | in (encoding_hyptable, rule::rules) end) | |
| 25217 | 281 | ths ((encoding, Termtab.empty, Sorttab.empty), []) | 
| 23174 | 282 | |
| 25520 | 283 | fun make_cache_pattern t (encoding, cache_patterns) = | 
| 26626 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 284 | let | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 285 | val (encoding, a) = remove_types encoding t | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 286 | val (_,_,p) = make_pattern encoding 0 Inttab.empty a | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 287 | in | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 288 | (encoding, p::cache_patterns) | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 289 | end | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 290 | |
| 46534 | 291 | val (encoding, _) = fold_rev make_cache_pattern cache_pattern_terms (encoding, []) | 
| 25520 | 292 | |
| 23663 | 293 | val prog = | 
| 26626 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 294 | case machine of | 
| 46534 | 295 | BARRAS => ProgBarras (AM_Interpreter.compile rules) | 
| 296 | | BARRAS_COMPILED => ProgBarrasC (AM_Compiler.compile rules) | |
| 297 | | HASKELL => ProgHaskell (AM_GHC.compile rules) | |
| 298 | | SML => ProgSML (AM_SML.compile rules) | |
| 23174 | 299 | |
| 23663 | 300 | fun has_witness s = not (null (Sign.witness_sorts thy [] [s])) | 
| 23174 | 301 | |
| 26626 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 302 | val shyptable = fold Sorttab.delete (filter has_witness (Sorttab.keys (shyptable))) shyptable | 
| 23663 | 303 | |
| 52788 | 304 | in (thy, encoding, Termtab.keys hyptable, shyptable, prog, stamp, default_naming) end | 
| 25217 | 305 | |
| 32740 | 306 | fun make_with_cache machine thy cache_patterns raw_thms = | 
| 307 | Computer (Unsynchronized.ref (SOME (make_internal machine thy (Unsynchronized.ref ()) Encode.empty cache_patterns raw_thms))) | |
| 23663 | 308 | |
| 25520 | 309 | fun make machine thy raw_thms = make_with_cache machine thy [] raw_thms | 
| 310 | ||
| 311 | fun update_with_cache computer cache_patterns raw_thms = | |
| 25217 | 312 | let | 
| 26626 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 313 | val c = make_internal (machine_of_prog (prog_of computer)) (theory_of computer) (stamp_of computer) | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 314 | (encoding_of computer) cache_patterns raw_thms | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 315 | val _ = (ref_of computer) := SOME c | 
| 25217 | 316 | in | 
| 26626 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 317 | () | 
| 25217 | 318 | end | 
| 319 | ||
| 25520 | 320 | fun update computer raw_thms = update_with_cache computer [] raw_thms | 
| 321 | ||
| 25217 | 322 | fun runprog (ProgBarras p) = AM_Interpreter.run p | 
| 323 | | runprog (ProgBarrasC p) = AM_Compiler.run p | |
| 324 | | runprog (ProgHaskell p) = AM_GHC.run p | |
| 26626 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 325 | | runprog (ProgSML p) = AM_SML.run p | 
| 25217 | 326 | |
| 327 | (* ------------------------------------------------------------------------------------- *) | |
| 328 | (* An oracle for exporting theorems; must only be accessible from inside this structure! *) | |
| 329 | (* ------------------------------------------------------------------------------------- *) | |
| 330 | ||
| 331 | fun merge_hyps hyps1 hyps2 = | |
| 332 | let | |
| 333 | fun add hyps tab = fold (fn h => fn tab => Termtab.update (h, ()) tab) hyps tab | |
| 334 | in | |
| 335 | Termtab.keys (add hyps2 (add hyps1 Termtab.empty)) | |
| 336 | end | |
| 337 | ||
| 338 | fun add_shyps shyps tab = fold (fn h => fn tab => Sorttab.update (h, ()) tab) shyps tab | |
| 339 | ||
| 340 | fun merge_shyps shyps1 shyps2 = Sorttab.keys (add_shyps shyps2 (add_shyps shyps1 Sorttab.empty)) | |
| 341 | ||
| 28290 | 342 | val (_, export_oracle) = Context.>>> (Context.map_theory_result | 
| 69597 | 343 | (Thm.add_oracle (\<^binding>\<open>compute\<close>, fn (thy, hyps, shyps, prop) => | 
| 25217 | 344 | let | 
| 26626 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 345 | val shyptab = add_shyps shyps Sorttab.empty | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 346 | fun delete s shyptab = Sorttab.delete s shyptab handle Sorttab.UNDEF _ => shyptab | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 347 | fun delete_term t shyptab = fold delete (Sorts.insert_term t []) shyptab | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 348 | fun has_witness s = not (null (Sign.witness_sorts thy [] [s])) | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 349 | val shyptab = fold Sorttab.delete (filter has_witness (Sorttab.keys (shyptab))) shyptab | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 350 | val shyps = if Sorttab.is_empty shyptab then [] else Sorttab.keys (fold delete_term (prop::hyps) shyptab) | 
| 31322 
526e149999cc
attempt to eliminate adhoc makestring at runtime (which is not well-defined);
 wenzelm parents: 
30288diff
changeset | 351 | val _ = | 
| 
526e149999cc
attempt to eliminate adhoc makestring at runtime (which is not well-defined);
 wenzelm parents: 
30288diff
changeset | 352 | if not (null shyps) then | 
| 
526e149999cc
attempt to eliminate adhoc makestring at runtime (which is not well-defined);
 wenzelm parents: 
30288diff
changeset | 353 |             raise Compute ("dangling sort hypotheses: " ^
 | 
| 
526e149999cc
attempt to eliminate adhoc makestring at runtime (which is not well-defined);
 wenzelm parents: 
30288diff
changeset | 354 | commas (map (Syntax.string_of_sort_global thy) shyps)) | 
| 
526e149999cc
attempt to eliminate adhoc makestring at runtime (which is not well-defined);
 wenzelm parents: 
30288diff
changeset | 355 | else () | 
| 25217 | 356 | in | 
| 59621 
291934bac95e
Thm.cterm_of and Thm.ctyp_of operate on local context;
 wenzelm parents: 
59586diff
changeset | 357 | Thm.global_cterm_of thy (fold_rev (fn hyp => fn p => Logic.mk_implies (hyp, p)) hyps prop) | 
| 28290 | 358 | end))); | 
| 25217 | 359 | |
| 360 | fun export_thm thy hyps shyps prop = | |
| 361 | let | |
| 28290 | 362 | val th = export_oracle (thy, hyps, shyps, prop) | 
| 59621 
291934bac95e
Thm.cterm_of and Thm.ctyp_of operate on local context;
 wenzelm parents: 
59586diff
changeset | 363 | val hyps = map (fn h => Thm.assume (Thm.global_cterm_of thy h)) hyps | 
| 25217 | 364 | in | 
| 36945 | 365 | fold (fn h => fn p => Thm.implies_elim p h) hyps th | 
| 25217 | 366 | end | 
| 26626 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 367 | |
| 25217 | 368 | (* --------- Rewrite ----------- *) | 
| 369 | ||
| 60336 | 370 | fun rewrite computer raw_ct = | 
| 25217 | 371 | let | 
| 60336 | 372 | val thy = theory_of computer | 
| 373 | val ct = Thm.transfer_cterm thy raw_ct | |
| 59586 | 374 | val t' = Thm.term_of ct | 
| 375 | val ty = Thm.typ_of_cterm ct | |
| 26626 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 376 | val naming = naming_of computer | 
| 25217 | 377 | val (encoding, t) = remove_types (encoding_of computer) t' | 
| 378 | val t = runprog (prog_of computer) t | |
| 379 | val t = infer_types naming encoding ty t | |
| 26626 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 380 | val eq = Logic.mk_equals (t', t) | 
| 25217 | 381 | in | 
| 382 | export_thm thy (hyps_of computer) (Sorttab.keys (shyptab_of computer)) eq | |
| 383 | end | |
| 384 | ||
| 385 | (* --------- Simplify ------------ *) | |
| 23663 | 386 | |
| 25217 | 387 | datatype prem = EqPrem of AbstractMachine.term * AbstractMachine.term * Term.typ * int | 
| 26626 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 388 | | Prem of AbstractMachine.term | 
| 52788 | 389 | datatype theorem = Theorem of theory * unit Unsynchronized.ref * (int * typ) Symtab.table * (AbstractMachine.term option) Inttab.table | 
| 25217 | 390 | * prem list * AbstractMachine.term * term list * sort list | 
| 391 | ||
| 392 | ||
| 393 | exception ParamSimplify of computer * theorem | |
| 394 | ||
| 60336 | 395 | fun make_theorem computer raw_th vars = | 
| 25217 | 396 | let | 
| 60336 | 397 | val thy = theory_of computer | 
| 398 | val th = Thm.transfer thy raw_th | |
| 25217 | 399 | |
| 400 | val (ComputeThm (hyps, shyps, prop)) = thm2cthm th | |
| 401 | ||
| 402 | val encoding = encoding_of computer | |
| 403 | ||
| 404 | (* variables in the theorem are identified upfront *) | |
| 405 | fun collect_vars (Abs (_, _, t)) tab = collect_vars t tab | |
| 406 | | collect_vars (a $ b) tab = collect_vars b (collect_vars a tab) | |
| 407 | | collect_vars (Const _) tab = tab | |
| 408 | | collect_vars (Free _) tab = tab | |
| 409 | | collect_vars (Var ((s, i), ty)) tab = | |
| 26626 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 410 | if List.find (fn x => x=s) vars = NONE then | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 411 | tab | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 412 | else | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 413 | (case Symtab.lookup tab s of | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 414 | SOME ((s',i'),ty') => | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 415 | if s' <> s orelse i' <> i orelse ty <> ty' then | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 416 |                          raise Compute ("make_theorem: variable name '"^s^"' is not unique")
 | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 417 | else | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 418 | tab | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 419 | | NONE => Symtab.update (s, ((s, i), ty)) tab) | 
| 25217 | 420 | val vartab = collect_vars prop Symtab.empty | 
| 421 | fun encodevar (s, t as (_, ty)) (encoding, tab) = | |
| 26626 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 422 | let | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 423 | val (x, encoding) = Encode.insert (Var t) encoding | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 424 | in | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 425 | (encoding, Symtab.update (s, (x, ty)) tab) | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 426 | end | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 427 | val (encoding, vartab) = Symtab.fold encodevar vartab (encoding, Symtab.empty) | 
| 46531 | 428 | val varsubst = Inttab.make (map (fn (_, (x, _)) => (x, NONE)) (Symtab.dest vartab)) | 
| 23174 | 429 | |
| 25217 | 430 | (* make the premises and the conclusion *) | 
| 431 | fun mk_prem encoding t = | |
| 26626 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 432 | (let | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 433 | val (a, b) = Logic.dest_equals t | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 434 | val ty = type_of a | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 435 | val (encoding, a) = remove_types encoding a | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 436 | val (encoding, b) = remove_types encoding b | 
| 56245 | 437 | val (eq, encoding) = | 
| 69597 | 438 | Encode.insert (Const (\<^const_name>\<open>Pure.eq\<close>, ty --> ty --> \<^typ>\<open>prop\<close>)) encoding | 
| 26626 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 439 | in | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 440 | (encoding, EqPrem (a, b, ty, eq)) | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 441 | end handle TERM _ => let val (encoding, t) = remove_types encoding t in (encoding, Prem t) end) | 
| 25217 | 442 | val (encoding, prems) = | 
| 26626 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 443 | (fold_rev (fn t => fn (encoding, l) => | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 444 | case mk_prem encoding t of | 
| 25217 | 445 | (encoding, t) => (encoding, t::l)) (Logic.strip_imp_prems prop) (encoding, [])) | 
| 446 | val (encoding, concl) = remove_types encoding (Logic.strip_imp_concl prop) | |
| 447 | val _ = set_encoding computer encoding | |
| 448 | in | |
| 60336 | 449 | Theorem (thy, stamp_of computer, vartab, varsubst, prems, concl, hyps, shyps) | 
| 25217 | 450 | end | 
| 451 | ||
| 52788 | 452 | fun theory_of_theorem (Theorem (thy,_,_,_,_,_,_,_)) = thy | 
| 453 | fun update_theory thy (Theorem (_,p0,p1,p2,p3,p4,p5,p6)) = Theorem (thy,p0,p1,p2,p3,p4,p5,p6) | |
| 25217 | 454 | fun stamp_of_theorem (Theorem (_,s, _, _, _, _, _, _)) = s | 
| 455 | fun vartab_of_theorem (Theorem (_,_,vt,_,_,_,_,_)) = vt | |
| 456 | fun varsubst_of_theorem (Theorem (_,_,_,vs,_,_,_,_)) = vs | |
| 457 | fun update_varsubst vs (Theorem (p0,p1,p2,_,p3,p4,p5,p6)) = Theorem (p0,p1,p2,vs,p3,p4,p5,p6) | |
| 458 | fun prems_of_theorem (Theorem (_,_,_,_,prems,_,_,_)) = prems | |
| 459 | fun update_prems prems (Theorem (p0,p1,p2,p3,_,p4,p5,p6)) = Theorem (p0,p1,p2,p3,prems,p4,p5,p6) | |
| 460 | fun concl_of_theorem (Theorem (_,_,_,_,_,concl,_,_)) = concl | |
| 461 | fun hyps_of_theorem (Theorem (_,_,_,_,_,_,hyps,_)) = hyps | |
| 462 | fun update_hyps hyps (Theorem (p0,p1,p2,p3,p4,p5,_,p6)) = Theorem (p0,p1,p2,p3,p4,p5,hyps,p6) | |
| 463 | fun shyps_of_theorem (Theorem (_,_,_,_,_,_,_,shyps)) = shyps | |
| 464 | fun update_shyps shyps (Theorem (p0,p1,p2,p3,p4,p5,p6,_)) = Theorem (p0,p1,p2,p3,p4,p5,p6,shyps) | |
| 465 | ||
| 466 | fun check_compatible computer th s = | |
| 467 | if stamp_of computer <> stamp_of_theorem th then | |
| 26626 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 468 | raise Compute (s^": computer and theorem are incompatible") | 
| 25217 | 469 | else () | 
| 470 | ||
| 471 | fun instantiate computer insts th = | |
| 472 | let | |
| 473 | val _ = check_compatible computer th | |
| 474 | ||
| 475 | val thy = theory_of computer | |
| 476 | ||
| 477 | val vartab = vartab_of_theorem th | |
| 478 | ||
| 479 | fun rewrite computer t = | |
| 480 | let | |
| 481 | val (encoding, t) = remove_types (encoding_of computer) t | |
| 482 | val t = runprog (prog_of computer) t | |
| 26626 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 483 | val _ = set_encoding computer encoding | 
| 23174 | 484 | in | 
| 485 | t | |
| 486 | end | |
| 487 | ||
| 25217 | 488 | fun assert_varfree vs t = | 
| 26626 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 489 | if AbstractMachine.forall_consts (fn x => Inttab.lookup vs x = NONE) t then | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 490 | () | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 491 | else | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 492 | raise Compute "instantiate: assert_varfree failed" | 
| 25217 | 493 | |
| 494 | fun assert_closed t = | |
| 26626 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 495 | if AbstractMachine.closed t then | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 496 | () | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 497 | else | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 498 | raise Compute "instantiate: not a closed term" | 
| 23663 | 499 | |
| 60336 | 500 | fun compute_inst (s, raw_ct) vs = | 
| 26626 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 501 | let | 
| 60336 | 502 | val ct = Thm.transfer_cterm thy raw_ct | 
| 59586 | 503 | val ty = Thm.typ_of_cterm ct | 
| 26626 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 504 | in | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 505 | (case Symtab.lookup vartab s of | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 506 |                  NONE => raise Compute ("instantiate: variable '"^s^"' not found in theorem")
 | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 507 | | SOME (x, ty') => | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 508 | (case Inttab.lookup vs x of | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 509 |                       SOME (SOME _) => raise Compute ("instantiate: variable '"^s^"' has already been instantiated")
 | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 510 | | SOME NONE => | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 511 | if ty <> ty' then | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 512 |                           raise Compute ("instantiate: wrong type for variable '"^s^"'")
 | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 513 | else | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 514 | let | 
| 59582 | 515 | val t = rewrite computer (Thm.term_of ct) | 
| 26626 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 516 | val _ = assert_varfree vs t | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 517 | val _ = assert_closed t | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 518 | in | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 519 | Inttab.update (x, SOME t) vs | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 520 | end | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 521 | | NONE => raise Compute "instantiate: internal error")) | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 522 | end | 
| 23174 | 523 | |
| 25217 | 524 | val vs = fold compute_inst insts (varsubst_of_theorem th) | 
| 525 | in | |
| 526 | update_varsubst vs th | |
| 527 | end | |
| 23174 | 528 | |
| 25217 | 529 | fun match_aterms subst = | 
| 26626 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 530 | let | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 531 | exception no_match | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 532 | open AbstractMachine | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 533 | fun match subst (b as (Const c)) a = | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 534 | if a = b then subst | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 535 | else | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 536 | (case Inttab.lookup subst c of | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 537 | SOME (SOME a') => if a=a' then subst else raise no_match | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 538 | | SOME NONE => if AbstractMachine.closed a then | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 539 | Inttab.update (c, SOME a) subst | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 540 | else raise no_match | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 541 | | NONE => raise no_match) | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 542 | | match subst (b as (Var _)) a = if a=b then subst else raise no_match | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 543 | | match subst (App (u, v)) (App (u', v')) = match (match subst u u') v v' | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 544 | | match subst (Abs u) (Abs u') = match subst u u' | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 545 | | match subst _ _ = raise no_match | 
| 23663 | 546 | in | 
| 26626 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 547 | fn b => fn a => (SOME (match subst b a) handle no_match => NONE) | 
| 25217 | 548 | end | 
| 549 | ||
| 550 | fun apply_subst vars_allowed subst = | |
| 551 | let | |
| 26626 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 552 | open AbstractMachine | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 553 | fun app (t as (Const c)) = | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 554 | (case Inttab.lookup subst c of | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 555 | NONE => t | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 556 | | SOME (SOME t) => Computed t | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 557 | | SOME NONE => if vars_allowed then t else raise Compute "apply_subst: no vars allowed") | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 558 | | app (t as (Var _)) = t | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 559 | | app (App (u, v)) = App (app u, app v) | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 560 | | app (Abs m) = Abs (app m) | 
| 25217 | 561 | in | 
| 26626 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 562 | app | 
| 23663 | 563 | end | 
| 564 | ||
| 25217 | 565 | fun splicein n l L = List.take (L, n) @ l @ List.drop (L, n+1) | 
| 23663 | 566 | |
| 25217 | 567 | fun evaluate_prem computer prem_no th = | 
| 568 | let | |
| 569 | val _ = check_compatible computer th | |
| 570 | val prems = prems_of_theorem th | |
| 571 | val varsubst = varsubst_of_theorem th | |
| 572 | fun run vars_allowed t = | |
| 26626 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 573 | runprog (prog_of computer) (apply_subst vars_allowed varsubst t) | 
| 25217 | 574 | in | 
| 42364 | 575 | case nth prems prem_no of | 
| 26626 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 576 | Prem _ => raise Compute "evaluate_prem: no equality premise" | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 577 | | EqPrem (a, b, ty, _) => | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 578 | let | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 579 | val a' = run false a | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 580 | val b' = run true b | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 581 | in | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 582 | case match_aterms varsubst b' a' of | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 583 | NONE => | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 584 | let | 
| 69597 | 585 | fun mk s = Syntax.string_of_term_global \<^theory>\<open>Pure\<close> | 
| 31322 
526e149999cc
attempt to eliminate adhoc makestring at runtime (which is not well-defined);
 wenzelm parents: 
30288diff
changeset | 586 | (infer_types (naming_of computer) (encoding_of computer) ty s) | 
| 26626 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 587 | val left = "computed left side: "^(mk a') | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 588 | val right = "computed right side: "^(mk b') | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 589 | in | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 590 |                     raise Compute ("evaluate_prem: cannot assign computed left to right hand side\n"^left^"\n"^right^"\n")
 | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 591 | end | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 592 | | SOME varsubst => | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 593 | update_prems (splicein prem_no [] prems) (update_varsubst varsubst th) | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 594 | end | 
| 25217 | 595 | end | 
| 23663 | 596 | |
| 25217 | 597 | fun prem2term (Prem t) = t | 
| 598 | | prem2term (EqPrem (a,b,_,eq)) = | |
| 599 | AbstractMachine.App (AbstractMachine.App (AbstractMachine.Const eq, a), b) | |
| 23663 | 600 | |
| 60336 | 601 | fun modus_ponens computer prem_no raw_th' th = | 
| 25217 | 602 | let | 
| 60336 | 603 | val thy = theory_of computer | 
| 25217 | 604 | val _ = check_compatible computer th | 
| 60336 | 605 | val _ = | 
| 60948 | 606 | Context.subthy (theory_of_theorem th, thy) orelse raise Compute "modus_ponens: bad theory" | 
| 60336 | 607 | val th' = make_theorem computer (Thm.transfer thy raw_th') [] | 
| 25217 | 608 | val varsubst = varsubst_of_theorem th | 
| 609 | fun run vars_allowed t = | |
| 26626 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 610 | runprog (prog_of computer) (apply_subst vars_allowed varsubst t) | 
| 25217 | 611 | val prems = prems_of_theorem th | 
| 42364 | 612 | val prem = run true (prem2term (nth prems prem_no)) | 
| 25217 | 613 | val concl = run false (concl_of_theorem th') | 
| 614 | in | |
| 615 | case match_aterms varsubst prem concl of | |
| 26626 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 616 | NONE => raise Compute "modus_ponens: conclusion does not match premise" | 
| 25217 | 617 | | SOME varsubst => | 
| 26626 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 618 | let | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 619 | val th = update_varsubst varsubst th | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 620 | val th = update_prems (splicein prem_no (prems_of_theorem th') prems) th | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 621 | val th = update_hyps (merge_hyps (hyps_of_theorem th) (hyps_of_theorem th')) th | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 622 | val th = update_shyps (merge_shyps (shyps_of_theorem th) (shyps_of_theorem th')) th | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 623 | in | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 624 | update_theory thy th | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
25520diff
changeset | 625 | end | 
| 25217 | 626 | end | 
| 627 | ||
| 628 | fun simplify computer th = | |
| 629 | let | |
| 630 | val _ = check_compatible computer th | |
| 631 | val varsubst = varsubst_of_theorem th | |
| 632 | val encoding = encoding_of computer | |
| 633 | val naming = naming_of computer | |
| 69597 | 634 | fun infer t = infer_types naming encoding \<^typ>\<open>prop\<close> t | 
| 25217 | 635 | fun run t = infer (runprog (prog_of computer) (apply_subst true varsubst t)) | 
| 636 | fun runprem p = run (prem2term p) | |
| 637 | val prop = Logic.list_implies (map runprem (prems_of_theorem th), run (concl_of_theorem th)) | |
| 638 | val hyps = merge_hyps (hyps_of computer) (hyps_of_theorem th) | |
| 639 | val shyps = merge_shyps (shyps_of_theorem th) (Sorttab.keys (shyptab_of computer)) | |
| 640 | in | |
| 641 | export_thm (theory_of_theorem th) hyps shyps prop | |
| 642 | end | |
| 23174 | 643 | |
| 644 | end | |
| 23663 | 645 |