src/Pure/primitive_defs.ML
author wenzelm
Tue Sep 26 20:54:40 2017 +0200 (21 months ago)
changeset 66695 91500c024c7f
parent 64596 51f8e259de50
child 67721 5348bea4accd
permissions -rw-r--r--
tuned;
wenzelm@24259
     1
(*  Title:      Pure/primitive_defs.ML
wenzelm@24259
     2
    Author:     Makarius
wenzelm@24259
     3
wenzelm@24259
     4
Primitive definition forms.
wenzelm@24259
     5
*)
wenzelm@24259
     6
wenzelm@24259
     7
signature PRIMITIVE_DEFS =
wenzelm@24259
     8
sig
wenzelm@63042
     9
  val dest_def: Proof.context ->
wenzelm@63042
    10
    {check_head: term -> bool,
wenzelm@63042
    11
     check_free_lhs: string -> bool,
wenzelm@63042
    12
     check_free_rhs: string -> bool,
wenzelm@63042
    13
     check_tfree: string -> bool} ->
wenzelm@63395
    14
    term -> (term * term) * term list * term
wenzelm@24259
    15
  val abs_def: term -> term * term
wenzelm@24259
    16
end;
wenzelm@24259
    17
wenzelm@33385
    18
structure Primitive_Defs: PRIMITIVE_DEFS =
wenzelm@24259
    19
struct
wenzelm@24259
    20
wenzelm@24259
    21
fun term_kind (Const _) = "existing constant "
wenzelm@24259
    22
  | term_kind (Free _) = "free variable "
wenzelm@24259
    23
  | term_kind (Bound _) = "bound variable "
wenzelm@24259
    24
  | term_kind _ = "";
wenzelm@24259
    25
wenzelm@24259
    26
(*c x == t[x] to !!x. c x == t[x]*)
wenzelm@63042
    27
fun dest_def ctxt {check_head, check_free_lhs, check_free_rhs, check_tfree} eq =
wenzelm@24259
    28
  let
wenzelm@24259
    29
    fun err msg = raise TERM (msg, [eq]);
wenzelm@24259
    30
    val eq_vars = Term.strip_all_vars eq;
wenzelm@24259
    31
    val eq_body = Term.strip_all_body eq;
wenzelm@24259
    32
wenzelm@24259
    33
    val display_terms =
wenzelm@42284
    34
      commas_quote o map (Syntax.string_of_term ctxt o Syntax_Trans.bound_vars eq_vars);
wenzelm@24981
    35
    val display_types = commas_quote o map (Syntax.string_of_typ ctxt);
wenzelm@24259
    36
wenzelm@64596
    37
    val (raw_lhs, rhs) = Logic.dest_equals eq_body handle TERM _ => err "Not a meta-equality (\<equiv>)";
wenzelm@24259
    38
    val lhs = Envir.beta_eta_contract raw_lhs;
wenzelm@24259
    39
    val (head, args) = Term.strip_comb lhs;
wenzelm@24259
    40
    val head_tfrees = Term.add_tfrees head [];
wenzelm@24259
    41
wenzelm@24259
    42
    fun check_arg (Bound _) = true
wenzelm@63042
    43
      | check_arg (Free (x, _)) = check_free_lhs x
wenzelm@56243
    44
      | check_arg (Const ("Pure.type", Type ("itself", [TFree _]))) = true
wenzelm@24259
    45
      | check_arg _ = false;
wenzelm@24259
    46
    fun close_arg (Bound _) t = t
wenzelm@46214
    47
      | close_arg x t = Logic.all x t;
wenzelm@24259
    48
wenzelm@24259
    49
    val lhs_bads = filter_out check_arg args;
wenzelm@24259
    50
    val lhs_dups = duplicates (op aconv) args;
wenzelm@24259
    51
    val rhs_extras = Term.fold_aterms (fn v as Free (x, _) =>
wenzelm@63042
    52
      if check_free_rhs x orelse member (op aconv) args v then I
wenzelm@24259
    53
      else insert (op aconv) v | _ => I) rhs [];
wenzelm@24259
    54
    val rhs_extrasT = Term.fold_aterms (Term.fold_types (fn v as TFree (a, S) =>
wenzelm@63042
    55
      if check_tfree a orelse member (op =) head_tfrees (a, S) then I
wenzelm@24259
    56
      else insert (op =) v | _ => I)) rhs [];
wenzelm@24259
    57
  in
wenzelm@24259
    58
    if not (check_head head) then
wenzelm@24259
    59
      err ("Bad head of lhs: " ^ term_kind head ^ display_terms [head])
wenzelm@24259
    60
    else if not (null lhs_bads) then
wenzelm@24259
    61
      err ("Bad arguments on lhs: " ^ display_terms lhs_bads)
wenzelm@24259
    62
    else if not (null lhs_dups) then
wenzelm@24259
    63
      err ("Duplicate arguments on lhs: " ^ display_terms lhs_dups)
wenzelm@24259
    64
    else if not (null rhs_extras) then
wenzelm@24259
    65
      err ("Extra variables on rhs: " ^ display_terms rhs_extras)
wenzelm@24259
    66
    else if not (null rhs_extrasT) then
wenzelm@24259
    67
      err ("Extra type variables on rhs: " ^ display_types rhs_extrasT)
wenzelm@24259
    68
    else if exists_subterm (fn t => t aconv head) rhs then
wenzelm@24259
    69
      err "Entity to be defined occurs on rhs"
wenzelm@24259
    70
    else
wenzelm@63395
    71
      ((lhs, rhs), args,
wenzelm@63395
    72
        fold_rev close_arg args (Logic.list_all (eq_vars, (Logic.mk_equals (lhs, rhs)))))
wenzelm@24259
    73
  end;
wenzelm@24259
    74
wenzelm@24259
    75
(*!!x. c x == t[x] to c == %x. t[x]*)
wenzelm@24259
    76
fun abs_def eq =
wenzelm@24259
    77
  let
wenzelm@24259
    78
    val body = Term.strip_all_body eq;
wenzelm@24259
    79
    val vars = map Free (Term.rename_wrt_term body (Term.strip_all_vars eq));
wenzelm@24259
    80
    val (lhs, rhs) = Logic.dest_equals (Term.subst_bounds (vars, body));
wenzelm@24259
    81
    val (lhs', args) = Term.strip_comb lhs;
wenzelm@44241
    82
    val rhs' = fold_rev (absfree o dest_Free) args rhs;
wenzelm@24259
    83
  in (lhs', rhs') end;
wenzelm@24259
    84
wenzelm@24259
    85
end;