src/Pure/Tools/compute.ML
author urbanc
Fri, 17 Nov 2006 17:32:30 +0100
changeset 21405 26b51f724fe6
parent 19502 369cde91963d
child 22705 6199df39688d
permissions -rw-r--r--
added an intro lemma for freshness of products; set up the simplifier so that it can deal with the compact and long notation for freshness constraints (FIXME: it should also be able to deal with the special case of freshness of atoms)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16781
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
     1
(*  Title:      Pure/Tools/compute.ML
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
     2
    ID:         $Id$
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
     3
    Author:     Steven Obua
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
     4
*)
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
     5
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
     6
signature COMPUTE = sig
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
     7
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
     8
    type computer
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
     9
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    10
    exception Make of string
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    11
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    12
    val basic_make : theory -> thm list -> computer
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    13
    val make : theory -> thm list -> computer
16851
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
    14
16781
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    15
    val compute : computer -> (int -> string) -> cterm -> term
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    16
    val theory_of : computer -> theory
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    17
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    18
    val rewrite_param : computer -> (int -> string) -> cterm -> thm
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    19
    val rewrite : computer -> cterm -> thm
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    20
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    21
end
16851
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
    22
17795
5b18c3343028 minor tweaks for Poplog/PML;
wenzelm
parents: 17412
diff changeset
    23
structure Compute : COMPUTE = struct
16781
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    24
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    25
exception Make of string;
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    26
19482
9f11af8f7ef9 tuned basic list operators (flat, maps, map_filter);
wenzelm
parents: 18708
diff changeset
    27
fun is_mono_typ (Type (_, list)) = forall is_mono_typ list
16781
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    28
  | is_mono_typ _ = false
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    29
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    30
fun is_mono_term (Const (_, t)) = is_mono_typ t
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    31
  | is_mono_term (Var (_, t)) = is_mono_typ t
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    32
  | is_mono_term (Free (_, t)) = is_mono_typ t
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    33
  | is_mono_term (Bound _) = true
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    34
  | is_mono_term (a $ b) = is_mono_term a andalso is_mono_term b
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    35
  | is_mono_term (Abs (_, ty, t)) = is_mono_typ ty andalso is_mono_term t
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    36
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    37
structure AMTermTab = TableFun (type key = AbstractMachine.term val ord = AM_Util.term_ord)
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    38
17795
5b18c3343028 minor tweaks for Poplog/PML;
wenzelm
parents: 17412
diff changeset
    39
fun add x y = x + y : int;
5b18c3343028 minor tweaks for Poplog/PML;
wenzelm
parents: 17412
diff changeset
    40
fun inc x = x + 1;
16781
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    41
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    42
exception Mono of term;
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    43
16851
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
    44
val remove_types =
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
    45
    let
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
    46
        fun remove_types_var table invtable ccount vcount ldepth t =
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17223
diff changeset
    47
            (case Termtab.lookup table t of
16851
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
    48
                 NONE =>
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
    49
                 let
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
    50
                     val a = AbstractMachine.Var vcount
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
    51
                 in
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17223
diff changeset
    52
                     (Termtab.update (t, a) table,
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17223
diff changeset
    53
                      AMTermTab.update (a, t) invtable,
16851
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
    54
                      ccount,
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
    55
                      inc vcount,
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
    56
                      AbstractMachine.Var (add vcount ldepth))
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
    57
                 end
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
    58
               | SOME (AbstractMachine.Var v) =>
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
    59
                 (table, invtable, ccount, vcount, AbstractMachine.Var (add v ldepth))
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
    60
               | SOME _ => sys_error "remove_types_var: lookup should be a var")
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
    61
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
    62
        fun remove_types_const table invtable ccount vcount ldepth t =
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17223
diff changeset
    63
            (case Termtab.lookup table t of
16851
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
    64
                 NONE =>
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
    65
                 let
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
    66
                     val a = AbstractMachine.Const ccount
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
    67
                 in
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17223
diff changeset
    68
                     (Termtab.update (t, a) table,
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17223
diff changeset
    69
                      AMTermTab.update (a, t) invtable,
16851
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
    70
                      inc ccount,
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
    71
                      vcount,
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
    72
                      a)
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
    73
                 end
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
    74
               | SOME (c as AbstractMachine.Const _) =>
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
    75
                 (table, invtable, ccount, vcount, c)
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
    76
               | SOME _ => sys_error "remove_types_const: lookup should be a const")
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
    77
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
    78
        fun remove_types table invtable ccount vcount ldepth t =
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
    79
            case t of
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
    80
                Var (_, ty) =>
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
    81
                  if is_mono_typ ty then remove_types_var table invtable ccount vcount ldepth t
16781
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    82
                  else raise (Mono t)
16851
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
    83
              | Free (_, ty) =>
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
    84
                  if is_mono_typ ty then remove_types_var table invtable ccount vcount ldepth t
16781
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    85
                  else raise (Mono t)
16851
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
    86
              | Const (_, ty) =>
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
    87
                  if is_mono_typ ty then remove_types_const table invtable ccount vcount ldepth t
16781
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
    88
                  else raise (Mono t)
16851
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
    89
              | Abs (_, ty, t') =>
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
    90
                if is_mono_typ ty then
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
    91
                    let
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
    92
                        val (table, invtable, ccount, vcount, t') =
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
    93
                            remove_types table invtable ccount vcount (inc ldepth) t'
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
    94
                    in
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
    95
                        (table, invtable, ccount, vcount, AbstractMachine.Abs t')
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
    96
                    end
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
    97
                else
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
    98
                    raise (Mono t)
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
    99
              | a $ b =>
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   100
                let
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   101
                    val (table, invtable, ccount, vcount, a) =
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   102
                        remove_types table invtable ccount vcount ldepth a
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   103
                    val (table, invtable, ccount, vcount, b) =
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   104
                        remove_types table invtable ccount vcount ldepth b
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   105
                in
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   106
                    (table, invtable, ccount, vcount, AbstractMachine.App (a,b))
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   107
                end
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   108
              | Bound b => (table, invtable, ccount, vcount, AbstractMachine.Var b)
16781
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   109
    in
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   110
     fn (table, invtable, ccount, vcount) => remove_types table invtable ccount vcount 0
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   111
    end
16851
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   112
16781
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   113
fun infer_types naming =
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   114
    let
16851
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   115
        fun infer_types invtable ldepth bounds ty (AbstractMachine.Var v) =
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   116
            if v < ldepth then (Bound v, List.nth (bounds, v)) else
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17223
diff changeset
   117
            (case AMTermTab.lookup invtable (AbstractMachine.Var (v-ldepth)) of
16851
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   118
                 SOME (t as Var (_, ty)) => (t, ty)
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   119
               | SOME (t as Free (_, ty)) => (t, ty)
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   120
               | _ => sys_error "infer_types: lookup should deliver Var or Free")
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   121
          | infer_types invtable ldepth _ ty (c as AbstractMachine.Const _) =
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17223
diff changeset
   122
            (case AMTermTab.lookup invtable c of
16851
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   123
                 SOME (c as Const (_, ty)) => (c, ty)
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   124
               | _ => sys_error "infer_types: lookup should deliver Const")
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   125
          | infer_types invtable ldepth bounds (n,ty) (AbstractMachine.App (a, b)) =
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   126
            let
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   127
                val (a, aty) = infer_types invtable ldepth bounds (n+1, ty) a
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   128
                val (adom, arange) =
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   129
                    case aty of
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   130
                        Type ("fun", [dom, range]) => (dom, range)
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   131
                      | _ => sys_error "infer_types: function type expected"
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   132
                val (b, bty) = infer_types invtable ldepth bounds (0, adom) b
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   133
            in
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   134
                (a $ b, arange)
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   135
            end
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   136
          | infer_types invtable ldepth bounds (0, ty as Type ("fun", [dom, range]))
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   137
              (AbstractMachine.Abs m) =
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   138
            let
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   139
                val (m, _) = infer_types invtable (ldepth+1) (dom::bounds) (0, range) m
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   140
            in
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   141
                (Abs (naming ldepth, dom, m), ty)
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   142
            end
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   143
          | infer_types invtable ldepth bounds ty (AbstractMachine.Abs m) =
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   144
            sys_error "infer_types: cannot infer type of abstraction"
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   145
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   146
        fun infer invtable ty term =
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   147
            let
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   148
                val (term', _) = infer_types invtable 0 [] (0, ty) term
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   149
            in
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   150
                term'
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   151
            end
16781
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   152
    in
16851
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   153
        infer
16781
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   154
    end
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   155
17795
5b18c3343028 minor tweaks for Poplog/PML;
wenzelm
parents: 17412
diff changeset
   156
datatype computer =
5b18c3343028 minor tweaks for Poplog/PML;
wenzelm
parents: 17412
diff changeset
   157
  Computer of theory_ref *
5b18c3343028 minor tweaks for Poplog/PML;
wenzelm
parents: 17412
diff changeset
   158
    (AbstractMachine.term Termtab.table * term AMTermTab.table * int * AbstractMachine.program)
16781
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   159
16851
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   160
fun basic_make thy raw_ths =
16781
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   161
    let
16851
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   162
        val ths = map (Thm.transfer thy) raw_ths;
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   163
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   164
        fun thm2rule table invtable ccount th =
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   165
            let
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   166
                val prop = Drule.plain_prop_of th
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   167
                  handle THM _ => raise (Make "theorems must be plain propositions")
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   168
                val (a, b) = Logic.dest_equals prop
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   169
                  handle TERM _ => raise (Make "theorems must be meta-level equations")
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   170
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   171
                val (table, invtable, ccount, vcount, prop) =
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   172
                    remove_types (table, invtable, ccount, 0) (a$b)
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   173
                    handle Mono _ => raise (Make "no type variables allowed")
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   174
                val (left, right) =
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   175
                    (case prop of AbstractMachine.App x => x | _ =>
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   176
                      sys_error "make: remove_types should deliver application")
16781
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   177
16851
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   178
                fun make_pattern table invtable n vars (var as AbstractMachine.Var v) =
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   179
                    let
19502
wenzelm
parents: 19482
diff changeset
   180
                        val var' = the (AMTermTab.lookup invtable var)
16851
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   181
                        val table = Termtab.delete var' table
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   182
                        val invtable = AMTermTab.delete var invtable
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17223
diff changeset
   183
                        val vars = Inttab.update_new (v, n) vars handle Inttab.DUP _ =>
16851
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   184
                          raise (Make "no duplicate variable in pattern allowed")
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   185
                    in
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   186
                        (table, invtable, n+1, vars, AbstractMachine.PVar)
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   187
                    end
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   188
                  | make_pattern table invtable n vars (AbstractMachine.Abs _) =
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   189
                      raise (Make "no lambda abstractions allowed in pattern")
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   190
                  | make_pattern table invtable n vars (AbstractMachine.Const c) =
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   191
                    (table, invtable, n, vars, AbstractMachine.PConst (c, []))
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   192
                  | make_pattern table invtable n vars (AbstractMachine.App (a, b)) =
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   193
                    let
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   194
                        val (table, invtable, n, vars, pa) =
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   195
                            make_pattern table invtable n vars a
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   196
                        val (table, invtable, n, vars, pb) =
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   197
                            make_pattern table invtable n vars b
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   198
                    in
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   199
                        case pa of
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   200
                            AbstractMachine.PVar =>
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   201
                              raise (Make "patterns may not start with a variable")
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   202
                          | AbstractMachine.PConst (c, args) =>
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   203
                              (table, invtable, n, vars, AbstractMachine.PConst (c, args@[pb]))
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   204
                    end
16781
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   205
16851
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   206
                val (table, invtable, vcount, vars, pattern) =
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   207
                    make_pattern table invtable 0 Inttab.empty left
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   208
                val _ = (case pattern of
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   209
                           AbstractMachine.PVar =>
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   210
                             raise (Make "patterns may not start with a variable")
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   211
                         | _ => ())
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   212
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   213
                (* at this point, there shouldn't be any variables
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   214
                  left in table or invtable, only constants *)
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   215
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   216
                (* finally, provide a function for renaming the
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   217
                  pattern bound variables on the right hand side *)
16781
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   218
16851
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   219
                fun rename ldepth vars (var as AbstractMachine.Var v) =
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   220
                    if v < ldepth then var
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17223
diff changeset
   221
                    else (case Inttab.lookup vars (v - ldepth) of
16851
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   222
                              NONE => raise (Make "new variable on right hand side")
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   223
                            | SOME n => AbstractMachine.Var ((vcount-n-1)+ldepth))
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   224
                  | rename ldepth vars (c as AbstractMachine.Const _) = c
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   225
                  | rename ldepth vars (AbstractMachine.App (a, b)) =
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   226
                    AbstractMachine.App (rename ldepth vars a, rename ldepth vars b)
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   227
                  | rename ldepth vars (AbstractMachine.Abs m) =
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   228
                    AbstractMachine.Abs (rename (ldepth+1) vars m)
16781
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   229
16851
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   230
            in
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   231
                (table, invtable, ccount, (pattern, rename 0 vars right))
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   232
            end
16781
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   233
16851
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   234
        val (table, invtable, ccount, rules) =
17223
430edc6b7826 curried_lookup/update;
wenzelm
parents: 16851
diff changeset
   235
          fold_rev (fn th => fn (table, invtable, ccount, rules) =>
430edc6b7826 curried_lookup/update;
wenzelm
parents: 16851
diff changeset
   236
            let
430edc6b7826 curried_lookup/update;
wenzelm
parents: 16851
diff changeset
   237
              val (table, invtable, ccount, rule) =
430edc6b7826 curried_lookup/update;
wenzelm
parents: 16851
diff changeset
   238
                  thm2rule table invtable ccount th
430edc6b7826 curried_lookup/update;
wenzelm
parents: 16851
diff changeset
   239
            in (table, invtable, ccount, rule::rules) end)
430edc6b7826 curried_lookup/update;
wenzelm
parents: 16851
diff changeset
   240
          ths (Termtab.empty, AMTermTab.empty, 0, [])
16851
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   241
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   242
        val prog = AbstractMachine.compile rules
16781
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   243
17795
5b18c3343028 minor tweaks for Poplog/PML;
wenzelm
parents: 17412
diff changeset
   244
    in Computer (Theory.self_ref thy, (table, invtable, ccount, prog)) end
16781
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   245
16851
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   246
fun make thy ths =
16781
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   247
    let
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   248
      val (_, {mk_rews={mk=mk,mk_eq_True=emk, ...},...}) = rep_ss (simpset_of thy)
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   249
      fun mk_eq_True th = (case emk th of NONE => [th] | SOME th' => [th, th'])
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   250
    in
19482
9f11af8f7ef9 tuned basic list operators (flat, maps, map_filter);
wenzelm
parents: 18708
diff changeset
   251
      basic_make thy (maps mk (maps mk_eq_True ths))
16781
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   252
    end
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   253
17795
5b18c3343028 minor tweaks for Poplog/PML;
wenzelm
parents: 17412
diff changeset
   254
fun compute (Computer r) naming ct =
16781
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   255
    let
16851
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   256
        val {t=t, T=ty, thy=ctthy, ...} = rep_cterm ct
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   257
        val (rthy, (table, invtable, ccount, prog)) = r
16781
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   258
        val thy = Theory.merge (Theory.deref rthy, ctthy)
16851
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   259
        val (table, invtable, ccount, vcount, t) = remove_types (table, invtable, ccount, 0) t
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   260
        val t = AbstractMachine.run prog t
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   261
        val t = infer_types naming invtable ty t
16781
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   262
    in
16851
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   263
        t
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   264
    end
16781
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   265
17795
5b18c3343028 minor tweaks for Poplog/PML;
wenzelm
parents: 17412
diff changeset
   266
fun theory_of (Computer (rthy, _)) = Theory.deref rthy
16851
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   267
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   268
fun default_naming i = "v_" ^ Int.toString i
16781
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   269
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   270
exception Param of computer * (int -> string) * cterm;
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   271
16851
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   272
fun rewrite_param r n ct =
16781
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   273
    let val thy = theory_of_cterm ct in
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   274
      invoke_oracle_i thy "Pure.compute" (thy, Param (r, n, ct))
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   275
    end
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   276
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   277
fun rewrite r ct = rewrite_param r default_naming ct
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   278
18708
4b3dadb4fe33 setup: theory -> theory;
wenzelm
parents: 17795
diff changeset
   279
4b3dadb4fe33 setup: theory -> theory;
wenzelm
parents: 17795
diff changeset
   280
(* theory setup *)
4b3dadb4fe33 setup: theory -> theory;
wenzelm
parents: 17795
diff changeset
   281
16851
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   282
fun compute_oracle (thy, Param (r, naming, ct)) =
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   283
    let
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   284
        val _ = Theory.assert_super (theory_of r) thy
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   285
        val t' = compute r naming ct
16781
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   286
    in
16851
551462cc8ca0 use sys_error instead of exception Internal;
wenzelm
parents: 16781
diff changeset
   287
        Logic.mk_equals (term_of ct, t')
16781
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   288
    end
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   289
18708
4b3dadb4fe33 setup: theory -> theory;
wenzelm
parents: 17795
diff changeset
   290
val _ = Context.add_setup (Theory.add_oracle ("compute", compute_oracle))
16781
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   291
663235466562 - introduce Pure/Tools directory
obua
parents:
diff changeset
   292
end