src/HOLCF/Tools/fixrec.ML
author huffman
Mon, 22 Mar 2010 14:44:37 -0700
changeset 35903 0b43ff2d2e91
parent 35780 98fd7910f70a
child 36628 1a251f69e96b
permissions -rw-r--r--
fix ML warnings in fixrec.ML
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31738
7b9b9ba532ca discontinued ancient tradition to suffix certain ML module names with "_package"
haftmann
parents: 31177
diff changeset
     1
(*  Title:      HOLCF/Tools/fixrec.ML
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
     2
    Author:     Amber Telfer and Brian Huffman
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
     3
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
     4
Recursive function definition package for HOLCF.
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
     5
*)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
     6
31738
7b9b9ba532ca discontinued ancient tradition to suffix certain ML module names with "_package"
haftmann
parents: 31177
diff changeset
     7
signature FIXREC =
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
     8
sig
30485
99def5248e7f removed legacy_infer_term, legacy_infer_prop;
wenzelm
parents: 30364
diff changeset
     9
  val add_fixrec: bool -> (binding * typ option * mixfix) list
99def5248e7f removed legacy_infer_term, legacy_infer_prop;
wenzelm
parents: 30364
diff changeset
    10
    -> (Attrib.binding * term) list -> local_theory -> local_theory
99def5248e7f removed legacy_infer_term, legacy_infer_prop;
wenzelm
parents: 30364
diff changeset
    11
  val add_fixrec_cmd: bool -> (binding * string option * mixfix) list
30158
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
    12
    -> (Attrib.binding * string) list -> local_theory -> local_theory
30485
99def5248e7f removed legacy_infer_term, legacy_infer_prop;
wenzelm
parents: 30364
diff changeset
    13
  val add_fixpat: Thm.binding * term list -> theory -> theory
99def5248e7f removed legacy_infer_term, legacy_infer_prop;
wenzelm
parents: 30364
diff changeset
    14
  val add_fixpat_cmd: Attrib.binding * string list -> theory -> theory
30131
6be1be402ef0 use TheoryData to keep track of pattern match combinators
huffman
parents: 29585
diff changeset
    15
  val add_matchers: (string * string) list -> theory -> theory
33442
5d78b2bd27de made SML/NJ happy;
wenzelm
parents: 33430
diff changeset
    16
  val fixrec_simp_add: attribute
5d78b2bd27de made SML/NJ happy;
wenzelm
parents: 33430
diff changeset
    17
  val fixrec_simp_del: attribute
33425
7e4f3c66190d add fixrec_simp attribute and method (eventually to replace fixpat)
huffman
parents: 33401
diff changeset
    18
  val fixrec_simp_tac: Proof.context -> int -> tactic
30131
6be1be402ef0 use TheoryData to keep track of pattern match combinators
huffman
parents: 29585
diff changeset
    19
  val setup: theory -> theory
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    20
end;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    21
31738
7b9b9ba532ca discontinued ancient tradition to suffix certain ML module names with "_package"
haftmann
parents: 31177
diff changeset
    22
structure Fixrec :> FIXREC =
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    23
struct
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    24
35527
f4282471461d fixrec and repdef modules import holcf_library
huffman
parents: 35525
diff changeset
    25
open HOLCF_Library;
f4282471461d fixrec and repdef modules import holcf_library
huffman
parents: 35525
diff changeset
    26
f4282471461d fixrec and repdef modules import holcf_library
huffman
parents: 35525
diff changeset
    27
infixr 6 ->>;
f4282471461d fixrec and repdef modules import holcf_library
huffman
parents: 35525
diff changeset
    28
infix -->>;
f4282471461d fixrec and repdef modules import holcf_library
huffman
parents: 35525
diff changeset
    29
infix 9 `;
f4282471461d fixrec and repdef modules import holcf_library
huffman
parents: 35525
diff changeset
    30
31095
b79d140f6d0b simplify fixrec proofs for mutually-recursive definitions; generate better fixpoint induction rules
huffman
parents: 31023
diff changeset
    31
val def_cont_fix_eq = @{thm def_cont_fix_eq};
b79d140f6d0b simplify fixrec proofs for mutually-recursive definitions; generate better fixpoint induction rules
huffman
parents: 31023
diff changeset
    32
val def_cont_fix_ind = @{thm def_cont_fix_ind};
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    33
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    34
fun fixrec_err s = error ("fixrec definition error:\n" ^ s);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    35
fun fixrec_eq_err thy s eq =
26939
1035c89b4c02 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26343
diff changeset
    36
  fixrec_err (s ^ "\nin\n" ^ quote (Syntax.string_of_term_global thy eq));
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    37
30132
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    38
(*************************************************************************)
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    39
(***************************** building types ****************************)
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    40
(*************************************************************************)
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    41
33401
fc43fa403a69 add fixrec support for HOL pair constructor patterns
huffman
parents: 33004
diff changeset
    42
local
fc43fa403a69 add fixrec support for HOL pair constructor patterns
huffman
parents: 33004
diff changeset
    43
35525
fa231b86cb1e proper names for types cfun, sprod, ssum
huffman
parents: 33766
diff changeset
    44
fun binder_cfun (Type(@{type_name cfun},[T, U])) = T :: binder_cfun U
33401
fc43fa403a69 add fixrec support for HOL pair constructor patterns
huffman
parents: 33004
diff changeset
    45
  | binder_cfun (Type(@{type_name "fun"},[T, U])) = T :: binder_cfun U
fc43fa403a69 add fixrec support for HOL pair constructor patterns
huffman
parents: 33004
diff changeset
    46
  | binder_cfun _   =  [];
fc43fa403a69 add fixrec support for HOL pair constructor patterns
huffman
parents: 33004
diff changeset
    47
35525
fa231b86cb1e proper names for types cfun, sprod, ssum
huffman
parents: 33766
diff changeset
    48
fun body_cfun (Type(@{type_name cfun},[T, U])) = body_cfun U
33401
fc43fa403a69 add fixrec support for HOL pair constructor patterns
huffman
parents: 33004
diff changeset
    49
  | body_cfun (Type(@{type_name "fun"},[T, U])) = body_cfun U
fc43fa403a69 add fixrec support for HOL pair constructor patterns
huffman
parents: 33004
diff changeset
    50
  | body_cfun T   =  T;
fc43fa403a69 add fixrec support for HOL pair constructor patterns
huffman
parents: 33004
diff changeset
    51
fc43fa403a69 add fixrec support for HOL pair constructor patterns
huffman
parents: 33004
diff changeset
    52
fun strip_cfun T : typ list * typ =
fc43fa403a69 add fixrec support for HOL pair constructor patterns
huffman
parents: 33004
diff changeset
    53
  (binder_cfun T, body_cfun T);
fc43fa403a69 add fixrec support for HOL pair constructor patterns
huffman
parents: 33004
diff changeset
    54
fc43fa403a69 add fixrec support for HOL pair constructor patterns
huffman
parents: 33004
diff changeset
    55
in
fc43fa403a69 add fixrec support for HOL pair constructor patterns
huffman
parents: 33004
diff changeset
    56
35527
f4282471461d fixrec and repdef modules import holcf_library
huffman
parents: 35525
diff changeset
    57
fun matcherT (T, U) =
f4282471461d fixrec and repdef modules import holcf_library
huffman
parents: 35525
diff changeset
    58
  body_cfun T ->> (binder_cfun T -->> U) ->> U;
30912
4022298c1a86 change definition of match combinators for fixrec package
huffman
parents: 30485
diff changeset
    59
33401
fc43fa403a69 add fixrec support for HOL pair constructor patterns
huffman
parents: 33004
diff changeset
    60
end
30132
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    61
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    62
(*************************************************************************)
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    63
(***************************** building terms ****************************)
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    64
(*************************************************************************)
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    65
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    66
val mk_trp = HOLogic.mk_Trueprop;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    67
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    68
(* splits a cterm into the right and lefthand sides of equality *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    69
fun dest_eqs t = HOLogic.dest_eq (HOLogic.dest_Trueprop t);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    70
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    71
(* similar to Thm.head_of, but for continuous application *)
26045
02aa3f166c7f use ML antiquotations
huffman
parents: 25557
diff changeset
    72
fun chead_of (Const(@{const_name Rep_CFun},_)$f$t) = chead_of f
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    73
  | chead_of u = u;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    74
30132
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    75
infix 0 ==;  val (op ==) = Logic.mk_equals;
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    76
infix 1 ===; val (op ===) = HOLogic.mk_eq;
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    77
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    78
fun mk_mplus (t, u) =
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    79
  let val mT = Term.fastype_of t
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    80
  in Const(@{const_name Fixrec.mplus}, mT ->> mT ->> mT) ` t ` u end;
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    81
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    82
fun mk_run t =
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    83
  let val mT = Term.fastype_of t
35527
f4282471461d fixrec and repdef modules import holcf_library
huffman
parents: 35525
diff changeset
    84
      val T = dest_matchT mT
30132
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    85
  in Const(@{const_name Fixrec.run}, mT ->> T) ` t end;
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    86
31095
b79d140f6d0b simplify fixrec proofs for mutually-recursive definitions; generate better fixpoint induction rules
huffman
parents: 31023
diff changeset
    87
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    88
(*************************************************************************)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    89
(************* fixed-point definitions and unfolding theorems ************)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    90
(*************************************************************************)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    91
33519
e31a85f92ce9 adapted Generic_Data, Proof_Data;
wenzelm
parents: 33507
diff changeset
    92
structure FixrecUnfoldData = Generic_Data
e31a85f92ce9 adapted Generic_Data, Proof_Data;
wenzelm
parents: 33507
diff changeset
    93
(
33425
7e4f3c66190d add fixrec_simp attribute and method (eventually to replace fixpat)
huffman
parents: 33401
diff changeset
    94
  type T = thm Symtab.table;
7e4f3c66190d add fixrec_simp attribute and method (eventually to replace fixpat)
huffman
parents: 33401
diff changeset
    95
  val empty = Symtab.empty;
7e4f3c66190d add fixrec_simp attribute and method (eventually to replace fixpat)
huffman
parents: 33401
diff changeset
    96
  val extend = I;
33519
e31a85f92ce9 adapted Generic_Data, Proof_Data;
wenzelm
parents: 33507
diff changeset
    97
  fun merge data : T = Symtab.merge (K true) data;
33425
7e4f3c66190d add fixrec_simp attribute and method (eventually to replace fixpat)
huffman
parents: 33401
diff changeset
    98
);
7e4f3c66190d add fixrec_simp attribute and method (eventually to replace fixpat)
huffman
parents: 33401
diff changeset
    99
7e4f3c66190d add fixrec_simp attribute and method (eventually to replace fixpat)
huffman
parents: 33401
diff changeset
   100
local
7e4f3c66190d add fixrec_simp attribute and method (eventually to replace fixpat)
huffman
parents: 33401
diff changeset
   101
7e4f3c66190d add fixrec_simp attribute and method (eventually to replace fixpat)
huffman
parents: 33401
diff changeset
   102
fun name_of (Const (n, T)) = n
7e4f3c66190d add fixrec_simp attribute and method (eventually to replace fixpat)
huffman
parents: 33401
diff changeset
   103
  | name_of (Free (n, T)) = n
35770
a57ab2c01369 fixes to allow using fixrec_simp inside a locale, with test in ex/Fixrec_ex.thy
huffman
parents: 35769
diff changeset
   104
  | name_of t = raise TERM ("Fixrec.add_unfold: lhs not a constant", [t]);
33425
7e4f3c66190d add fixrec_simp attribute and method (eventually to replace fixpat)
huffman
parents: 33401
diff changeset
   105
7e4f3c66190d add fixrec_simp attribute and method (eventually to replace fixpat)
huffman
parents: 33401
diff changeset
   106
val lhs_name =
35770
a57ab2c01369 fixes to allow using fixrec_simp inside a locale, with test in ex/Fixrec_ex.thy
huffman
parents: 35769
diff changeset
   107
  name_of o head_of o fst o HOLogic.dest_eq o HOLogic.dest_Trueprop o prop_of;
33425
7e4f3c66190d add fixrec_simp attribute and method (eventually to replace fixpat)
huffman
parents: 33401
diff changeset
   108
7e4f3c66190d add fixrec_simp attribute and method (eventually to replace fixpat)
huffman
parents: 33401
diff changeset
   109
in
7e4f3c66190d add fixrec_simp attribute and method (eventually to replace fixpat)
huffman
parents: 33401
diff changeset
   110
33442
5d78b2bd27de made SML/NJ happy;
wenzelm
parents: 33430
diff changeset
   111
val add_unfold : attribute =
33425
7e4f3c66190d add fixrec_simp attribute and method (eventually to replace fixpat)
huffman
parents: 33401
diff changeset
   112
  Thm.declaration_attribute
7e4f3c66190d add fixrec_simp attribute and method (eventually to replace fixpat)
huffman
parents: 33401
diff changeset
   113
    (fn th => FixrecUnfoldData.map (Symtab.insert (K true) (lhs_name th, th)));
7e4f3c66190d add fixrec_simp attribute and method (eventually to replace fixpat)
huffman
parents: 33401
diff changeset
   114
7e4f3c66190d add fixrec_simp attribute and method (eventually to replace fixpat)
huffman
parents: 33401
diff changeset
   115
end
7e4f3c66190d add fixrec_simp attribute and method (eventually to replace fixpat)
huffman
parents: 33401
diff changeset
   116
30158
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   117
fun add_fixdefs
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   118
  (fixes : ((binding * typ) * mixfix) list)
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   119
  (spec : (Attrib.binding * term) list)
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   120
  (lthy : local_theory) =
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   121
  let
31095
b79d140f6d0b simplify fixrec proofs for mutually-recursive definitions; generate better fixpoint induction rules
huffman
parents: 31023
diff changeset
   122
    val thy = ProofContext.theory_of lthy;
30223
24d975352879 renamed Binding.name_pos to Binding.make, renamed Binding.base_name to Binding.name_of, renamed Binding.map_base to Binding.map_name, added mandatory flag to Binding.qualify;
wenzelm
parents: 30211
diff changeset
   123
    val names = map (Binding.name_of o fst o fst) fixes;
30158
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   124
    val all_names = space_implode "_" names;
32149
ef59550a55d3 renamed simpset_of to global_simpset_of, and local_simpset_of to simpset_of -- same for claset and clasimpset;
wenzelm
parents: 31740
diff changeset
   125
    val (lhss, rhss) = ListPair.unzip (map (dest_eqs o snd) spec);
31095
b79d140f6d0b simplify fixrec proofs for mutually-recursive definitions; generate better fixpoint induction rules
huffman
parents: 31023
diff changeset
   126
    val functional = lambda_tuple lhss (mk_tuple rhss);
b79d140f6d0b simplify fixrec proofs for mutually-recursive definitions; generate better fixpoint induction rules
huffman
parents: 31023
diff changeset
   127
    val fixpoint = mk_fix (mk_cabs functional);
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   128
    
31095
b79d140f6d0b simplify fixrec proofs for mutually-recursive definitions; generate better fixpoint induction rules
huffman
parents: 31023
diff changeset
   129
    val cont_thm =
b79d140f6d0b simplify fixrec proofs for mutually-recursive definitions; generate better fixpoint induction rules
huffman
parents: 31023
diff changeset
   130
      Goal.prove lthy [] [] (mk_trp (mk_cont functional))
32149
ef59550a55d3 renamed simpset_of to global_simpset_of, and local_simpset_of to simpset_of -- same for claset and clasimpset;
wenzelm
parents: 31740
diff changeset
   131
        (K (simp_tac (simpset_of lthy) 1));
31095
b79d140f6d0b simplify fixrec proofs for mutually-recursive definitions; generate better fixpoint induction rules
huffman
parents: 31023
diff changeset
   132
30158
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   133
    fun one_def (l as Free(n,_)) r =
30364
577edc39b501 moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents: 30280
diff changeset
   134
          let val b = Long_Name.base_name n
30158
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   135
          in ((Binding.name (b^"_def"), []), r) end
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   136
      | one_def _ _ = fixrec_err "fixdefs: lhs not of correct form";
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   137
    fun defs [] _ = []
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   138
      | defs (l::[]) r = [one_def l r]
31095
b79d140f6d0b simplify fixrec proofs for mutually-recursive definitions; generate better fixpoint induction rules
huffman
parents: 31023
diff changeset
   139
      | defs (l::ls) r = one_def l (mk_fst r) :: defs ls (mk_snd r);
30158
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   140
    val fixdefs = defs lhss fixpoint;
35772
ea0ac5538c53 avoid unnecessary primed variable names
huffman
parents: 35770
diff changeset
   141
    val (fixdef_thms : (term * (string * thm)) list, lthy) = lthy
33766
c679f05600cd adapted Local_Theory.define -- eliminated odd thm kind;
wenzelm
parents: 33726
diff changeset
   142
      |> fold_map Local_Theory.define (map (apfst fst) fixes ~~ fixdefs);
31095
b79d140f6d0b simplify fixrec proofs for mutually-recursive definitions; generate better fixpoint induction rules
huffman
parents: 31023
diff changeset
   143
    fun pair_equalI (thm1, thm2) = @{thm Pair_equalI} OF [thm1, thm2];
b79d140f6d0b simplify fixrec proofs for mutually-recursive definitions; generate better fixpoint induction rules
huffman
parents: 31023
diff changeset
   144
    val tuple_fixdef_thm = foldr1 pair_equalI (map (snd o snd) fixdef_thms);
b79d140f6d0b simplify fixrec proofs for mutually-recursive definitions; generate better fixpoint induction rules
huffman
parents: 31023
diff changeset
   145
    val P = Var (("P", 0), map Term.fastype_of lhss ---> HOLogic.boolT);
b79d140f6d0b simplify fixrec proofs for mutually-recursive definitions; generate better fixpoint induction rules
huffman
parents: 31023
diff changeset
   146
    val predicate = lambda_tuple lhss (list_comb (P, lhss));
b79d140f6d0b simplify fixrec proofs for mutually-recursive definitions; generate better fixpoint induction rules
huffman
parents: 31023
diff changeset
   147
    val tuple_induct_thm = (def_cont_fix_ind OF [tuple_fixdef_thm, cont_thm])
b79d140f6d0b simplify fixrec proofs for mutually-recursive definitions; generate better fixpoint induction rules
huffman
parents: 31023
diff changeset
   148
      |> Drule.instantiate' [] [SOME (Thm.cterm_of thy predicate)]
35624
c4e29a0bb8c1 modernized structure Local_Defs;
wenzelm
parents: 35527
diff changeset
   149
      |> Local_Defs.unfold lthy @{thms split_paired_all split_conv split_strict};
31095
b79d140f6d0b simplify fixrec proofs for mutually-recursive definitions; generate better fixpoint induction rules
huffman
parents: 31023
diff changeset
   150
    val tuple_unfold_thm = (def_cont_fix_eq OF [tuple_fixdef_thm, cont_thm])
35772
ea0ac5538c53 avoid unnecessary primed variable names
huffman
parents: 35770
diff changeset
   151
      |> Local_Defs.unfold lthy @{thms split_conv};
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   152
    fun unfolds [] thm = []
33425
7e4f3c66190d add fixrec_simp attribute and method (eventually to replace fixpat)
huffman
parents: 33401
diff changeset
   153
      | unfolds (n::[]) thm = [(n, thm)]
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   154
      | unfolds (n::ns) thm = let
31095
b79d140f6d0b simplify fixrec proofs for mutually-recursive definitions; generate better fixpoint induction rules
huffman
parents: 31023
diff changeset
   155
          val thmL = thm RS @{thm Pair_eqD1};
b79d140f6d0b simplify fixrec proofs for mutually-recursive definitions; generate better fixpoint induction rules
huffman
parents: 31023
diff changeset
   156
          val thmR = thm RS @{thm Pair_eqD2};
33425
7e4f3c66190d add fixrec_simp attribute and method (eventually to replace fixpat)
huffman
parents: 33401
diff changeset
   157
        in (n, thmL) :: unfolds ns thmR end;
31095
b79d140f6d0b simplify fixrec proofs for mutually-recursive definitions; generate better fixpoint induction rules
huffman
parents: 31023
diff changeset
   158
    val unfold_thms = unfolds names tuple_unfold_thm;
33425
7e4f3c66190d add fixrec_simp attribute and method (eventually to replace fixpat)
huffman
parents: 33401
diff changeset
   159
    val induct_note : Attrib.binding * Thm.thm list =
7e4f3c66190d add fixrec_simp attribute and method (eventually to replace fixpat)
huffman
parents: 33401
diff changeset
   160
      let
35769
500c32e5fadc fixrec now generates qualified theorem names
huffman
parents: 35624
diff changeset
   161
        val thm_name = Binding.qualify true all_names (Binding.name "induct");
33425
7e4f3c66190d add fixrec_simp attribute and method (eventually to replace fixpat)
huffman
parents: 33401
diff changeset
   162
      in
7e4f3c66190d add fixrec_simp attribute and method (eventually to replace fixpat)
huffman
parents: 33401
diff changeset
   163
        ((thm_name, []), [tuple_induct_thm])
7e4f3c66190d add fixrec_simp attribute and method (eventually to replace fixpat)
huffman
parents: 33401
diff changeset
   164
      end;
7e4f3c66190d add fixrec_simp attribute and method (eventually to replace fixpat)
huffman
parents: 33401
diff changeset
   165
    fun unfold_note (name, thm) : Attrib.binding * Thm.thm list =
7e4f3c66190d add fixrec_simp attribute and method (eventually to replace fixpat)
huffman
parents: 33401
diff changeset
   166
      let
35769
500c32e5fadc fixrec now generates qualified theorem names
huffman
parents: 35624
diff changeset
   167
        val thm_name = Binding.qualify true name (Binding.name "unfold");
33425
7e4f3c66190d add fixrec_simp attribute and method (eventually to replace fixpat)
huffman
parents: 33401
diff changeset
   168
        val src = Attrib.internal (K add_unfold);
7e4f3c66190d add fixrec_simp attribute and method (eventually to replace fixpat)
huffman
parents: 33401
diff changeset
   169
      in
7e4f3c66190d add fixrec_simp attribute and method (eventually to replace fixpat)
huffman
parents: 33401
diff changeset
   170
        ((thm_name, [src]), [thm])
7e4f3c66190d add fixrec_simp attribute and method (eventually to replace fixpat)
huffman
parents: 33401
diff changeset
   171
      end;
35772
ea0ac5538c53 avoid unnecessary primed variable names
huffman
parents: 35770
diff changeset
   172
    val (thmss, lthy) = lthy
33671
4b0f2599ed48 modernized structure Local_Theory;
wenzelm
parents: 33670
diff changeset
   173
      |> fold_map Local_Theory.note (induct_note :: map unfold_note unfold_thms);
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   174
  in
35772
ea0ac5538c53 avoid unnecessary primed variable names
huffman
parents: 35770
diff changeset
   175
    (lthy, names, fixdef_thms, map snd unfold_thms)
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   176
  end;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   177
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   178
(*************************************************************************)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   179
(*********** monadic notation and pattern matching compilation ***********)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   180
(*************************************************************************)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   181
33522
737589bb9bb8 adapted Theory_Data;
wenzelm
parents: 33519
diff changeset
   182
structure FixrecMatchData = Theory_Data
737589bb9bb8 adapted Theory_Data;
wenzelm
parents: 33519
diff changeset
   183
(
30131
6be1be402ef0 use TheoryData to keep track of pattern match combinators
huffman
parents: 29585
diff changeset
   184
  type T = string Symtab.table;
6be1be402ef0 use TheoryData to keep track of pattern match combinators
huffman
parents: 29585
diff changeset
   185
  val empty = Symtab.empty;
6be1be402ef0 use TheoryData to keep track of pattern match combinators
huffman
parents: 29585
diff changeset
   186
  val extend = I;
33522
737589bb9bb8 adapted Theory_Data;
wenzelm
parents: 33519
diff changeset
   187
  fun merge data = Symtab.merge (K true) data;
30131
6be1be402ef0 use TheoryData to keep track of pattern match combinators
huffman
parents: 29585
diff changeset
   188
);
6be1be402ef0 use TheoryData to keep track of pattern match combinators
huffman
parents: 29585
diff changeset
   189
6be1be402ef0 use TheoryData to keep track of pattern match combinators
huffman
parents: 29585
diff changeset
   190
(* associate match functions with pattern constants *)
6be1be402ef0 use TheoryData to keep track of pattern match combinators
huffman
parents: 29585
diff changeset
   191
fun add_matchers ms = FixrecMatchData.map (fold Symtab.update ms);
6be1be402ef0 use TheoryData to keep track of pattern match combinators
huffman
parents: 29585
diff changeset
   192
30157
40919ebde2c9 add function taken_names
huffman
parents: 30132
diff changeset
   193
fun taken_names (t : term) : bstring list =
40919ebde2c9 add function taken_names
huffman
parents: 30132
diff changeset
   194
  let
30364
577edc39b501 moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents: 30280
diff changeset
   195
    fun taken (Const(a,_), bs) = insert (op =) (Long_Name.base_name a) bs
30157
40919ebde2c9 add function taken_names
huffman
parents: 30132
diff changeset
   196
      | taken (Free(a,_) , bs) = insert (op =) a bs
40919ebde2c9 add function taken_names
huffman
parents: 30132
diff changeset
   197
      | taken (f $ u     , bs) = taken (f, taken (u, bs))
40919ebde2c9 add function taken_names
huffman
parents: 30132
diff changeset
   198
      | taken (Abs(a,_,t), bs) = taken (t, insert (op =) a bs)
40919ebde2c9 add function taken_names
huffman
parents: 30132
diff changeset
   199
      | taken (_         , bs) = bs;
40919ebde2c9 add function taken_names
huffman
parents: 30132
diff changeset
   200
  in
40919ebde2c9 add function taken_names
huffman
parents: 30132
diff changeset
   201
    taken (t, [])
40919ebde2c9 add function taken_names
huffman
parents: 30132
diff changeset
   202
  end;
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   203
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   204
(* builds a monadic term for matching a constructor pattern *)
30131
6be1be402ef0 use TheoryData to keep track of pattern match combinators
huffman
parents: 29585
diff changeset
   205
fun pre_build match_name pat rhs vs taken =
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   206
  case pat of
26045
02aa3f166c7f use ML antiquotations
huffman
parents: 25557
diff changeset
   207
    Const(@{const_name Rep_CFun},_)$f$(v as Free(n,T)) =>
30131
6be1be402ef0 use TheoryData to keep track of pattern match combinators
huffman
parents: 29585
diff changeset
   208
      pre_build match_name f rhs (v::vs) taken
26045
02aa3f166c7f use ML antiquotations
huffman
parents: 25557
diff changeset
   209
  | Const(@{const_name Rep_CFun},_)$f$x =>
30131
6be1be402ef0 use TheoryData to keep track of pattern match combinators
huffman
parents: 29585
diff changeset
   210
      let val (rhs', v, taken') = pre_build match_name x rhs [] taken;
6be1be402ef0 use TheoryData to keep track of pattern match combinators
huffman
parents: 29585
diff changeset
   211
      in pre_build match_name f rhs' (v::vs) taken' end
33401
fc43fa403a69 add fixrec support for HOL pair constructor patterns
huffman
parents: 33004
diff changeset
   212
  | f$(v as Free(n,T)) =>
fc43fa403a69 add fixrec support for HOL pair constructor patterns
huffman
parents: 33004
diff changeset
   213
      pre_build match_name f rhs (v::vs) taken
fc43fa403a69 add fixrec support for HOL pair constructor patterns
huffman
parents: 33004
diff changeset
   214
  | f$x =>
fc43fa403a69 add fixrec support for HOL pair constructor patterns
huffman
parents: 33004
diff changeset
   215
      let val (rhs', v, taken') = pre_build match_name x rhs [] taken;
fc43fa403a69 add fixrec support for HOL pair constructor patterns
huffman
parents: 33004
diff changeset
   216
      in pre_build match_name f rhs' (v::vs) taken' end
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   217
  | Const(c,T) =>
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   218
      let
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   219
        val n = Name.variant taken "v";
35525
fa231b86cb1e proper names for types cfun, sprod, ssum
huffman
parents: 33766
diff changeset
   220
        fun result_type (Type(@{type_name cfun},[_,T])) (x::xs) = result_type T xs
33401
fc43fa403a69 add fixrec support for HOL pair constructor patterns
huffman
parents: 33004
diff changeset
   221
          | result_type (Type (@{type_name "fun"},[_,T])) (x::xs) = result_type T xs
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   222
          | result_type T _ = T;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   223
        val v = Free(n, result_type T vs);
35527
f4282471461d fixrec and repdef modules import holcf_library
huffman
parents: 35525
diff changeset
   224
        val m = Const(match_name c, matcherT (T, fastype_of rhs));
30912
4022298c1a86 change definition of match combinators for fixrec package
huffman
parents: 30485
diff changeset
   225
        val k = big_lambdas vs rhs;
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   226
      in
30912
4022298c1a86 change definition of match combinators for fixrec package
huffman
parents: 30485
diff changeset
   227
        (m`v`k, v, n::taken)
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   228
      end
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   229
  | Free(n,_) => fixrec_err ("expected constructor, found free variable " ^ quote n)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   230
  | _ => fixrec_err "pre_build: invalid pattern";
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   231
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   232
(* builds a monadic term for matching a function definition pattern *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   233
(* returns (name, arity, matcher) *)
30131
6be1be402ef0 use TheoryData to keep track of pattern match combinators
huffman
parents: 29585
diff changeset
   234
fun building match_name pat rhs vs taken =
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   235
  case pat of
26045
02aa3f166c7f use ML antiquotations
huffman
parents: 25557
diff changeset
   236
    Const(@{const_name Rep_CFun}, _)$f$(v as Free(n,T)) =>
30131
6be1be402ef0 use TheoryData to keep track of pattern match combinators
huffman
parents: 29585
diff changeset
   237
      building match_name f rhs (v::vs) taken
26045
02aa3f166c7f use ML antiquotations
huffman
parents: 25557
diff changeset
   238
  | Const(@{const_name Rep_CFun}, _)$f$x =>
30131
6be1be402ef0 use TheoryData to keep track of pattern match combinators
huffman
parents: 29585
diff changeset
   239
      let val (rhs', v, taken') = pre_build match_name x rhs [] taken;
6be1be402ef0 use TheoryData to keep track of pattern match combinators
huffman
parents: 29585
diff changeset
   240
      in building match_name f rhs' (v::vs) taken' end
30158
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   241
  | Free(_,_) => ((pat, length vs), big_lambdas vs rhs)
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   242
  | Const(_,_) => ((pat, length vs), big_lambdas vs rhs)
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   243
  | _ => fixrec_err ("function is not declared as constant in theory: "
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   244
                    ^ ML_Syntax.print_term pat);
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   245
30158
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   246
fun strip_alls t =
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   247
  if Logic.is_all t then strip_alls (snd (Logic.dest_all t)) else t;
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   248
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   249
fun match_eq match_name eq =
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   250
  let
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   251
    val (lhs,rhs) = dest_eqs (Logic.strip_imp_concl (strip_alls eq));
30131
6be1be402ef0 use TheoryData to keep track of pattern match combinators
huffman
parents: 29585
diff changeset
   252
  in
30157
40919ebde2c9 add function taken_names
huffman
parents: 30132
diff changeset
   253
    building match_name lhs (mk_return rhs) [] (taken_names eq)
30131
6be1be402ef0 use TheoryData to keep track of pattern match combinators
huffman
parents: 29585
diff changeset
   254
  end;
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   255
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   256
(* returns the sum (using +++) of the terms in ms *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   257
(* also applies "run" to the result! *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   258
fun fatbar arity ms =
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   259
  let
30132
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
   260
    fun LAM_Ts 0 t = ([], Term.fastype_of t)
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
   261
      | LAM_Ts n (_ $ Abs(_,T,t)) =
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
   262
          let val (Ts, U) = LAM_Ts (n-1) t in (T::Ts, U) end
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
   263
      | LAM_Ts _ _ = fixrec_err "fatbar: internal error, not enough LAMs";
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   264
    fun unLAM 0 t = t
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   265
      | unLAM n (_$Abs(_,_,t)) = unLAM (n-1) t
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   266
      | unLAM _ _ = fixrec_err "fatbar: internal error, not enough LAMs";
30132
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
   267
    fun reLAM ([], U) t = t
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
   268
      | reLAM (T::Ts, U) t = reLAM (Ts, T ->> U) (cabs_const(T,U)$Abs("",T,t));
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
   269
    val msum = foldr1 mk_mplus (map (unLAM arity) ms);
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
   270
    val (Ts, U) = LAM_Ts arity (hd ms)
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   271
  in
35527
f4282471461d fixrec and repdef modules import holcf_library
huffman
parents: 35525
diff changeset
   272
    reLAM (rev Ts, dest_matchT U) (mk_run msum)
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   273
  end;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   274
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   275
(* this is the pattern-matching compiler function *)
30158
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   276
fun compile_pats match_name eqs =
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   277
  let
35903
0b43ff2d2e91 fix ML warnings in fixrec.ML
huffman
parents: 35780
diff changeset
   278
    val ((names, arities), mats) =
30158
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   279
      apfst ListPair.unzip (ListPair.unzip (map (match_eq match_name) eqs));
35903
0b43ff2d2e91 fix ML warnings in fixrec.ML
huffman
parents: 35780
diff changeset
   280
    val cname =
0b43ff2d2e91 fix ML warnings in fixrec.ML
huffman
parents: 35780
diff changeset
   281
        case distinct (op =) names of
0b43ff2d2e91 fix ML warnings in fixrec.ML
huffman
parents: 35780
diff changeset
   282
          [n] => n
0b43ff2d2e91 fix ML warnings in fixrec.ML
huffman
parents: 35780
diff changeset
   283
        | _ => fixrec_err "all equations in block must define the same function";
0b43ff2d2e91 fix ML warnings in fixrec.ML
huffman
parents: 35780
diff changeset
   284
    val arity =
0b43ff2d2e91 fix ML warnings in fixrec.ML
huffman
parents: 35780
diff changeset
   285
        case distinct (op =) arities of
0b43ff2d2e91 fix ML warnings in fixrec.ML
huffman
parents: 35780
diff changeset
   286
          [a] => a
0b43ff2d2e91 fix ML warnings in fixrec.ML
huffman
parents: 35780
diff changeset
   287
        | _ => fixrec_err "all equations in block must have the same arity";
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   288
    val rhs = fatbar arity mats;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   289
  in
30132
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
   290
    mk_trp (cname === rhs)
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   291
  end;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   292
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   293
(*************************************************************************)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   294
(********************** Proving associated theorems **********************)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   295
(*************************************************************************)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   296
33519
e31a85f92ce9 adapted Generic_Data, Proof_Data;
wenzelm
parents: 33507
diff changeset
   297
structure FixrecSimpData = Generic_Data
e31a85f92ce9 adapted Generic_Data, Proof_Data;
wenzelm
parents: 33507
diff changeset
   298
(
33425
7e4f3c66190d add fixrec_simp attribute and method (eventually to replace fixpat)
huffman
parents: 33401
diff changeset
   299
  type T = simpset;
7e4f3c66190d add fixrec_simp attribute and method (eventually to replace fixpat)
huffman
parents: 33401
diff changeset
   300
  val empty =
7e4f3c66190d add fixrec_simp attribute and method (eventually to replace fixpat)
huffman
parents: 33401
diff changeset
   301
    HOL_basic_ss
7e4f3c66190d add fixrec_simp attribute and method (eventually to replace fixpat)
huffman
parents: 33401
diff changeset
   302
      addsimps simp_thms
7e4f3c66190d add fixrec_simp attribute and method (eventually to replace fixpat)
huffman
parents: 33401
diff changeset
   303
      addsimps [@{thm beta_cfun}]
7e4f3c66190d add fixrec_simp attribute and method (eventually to replace fixpat)
huffman
parents: 33401
diff changeset
   304
      addsimprocs [@{simproc cont_proc}];
7e4f3c66190d add fixrec_simp attribute and method (eventually to replace fixpat)
huffman
parents: 33401
diff changeset
   305
  val extend = I;
33519
e31a85f92ce9 adapted Generic_Data, Proof_Data;
wenzelm
parents: 33507
diff changeset
   306
  val merge = merge_ss;
33425
7e4f3c66190d add fixrec_simp attribute and method (eventually to replace fixpat)
huffman
parents: 33401
diff changeset
   307
);
7e4f3c66190d add fixrec_simp attribute and method (eventually to replace fixpat)
huffman
parents: 33401
diff changeset
   308
7e4f3c66190d add fixrec_simp attribute and method (eventually to replace fixpat)
huffman
parents: 33401
diff changeset
   309
fun fixrec_simp_tac ctxt =
7e4f3c66190d add fixrec_simp attribute and method (eventually to replace fixpat)
huffman
parents: 33401
diff changeset
   310
  let
7e4f3c66190d add fixrec_simp attribute and method (eventually to replace fixpat)
huffman
parents: 33401
diff changeset
   311
    val tab = FixrecUnfoldData.get (Context.Proof ctxt);
35780
98fd7910f70a use Simplifier.context to avoid 'no proof context in simpset' errors from fixrec_simp after theory merge
huffman
parents: 35779
diff changeset
   312
    val ss = Simplifier.context ctxt (FixrecSimpData.get (Context.Proof ctxt));
33425
7e4f3c66190d add fixrec_simp attribute and method (eventually to replace fixpat)
huffman
parents: 33401
diff changeset
   313
    fun concl t =
7e4f3c66190d add fixrec_simp attribute and method (eventually to replace fixpat)
huffman
parents: 33401
diff changeset
   314
      if Logic.is_all t then concl (snd (Logic.dest_all t))
7e4f3c66190d add fixrec_simp attribute and method (eventually to replace fixpat)
huffman
parents: 33401
diff changeset
   315
      else HOLogic.dest_Trueprop (Logic.strip_imp_concl t);
7e4f3c66190d add fixrec_simp attribute and method (eventually to replace fixpat)
huffman
parents: 33401
diff changeset
   316
    fun tac (t, i) =
33430
c7dfeb7b0b0e better error handling for fixrec_simp
huffman
parents: 33427
diff changeset
   317
      let
35903
0b43ff2d2e91 fix ML warnings in fixrec.ML
huffman
parents: 35780
diff changeset
   318
        val (c, T) =
0b43ff2d2e91 fix ML warnings in fixrec.ML
huffman
parents: 35780
diff changeset
   319
            (dest_Const o head_of o chead_of o fst o HOLogic.dest_eq o concl) t;
33430
c7dfeb7b0b0e better error handling for fixrec_simp
huffman
parents: 33427
diff changeset
   320
        val unfold_thm = the (Symtab.lookup tab c);
c7dfeb7b0b0e better error handling for fixrec_simp
huffman
parents: 33427
diff changeset
   321
        val rule = unfold_thm RS @{thm ssubst_lhs};
c7dfeb7b0b0e better error handling for fixrec_simp
huffman
parents: 33427
diff changeset
   322
      in
c7dfeb7b0b0e better error handling for fixrec_simp
huffman
parents: 33427
diff changeset
   323
        CHANGED (rtac rule i THEN asm_simp_tac ss i)
c7dfeb7b0b0e better error handling for fixrec_simp
huffman
parents: 33427
diff changeset
   324
      end
33425
7e4f3c66190d add fixrec_simp attribute and method (eventually to replace fixpat)
huffman
parents: 33401
diff changeset
   325
  in
33505
03221db9df29 use try instead of handle
huffman
parents: 33430
diff changeset
   326
    SUBGOAL (fn ti => the_default no_tac (try tac ti))
33425
7e4f3c66190d add fixrec_simp attribute and method (eventually to replace fixpat)
huffman
parents: 33401
diff changeset
   327
  end;
7e4f3c66190d add fixrec_simp attribute and method (eventually to replace fixpat)
huffman
parents: 33401
diff changeset
   328
33442
5d78b2bd27de made SML/NJ happy;
wenzelm
parents: 33430
diff changeset
   329
val fixrec_simp_add : attribute =
33425
7e4f3c66190d add fixrec_simp attribute and method (eventually to replace fixpat)
huffman
parents: 33401
diff changeset
   330
  Thm.declaration_attribute
7e4f3c66190d add fixrec_simp attribute and method (eventually to replace fixpat)
huffman
parents: 33401
diff changeset
   331
    (fn th => FixrecSimpData.map (fn ss => ss addsimps [th]));
7e4f3c66190d add fixrec_simp attribute and method (eventually to replace fixpat)
huffman
parents: 33401
diff changeset
   332
33442
5d78b2bd27de made SML/NJ happy;
wenzelm
parents: 33430
diff changeset
   333
val fixrec_simp_del : attribute =
33425
7e4f3c66190d add fixrec_simp attribute and method (eventually to replace fixpat)
huffman
parents: 33401
diff changeset
   334
  Thm.declaration_attribute
7e4f3c66190d add fixrec_simp attribute and method (eventually to replace fixpat)
huffman
parents: 33401
diff changeset
   335
    (fn th => FixrecSimpData.map (fn ss => ss delsimps [th]));
7e4f3c66190d add fixrec_simp attribute and method (eventually to replace fixpat)
huffman
parents: 33401
diff changeset
   336
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   337
(* proves a block of pattern matching equations as theorems, using unfold *)
32149
ef59550a55d3 renamed simpset_of to global_simpset_of, and local_simpset_of to simpset_of -- same for claset and clasimpset;
wenzelm
parents: 31740
diff changeset
   338
fun make_simps ctxt (unfold_thm, eqns : (Attrib.binding * term) list) =
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   339
  let
30158
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   340
    val tacs =
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   341
      [rtac (unfold_thm RS @{thm ssubst_lhs}) 1,
32149
ef59550a55d3 renamed simpset_of to global_simpset_of, and local_simpset_of to simpset_of -- same for claset and clasimpset;
wenzelm
parents: 31740
diff changeset
   342
       asm_simp_tac (simpset_of ctxt) 1];
ef59550a55d3 renamed simpset_of to global_simpset_of, and local_simpset_of to simpset_of -- same for claset and clasimpset;
wenzelm
parents: 31740
diff changeset
   343
    fun prove_term t = Goal.prove ctxt [] [] t (K (EVERY tacs));
30158
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   344
    fun prove_eqn (bind, eqn_t) = (bind, prove_term eqn_t);
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   345
  in
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   346
    map prove_eqn eqns
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   347
  end;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   348
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   349
(*************************************************************************)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   350
(************************* Main fixrec function **************************)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   351
(*************************************************************************)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   352
30158
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   353
local
31738
7b9b9ba532ca discontinued ancient tradition to suffix certain ML module names with "_package"
haftmann
parents: 31177
diff changeset
   354
(* code adapted from HOL/Tools/primrec.ML *)
30158
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   355
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   356
fun gen_fixrec
30485
99def5248e7f removed legacy_infer_term, legacy_infer_prop;
wenzelm
parents: 30364
diff changeset
   357
  prep_spec
30158
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   358
  (strict : bool)
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   359
  raw_fixes
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   360
  raw_spec
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   361
  (lthy : local_theory) =
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   362
  let
30158
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   363
    val (fixes : ((binding * typ) * mixfix) list,
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   364
         spec : (Attrib.binding * term) list) =
30485
99def5248e7f removed legacy_infer_term, legacy_infer_prop;
wenzelm
parents: 30364
diff changeset
   365
          fst (prep_spec raw_fixes raw_spec lthy);
30158
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   366
    val chead_of_spec =
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   367
      chead_of o fst o dest_eqs o Logic.strip_imp_concl o strip_alls o snd;
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   368
    fun name_of (Free (n, _)) = n
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   369
      | name_of t = fixrec_err ("unknown term");
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   370
    val all_names = map (name_of o chead_of_spec) spec;
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   371
    val names = distinct (op =) all_names;
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   372
    fun block_of_name n =
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   373
      map_filter
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   374
        (fn (m,eq) => if m = n then SOME eq else NONE)
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   375
        (all_names ~~ spec);
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   376
    val blocks = map block_of_name names;
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   377
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   378
    val matcher_tab = FixrecMatchData.get (ProofContext.theory_of lthy);
30131
6be1be402ef0 use TheoryData to keep track of pattern match combinators
huffman
parents: 29585
diff changeset
   379
    fun match_name c =
30158
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   380
      case Symtab.lookup matcher_tab c of SOME m => m
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   381
        | NONE => fixrec_err ("unknown pattern constructor: " ^ c);
30131
6be1be402ef0 use TheoryData to keep track of pattern match combinators
huffman
parents: 29585
diff changeset
   382
30158
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   383
    val matches = map (compile_pats match_name) (map (map snd) blocks);
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   384
    val spec' = map (pair Attrib.empty_binding) matches;
35772
ea0ac5538c53 avoid unnecessary primed variable names
huffman
parents: 35770
diff changeset
   385
    val (lthy, cnames, fixdef_thms, unfold_thms) =
30158
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   386
      add_fixdefs fixes spec' lthy;
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   387
  in
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   388
    if strict then let (* only prove simp rules if strict = true *)
30158
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   389
      val simps : (Attrib.binding * thm) list list =
35772
ea0ac5538c53 avoid unnecessary primed variable names
huffman
parents: 35770
diff changeset
   390
        map (make_simps lthy) (unfold_thms ~~ blocks);
30158
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   391
      fun mk_bind n : Attrib.binding =
35769
500c32e5fadc fixrec now generates qualified theorem names
huffman
parents: 35624
diff changeset
   392
       (Binding.qualify true n (Binding.name "simps"),
30158
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   393
         [Attrib.internal (K Simplifier.simp_add)]);
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   394
      val simps1 : (Attrib.binding * thm list) list =
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   395
        map (fn (n,xs) => (mk_bind n, map snd xs)) (names ~~ simps);
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   396
      val simps2 : (Attrib.binding * thm list) list =
32952
aeb1e44fbc19 replaced String.concat by implode;
wenzelm
parents: 32149
diff changeset
   397
        map (apsnd (fn thm => [thm])) (flat simps);
35772
ea0ac5538c53 avoid unnecessary primed variable names
huffman
parents: 35770
diff changeset
   398
      val (_, lthy) = lthy
33671
4b0f2599ed48 modernized structure Local_Theory;
wenzelm
parents: 33670
diff changeset
   399
        |> fold_map Local_Theory.note (simps1 @ simps2);
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   400
    in
35772
ea0ac5538c53 avoid unnecessary primed variable names
huffman
parents: 35770
diff changeset
   401
      lthy
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   402
    end
35772
ea0ac5538c53 avoid unnecessary primed variable names
huffman
parents: 35770
diff changeset
   403
    else lthy
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   404
  end;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   405
30158
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   406
in
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   407
33726
0878aecbf119 eliminated slightly odd name space grouping -- now managed by Isar toplevel;
wenzelm
parents: 33671
diff changeset
   408
val add_fixrec = gen_fixrec Specification.check_spec;
0878aecbf119 eliminated slightly odd name space grouping -- now managed by Isar toplevel;
wenzelm
parents: 33671
diff changeset
   409
val add_fixrec_cmd = gen_fixrec Specification.read_spec;
30158
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   410
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   411
end; (* local *)
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   412
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   413
(*************************************************************************)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   414
(******************************** Fixpat *********************************)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   415
(*************************************************************************)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   416
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   417
fun fix_pat thy t = 
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   418
  let
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   419
    val T = fastype_of t;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   420
    val eq = mk_trp (HOLogic.eq_const T $ t $ Var (("x",0),T));
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   421
    val cname = case chead_of t of Const(c,_) => c | _ =>
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   422
              fixrec_err "function is not declared as constant in theory";
35769
500c32e5fadc fixrec now generates qualified theorem names
huffman
parents: 35624
diff changeset
   423
    val unfold_thm = PureThy.get_thm thy (cname^".unfold");
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   424
    val simp = Goal.prove_global thy [] [] eq
32149
ef59550a55d3 renamed simpset_of to global_simpset_of, and local_simpset_of to simpset_of -- same for claset and clasimpset;
wenzelm
parents: 31740
diff changeset
   425
          (fn _ => EVERY [stac unfold_thm 1, simp_tac (global_simpset_of thy) 1]);
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   426
  in simp end;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   427
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   428
fun gen_add_fixpat prep_term prep_attrib ((name, srcs), strings) thy =
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   429
  let
35779
7de1e14d9277 fixpat command prints legacy_feature warning
huffman
parents: 35772
diff changeset
   430
    val _ = legacy_feature "\"fixpat\"; prefer \"fixrec_simp\" method instead";
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   431
    val atts = map (prep_attrib thy) srcs;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   432
    val ts = map (prep_term thy) strings;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   433
    val simps = map (fix_pat thy) ts;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   434
  in
29585
c23295521af5 binding replaces bstring
haftmann
parents: 29581
diff changeset
   435
    (snd o PureThy.add_thmss [((name, simps), atts)]) thy
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   436
  end;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   437
30485
99def5248e7f removed legacy_infer_term, legacy_infer_prop;
wenzelm
parents: 30364
diff changeset
   438
val add_fixpat = gen_add_fixpat Sign.cert_term (K I);
99def5248e7f removed legacy_infer_term, legacy_infer_prop;
wenzelm
parents: 30364
diff changeset
   439
val add_fixpat_cmd = gen_add_fixpat Syntax.read_term_global Attrib.attribute;
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   440
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   441
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   442
(*************************************************************************)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   443
(******************************** Parsers ********************************)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   444
(*************************************************************************)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   445
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   446
local structure P = OuterParse and K = OuterKeyword in
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   447
30485
99def5248e7f removed legacy_infer_term, legacy_infer_prop;
wenzelm
parents: 30364
diff changeset
   448
val _ = OuterSyntax.local_theory "fixrec" "define recursive functions (HOLCF)" K.thy_decl
99def5248e7f removed legacy_infer_term, legacy_infer_prop;
wenzelm
parents: 30364
diff changeset
   449
  ((P.opt_keyword "permissive" >> not) -- P.fixes -- SpecParse.where_alt_specs
99def5248e7f removed legacy_infer_term, legacy_infer_prop;
wenzelm
parents: 30364
diff changeset
   450
    >> (fn ((strict, fixes), specs) => add_fixrec_cmd strict fixes specs));
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   451
30485
99def5248e7f removed legacy_infer_term, legacy_infer_prop;
wenzelm
parents: 30364
diff changeset
   452
val _ = OuterSyntax.command "fixpat" "define rewrites for fixrec functions" K.thy_decl
99def5248e7f removed legacy_infer_term, legacy_infer_prop;
wenzelm
parents: 30364
diff changeset
   453
  (SpecParse.specs >> (Toplevel.theory o add_fixpat_cmd));
33425
7e4f3c66190d add fixrec_simp attribute and method (eventually to replace fixpat)
huffman
parents: 33401
diff changeset
   454
30485
99def5248e7f removed legacy_infer_term, legacy_infer_prop;
wenzelm
parents: 30364
diff changeset
   455
end;
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   456
33425
7e4f3c66190d add fixrec_simp attribute and method (eventually to replace fixpat)
huffman
parents: 33401
diff changeset
   457
val setup =
33522
737589bb9bb8 adapted Theory_Data;
wenzelm
parents: 33519
diff changeset
   458
  Attrib.setup @{binding fixrec_simp}
33427
3182812d33ed domain package registers fixrec_simp lemmas
huffman
parents: 33425
diff changeset
   459
                     (Attrib.add_del fixrec_simp_add fixrec_simp_del)
33425
7e4f3c66190d add fixrec_simp attribute and method (eventually to replace fixpat)
huffman
parents: 33401
diff changeset
   460
                     "declaration of fixrec simp rule"
7e4f3c66190d add fixrec_simp attribute and method (eventually to replace fixpat)
huffman
parents: 33401
diff changeset
   461
  #> Method.setup @{binding fixrec_simp}
7e4f3c66190d add fixrec_simp attribute and method (eventually to replace fixpat)
huffman
parents: 33401
diff changeset
   462
                     (Scan.succeed (SIMPLE_METHOD' o fixrec_simp_tac))
7e4f3c66190d add fixrec_simp attribute and method (eventually to replace fixpat)
huffman
parents: 33401
diff changeset
   463
                     "pattern prover for fixrec constants";
30131
6be1be402ef0 use TheoryData to keep track of pattern match combinators
huffman
parents: 29585
diff changeset
   464
24867
e5b55d7be9bb simplified interfaces for outer syntax;
wenzelm
parents: 24707
diff changeset
   465
end;