src/HOLCF/Tools/fixrec_package.ML
author haftmann
Fri, 05 Dec 2008 18:43:42 +0100
changeset 29006 abe0f11cfa4e
parent 28965 1de908189869
child 29581 b3b33e0298eb
permissions -rw-r--r--
Name.name_of -> Binding.base_name
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
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
     9
  val legacy_infer_term: theory -> term -> term
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    10
  val legacy_infer_prop: theory -> term -> term
28084
a05ca48ef263 type Attrib.binding abbreviates Name.binding without attributes;
wenzelm
parents: 28083
diff changeset
    11
  val add_fixrec: bool -> (Attrib.binding * string) list list -> theory -> theory
28965
1de908189869 cleaned up binding module and related code
haftmann
parents: 28084
diff changeset
    12
  val add_fixrec_i: bool -> ((Binding.T * attribute list) * term) list list -> theory -> theory
28084
a05ca48ef263 type Attrib.binding abbreviates Name.binding without attributes;
wenzelm
parents: 28083
diff changeset
    13
  val add_fixpat: Attrib.binding * string list -> theory -> theory
28965
1de908189869 cleaned up binding module and related code
haftmann
parents: 28084
diff changeset
    14
  val add_fixpat_i: (Binding.T * attribute list) * term list -> theory -> theory
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    15
end;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    16
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    17
structure FixrecPackage: FIXREC_PACKAGE =
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    18
struct
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    19
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    20
(* legacy type inference *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    21
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    22
fun legacy_infer_term thy t =
24493
d4380e9b287b replaced ProofContext.infer_types by general Syntax.check_terms;
wenzelm
parents: 23779
diff changeset
    23
  singleton (Syntax.check_terms (ProofContext.init thy)) (Sign.intern_term thy t);
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    24
24680
0d355aa59e67 TypeInfer.constrain: canonical argument order;
wenzelm
parents: 24493
diff changeset
    25
fun legacy_infer_prop thy t = legacy_infer_term thy (TypeInfer.constrain propT t);
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    26
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    27
26045
02aa3f166c7f use ML antiquotations
huffman
parents: 25557
diff changeset
    28
val fix_eq2 = @{thm fix_eq2};
02aa3f166c7f use ML antiquotations
huffman
parents: 25557
diff changeset
    29
val def_fix_ind = @{thm def_fix_ind};
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    30
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    31
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    32
fun fixrec_err s = error ("fixrec definition error:\n" ^ s);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    33
fun fixrec_eq_err thy s eq =
26939
1035c89b4c02 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26343
diff changeset
    34
  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
    35
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    36
(* ->> is taken from holcf_logic.ML *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    37
(* TODO: fix dependencies so we can import HOLCFLogic here *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    38
infixr 6 ->>;
26045
02aa3f166c7f use ML antiquotations
huffman
parents: 25557
diff changeset
    39
fun S ->> T = Type (@{type_name "->"},[S,T]);
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    40
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    41
(* extern_name is taken from domain/library.ML *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    42
fun extern_name con = case Symbol.explode con of 
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    43
		   ("o"::"p"::" "::rest) => implode rest
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    44
		   | _ => con;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    45
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    46
val mk_trp = HOLogic.mk_Trueprop;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    47
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    48
(* splits a cterm into the right and lefthand sides of equality *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    49
fun dest_eqs t = HOLogic.dest_eq (HOLogic.dest_Trueprop t);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    50
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    51
(* similar to Thm.head_of, but for continuous application *)
26045
02aa3f166c7f use ML antiquotations
huffman
parents: 25557
diff changeset
    52
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
    53
  | chead_of u = u;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    54
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    55
(* these are helpful functions copied from HOLCF/domain/library.ML *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    56
fun %: s = Free(s,dummyT);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    57
fun %%: s = Const(s,dummyT);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    58
infix 0 ==;  fun S ==  T = %%:"==" $ S $ T;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    59
infix 1 ===; fun S === T = %%:"op =" $ S $ T;
26045
02aa3f166c7f use ML antiquotations
huffman
parents: 25557
diff changeset
    60
infix 9 `  ; fun f ` x = %%:@{const_name Rep_CFun} $ f $ x;
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    61
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    62
(* builds the expression (LAM v. rhs) *)
26045
02aa3f166c7f use ML antiquotations
huffman
parents: 25557
diff changeset
    63
fun big_lambda v rhs = %%:@{const_name Abs_CFun}$(Term.lambda v rhs);
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    64
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    65
(* builds the expression (LAM v1 v2 .. vn. rhs) *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    66
fun big_lambdas [] rhs = rhs
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    67
  | big_lambdas (v::vs) rhs = big_lambda v (big_lambdas vs rhs);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    68
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    69
(* builds the expression (LAM <v1,v2,..,vn>. rhs) *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    70
fun lambda_ctuple [] rhs = big_lambda (%:"unit") rhs
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    71
  | lambda_ctuple (v::[]) rhs = big_lambda v rhs
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    72
  | lambda_ctuple (v::vs) rhs =
26045
02aa3f166c7f use ML antiquotations
huffman
parents: 25557
diff changeset
    73
      %%:@{const_name csplit}`(big_lambda v (lambda_ctuple vs rhs));
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    74
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    75
(* builds the expression <v1,v2,..,vn> *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    76
fun mk_ctuple [] = %%:"UU"
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    77
|   mk_ctuple (t::[]) = t
26045
02aa3f166c7f use ML antiquotations
huffman
parents: 25557
diff changeset
    78
|   mk_ctuple (t::ts) = %%:@{const_name cpair}`t`(mk_ctuple ts);
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    79
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    80
(*************************************************************************)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    81
(************* fixed-point definitions and unfolding theorems ************)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    82
(*************************************************************************)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    83
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    84
fun add_fixdefs eqs thy =
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    85
  let
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    86
    val (lhss,rhss) = ListPair.unzip (map dest_eqs eqs);
26045
02aa3f166c7f use ML antiquotations
huffman
parents: 25557
diff changeset
    87
    val fixpoint = %%:@{const_name fix}`lambda_ctuple lhss (mk_ctuple rhss);
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
    fun one_def (l as Const(n,T)) r =
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    90
          let val b = Sign.base_name n in (b, (b^"_def", l == r)) end
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    91
      | one_def _ _ = fixrec_err "fixdefs: lhs not of correct form";
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    92
    fun defs [] _ = []
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    93
      | defs (l::[]) r = [one_def l r]
26045
02aa3f166c7f use ML antiquotations
huffman
parents: 25557
diff changeset
    94
      | defs (l::ls) r = one_def l (%%:@{const_name cfst}`r) :: defs ls (%%:@{const_name csnd}`r);
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    95
    val (names, pre_fixdefs) = ListPair.unzip (defs lhss fixpoint);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    96
    
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    97
    val fixdefs = map (apsnd (legacy_infer_prop thy)) pre_fixdefs;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    98
    val (fixdef_thms, thy') =
27691
ce171cbd4b93 PureThy: dropped note_thmss_qualified, dropped _i suffix
haftmann
parents: 26939
diff changeset
    99
      PureThy.add_defs false (map Thm.no_attributes fixdefs) thy;
25132
dffe405b090d removed obsolete ML bindings;
wenzelm
parents: 24867
diff changeset
   100
    val ctuple_fixdef_thm = foldr1 (fn (x,y) => @{thm cpair_equalI} OF [x,y]) fixdef_thms;
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   101
    
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   102
    val ctuple_unfold = legacy_infer_term thy' (mk_trp (mk_ctuple lhss === mk_ctuple rhss));
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   103
    val ctuple_unfold_thm = Goal.prove_global thy' [] [] ctuple_unfold
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   104
          (fn _ => EVERY [rtac (ctuple_fixdef_thm RS fix_eq2 RS trans) 1,
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   105
                    simp_tac (simpset_of thy') 1]);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   106
    val ctuple_induct_thm =
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   107
          (space_implode "_" names ^ "_induct", ctuple_fixdef_thm RS def_fix_ind);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   108
    
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   109
    fun unfolds [] thm = []
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   110
      | unfolds (n::[]) thm = [(n^"_unfold", thm)]
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   111
      | unfolds (n::ns) thm = let
25132
dffe405b090d removed obsolete ML bindings;
wenzelm
parents: 24867
diff changeset
   112
          val thmL = thm RS @{thm cpair_eqD1};
dffe405b090d removed obsolete ML bindings;
wenzelm
parents: 24867
diff changeset
   113
          val thmR = thm RS @{thm cpair_eqD2};
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   114
        in (n^"_unfold", thmL) :: unfolds ns thmR end;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   115
    val unfold_thms = unfolds names ctuple_unfold_thm;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   116
    val thms = ctuple_induct_thm :: unfold_thms;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   117
    val (_, thy'') = PureThy.add_thms (map Thm.no_attributes thms) thy';
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   118
  in
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   119
    (thy'', names, fixdef_thms, map snd unfold_thms)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   120
  end;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   121
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   122
(*************************************************************************)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   123
(*********** monadic notation and pattern matching compilation ***********)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   124
(*************************************************************************)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   125
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   126
fun add_names (Const(a,_), bs) = insert (op =) (Sign.base_name a) bs
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   127
  | add_names (Free(a,_) , bs) = insert (op =) a bs
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   128
  | add_names (f $ u     , bs) = add_names (f, add_names(u, bs))
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   129
  | add_names (Abs(a,_,t), bs) = add_names (t, insert (op =) a bs)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   130
  | add_names (_         , bs) = bs;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   131
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   132
fun add_terms ts xs = foldr add_names xs ts;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   133
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   134
(* builds a monadic term for matching a constructor pattern *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   135
fun pre_build pat rhs vs taken =
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   136
  case pat of
26045
02aa3f166c7f use ML antiquotations
huffman
parents: 25557
diff changeset
   137
    Const(@{const_name Rep_CFun},_)$f$(v as Free(n,T)) =>
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   138
      pre_build f rhs (v::vs) taken
26045
02aa3f166c7f use ML antiquotations
huffman
parents: 25557
diff changeset
   139
  | Const(@{const_name Rep_CFun},_)$f$x =>
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   140
      let val (rhs', v, taken') = pre_build x rhs [] taken;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   141
      in pre_build f rhs' (v::vs) taken' end
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   142
  | Const(c,T) =>
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   143
      let
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   144
        val n = Name.variant taken "v";
26045
02aa3f166c7f use ML antiquotations
huffman
parents: 25557
diff changeset
   145
        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
   146
          | result_type T _ = T;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   147
        val v = Free(n, result_type T vs);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   148
        val m = "match_"^(extern_name(Sign.base_name c));
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   149
        val k = lambda_ctuple vs rhs;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   150
      in
26045
02aa3f166c7f use ML antiquotations
huffman
parents: 25557
diff changeset
   151
        (%%:@{const_name Fixrec.bind}`(%%:m`v)`k, v, n::taken)
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   152
      end
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   153
  | Free(n,_) => fixrec_err ("expected constructor, found free variable " ^ quote n)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   154
  | _ => fixrec_err "pre_build: invalid pattern";
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   155
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   156
(* builds a monadic term for matching a function definition pattern *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   157
(* returns (name, arity, matcher) *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   158
fun building pat rhs vs taken =
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   159
  case pat of
26045
02aa3f166c7f use ML antiquotations
huffman
parents: 25557
diff changeset
   160
    Const(@{const_name Rep_CFun}, _)$f$(v as Free(n,T)) =>
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   161
      building f rhs (v::vs) taken
26045
02aa3f166c7f use ML antiquotations
huffman
parents: 25557
diff changeset
   162
  | Const(@{const_name Rep_CFun}, _)$f$x =>
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   163
      let val (rhs', v, taken') = pre_build x rhs [] taken;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   164
      in building f rhs' (v::vs) taken' end
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   165
  | Const(name,_) => (name, length vs, big_lambdas vs rhs)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   166
  | _ => fixrec_err "function is not declared as constant in theory";
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   167
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   168
fun match_eq eq = 
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   169
  let val (lhs,rhs) = dest_eqs eq;
26045
02aa3f166c7f use ML antiquotations
huffman
parents: 25557
diff changeset
   170
  in building lhs (%%:@{const_name Fixrec.return}`rhs) [] (add_terms [eq] []) end;
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   171
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   172
(* returns the sum (using +++) of the terms in ms *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   173
(* also applies "run" to the result! *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   174
fun fatbar arity ms =
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   175
  let
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   176
    fun unLAM 0 t = t
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   177
      | unLAM n (_$Abs(_,_,t)) = unLAM (n-1) t
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   178
      | unLAM _ _ = fixrec_err "fatbar: internal error, not enough LAMs";
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   179
    fun reLAM 0 t = t
26045
02aa3f166c7f use ML antiquotations
huffman
parents: 25557
diff changeset
   180
      | reLAM n t = reLAM (n-1) (%%:@{const_name Abs_CFun} $ Abs("",dummyT,t));
02aa3f166c7f use ML antiquotations
huffman
parents: 25557
diff changeset
   181
    fun mplus (x,y) = %%:@{const_name Fixrec.mplus}`x`y;
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   182
    val msum = foldr1 mplus (map (unLAM arity) ms);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   183
  in
26045
02aa3f166c7f use ML antiquotations
huffman
parents: 25557
diff changeset
   184
    reLAM arity (%%:@{const_name Fixrec.run}`msum)
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   185
  end;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   186
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   187
fun unzip3 [] = ([],[],[])
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   188
  | unzip3 ((x,y,z)::ts) =
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   189
      let val (xs,ys,zs) = unzip3 ts
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   190
      in (x::xs, y::ys, z::zs) end;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   191
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   192
(* this is the pattern-matching compiler function *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   193
fun compile_pats eqs = 
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   194
  let
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   195
    val ((n::names),(a::arities),mats) = unzip3 (map match_eq eqs);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   196
    val cname = if forall (fn x => n=x) names then n
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   197
          else fixrec_err "all equations in block must define the same function";
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   198
    val arity = if forall (fn x => a=x) arities then a
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   199
          else fixrec_err "all equations in block must have the same arity";
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   200
    val rhs = fatbar arity mats;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   201
  in
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   202
    mk_trp (%%:cname === rhs)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   203
  end;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   204
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   205
(*************************************************************************)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   206
(********************** Proving associated theorems **********************)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   207
(*************************************************************************)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   208
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   209
(* proves a block of pattern matching equations as theorems, using unfold *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   210
fun make_simps thy (unfold_thm, eqns) =
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   211
  let
25132
dffe405b090d removed obsolete ML bindings;
wenzelm
parents: 24867
diff changeset
   212
    val tacs = [rtac (unfold_thm RS @{thm ssubst_lhs}) 1, asm_simp_tac (simpset_of thy) 1];
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   213
    fun prove_term t = Goal.prove_global thy [] [] t (K (EVERY tacs));
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   214
    fun prove_eqn ((name, eqn_t), atts) = ((name, prove_term eqn_t), atts);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   215
  in
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   216
    map prove_eqn eqns
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   217
  end;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   218
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   219
(*************************************************************************)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   220
(************************* Main fixrec function **************************)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   221
(*************************************************************************)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   222
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   223
fun gen_add_fixrec prep_prop prep_attrib strict blocks thy =
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   224
  let
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   225
    val eqns = List.concat blocks;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   226
    val lengths = map length blocks;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   227
    
28083
103d9282a946 explicit type Name.binding for higher-specification elements;
wenzelm
parents: 27691
diff changeset
   228
    val ((bindings, srcss), strings) = apfst split_list (split_list eqns);
29006
abe0f11cfa4e Name.name_of -> Binding.base_name
haftmann
parents: 28965
diff changeset
   229
    val names = map Binding.base_name bindings;
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   230
    val atts = map (map (prep_attrib thy)) srcss;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   231
    val eqn_ts = map (prep_prop thy) strings;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   232
    val rec_ts = map (fn eq => chead_of (fst (dest_eqs (Logic.strip_imp_concl eq)))
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   233
      handle TERM _ => fixrec_eq_err thy "not a proper equation" eq) eqn_ts;
25557
ea6b11021e79 added new primrec package
haftmann
parents: 25132
diff changeset
   234
    val (_, eqn_ts') = OldPrimrecPackage.unify_consts thy rec_ts eqn_ts;
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   235
    
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   236
    fun unconcat [] _ = []
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   237
      | unconcat (n::ns) xs = List.take (xs,n) :: unconcat ns (List.drop (xs,n));
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   238
    val pattern_blocks = unconcat lengths (map Logic.strip_imp_concl eqn_ts');
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   239
    val compiled_ts = map (legacy_infer_term thy o compile_pats) pattern_blocks;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   240
    val (thy', cnames, fixdef_thms, unfold_thms) = add_fixdefs compiled_ts thy;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   241
  in
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   242
    if strict then let (* only prove simp rules if strict = true *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   243
      val eqn_blocks = unconcat lengths ((names ~~ eqn_ts') ~~ atts);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   244
      val simps = List.concat (map (make_simps thy') (unfold_thms ~~ eqn_blocks));
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   245
      val (simp_thms, thy'') = PureThy.add_thms simps thy';
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   246
      
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   247
      val simp_names = map (fn name => name^"_simps") cnames;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   248
      val simp_attribute = rpair [Simplifier.simp_add];
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   249
      val simps' = map simp_attribute (simp_names ~~ unconcat lengths simp_thms);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   250
    in
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   251
      (snd o PureThy.add_thmss simps') thy''
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   252
    end
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   253
    else thy'
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   254
  end;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   255
24707
dfeb98f84e93 Syntax.parse/check/read;
wenzelm
parents: 24680
diff changeset
   256
val add_fixrec = gen_add_fixrec Syntax.read_prop_global Attrib.attribute;
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   257
val add_fixrec_i = gen_add_fixrec Sign.cert_prop (K I);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   258
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   259
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   260
(*************************************************************************)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   261
(******************************** Fixpat *********************************)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   262
(*************************************************************************)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   263
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   264
fun fix_pat thy t = 
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   265
  let
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   266
    val T = fastype_of t;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   267
    val eq = mk_trp (HOLogic.eq_const T $ t $ Var (("x",0),T));
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   268
    val cname = case chead_of t of Const(c,_) => c | _ =>
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   269
              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
   270
    val unfold_thm = PureThy.get_thm thy (cname^"_unfold");
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   271
    val simp = Goal.prove_global thy [] [] eq
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   272
          (fn _ => EVERY [stac unfold_thm 1, simp_tac (simpset_of thy) 1]);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   273
  in simp 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
fun gen_add_fixpat prep_term prep_attrib ((name, srcs), strings) thy =
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   276
  let
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   277
    val atts = map (prep_attrib thy) srcs;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   278
    val ts = map (prep_term thy) strings;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   279
    val simps = map (fix_pat thy) ts;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   280
  in
29006
abe0f11cfa4e Name.name_of -> Binding.base_name
haftmann
parents: 28965
diff changeset
   281
    (snd o PureThy.add_thmss [((Binding.base_name name, simps), atts)]) thy
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   282
  end;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   283
24707
dfeb98f84e93 Syntax.parse/check/read;
wenzelm
parents: 24680
diff changeset
   284
val add_fixpat = gen_add_fixpat Syntax.read_term_global Attrib.attribute;
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   285
val add_fixpat_i = gen_add_fixpat Sign.cert_term (K I);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   286
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   287
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   288
(*************************************************************************)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   289
(******************************** Parsers ********************************)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   290
(*************************************************************************)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   291
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   292
local structure P = OuterParse and K = OuterKeyword in
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   293
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   294
val fixrec_eqn = SpecParse.opt_thm_name ":" -- P.prop;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   295
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   296
val fixrec_strict = P.opt_keyword "permissive" >> not;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   297
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   298
val fixrec_decl = fixrec_strict -- P.and_list1 (Scan.repeat1 fixrec_eqn);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   299
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   300
(* this builds a parser for a new keyword, fixrec, whose functionality 
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   301
is defined by add_fixrec *)
24867
e5b55d7be9bb simplified interfaces for outer syntax;
wenzelm
parents: 24707
diff changeset
   302
val _ =
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   303
  OuterSyntax.command "fixrec" "define recursive functions (HOLCF)" K.thy_decl
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   304
    (fixrec_decl >> (Toplevel.theory o uncurry add_fixrec));
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   305
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   306
(* fixpat parser *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   307
val fixpat_decl = SpecParse.opt_thm_name ":" -- Scan.repeat1 P.prop;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   308
24867
e5b55d7be9bb simplified interfaces for outer syntax;
wenzelm
parents: 24707
diff changeset
   309
val _ =
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   310
  OuterSyntax.command "fixpat" "define rewrites for fixrec functions" K.thy_decl
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   311
    (fixpat_decl >> (Toplevel.theory o add_fixpat));
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   312
26045
02aa3f166c7f use ML antiquotations
huffman
parents: 25557
diff changeset
   313
end; (* local structure *)
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   314
24867
e5b55d7be9bb simplified interfaces for outer syntax;
wenzelm
parents: 24707
diff changeset
   315
end;