src/HOL/Nominal/nominal_primrec.ML
author wenzelm
Fri, 19 Jan 2007 22:08:08 +0100
changeset 22101 6d13239d5f52
parent 21767 78ed5e22766a
child 22330 00ca68f5ce29
permissions -rw-r--r--
moved parts of OuterParse to SpecParse;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
21541
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
     1
(*  Title:      HOL/Nominal/nominal_primrec.ML
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
     2
    ID:         $Id$
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
     3
    Author:     Stefan Berghofer, TU Muenchen and Norbert Voelker, FernUni Hagen
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
     4
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
     5
Package for defining functions on nominal datatypes by primitive recursion.
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
     6
Taken from HOL/Tools/primrec_package.ML
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
     7
*)
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
     8
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
     9
signature NOMINAL_PRIMREC =
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    10
sig
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    11
  val quiet_mode: bool ref
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    12
  val add_primrec: string -> string list option -> string option ->
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    13
    ((bstring * string) * Attrib.src list) list -> theory -> Proof.state
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    14
  val add_primrec_unchecked: string -> string list option -> string option ->
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    15
    ((bstring * string) * Attrib.src list) list -> theory -> Proof.state
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    16
  val add_primrec_i: string -> term list option -> term option ->
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    17
    ((bstring * term) * attribute list) list -> theory -> Proof.state
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    18
  val add_primrec_unchecked_i: string -> term list option -> term option ->
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    19
    ((bstring * term) * attribute list) list -> theory -> Proof.state
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    20
end;
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    21
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    22
structure NominalPrimrec : NOMINAL_PRIMREC =
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    23
struct
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    24
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    25
open DatatypeAux;
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    26
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    27
exception RecError of string;
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    28
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    29
fun primrec_err s = error ("Nominal primrec definition error:\n" ^ s);
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    30
fun primrec_eq_err thy s eq =
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    31
  primrec_err (s ^ "\nin\n" ^ quote (Sign.string_of_term thy eq));
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    32
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    33
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    34
(* messages *)
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    35
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    36
val quiet_mode = ref false;
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    37
fun message s = if ! quiet_mode then () else writeln s;
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    38
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    39
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    40
(* preprocessing of equations *)
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    41
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    42
fun process_eqn thy eq rec_fns = 
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    43
  let
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    44
    val (lhs, rhs) = 
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    45
      if null (term_vars eq) then
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    46
        HOLogic.dest_eq (HOLogic.dest_Trueprop (Logic.strip_imp_concl eq))
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    47
        handle TERM _ => raise RecError "not a proper equation"
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    48
      else raise RecError "illegal schematic variable(s)";
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    49
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    50
    val (recfun, args) = strip_comb lhs;
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    51
    val fnameT = dest_Const recfun handle TERM _ => 
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    52
      raise RecError "function is not declared as constant in theory";
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    53
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    54
    val (ls', rest)  = take_prefix is_Free args;
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    55
    val (middle, rs') = take_suffix is_Free rest;
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    56
    val rpos = length ls';
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    57
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    58
    val (constr, cargs') = if null middle then raise RecError "constructor missing"
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    59
      else strip_comb (hd middle);
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    60
    val (cname, T) = dest_Const constr
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    61
      handle TERM _ => raise RecError "ill-formed constructor";
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    62
    val (tname, _) = dest_Type (body_type T) handle TYPE _ =>
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    63
      raise RecError "cannot determine datatype associated with function"
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    64
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    65
    val (ls, cargs, rs) =
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    66
      (map dest_Free ls', map dest_Free cargs', map dest_Free rs')
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    67
      handle TERM _ => raise RecError "illegal argument in pattern";
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    68
    val lfrees = ls @ rs @ cargs;
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    69
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    70
    fun check_vars _ [] = ()
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    71
      | check_vars s vars = raise RecError (s ^ commas_quote (map fst vars))
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    72
  in
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    73
    if length middle > 1 then 
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    74
      raise RecError "more than one non-variable in pattern"
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    75
    else
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    76
     (check_vars "repeated variable names in pattern: " (duplicates (op =) lfrees);
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    77
      check_vars "extra variables on rhs: "
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    78
        (map dest_Free (term_frees rhs) \\ lfrees);
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    79
      case AList.lookup (op =) rec_fns fnameT of
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    80
        NONE =>
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    81
          (fnameT, (tname, rpos, [(cname, (ls, cargs, rs, rhs, eq))]))::rec_fns
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    82
      | SOME (_, rpos', eqns) =>
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    83
          if AList.defined (op =) eqns cname then
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    84
            raise RecError "constructor already occurred as pattern"
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    85
          else if rpos <> rpos' then
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    86
            raise RecError "position of recursive argument inconsistent"
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    87
          else
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    88
            AList.update (op =) (fnameT, (tname, rpos, (cname, (ls, cargs, rs, rhs, eq))::eqns))
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    89
              rec_fns)
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    90
  end
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    91
  handle RecError s => primrec_eq_err thy s eq;
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    92
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    93
val param_err = "Parameters must be the same for all recursive functions";
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    94
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    95
fun process_fun thy descr rec_eqns (i, fnameT as (fname, _)) (fnameTs, fnss) =
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    96
  let
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    97
    val (_, (tname, _, constrs)) = List.nth (descr, i);
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    98
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
    99
    (* substitute "fname ls x rs" by "y" for (x, (_, y)) in subs *)
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   100
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   101
    fun subst [] t fs = (t, fs)
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   102
      | subst subs (Abs (a, T, t)) fs =
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   103
          fs
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   104
          |> subst subs t
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   105
          |-> (fn t' => pair (Abs (a, T, t')))
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   106
      | subst subs (t as (_ $ _)) fs =
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   107
          let
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   108
            val (f, ts) = strip_comb t;
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   109
          in
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   110
            if is_Const f andalso dest_Const f mem map fst rec_eqns then
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   111
              let
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   112
                val fnameT' as (fname', _) = dest_Const f;
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   113
                val (_, rpos, eqns) = the (AList.lookup (op =) rec_eqns fnameT');
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   114
                val ls = Library.take (rpos, ts);
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   115
                val rest = Library.drop (rpos, ts);
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   116
                val (x', rs) = (hd rest, tl rest)
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   117
                  handle Empty => raise RecError ("not enough arguments\
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   118
                   \ in recursive application\nof function " ^ quote fname' ^ " on rhs");
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   119
                val _ = (case eqns of
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   120
                    (_, (ls', _, rs', _, _)) :: _ =>
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   121
                      if ls = map Free ls' andalso rs = map Free rs' then ()
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   122
                      else raise RecError param_err
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   123
                  | _ => ());
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   124
                val (x, xs) = strip_comb x'
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   125
              in case AList.lookup (op =) subs x
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   126
               of NONE =>
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   127
                    fs
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   128
                    |> fold_map (subst subs) ts
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   129
                    |-> (fn ts' => pair (list_comb (f, ts')))
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   130
                | SOME (i', y) =>
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   131
                    fs
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   132
                    |> fold_map (subst subs) xs
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   133
                    ||> process_fun thy descr rec_eqns (i', fnameT')
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   134
                    |-> (fn ts' => pair (list_comb (y, ts')))
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   135
              end
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   136
            else
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   137
              fs
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   138
              |> fold_map (subst subs) (f :: ts)
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   139
              |-> (fn (f'::ts') => pair (list_comb (f', ts')))
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   140
          end
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   141
      | subst _ t fs = (t, fs);
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   142
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   143
    (* translate rec equations into function arguments suitable for rec comb *)
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   144
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   145
    fun trans eqns (cname, cargs) (fnameTs', fnss', fns) =
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   146
      (case AList.lookup (op =) eqns cname of
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   147
          NONE => (warning ("No equation for constructor " ^ quote cname ^
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   148
            "\nin definition of function " ^ quote fname);
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   149
              (fnameTs', fnss', (Const ("arbitrary", dummyT))::fns))
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   150
        | SOME (ls, cargs', rs, rhs, eq) =>
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   151
            let
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   152
              val recs = filter (is_rec_type o snd) (cargs' ~~ cargs);
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   153
              val rargs = map fst recs;
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   154
              val subs = map (rpair dummyT o fst) 
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   155
                (rev (rename_wrt_term rhs rargs));
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   156
              val (rhs', (fnameTs'', fnss'')) = 
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   157
                  (subst (map (fn ((x, y), z) =>
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   158
                               (Free x, (body_index y, Free z)))
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   159
                          (recs ~~ subs)) rhs (fnameTs', fnss'))
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   160
                  handle RecError s => primrec_eq_err thy s eq
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   161
            in (fnameTs'', fnss'', 
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   162
                (list_abs_free (cargs' @ subs, rhs'))::fns)
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   163
            end)
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   164
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   165
  in (case AList.lookup (op =) fnameTs i of
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   166
      NONE =>
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   167
        if exists (equal fnameT o snd) fnameTs then
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   168
          raise RecError ("inconsistent functions for datatype " ^ quote tname)
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   169
        else
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   170
          let
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   171
            val SOME (_, _, eqns as (_, (ls, _, rs, _, _)) :: _) =
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   172
              AList.lookup (op =) rec_eqns fnameT;
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   173
            val (fnameTs', fnss', fns) = fold_rev (trans eqns) constrs
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   174
              ((i, fnameT)::fnameTs, fnss, []) 
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   175
          in
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   176
            (fnameTs', (i, (fname, ls, rs, fns))::fnss')
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   177
          end
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   178
    | SOME fnameT' =>
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   179
        if fnameT = fnameT' then (fnameTs, fnss)
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   180
        else raise RecError ("inconsistent functions for datatype " ^ quote tname))
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   181
  end;
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   182
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   183
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   184
(* prepare functions needed for definitions *)
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   185
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   186
fun get_fns fns ((i : int, (tname, _, constrs)), rec_name) (fs, defs) =
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   187
  case AList.lookup (op =) fns i of
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   188
     NONE =>
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   189
       let
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   190
         val dummy_fns = map (fn (_, cargs) => Const ("arbitrary",
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   191
           replicate ((length cargs) + (length (List.filter is_rec_type cargs)))
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   192
             dummyT ---> HOLogic.unitT)) constrs;
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   193
         val _ = warning ("No function definition for datatype " ^ quote tname)
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   194
       in
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   195
         (dummy_fns @ fs, defs)
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   196
       end
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   197
   | SOME (fname, ls, rs, fs') => (fs' @ fs, (fname, ls, rs, rec_name, tname) :: defs);
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   198
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   199
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   200
(* make definition *)
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   201
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   202
fun make_def thy fs (fname, ls, rs, rec_name, tname) =
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   203
  let
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   204
    val used = map fst (fold Term.add_frees fs []);
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   205
    val x = (Name.variant used "x", dummyT);
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   206
    val frees = ls @ x :: rs;
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   207
    val rhs = list_abs_free (frees,
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   208
      list_comb (Const (rec_name, dummyT), fs @ [Free x]))
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   209
    val defpair = (Sign.base_name fname ^ "_" ^ Sign.base_name tname ^ "_def",
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   210
                   Logic.mk_equals (Const (fname, dummyT), rhs));
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   211
    val defpair' as (_, _ $ _ $ t) = Theory.inferT_axm thy defpair
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   212
  in (defpair', subst_bounds (rev (map Free frees), strip_abs_body t)) end;
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   213
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   214
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   215
(* find datatypes which contain all datatypes in tnames' *)
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   216
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   217
fun find_dts (dt_info : NominalPackage.nominal_datatype_info Symtab.table) _ [] = []
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   218
  | find_dts dt_info tnames' (tname::tnames) =
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   219
      (case Symtab.lookup dt_info tname of
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   220
          NONE => primrec_err (quote tname ^ " is not a nominal datatype")
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   221
        | SOME dt =>
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   222
            if tnames' subset (map (#1 o snd) (#descr dt)) then
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   223
              (tname, dt)::(find_dts dt_info tnames' tnames)
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   224
            else find_dts dt_info tnames' tnames);
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   225
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   226
fun common_prefix eq ([], _) = []
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   227
  | common_prefix eq (_, []) = []
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   228
  | common_prefix eq (x :: xs, y :: ys) =
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   229
      if eq (x, y) then x :: common_prefix eq (xs, ys) else [];
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   230
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   231
local
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   232
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   233
fun gen_primrec_i note def alt_name invs fctxt eqns_atts thy =
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   234
  let
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   235
    val (eqns, atts) = split_list eqns_atts;
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   236
    val dt_info = NominalPackage.get_nominal_datatypes thy;
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   237
    val rec_eqns = fold_rev (process_eqn thy o snd) eqns [];
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   238
    val lsrs :: lsrss = maps (fn (_, (_, _, eqns)) =>
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   239
      map (fn (_, (ls, _, rs, _, _)) => ls @ rs) eqns) rec_eqns
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   240
    val _ =
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   241
      (if forall (curry eq_set lsrs) lsrss andalso forall
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   242
         (fn (_, (_, _, (_, (ls, _, rs, _, _)) :: eqns)) =>
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   243
               forall (fn (_, (ls', _, rs', _, _)) =>
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   244
                 ls = ls' andalso rs = rs') eqns
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   245
           | _ => true) rec_eqns
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   246
       then () else raise RecError param_err);
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   247
    val tnames = distinct (op =) (map (#1 o snd) rec_eqns);
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   248
    val dts = find_dts dt_info tnames tnames;
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   249
    val main_fns = 
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   250
      map (fn (tname, {index, ...}) =>
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   251
        (index, 
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   252
          (fst o the o find_first (fn f => (#1 o snd) f = tname)) rec_eqns))
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   253
      dts;
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   254
    val {descr, rec_names, rec_rewrites, ...} = 
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   255
      if null dts then
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   256
        primrec_err ("datatypes " ^ commas_quote tnames ^ "\nare not mutually recursive")
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   257
      else snd (hd dts);
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   258
    val (fnameTs, fnss) =
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   259
      fold_rev (process_fun thy descr rec_eqns) main_fns ([], []);
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   260
    val (fs, defs) = fold_rev (get_fns fnss) (descr ~~ rec_names) ([], []);
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   261
    val defs' = map (make_def thy fs) defs;
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   262
    val nameTs1 = map snd fnameTs;
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   263
    val nameTs2 = map fst rec_eqns;
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   264
    val _ = if gen_eq_set (op =) (nameTs1, nameTs2) then ()
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   265
            else primrec_err ("functions " ^ commas_quote (map fst nameTs2) ^
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   266
              "\nare not mutually recursive");
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   267
    val primrec_name =
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   268
      if alt_name = "" then (space_implode "_" (map (Sign.base_name o #1) defs)) else alt_name;
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   269
    val (defs_thms', thy') =
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   270
      thy
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   271
      |> Theory.add_path primrec_name
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   272
      |> fold_map def (map (fn ((name, t), _) => ((name, []), t)) defs');
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   273
    val cert = cterm_of thy';
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   274
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   275
    fun mk_idx eq =
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   276
      let
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   277
        val Const c = head_of (fst (HOLogic.dest_eq (HOLogic.dest_Trueprop
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   278
          (Logic.strip_imp_concl eq))));
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   279
        val SOME i = AList.lookup op = (map swap fnameTs) c;
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   280
        val SOME (_, _, constrs) = AList.lookup op = descr i;
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   281
        val SOME (_, _, eqns) = AList.lookup op = rec_eqns c;
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   282
        val SOME (cname, (_, cargs, _, _, _)) = find_first
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   283
          (fn (_, (_, _, _, _, eq')) => eq = eq') eqns
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   284
      in (i, find_index (fn (cname', _) => cname = cname') constrs, cargs) end;
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   285
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   286
    val rec_rewritess =
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   287
      unflat (map (fn (_, (_, _, constrs)) => constrs) descr) rec_rewrites;
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   288
    val fvars = rec_rewrites |> hd |> concl_of |> HOLogic.dest_Trueprop |>
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   289
      HOLogic.dest_eq |> fst |> strip_comb |> snd |> take_prefix is_Var |> fst;
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   290
    val (pvars, ctxtvars) = List.partition
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   291
      (equal HOLogic.boolT o body_type o snd)
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   292
      (fold Term.add_vars (map Logic.strip_assums_concl
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   293
        (prems_of (hd rec_rewrites))) [] \\ map dest_Var fvars);
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   294
    val cfs = defs' |> hd |> snd |> strip_comb |> snd |>
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   295
      curry (List.take o swap) (length fvars) |> map cert;
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   296
    val invs' = (case invs of
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   297
        NONE => map (fn (i, _) =>
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   298
          let
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   299
            val SOME (_, T) = AList.lookup op = fnameTs i
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   300
            val (Ts, U) = strip_type T
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   301
          in
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   302
            Abs ("x", List.drop (Ts, length lsrs + 1) ---> U, HOLogic.true_const)
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   303
          end) descr
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   304
      | SOME invs' => invs');
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   305
    val inst = (map cert fvars ~~ cfs) @
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   306
      (map (cert o Var) pvars ~~ map cert invs') @
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   307
      (case ctxtvars of
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   308
         [ctxtvar] => [(cert (Var ctxtvar), cert (the_default HOLogic.unit fctxt))]
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   309
       | _ => []);
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   310
    val rec_rewrites' = map (fn (_, eq) =>
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   311
      let
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   312
        val (i, j, cargs) = mk_idx eq
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   313
        val th = nth (nth rec_rewritess i) j;
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   314
        val cargs' = th |> concl_of |> HOLogic.dest_Trueprop |>
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   315
          HOLogic.dest_eq |> fst |> strip_comb |> snd |> split_last |> snd |>
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   316
          strip_comb |> snd
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   317
      in (cargs, Logic.strip_imp_prems eq,
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   318
        Drule.cterm_instantiate (inst @
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   319
          (map (cterm_of thy') cargs' ~~ map (cterm_of thy' o Free) cargs)) th)
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   320
      end) eqns;
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   321
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   322
    val prems = foldr1 (common_prefix op aconv) (map (prems_of o #3) rec_rewrites');
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   323
    val cprems = map cert prems;
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   324
    val asms = map Thm.assume cprems;
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   325
    val premss = map (fn (cargs, eprems, eqn) =>
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   326
      map (fn t => list_all_free (cargs, Logic.list_implies (eprems, t)))
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   327
        (List.drop (prems_of eqn, length prems))) rec_rewrites';
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   328
    val cpremss = map (map cert) premss;
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   329
    val asmss = map (map Thm.assume) cpremss;
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   330
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   331
    fun mk_eqn ((cargs, eprems, eqn), asms') =
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   332
      let
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   333
        val ceprems = map cert eprems;
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   334
        val asms'' = map Thm.assume ceprems;
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   335
        val ccargs = map (cert o Free) cargs;
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   336
        val asms''' = map (fn th => implies_elim_list
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   337
          (forall_elim_list ccargs th) asms'') asms'
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   338
      in
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   339
        implies_elim_list eqn (asms @ asms''') |>
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   340
        implies_intr_list ceprems |>
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   341
        forall_intr_list ccargs
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   342
      end;
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   343
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   344
    val rule_prems = cprems @ flat cpremss;
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   345
    val rule = implies_intr_list rule_prems
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   346
      (foldr1 (uncurry Conjunction.intr) (map mk_eqn (rec_rewrites' ~~ asmss)));
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   347
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   348
    val goals = map (fn ((cargs, _, _), (_, eqn)) =>
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   349
      (list_all_free (cargs, eqn), [])) (rec_rewrites' ~~ eqns);
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   350
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   351
  in
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   352
    thy' |>
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   353
    ProofContext.init |>
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   354
    Proof.theorem_i NONE
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   355
      (fn thss => ProofContext.theory (fn thy =>
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   356
         let
21618
1cbb1134cb6c Added missing "standard"
nipkow
parents: 21541
diff changeset
   357
           val simps = map standard (flat thss);
21541
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   358
           val (simps', thy') =
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   359
             fold_map note ((map fst eqns ~~ atts) ~~ map single simps) thy;
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   360
           val simps'' = maps snd simps'
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   361
         in
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   362
           thy'
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   363
           |> note (("simps", [Simplifier.simp_add]), simps'')
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   364
           |> snd
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   365
           |> Theory.parent_path
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   366
         end))
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   367
      [goals] |>
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   368
    Proof.apply (Method.Basic (fn ctxt => Method.RAW_METHOD (fn ths =>
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   369
      rewrite_goals_tac (map snd defs_thms') THEN
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   370
      compose_tac (false, rule, length rule_prems) 1))) |>
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   371
    Seq.hd
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   372
  end;
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   373
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   374
fun gen_primrec note def alt_name invs fctxt eqns thy =
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   375
  let
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   376
    fun readt T s = term_of (Thm.read_cterm thy (s, T));
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   377
    val ((names, strings), srcss) = apfst split_list (split_list eqns);
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   378
    val atts = map (map (Attrib.attribute thy)) srcss;
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   379
    val eqn_ts = map (fn s => readt propT s
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   380
      handle ERROR msg => cat_error msg ("The error(s) above occurred for " ^ s)) strings;
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   381
    val rec_ts = map (fn eq => head_of (fst (HOLogic.dest_eq
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   382
      (HOLogic.dest_Trueprop (Logic.strip_imp_concl eq))))
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   383
      handle TERM _ => primrec_eq_err thy "not a proper equation" eq) eqn_ts;
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   384
    val (_, eqn_ts') = OldInductivePackage.unify_consts thy rec_ts eqn_ts
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   385
  in
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   386
    gen_primrec_i note def alt_name
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   387
      (Option.map (map (readt TypeInfer.logicT)) invs)
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   388
      (Option.map (readt TypeInfer.logicT) fctxt)
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   389
      (names ~~ eqn_ts' ~~ atts) thy
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   390
  end;
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   391
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   392
fun thy_note ((name, atts), thms) =
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   393
  PureThy.add_thmss [((name, thms), atts)] #-> (fn [thms] => pair (name, thms));
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   394
fun thy_def false ((name, atts), t) =
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   395
      PureThy.add_defs_i false [((name, t), atts)] #-> (fn [thm] => pair (name, thm))
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   396
  | thy_def true ((name, atts), t) =
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   397
      PureThy.add_defs_unchecked_i false [((name, t), atts)] #-> (fn [thm] => pair (name, thm));
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   398
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   399
in
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   400
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   401
val add_primrec = gen_primrec thy_note (thy_def false);
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   402
val add_primrec_unchecked = gen_primrec thy_note (thy_def true);
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   403
val add_primrec_i = gen_primrec_i thy_note (thy_def false);
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   404
val add_primrec_unchecked_i = gen_primrec_i thy_note (thy_def true);
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   405
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   406
end; (*local*)
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   407
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   408
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   409
(* outer syntax *)
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   410
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   411
local structure P = OuterParse and K = OuterKeyword in
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   412
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   413
val parser1 = P.$$$ "freshness_context" |-- P.$$$ ":" |-- (P.term >> SOME);
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   414
val parser2 =
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   415
  P.$$$ "invariant" |-- P.$$$ ":" |--
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   416
    (Scan.repeat1 P.term >> SOME) -- Scan.optional parser1 NONE ||
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   417
  (parser1 >> pair NONE);
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   418
val parser3 =
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   419
  P.name -- Scan.optional parser2 (NONE, NONE) ||
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   420
  (parser2 >> pair "");
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   421
val parser4 =
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   422
  (P.$$$ "unchecked" >> K true) -- Scan.optional parser3 ("", (NONE, NONE)) ||
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   423
  (parser3 >> pair false);
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   424
val options =
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   425
  Scan.optional (P.$$$ "(" |-- P.!!!
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   426
    (parser4 --| P.$$$ ")")) (false, ("", (NONE, NONE)));
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   427
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   428
val primrec_decl =
22101
6d13239d5f52 moved parts of OuterParse to SpecParse;
wenzelm
parents: 21767
diff changeset
   429
  options -- Scan.repeat1 (SpecParse.opt_thm_name ":" -- P.prop);
21541
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   430
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   431
val primrecP =
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   432
  OuterSyntax.command "nominal_primrec" "define primitive recursive functions on nominal datatypes" K.thy_goal
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   433
    (primrec_decl >> (fn ((unchecked, (alt_name, (invs, fctxt))), eqns) =>
21767
78ed5e22766a nominal_primrec now prints initial proof state.
berghofe
parents: 21618
diff changeset
   434
      Toplevel.print o Toplevel.theory_to_proof
21541
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   435
        ((if unchecked then add_primrec_unchecked else add_primrec) alt_name invs fctxt
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   436
          (map P.triple_swap eqns))));
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   437
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   438
val _ = OuterSyntax.add_parsers [primrecP];
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   439
val _ = OuterSyntax.add_keywords ["invariant", "freshness_context"];
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   440
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   441
end;
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   442
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   443
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   444
end;
ea881fbe0489 Implemented new "nominal_primrec" command for defining
berghofe
parents:
diff changeset
   445