src/HOLCF/Tools/fixrec_package.ML
author bulwahn
Mon, 11 May 2009 09:18:42 +0200
changeset 31106 9a1178204dc0
parent 30485 99def5248e7f
child 30912 4022298c1a86
permissions -rw-r--r--
Added pred_code command
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
     1
(*  Title:      HOLCF/Tools/fixrec_package.ML
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
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
     7
signature FIXREC_PACKAGE =
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
6be1be402ef0 use TheoryData to keep track of pattern match combinators
huffman
parents: 29585
diff changeset
    16
  val setup: theory -> theory
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    17
end;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    18
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    19
structure FixrecPackage: FIXREC_PACKAGE =
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    20
struct
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    21
26045
02aa3f166c7f use ML antiquotations
huffman
parents: 25557
diff changeset
    22
val fix_eq2 = @{thm fix_eq2};
02aa3f166c7f use ML antiquotations
huffman
parents: 25557
diff changeset
    23
val def_fix_ind = @{thm def_fix_ind};
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    24
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    25
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    26
fun fixrec_err s = error ("fixrec definition error:\n" ^ s);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    27
fun fixrec_eq_err thy s eq =
26939
1035c89b4c02 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26343
diff changeset
    28
  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
    29
30132
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    30
(*************************************************************************)
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    31
(***************************** building types ****************************)
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    32
(*************************************************************************)
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    33
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    34
(* ->> is taken from holcf_logic.ML *)
30132
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    35
fun cfunT (T, U) = Type(@{type_name "->"}, [T, U]);
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    36
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    37
infixr 6 ->>; val (op ->>) = cfunT;
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    38
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    39
fun dest_cfunT (Type(@{type_name "->"}, [T, U])) = (T, U)
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    40
  | dest_cfunT T = raise TYPE ("dest_cfunT", [T], []);
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    41
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    42
fun binder_cfun (Type(@{type_name "->"},[T, U])) = T :: binder_cfun U
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    43
  | binder_cfun _   =  [];
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    44
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    45
fun body_cfun (Type(@{type_name "->"},[T, U])) = body_cfun U
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    46
  | body_cfun T   =  T;
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    47
30132
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    48
fun strip_cfun T : typ list * typ =
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    49
  (binder_cfun T, body_cfun T);
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    50
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    51
fun maybeT T = Type(@{type_name "maybe"}, [T]);
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    52
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    53
fun dest_maybeT (Type(@{type_name "maybe"}, [T])) = T
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    54
  | dest_maybeT T = raise TYPE ("dest_maybeT", [T], []);
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    55
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    56
fun tupleT [] = @{typ "unit"}
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    57
  | tupleT [T] = T
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    58
  | tupleT (T :: Ts) = HOLogic.mk_prodT (T, tupleT Ts);
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    59
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    60
fun matchT T = body_cfun T ->> maybeT (tupleT (binder_cfun T));
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
fun capply_const (S, T) =
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    76
  Const(@{const_name Rep_CFun}, (S ->> T) --> (S --> T));
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 cabs_const (S, T) =
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    79
  Const(@{const_name Abs_CFun}, (S --> T) --> (S ->> T));
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    80
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    81
fun mk_capply (t, u) =
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    82
  let val (S, T) =
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    83
    case Term.fastype_of t of
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    84
        Type(@{type_name "->"}, [S, T]) => (S, T)
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    85
      | _ => raise TERM ("mk_capply " ^ ML_Syntax.print_list ML_Syntax.print_term [t, u], [t, u]);
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    86
  in capply_const (S, T) $ t $ u end;
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    87
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    88
infix 0 ==;  val (op ==) = Logic.mk_equals;
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    89
infix 1 ===; val (op ===) = HOLogic.mk_eq;
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    90
infix 9 `  ; val (op `) = mk_capply;
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    91
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    92
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    93
fun mk_cpair (t, u) =
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    94
  let val T = Term.fastype_of t
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    95
      val U = Term.fastype_of u
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    96
      val cpairT = T ->> U ->> HOLogic.mk_prodT (T, U)
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    97
  in Const(@{const_name cpair}, cpairT) ` t ` u end;
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    98
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
    99
fun mk_cfst t =
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
   100
  let val T = Term.fastype_of t;
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
   101
      val (U, _) = HOLogic.dest_prodT T;
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
   102
  in Const(@{const_name cfst}, T ->> U) ` t end;
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
   103
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
   104
fun mk_csnd t =
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
   105
  let val T = Term.fastype_of t;
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
   106
      val (_, U) = HOLogic.dest_prodT T;
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
   107
  in Const(@{const_name csnd}, T ->> U) ` t end;
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
   108
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
   109
fun mk_csplit t =
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
   110
  let val (S, TU) = dest_cfunT (Term.fastype_of t);
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
   111
      val (T, U) = dest_cfunT TU;
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
   112
      val csplitT = (S ->> T ->> U) ->> HOLogic.mk_prodT (S, T) ->> U;
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
   113
  in Const(@{const_name csplit}, csplitT) ` t end;
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   114
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   115
(* builds the expression (LAM v. rhs) *)
30132
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
   116
fun big_lambda v rhs =
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
   117
  cabs_const (Term.fastype_of v, Term.fastype_of rhs) $ Term.lambda v rhs;
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   118
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   119
(* builds the expression (LAM v1 v2 .. vn. rhs) *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   120
fun big_lambdas [] rhs = rhs
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   121
  | big_lambdas (v::vs) rhs = big_lambda v (big_lambdas vs rhs);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   122
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   123
(* builds the expression (LAM <v1,v2,..,vn>. rhs) *)
30132
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
   124
fun lambda_ctuple [] rhs = big_lambda (Free("unit", HOLogic.unitT)) rhs
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   125
  | lambda_ctuple (v::[]) rhs = big_lambda v rhs
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   126
  | lambda_ctuple (v::vs) rhs =
30132
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
   127
      mk_csplit (big_lambda v (lambda_ctuple vs rhs));
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   128
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   129
(* builds the expression <v1,v2,..,vn> *)
30132
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
   130
fun mk_ctuple [] = @{term "UU::unit"}
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   131
|   mk_ctuple (t::[]) = t
30132
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
   132
|   mk_ctuple (t::ts) = mk_cpair (t, mk_ctuple ts);
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
   133
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
   134
fun mk_return t =
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
   135
  let val T = Term.fastype_of t
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
   136
  in Const(@{const_name Fixrec.return}, T ->> maybeT T) ` t end;
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
   137
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
   138
fun mk_bind (t, u) =
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
   139
  let val (T, mU) = dest_cfunT (Term.fastype_of u);
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
   140
      val bindT = maybeT T ->> (T ->> mU) ->> mU;
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
   141
  in Const(@{const_name Fixrec.bind}, bindT) ` t ` u end;
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
   142
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
   143
fun mk_mplus (t, u) =
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
   144
  let val mT = Term.fastype_of t
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
   145
  in Const(@{const_name Fixrec.mplus}, mT ->> mT ->> mT) ` t ` u end;
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
   146
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
   147
fun mk_run t =
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
   148
  let val mT = Term.fastype_of t
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
   149
      val T = dest_maybeT mT
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
   150
  in Const(@{const_name Fixrec.run}, mT ->> T) ` t end;
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
   151
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
   152
fun mk_fix t =
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
   153
  let val (T, _) = dest_cfunT (Term.fastype_of t)
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
   154
  in Const(@{const_name fix}, (T ->> T) ->> T) ` t end;
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   155
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   156
(*************************************************************************)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   157
(************* fixed-point definitions and unfolding theorems ************)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   158
(*************************************************************************)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   159
30158
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   160
fun add_fixdefs
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   161
  (fixes : ((binding * typ) * mixfix) list)
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   162
  (spec : (Attrib.binding * term) list)
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   163
  (lthy : local_theory) =
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   164
  let
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
   165
    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
   166
    val all_names = space_implode "_" names;
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   167
    val (lhss,rhss) = ListPair.unzip (map (dest_eqs o snd) spec);
30132
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
   168
    val fixpoint = mk_fix (lambda_ctuple lhss (mk_ctuple rhss));
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   169
    
30158
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   170
    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
   171
          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
   172
          in ((Binding.name (b^"_def"), []), r) end
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   173
      | one_def _ _ = fixrec_err "fixdefs: lhs not of correct form";
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   174
    fun defs [] _ = []
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   175
      | defs (l::[]) r = [one_def l r]
30132
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
   176
      | defs (l::ls) r = one_def l (mk_cfst r) :: defs ls (mk_csnd r);
30158
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   177
    val fixdefs = defs lhss fixpoint;
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   178
    val define_all = fold_map (LocalTheory.define Thm.definitionK);
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   179
    val (fixdef_thms : (term * (string * thm)) list, lthy') = lthy
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   180
      |> define_all (map (apfst fst) fixes ~~ fixdefs);
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   181
    fun cpair_equalI (thm1, thm2) = @{thm cpair_equalI} OF [thm1, thm2];
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   182
    val ctuple_fixdef_thm = foldr1 cpair_equalI (map (snd o snd) fixdef_thms);
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   183
    val ctuple_induct_thm = ctuple_fixdef_thm RS def_fix_ind;
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   184
    val ctuple_unfold_thm =
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   185
      Goal.prove lthy' [] [] (mk_trp (mk_ctuple lhss === mk_ctuple rhss))
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   186
        (fn _ => EVERY [rtac (ctuple_fixdef_thm RS fix_eq2 RS trans) 1,
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   187
                   simp_tac (local_simpset_of lthy') 1]);
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   188
    fun unfolds [] thm = []
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   189
      | unfolds (n::[]) thm = [(n^"_unfold", thm)]
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   190
      | unfolds (n::ns) thm = let
25132
dffe405b090d removed obsolete ML bindings;
wenzelm
parents: 24867
diff changeset
   191
          val thmL = thm RS @{thm cpair_eqD1};
dffe405b090d removed obsolete ML bindings;
wenzelm
parents: 24867
diff changeset
   192
          val thmR = thm RS @{thm cpair_eqD2};
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   193
        in (n^"_unfold", thmL) :: unfolds ns thmR end;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   194
    val unfold_thms = unfolds names ctuple_unfold_thm;
30158
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   195
    fun mk_note (n, thm) = ((Binding.name n, []), [thm]);
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   196
    val (thmss, lthy'') = lthy'
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   197
      |> fold_map (LocalTheory.note Thm.theoremK o mk_note)
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   198
        ((all_names ^ "_induct", ctuple_induct_thm) :: unfold_thms);
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   199
  in
30158
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   200
    (lthy'', names, fixdef_thms, map snd unfold_thms)
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   201
  end;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   202
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   203
(*************************************************************************)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   204
(*********** monadic notation and pattern matching compilation ***********)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   205
(*************************************************************************)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   206
30131
6be1be402ef0 use TheoryData to keep track of pattern match combinators
huffman
parents: 29585
diff changeset
   207
structure FixrecMatchData = TheoryDataFun (
6be1be402ef0 use TheoryData to keep track of pattern match combinators
huffman
parents: 29585
diff changeset
   208
  type T = string Symtab.table;
6be1be402ef0 use TheoryData to keep track of pattern match combinators
huffman
parents: 29585
diff changeset
   209
  val empty = Symtab.empty;
6be1be402ef0 use TheoryData to keep track of pattern match combinators
huffman
parents: 29585
diff changeset
   210
  val copy = I;
6be1be402ef0 use TheoryData to keep track of pattern match combinators
huffman
parents: 29585
diff changeset
   211
  val extend = I;
6be1be402ef0 use TheoryData to keep track of pattern match combinators
huffman
parents: 29585
diff changeset
   212
  fun merge _ tabs : T = Symtab.merge (K true) tabs;
6be1be402ef0 use TheoryData to keep track of pattern match combinators
huffman
parents: 29585
diff changeset
   213
);
6be1be402ef0 use TheoryData to keep track of pattern match combinators
huffman
parents: 29585
diff changeset
   214
6be1be402ef0 use TheoryData to keep track of pattern match combinators
huffman
parents: 29585
diff changeset
   215
(* associate match functions with pattern constants *)
6be1be402ef0 use TheoryData to keep track of pattern match combinators
huffman
parents: 29585
diff changeset
   216
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
   217
30157
40919ebde2c9 add function taken_names
huffman
parents: 30132
diff changeset
   218
fun taken_names (t : term) : bstring list =
40919ebde2c9 add function taken_names
huffman
parents: 30132
diff changeset
   219
  let
30364
577edc39b501 moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents: 30280
diff changeset
   220
    fun taken (Const(a,_), bs) = insert (op =) (Long_Name.base_name a) bs
30157
40919ebde2c9 add function taken_names
huffman
parents: 30132
diff changeset
   221
      | taken (Free(a,_) , bs) = insert (op =) a bs
40919ebde2c9 add function taken_names
huffman
parents: 30132
diff changeset
   222
      | taken (f $ u     , bs) = taken (f, taken (u, bs))
40919ebde2c9 add function taken_names
huffman
parents: 30132
diff changeset
   223
      | taken (Abs(a,_,t), bs) = taken (t, insert (op =) a bs)
40919ebde2c9 add function taken_names
huffman
parents: 30132
diff changeset
   224
      | taken (_         , bs) = bs;
40919ebde2c9 add function taken_names
huffman
parents: 30132
diff changeset
   225
  in
40919ebde2c9 add function taken_names
huffman
parents: 30132
diff changeset
   226
    taken (t, [])
40919ebde2c9 add function taken_names
huffman
parents: 30132
diff changeset
   227
  end;
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   228
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   229
(* 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
   230
fun pre_build match_name pat rhs vs taken =
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   231
  case pat of
26045
02aa3f166c7f use ML antiquotations
huffman
parents: 25557
diff changeset
   232
    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
   233
      pre_build match_name f rhs (v::vs) taken
26045
02aa3f166c7f use ML antiquotations
huffman
parents: 25557
diff changeset
   234
  | Const(@{const_name Rep_CFun},_)$f$x =>
30131
6be1be402ef0 use TheoryData to keep track of pattern match combinators
huffman
parents: 29585
diff changeset
   235
      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
   236
      in pre_build match_name f rhs' (v::vs) taken' end
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   237
  | Const(c,T) =>
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   238
      let
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   239
        val n = Name.variant taken "v";
26045
02aa3f166c7f use ML antiquotations
huffman
parents: 25557
diff changeset
   240
        fun result_type (Type(@{type_name "->"},[_,T])) (x::xs) = result_type T xs
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   241
          | result_type T _ = T;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   242
        val v = Free(n, result_type T vs);
30132
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
   243
        val m = Const(match_name c, matchT T);
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   244
        val k = lambda_ctuple vs rhs;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   245
      in
30132
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
   246
        (mk_bind (m`v, k), v, n::taken)
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   247
      end
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   248
  | Free(n,_) => fixrec_err ("expected constructor, found free variable " ^ quote n)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   249
  | _ => fixrec_err "pre_build: invalid pattern";
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   250
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   251
(* builds a monadic term for matching a function definition pattern *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   252
(* returns (name, arity, matcher) *)
30131
6be1be402ef0 use TheoryData to keep track of pattern match combinators
huffman
parents: 29585
diff changeset
   253
fun building match_name pat rhs vs taken =
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   254
  case pat of
26045
02aa3f166c7f use ML antiquotations
huffman
parents: 25557
diff changeset
   255
    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
   256
      building match_name f rhs (v::vs) taken
26045
02aa3f166c7f use ML antiquotations
huffman
parents: 25557
diff changeset
   257
  | Const(@{const_name Rep_CFun}, _)$f$x =>
30131
6be1be402ef0 use TheoryData to keep track of pattern match combinators
huffman
parents: 29585
diff changeset
   258
      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
   259
      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
   260
  | Free(_,_) => ((pat, length vs), big_lambdas vs rhs)
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   261
  | Const(_,_) => ((pat, length vs), big_lambdas vs rhs)
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   262
  | _ => 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
   263
                    ^ ML_Syntax.print_term pat);
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   264
30158
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   265
fun strip_alls t =
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   266
  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
   267
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   268
fun match_eq match_name eq =
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   269
  let
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   270
    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
   271
  in
30157
40919ebde2c9 add function taken_names
huffman
parents: 30132
diff changeset
   272
    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
   273
  end;
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   274
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   275
(* returns the sum (using +++) of the terms in ms *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   276
(* also applies "run" to the result! *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   277
fun fatbar arity ms =
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   278
  let
30132
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
   279
    fun LAM_Ts 0 t = ([], Term.fastype_of t)
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
   280
      | LAM_Ts n (_ $ Abs(_,T,t)) =
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
   281
          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
   282
      | LAM_Ts _ _ = fixrec_err "fatbar: internal error, not enough LAMs";
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   283
    fun unLAM 0 t = t
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   284
      | unLAM n (_$Abs(_,_,t)) = unLAM (n-1) t
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   285
      | unLAM _ _ = fixrec_err "fatbar: internal error, not enough LAMs";
30132
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
   286
    fun reLAM ([], U) t = t
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
   287
      | 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
   288
    val msum = foldr1 mk_mplus (map (unLAM arity) ms);
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
   289
    val (Ts, U) = LAM_Ts arity (hd ms)
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   290
  in
30132
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
   291
    reLAM (rev Ts, dest_maybeT U) (mk_run msum)
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   292
  end;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   293
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   294
(* this is the pattern-matching compiler function *)
30158
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   295
fun compile_pats match_name eqs =
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   296
  let
30158
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   297
    val (((n::names),(a::arities)),mats) =
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   298
      apfst ListPair.unzip (ListPair.unzip (map (match_eq match_name) eqs));
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   299
    val cname = if forall (fn x => n=x) names then n
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   300
          else fixrec_err "all equations in block must define the same function";
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   301
    val arity = if forall (fn x => a=x) arities then a
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   302
          else fixrec_err "all equations in block must have the same arity";
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   303
    val rhs = fatbar arity mats;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   304
  in
30132
243a05a67c41 avoid using legacy type inference
huffman
parents: 30131
diff changeset
   305
    mk_trp (cname === rhs)
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   306
  end;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   307
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   308
(*************************************************************************)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   309
(********************** Proving associated theorems **********************)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   310
(*************************************************************************)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   311
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   312
(* proves a block of pattern matching equations as theorems, using unfold *)
30158
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   313
fun make_simps lthy (unfold_thm, eqns : (Attrib.binding * term) list) =
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   314
  let
30158
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   315
    val tacs =
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   316
      [rtac (unfold_thm RS @{thm ssubst_lhs}) 1,
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   317
       asm_simp_tac (local_simpset_of lthy) 1];
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   318
    fun prove_term t = Goal.prove lthy [] [] t (K (EVERY tacs));
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   319
    fun prove_eqn (bind, eqn_t) = (bind, prove_term eqn_t);
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   320
  in
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   321
    map prove_eqn eqns
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   322
  end;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   323
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   324
(*************************************************************************)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   325
(************************* Main fixrec function **************************)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   326
(*************************************************************************)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   327
30158
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   328
local
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   329
(* code adapted from HOL/Tools/primrec_package.ML *)
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   330
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   331
fun gen_fixrec
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   332
  (set_group : bool)
30485
99def5248e7f removed legacy_infer_term, legacy_infer_prop;
wenzelm
parents: 30364
diff changeset
   333
  prep_spec
30158
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   334
  (strict : bool)
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   335
  raw_fixes
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   336
  raw_spec
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   337
  (lthy : local_theory) =
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   338
  let
30158
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   339
    val (fixes : ((binding * typ) * mixfix) list,
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   340
         spec : (Attrib.binding * term) list) =
30485
99def5248e7f removed legacy_infer_term, legacy_infer_prop;
wenzelm
parents: 30364
diff changeset
   341
          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
   342
    val chead_of_spec =
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   343
      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
   344
    fun name_of (Free (n, _)) = n
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   345
      | name_of t = fixrec_err ("unknown term");
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   346
    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
   347
    val names = distinct (op =) all_names;
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   348
    fun block_of_name n =
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   349
      map_filter
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   350
        (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
   351
        (all_names ~~ spec);
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   352
    val blocks = map block_of_name names;
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   353
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   354
    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
   355
    fun match_name c =
30158
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   356
      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
   357
        | NONE => fixrec_err ("unknown pattern constructor: " ^ c);
30131
6be1be402ef0 use TheoryData to keep track of pattern match combinators
huffman
parents: 29585
diff changeset
   358
30158
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   359
    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
   360
    val spec' = map (pair Attrib.empty_binding) matches;
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   361
    val (lthy', cnames, fixdef_thms, unfold_thms) =
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   362
      add_fixdefs fixes spec' lthy;
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   363
  in
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   364
    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
   365
      val simps : (Attrib.binding * thm) list list =
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   366
        map (make_simps lthy') (unfold_thms ~~ blocks);
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   367
      fun mk_bind n : Attrib.binding =
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   368
       (Binding.name (n ^ "_simps"),
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   369
         [Attrib.internal (K Simplifier.simp_add)]);
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   370
      val simps1 : (Attrib.binding * thm list) list =
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   371
        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
   372
      val simps2 : (Attrib.binding * thm list) list =
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   373
        map (apsnd (fn thm => [thm])) (List.concat simps);
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   374
      val (_, lthy'') = lthy'
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   375
        |> fold_map (LocalTheory.note Thm.theoremK) (simps1 @ simps2);
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   376
    in
30158
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   377
      lthy''
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   378
    end
30158
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   379
    else lthy'
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   380
  end;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   381
30158
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   382
in
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   383
30485
99def5248e7f removed legacy_infer_term, legacy_infer_prop;
wenzelm
parents: 30364
diff changeset
   384
val add_fixrec = gen_fixrec false Specification.check_spec;
99def5248e7f removed legacy_infer_term, legacy_infer_prop;
wenzelm
parents: 30364
diff changeset
   385
val add_fixrec_cmd = gen_fixrec true Specification.read_spec;
30158
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   386
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   387
end; (* local *)
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   388
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   389
(*************************************************************************)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   390
(******************************** Fixpat *********************************)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   391
(*************************************************************************)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   392
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   393
fun fix_pat thy t = 
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   394
  let
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   395
    val T = fastype_of t;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   396
    val eq = mk_trp (HOLogic.eq_const T $ t $ Var (("x",0),T));
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   397
    val cname = case chead_of t of Const(c,_) => c | _ =>
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   398
              fixrec_err "function is not declared as constant in theory";
26343
0dd2eab7b296 simplified get_thm(s): back to plain name argument;
wenzelm
parents: 26336
diff changeset
   399
    val unfold_thm = PureThy.get_thm thy (cname^"_unfold");
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   400
    val simp = Goal.prove_global thy [] [] eq
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   401
          (fn _ => EVERY [stac unfold_thm 1, simp_tac (simpset_of thy) 1]);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   402
  in simp end;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   403
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   404
fun gen_add_fixpat prep_term prep_attrib ((name, srcs), strings) thy =
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   405
  let
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   406
    val atts = map (prep_attrib thy) srcs;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   407
    val ts = map (prep_term thy) strings;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   408
    val simps = map (fix_pat thy) ts;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   409
  in
29585
c23295521af5 binding replaces bstring
haftmann
parents: 29581
diff changeset
   410
    (snd o PureThy.add_thmss [((name, simps), atts)]) thy
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   411
  end;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   412
30485
99def5248e7f removed legacy_infer_term, legacy_infer_prop;
wenzelm
parents: 30364
diff changeset
   413
val add_fixpat = gen_add_fixpat Sign.cert_term (K I);
99def5248e7f removed legacy_infer_term, legacy_infer_prop;
wenzelm
parents: 30364
diff changeset
   414
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
   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
(*************************************************************************)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   418
(******************************** Parsers ********************************)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   419
(*************************************************************************)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   420
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   421
local structure P = OuterParse and K = OuterKeyword in
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   422
30485
99def5248e7f removed legacy_infer_term, legacy_infer_prop;
wenzelm
parents: 30364
diff changeset
   423
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
   424
  ((P.opt_keyword "permissive" >> not) -- P.fixes -- SpecParse.where_alt_specs
99def5248e7f removed legacy_infer_term, legacy_infer_prop;
wenzelm
parents: 30364
diff changeset
   425
    >> (fn ((strict, fixes), specs) => add_fixrec_cmd strict fixes specs));
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   426
30485
99def5248e7f removed legacy_infer_term, legacy_infer_prop;
wenzelm
parents: 30364
diff changeset
   427
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
   428
  (SpecParse.specs >> (Toplevel.theory o add_fixpat_cmd));
30158
83c50c62cf23 fixrec package uses new-style syntax and local-theory interface
huffman
parents: 30157
diff changeset
   429
  
30485
99def5248e7f removed legacy_infer_term, legacy_infer_prop;
wenzelm
parents: 30364
diff changeset
   430
end;
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   431
30131
6be1be402ef0 use TheoryData to keep track of pattern match combinators
huffman
parents: 29585
diff changeset
   432
val setup = FixrecMatchData.init;
6be1be402ef0 use TheoryData to keep track of pattern match combinators
huffman
parents: 29585
diff changeset
   433
24867
e5b55d7be9bb simplified interfaces for outer syntax;
wenzelm
parents: 24707
diff changeset
   434
end;