src/HOLCF/fixrec_package.ML
author huffman
Fri, 17 Jun 2005 21:19:31 +0200
changeset 16461 e6b431cb8e0c
parent 16402 36f41d5e3b3e
child 16463 342d74ca8815
permissions -rw-r--r--
support theorem names and attributes for fixrec equations; also make them into simp rules by default, like primrec does
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
     1
(*  Title:      HOLCF/fixrec_package.ML
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
     2
    ID:         $Id$
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
     3
    Author:     Amber Telfer and Brian Huffman
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
     4
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
     5
Recursive function definition package for HOLCF.
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
     6
*)
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
     7
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
     8
signature FIXREC_PACKAGE =
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
     9
sig
16461
e6b431cb8e0c support theorem names and attributes for fixrec equations; also make them into simp rules by default, like primrec does
huffman
parents: 16402
diff changeset
    10
  val add_fixrec: ((string * Attrib.src list) * string) list list -> theory -> theory
16402
36f41d5e3b3e allow theorem attributes on fixpat declarations
huffman
parents: 16401
diff changeset
    11
  val add_fixpat: ((string * Attrib.src list) * string) list -> theory -> theory
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
    12
end;
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
    13
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
    14
structure FixrecPackage: FIXREC_PACKAGE =
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
    15
struct
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
    16
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
    17
local
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
    18
open ThyParse in
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
    19
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
    20
(* ->> is taken from holcf_logic.ML *)
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
    21
(* TODO: fix dependencies so we can import HOLCFLogic here *)
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
    22
infixr 6 ->>;
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
    23
fun S ->> T = Type ("Cfun.->",[S,T]);
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
    24
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
    25
(* extern_name is taken from domain/library.ML *)
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
    26
fun extern_name con = case Symbol.explode con of 
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
    27
		   ("o"::"p"::" "::rest) => implode rest
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
    28
		   | _ => con;
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
    29
16401
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    30
val mk_trp = HOLogic.mk_Trueprop;
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    31
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
    32
(* splits a cterm into the right and lefthand sides of equality *)
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
    33
fun dest_eqs (Const ("==", _)$lhs$rhs) = (lhs, rhs)
16461
e6b431cb8e0c support theorem names and attributes for fixrec equations; also make them into simp rules by default, like primrec does
huffman
parents: 16402
diff changeset
    34
  | dest_eqs (Const ("Trueprop", _)$(Const ("op =", _)$lhs$rhs)) = (lhs,rhs)
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
    35
  | dest_eqs t = sys_error (Sign.string_of_term (sign_of (the_context())) t);
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
    36
16461
e6b431cb8e0c support theorem names and attributes for fixrec equations; also make them into simp rules by default, like primrec does
huffman
parents: 16402
diff changeset
    37
(* similar to Thm.head_of, but for continuous application *)
e6b431cb8e0c support theorem names and attributes for fixrec equations; also make them into simp rules by default, like primrec does
huffman
parents: 16402
diff changeset
    38
fun chead_of (Const("Cfun.Rep_CFun",_)$f$t) = chead_of f
e6b431cb8e0c support theorem names and attributes for fixrec equations; also make them into simp rules by default, like primrec does
huffman
parents: 16402
diff changeset
    39
  | chead_of u = u;
e6b431cb8e0c support theorem names and attributes for fixrec equations; also make them into simp rules by default, like primrec does
huffman
parents: 16402
diff changeset
    40
16387
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
    41
(* these are helpful functions copied from HOLCF/domain/library.ML *)
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
    42
fun %: s = Free(s,dummyT);
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
    43
fun %%: s = Const(s,dummyT);
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
    44
infix 0 ==;  fun S ==  T = %%:"==" $ S $ T;
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
    45
infix 1 ===; fun S === T = %%:"op =" $ S $ T;
16401
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    46
infix 9 `  ; fun f ` x = %%:"Cfun.Rep_CFun" $ f $ x;
16387
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
    47
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
    48
(* infers the type of a term *)
16402
36f41d5e3b3e allow theorem attributes on fixpat declarations
huffman
parents: 16401
diff changeset
    49
(* similar to Theory.inferT_axm, but allows any type, not just propT *)
16401
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    50
fun infer sg t =
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    51
  fst (Sign.infer_types (Sign.pp sg) sg (K NONE) (K NONE) [] true ([t],dummyT));
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    52
16402
36f41d5e3b3e allow theorem attributes on fixpat declarations
huffman
parents: 16401
diff changeset
    53
(* Similar to Term.lambda, but also allows abstraction over constants *)
16401
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    54
fun lambda' (v as Free (x, T)) t = Abs (x, T, abstract_over (v, t))
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    55
  | lambda' (v as Var ((x, _), T)) t = Abs (x, T, abstract_over (v, t))
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    56
  | lambda' (v as Const (x, T)) t = Abs (Sign.base_name x, T, abstract_over (v, t))
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    57
  | lambda' v t = raise TERM ("lambda'", [v, t]);
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    58
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    59
(* builds the expression (LAM v. rhs) *)
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    60
fun big_lambda v rhs = %%:"Cfun.Abs_CFun"$(lambda' v rhs);
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    61
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    62
(* builds the expression (LAM v1 v2 .. vn. rhs) *)
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    63
fun big_lambdas [] rhs = rhs
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    64
  | big_lambdas (v::vs) rhs = big_lambda v (big_lambdas vs rhs);
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    65
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    66
(* builds the expression (LAM <v1,v2,..,vn>. rhs) *)
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    67
fun lambda_ctuple [] rhs = big_lambda (%:"unit") rhs
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    68
  | lambda_ctuple (v::[]) rhs = big_lambda v rhs
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    69
  | lambda_ctuple (v::vs) rhs =
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    70
      %%:"Cprod.csplit"`(big_lambda v (lambda_ctuple vs rhs));
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    71
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    72
(* builds the expression <v1,v2,..,vn> *)
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    73
fun mk_ctuple [] = %%:"UU"
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    74
|   mk_ctuple (t::[]) = t
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    75
|   mk_ctuple (t::ts) = %%:"Cprod.cpair"`t`(mk_ctuple ts);
16387
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
    76
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
    77
(*************************************************************************)
16402
36f41d5e3b3e allow theorem attributes on fixpat declarations
huffman
parents: 16401
diff changeset
    78
(************* fixed-point definitions and unfolding theorems ************)
16387
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
    79
(*************************************************************************)
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
    80
16401
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    81
fun add_fixdefs eqs thy =
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
    82
  let
16401
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    83
    val (lhss,rhss) = ListPair.unzip (map dest_eqs eqs);
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    84
    val fixpoint = %%:"Fix.fix"`lambda_ctuple lhss (mk_ctuple rhss);
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    85
    
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    86
    fun one_def (l as Const(n,T)) r =
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    87
          let val b = Sign.base_name n in (b, (b^"_fixdef", l == r)) end
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    88
      | one_def _ _ = sys_error "fixdefs: lhs not of correct form";
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    89
    fun defs [] _ = []
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    90
      | defs (l::[]) r = [one_def l r]
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    91
      | defs (l::ls) r = one_def l (%%:"Cprod.cfst"`r) :: defs ls (%%:"Cprod.csnd"`r);
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    92
    val (names, pre_fixdefs) = ListPair.unzip (defs lhss fixpoint);
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    93
    
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    94
    val fixdefs = map (inferT_axm (sign_of thy)) pre_fixdefs;
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    95
    val (thy', fixdef_thms) =
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    96
      PureThy.add_defs_i false (map Thm.no_attributes fixdefs) thy;
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    97
    val ctuple_fixdef_thm = foldr1 (fn (x,y) => cpair_equalI OF [x,y]) fixdef_thms;
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    98
    
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
    99
    fun mk_cterm t = let val sg' = sign_of thy' in cterm_of sg' (infer sg' t) end;
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
   100
    val ctuple_unfold_ct = mk_cterm (mk_trp (mk_ctuple lhss === mk_ctuple rhss));
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
   101
    val ctuple_unfold_thm = prove_goalw_cterm [] ctuple_unfold_ct
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
   102
          (fn _ => [rtac (ctuple_fixdef_thm RS fix_eq2 RS trans) 1,
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
   103
                    simp_tac (simpset_of thy') 1]);
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
   104
    val ctuple_induct_thm =
16461
e6b431cb8e0c support theorem names and attributes for fixrec equations; also make them into simp rules by default, like primrec does
huffman
parents: 16402
diff changeset
   105
          (space_implode "_" names ^ "_induct", ctuple_fixdef_thm RS def_fix_ind);
16401
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
   106
    
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
   107
    fun unfolds [] thm = []
16461
e6b431cb8e0c support theorem names and attributes for fixrec equations; also make them into simp rules by default, like primrec does
huffman
parents: 16402
diff changeset
   108
      | unfolds (n::[]) thm = [(n^"_unfold", thm)]
16401
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
   109
      | unfolds (n::ns) thm = let
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
   110
          val thmL = thm RS cpair_eqD1;
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
   111
          val thmR = thm RS cpair_eqD2;
16461
e6b431cb8e0c support theorem names and attributes for fixrec equations; also make them into simp rules by default, like primrec does
huffman
parents: 16402
diff changeset
   112
        in (n^"_unfold", thmL) :: unfolds ns thmR end;
e6b431cb8e0c support theorem names and attributes for fixrec equations; also make them into simp rules by default, like primrec does
huffman
parents: 16402
diff changeset
   113
    val unfold_thms = unfolds names ctuple_unfold_thm;
e6b431cb8e0c support theorem names and attributes for fixrec equations; also make them into simp rules by default, like primrec does
huffman
parents: 16402
diff changeset
   114
    val thms = ctuple_induct_thm :: unfold_thms;
e6b431cb8e0c support theorem names and attributes for fixrec equations; also make them into simp rules by default, like primrec does
huffman
parents: 16402
diff changeset
   115
    val (thy'', _) = PureThy.add_thms (map Thm.no_attributes thms) thy';
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   116
  in
16461
e6b431cb8e0c support theorem names and attributes for fixrec equations; also make them into simp rules by default, like primrec does
huffman
parents: 16402
diff changeset
   117
    (thy'', names, fixdef_thms, map snd unfold_thms)
16401
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
   118
  end;
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   119
16387
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   120
(*************************************************************************)
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   121
(*********** monadic notation and pattern matching compilation ***********)
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   122
(*************************************************************************)
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   123
16401
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
   124
fun add_names (Const(a,_), bs) = Sign.base_name a ins_string bs
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
   125
  | add_names (Free(a,_) , bs) = a ins_string bs
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
   126
  | add_names (f $ u     , bs) = add_names (f, add_names(u, bs))
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
   127
  | add_names (Abs(a,_,t), bs) = add_names (t, a ins_string bs)
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
   128
  | add_names (_         , bs) = bs;
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   129
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   130
fun add_terms ts xs = foldr add_names xs ts;
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   131
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   132
(* builds a monadic term for matching a constructor pattern *)
16387
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   133
fun pre_build pat rhs vs taken =
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   134
  case pat of
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   135
    Const("Cfun.Rep_CFun",_)$f$(v as Free(n,T)) =>
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   136
      pre_build f rhs (v::vs) taken
16387
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   137
  | Const("Cfun.Rep_CFun",_)$f$x =>
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   138
      let val (rhs', v, taken') = pre_build x rhs [] taken;
16387
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   139
      in pre_build f rhs' (v::vs) taken' end
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   140
  | Const(c,T) =>
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   141
      let
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   142
        val n = variant taken "v";
16387
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   143
        fun result_type (Type("Cfun.->",[_,T])) (x::xs) = result_type T xs
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   144
          | result_type T _ = T;
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   145
        val v = Free(n, result_type T vs);
16401
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
   146
        val m = "match_"^(extern_name(Sign.base_name c));
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
   147
        val k = lambda_ctuple vs rhs;
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   148
      in
16387
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   149
        (%%:"Fixrec.bind"`(%%:m`v)`k, v, n::taken)
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   150
      end;
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   151
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   152
(* builds a monadic term for matching a function definition pattern *)
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   153
(* returns (name, arity, matcher) *)
16387
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   154
fun building pat rhs vs taken =
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   155
  case pat of
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   156
    Const("Cfun.Rep_CFun", _)$f$(v as Free(n,T)) =>
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   157
      building f rhs (v::vs) taken
16387
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   158
  | Const("Cfun.Rep_CFun", _)$f$x =>
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   159
      let val (rhs', v, taken') = pre_build x rhs [] taken;
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   160
      in building f rhs' (v::vs) taken' end
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   161
  | Const(_,_) => (pat, length vs, big_lambdas vs rhs)
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   162
  | _ => sys_error "function is not declared as constant in theory";
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   163
16387
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   164
fun match_eq eq = 
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   165
  let
16387
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   166
    val (lhs,rhs) = dest_eqs eq;
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   167
    val (Const(name,_), arity, term) =
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   168
      building lhs (%%:"Fixrec.return"`rhs) [] (add_terms [eq] []);
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   169
  in (name, arity, term) end;
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   170
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   171
(* returns the sum (using +++) of the terms in ms *)
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   172
(* also applies "run" to the result! *)
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   173
fun fatbar arity ms =
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   174
  let
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   175
    fun unLAM 0 t = t
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   176
      | unLAM n (_$Abs(_,_,t)) = unLAM (n-1) t
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   177
      | unLAM _ _ = sys_error "FIXREC: internal error, not enough LAMs";
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   178
    fun reLAM 0 t = t
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   179
      | reLAM n t = reLAM (n-1) (%%:"Abs_CFun" $ Abs("",dummyT,t));
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   180
    fun mplus (x,y) = %%:"Fixrec.mplus"`x`y;
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   181
    val msum = foldr1 mplus (map (unLAM arity) ms);
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   182
  in
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   183
    reLAM arity (%%:"Fixrec.run"`msum)
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   184
  end;
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   185
16387
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   186
fun unzip3 [] = ([],[],[])
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   187
  | unzip3 ((x,y,z)::ts) =
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   188
      let val (xs,ys,zs) = unzip3 ts
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   189
      in (x::xs, y::ys, z::zs) end;
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   190
16387
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   191
(* this is the pattern-matching compiler function *)
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   192
fun compile_pats eqs = 
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   193
  let
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   194
    val ((n::names),(a::arities),mats) = unzip3 (map match_eq eqs);
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   195
    val cname = if forall (fn x => n=x) names then n
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   196
          else sys_error "FIXREC: all equations must define the same function";
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   197
    val arity = if forall (fn x => a=x) arities then a
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   198
          else sys_error "FIXREC: all equations must have the same arity";
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   199
    val rhs = fatbar arity mats;
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   200
  in
16401
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
   201
    mk_trp (%%:cname === rhs)
16387
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   202
  end;
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   203
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   204
(*************************************************************************)
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   205
(********************** Proving associated theorems **********************)
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   206
(*************************************************************************)
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   207
16461
e6b431cb8e0c support theorem names and attributes for fixrec equations; also make them into simp rules by default, like primrec does
huffman
parents: 16402
diff changeset
   208
fun prove_simp thy unfold_thm t =
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   209
  let
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   210
    val ss = simpset_of thy;
16461
e6b431cb8e0c support theorem names and attributes for fixrec equations; also make them into simp rules by default, like primrec does
huffman
parents: 16402
diff changeset
   211
    val ct = cterm_of thy t;
16387
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   212
    val thm = prove_goalw_cterm [] ct
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   213
      (fn _ => [SOLVE(stac unfold_thm 1 THEN simp_tac ss 1)])
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   214
        handle _ => sys_error (string_of_cterm ct^" :: proof failed on this equation.");
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   215
  in thm end;
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   216
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   217
(* this proves that each equation is a theorem *)
16461
e6b431cb8e0c support theorem names and attributes for fixrec equations; also make them into simp rules by default, like primrec does
huffman
parents: 16402
diff changeset
   218
fun prove_simps thy (unfold_thm,ts) = map (prove_simp thy unfold_thm) ts;
16401
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
   219
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
   220
(* proves the pattern matching equations as theorems, using unfold *)
16461
e6b431cb8e0c support theorem names and attributes for fixrec equations; also make them into simp rules by default, like primrec does
huffman
parents: 16402
diff changeset
   221
fun make_simps cnames unfold_thms namess attss tss thy = 
16401
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
   222
  let
16461
e6b431cb8e0c support theorem names and attributes for fixrec equations; also make them into simp rules by default, like primrec does
huffman
parents: 16402
diff changeset
   223
    val thm_names = map (fn name => name^"_simps") cnames;
e6b431cb8e0c support theorem names and attributes for fixrec equations; also make them into simp rules by default, like primrec does
huffman
parents: 16402
diff changeset
   224
    val rew_thmss = map (prove_simps thy) (unfold_thms ~~ tss);
e6b431cb8e0c support theorem names and attributes for fixrec equations; also make them into simp rules by default, like primrec does
huffman
parents: 16402
diff changeset
   225
    val thms = (List.concat namess ~~ List.concat rew_thmss) ~~ List.concat attss;
e6b431cb8e0c support theorem names and attributes for fixrec equations; also make them into simp rules by default, like primrec does
huffman
parents: 16402
diff changeset
   226
    val (thy',_) = PureThy.add_thms thms thy;
e6b431cb8e0c support theorem names and attributes for fixrec equations; also make them into simp rules by default, like primrec does
huffman
parents: 16402
diff changeset
   227
    val thmss = thm_names ~~ rew_thmss;
e6b431cb8e0c support theorem names and attributes for fixrec equations; also make them into simp rules by default, like primrec does
huffman
parents: 16402
diff changeset
   228
    val simp_attribute = rpair [Simplifier.simp_add_global];
16401
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
   229
  in
16461
e6b431cb8e0c support theorem names and attributes for fixrec equations; also make them into simp rules by default, like primrec does
huffman
parents: 16402
diff changeset
   230
    (#1 o PureThy.add_thmss (map simp_attribute thmss)) thy'
16401
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
   231
  end;
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   232
16387
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   233
(*************************************************************************)
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   234
(************************* Main fixrec function **************************)
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   235
(*************************************************************************)
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   236
16387
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   237
(* this calls the main processing function and then returns the new state *)
16461
e6b431cb8e0c support theorem names and attributes for fixrec equations; also make them into simp rules by default, like primrec does
huffman
parents: 16402
diff changeset
   238
fun add_fixrec blocks thy =
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   239
  let
16387
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   240
    val sg = sign_of thy;
16461
e6b431cb8e0c support theorem names and attributes for fixrec equations; also make them into simp rules by default, like primrec does
huffman
parents: 16402
diff changeset
   241
    fun split_list2 xss = split_list (map split_list xss);
e6b431cb8e0c support theorem names and attributes for fixrec equations; also make them into simp rules by default, like primrec does
huffman
parents: 16402
diff changeset
   242
    val ((namess, srcsss), strss) = apfst split_list2 (split_list2 blocks);
e6b431cb8e0c support theorem names and attributes for fixrec equations; also make them into simp rules by default, like primrec does
huffman
parents: 16402
diff changeset
   243
    val attss = map (map (map (Attrib.global_attribute thy))) srcsss;
e6b431cb8e0c support theorem names and attributes for fixrec equations; also make them into simp rules by default, like primrec does
huffman
parents: 16402
diff changeset
   244
    val tss = map (map (term_of o Thm.read_cterm sg o rpair propT)) strss;
e6b431cb8e0c support theorem names and attributes for fixrec equations; also make them into simp rules by default, like primrec does
huffman
parents: 16402
diff changeset
   245
    val ts' = map (infer sg o compile_pats) tss;
e6b431cb8e0c support theorem names and attributes for fixrec equations; also make them into simp rules by default, like primrec does
huffman
parents: 16402
diff changeset
   246
    val (thy', cnames, fixdef_thms, unfold_thms) = add_fixdefs ts' thy;
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   247
  in
16461
e6b431cb8e0c support theorem names and attributes for fixrec equations; also make them into simp rules by default, like primrec does
huffman
parents: 16402
diff changeset
   248
    make_simps cnames unfold_thms namess attss tss thy'
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   249
  end;
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   250
16387
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   251
(*************************************************************************)
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   252
(******************************** Fixpat *********************************)
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   253
(*************************************************************************)
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   254
16402
36f41d5e3b3e allow theorem attributes on fixpat declarations
huffman
parents: 16401
diff changeset
   255
fun fix_pat thy pat = 
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   256
  let
16402
36f41d5e3b3e allow theorem attributes on fixpat declarations
huffman
parents: 16401
diff changeset
   257
    val sg = sign_of thy;
36f41d5e3b3e allow theorem attributes on fixpat declarations
huffman
parents: 16401
diff changeset
   258
    val t = term_of (Thm.read_cterm sg (pat, dummyT));
16387
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   259
    val T = fastype_of t;
16401
57c35ede00b9 fixrec package now handles mutually-recursive definitions
huffman
parents: 16387
diff changeset
   260
    val eq = mk_trp (HOLogic.eq_const T $ t $ Var (("x",0),T));
16461
e6b431cb8e0c support theorem names and attributes for fixrec equations; also make them into simp rules by default, like primrec does
huffman
parents: 16402
diff changeset
   261
    val cname = case chead_of t of Const(c,_) => c | _ =>
e6b431cb8e0c support theorem names and attributes for fixrec equations; also make them into simp rules by default, like primrec does
huffman
parents: 16402
diff changeset
   262
              sys_error "FIXPAT: function is not declared as constant in theory";
e6b431cb8e0c support theorem names and attributes for fixrec equations; also make them into simp rules by default, like primrec does
huffman
parents: 16402
diff changeset
   263
    val unfold_thm = Goals.get_thm thy (cname^"_unfold");
16402
36f41d5e3b3e allow theorem attributes on fixpat declarations
huffman
parents: 16401
diff changeset
   264
    val rew = prove_goalw_cterm [] (cterm_of sg eq)
16387
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   265
          (fn _ => [stac unfold_thm 1, simp_tac (simpset_of thy) 1]);
16402
36f41d5e3b3e allow theorem attributes on fixpat declarations
huffman
parents: 16401
diff changeset
   266
  in rew end;
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   267
16402
36f41d5e3b3e allow theorem attributes on fixpat declarations
huffman
parents: 16401
diff changeset
   268
fun add_fixpat pats thy = 
36f41d5e3b3e allow theorem attributes on fixpat declarations
huffman
parents: 16401
diff changeset
   269
  let
36f41d5e3b3e allow theorem attributes on fixpat declarations
huffman
parents: 16401
diff changeset
   270
    val ((names, srcss), strings) = apfst ListPair.unzip (ListPair.unzip pats);
36f41d5e3b3e allow theorem attributes on fixpat declarations
huffman
parents: 16401
diff changeset
   271
    val atts = map (map (Attrib.global_attribute thy)) srcss;
16461
e6b431cb8e0c support theorem names and attributes for fixrec equations; also make them into simp rules by default, like primrec does
huffman
parents: 16402
diff changeset
   272
    val simps = map (fix_pat thy) strings;
e6b431cb8e0c support theorem names and attributes for fixrec equations; also make them into simp rules by default, like primrec does
huffman
parents: 16402
diff changeset
   273
    val (thy', _) = PureThy.add_thms ((names ~~ simps) ~~ atts) thy;
16402
36f41d5e3b3e allow theorem attributes on fixpat declarations
huffman
parents: 16401
diff changeset
   274
  in thy' end;
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   275
16387
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   276
(*************************************************************************)
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   277
(******************************** Parsers ********************************)
67f6044c1891 cleaned up and reorganized
huffman
parents: 16226
diff changeset
   278
(*************************************************************************)
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   279
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   280
local structure P = OuterParse and K = OuterSyntax.Keyword in
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   281
16461
e6b431cb8e0c support theorem names and attributes for fixrec equations; also make them into simp rules by default, like primrec does
huffman
parents: 16402
diff changeset
   282
val fixrec_decl = P.and_list1 (Scan.repeat1 (P.opt_thm_name ":" -- P.prop));
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   283
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   284
(* this builds a parser for a new keyword, fixrec, whose functionality 
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   285
is defined by add_fixrec *)
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   286
val fixrecP =
16402
36f41d5e3b3e allow theorem attributes on fixpat declarations
huffman
parents: 16401
diff changeset
   287
  OuterSyntax.command "fixrec" "define recursive functions (HOLCF)" K.thy_decl
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   288
    (fixrec_decl >> (Toplevel.theory o add_fixrec));
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   289
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   290
(* this adds the parser for fixrec to the syntax *)
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   291
val _ = OuterSyntax.add_parsers [fixrecP];
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   292
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   293
(* fixpat parser *)
16461
e6b431cb8e0c support theorem names and attributes for fixrec equations; also make them into simp rules by default, like primrec does
huffman
parents: 16402
diff changeset
   294
val fixpat_decl = Scan.repeat1 (P.opt_thm_name ":" -- P.prop);
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   295
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   296
val fixpatP =
16402
36f41d5e3b3e allow theorem attributes on fixpat declarations
huffman
parents: 16401
diff changeset
   297
  OuterSyntax.command "fixpat" "define rewrites for fixrec functions" K.thy_decl
16226
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   298
    (fixpat_decl >> (Toplevel.theory o add_fixpat));
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   299
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   300
val _ = OuterSyntax.add_parsers [fixpatP];
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   301
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   302
end; (* local structure *)
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   303
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   304
end; (* local open *)
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   305
c17ac524d866 implementation of fixrec package
huffman
parents:
diff changeset
   306
end; (* struct *)