src/Pure/primitive_defs.ML
author wenzelm
Thu, 11 Oct 2007 19:10:23 +0200
changeset 24981 4ec3f95190bf
parent 24259 c9e05c49d02c
child 29580 117b88da143c
permissions -rw-r--r--
dest/cert_def: replaced Pretty.pp by explicit Proof.context;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24259
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/primitive_defs.ML
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
     3
    Author:     Makarius
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
     4
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
     5
Primitive definition forms.
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
     6
*)
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
     7
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
     8
signature PRIMITIVE_DEFS =
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
     9
sig
24981
4ec3f95190bf dest/cert_def: replaced Pretty.pp by explicit Proof.context;
wenzelm
parents: 24259
diff changeset
    10
  val dest_def: Proof.context -> (term -> bool) -> (string -> bool) -> (string -> bool) ->
24259
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    11
    term -> (term * term) * term
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    12
  val abs_def: term -> term * term
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    13
  val mk_defpair: term * term -> string * term
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    14
end;
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    15
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    16
structure PrimitiveDefs: PRIMITIVE_DEFS =
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    17
struct
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    18
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    19
fun term_kind (Const _) = "existing constant "
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    20
  | term_kind (Free _) = "free variable "
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    21
  | term_kind (Bound _) = "bound variable "
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    22
  | term_kind _ = "";
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    23
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    24
(*c x == t[x] to !!x. c x == t[x]*)
24981
4ec3f95190bf dest/cert_def: replaced Pretty.pp by explicit Proof.context;
wenzelm
parents: 24259
diff changeset
    25
fun dest_def ctxt check_head is_fixed is_fixedT eq =
24259
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    26
  let
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    27
    fun err msg = raise TERM (msg, [eq]);
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    28
    val eq_vars = Term.strip_all_vars eq;
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    29
    val eq_body = Term.strip_all_body eq;
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    30
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    31
    val display_terms =
24981
4ec3f95190bf dest/cert_def: replaced Pretty.pp by explicit Proof.context;
wenzelm
parents: 24259
diff changeset
    32
      commas_quote o map (Syntax.string_of_term ctxt o Syntax.bound_vars eq_vars);
4ec3f95190bf dest/cert_def: replaced Pretty.pp by explicit Proof.context;
wenzelm
parents: 24259
diff changeset
    33
    val display_types = commas_quote o map (Syntax.string_of_typ ctxt);
24259
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    34
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    35
    val (raw_lhs, rhs) = Logic.dest_equals eq_body handle TERM _ => err "Not a meta-equality (==)";
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    36
    val lhs = Envir.beta_eta_contract raw_lhs;
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    37
    val (head, args) = Term.strip_comb lhs;
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    38
    val head_tfrees = Term.add_tfrees head [];
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    39
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    40
    fun check_arg (Bound _) = true
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    41
      | check_arg (Free (x, _)) = not (is_fixed x)
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    42
      | check_arg (Const ("TYPE", Type ("itself", [TFree _]))) = true
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    43
      | check_arg _ = false;
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    44
    fun close_arg (Bound _) t = t
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    45
      | close_arg x t = Term.all (Term.fastype_of x) $ lambda x t;
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    46
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    47
    val lhs_bads = filter_out check_arg args;
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    48
    val lhs_dups = duplicates (op aconv) args;
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    49
    val rhs_extras = Term.fold_aterms (fn v as Free (x, _) =>
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    50
      if is_fixed x orelse member (op aconv) args v then I
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    51
      else insert (op aconv) v | _ => I) rhs [];
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    52
    val rhs_extrasT = Term.fold_aterms (Term.fold_types (fn v as TFree (a, S) =>
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    53
      if is_fixedT a orelse member (op =) head_tfrees (a, S) then I
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    54
      else insert (op =) v | _ => I)) rhs [];
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    55
  in
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    56
    if not (check_head head) then
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    57
      err ("Bad head of lhs: " ^ term_kind head ^ display_terms [head])
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    58
    else if not (null lhs_bads) then
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    59
      err ("Bad arguments on lhs: " ^ display_terms lhs_bads)
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    60
    else if not (null lhs_dups) then
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    61
      err ("Duplicate arguments on lhs: " ^ display_terms lhs_dups)
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    62
    else if not (null rhs_extras) then
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    63
      err ("Extra variables on rhs: " ^ display_terms rhs_extras)
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    64
    else if not (null rhs_extrasT) then
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    65
      err ("Extra type variables on rhs: " ^ display_types rhs_extrasT)
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    66
    else if exists_subterm (fn t => t aconv head) rhs then
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    67
      err "Entity to be defined occurs on rhs"
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    68
    else
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    69
      ((lhs, rhs), fold_rev close_arg args (Term.list_all (eq_vars, (Logic.mk_equals (lhs, rhs)))))
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    70
  end;
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    71
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    72
(*!!x. c x == t[x] to c == %x. t[x]*)
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    73
fun abs_def eq =
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    74
  let
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    75
    val body = Term.strip_all_body eq;
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    76
    val vars = map Free (Term.rename_wrt_term body (Term.strip_all_vars eq));
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    77
    val (lhs, rhs) = Logic.dest_equals (Term.subst_bounds (vars, body));
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    78
    val (lhs', args) = Term.strip_comb lhs;
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    79
    val rhs' = Term.list_abs_free (map Term.dest_Free args, rhs);
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    80
  in (lhs', rhs') end;
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    81
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    82
fun mk_defpair (lhs, rhs) =
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    83
  (case Term.head_of lhs of
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    84
    Const (name, _) =>
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    85
      (NameSpace.base name ^ "_def", Logic.mk_equals (lhs, rhs))
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    86
  | _ => raise TERM ("Malformed definition: head of lhs not a constant", [lhs, rhs]));
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    87
c9e05c49d02c Primitive definition forms.
wenzelm
parents:
diff changeset
    88
end;