src/Pure/Isar/proof_context.ML
changeset 9291 23705d14be8f
parent 9274 21c302a2fd9a
child 9325 a7f3deedacdb
equal deleted inserted replaced
9290:be5924604010 9291:23705d14be8f
    25   val init: theory -> context
    25   val init: theory -> context
    26   val assumptions: context -> (cterm * ((int -> tactic) * (int -> tactic))) list
    26   val assumptions: context -> (cterm * ((int -> tactic) * (int -> tactic))) list
    27   val fixed_names: context -> string list
    27   val fixed_names: context -> string list
    28   val read_typ: context -> string -> typ
    28   val read_typ: context -> string -> typ
    29   val cert_typ: context -> typ -> typ
    29   val cert_typ: context -> typ -> typ
    30   val cert_skolem: context -> string -> string
    30   val intern_skolem: context -> term -> term
    31   val extern_skolem: context -> term -> term
    31   val extern_skolem: context -> term -> term
    32   val read_termTs: context -> (string * typ) list -> term list * (indexname * typ) list
    32   val read_termTs: context -> (string * typ) list -> term list * (indexname * typ) list
    33   val read_term: context -> string -> term
    33   val read_term: context -> string -> term
    34   val read_prop: context -> string -> term
    34   val read_prop: context -> string -> term
    35   val read_termT_pats: context -> (string * typ) list -> term list
    35   val read_termT_pats: context -> (string * typ) list -> term list
    77     -> context -> context * ((string * thm list) list * thm list)
    77     -> context -> context * ((string * thm list) list * thm list)
    78   val read_vars: context * (string list * string option) -> context * (string list * typ option)
    78   val read_vars: context * (string list * string option) -> context * (string list * typ option)
    79   val cert_vars: context * (string list * typ option) -> context * (string list * typ option)
    79   val cert_vars: context * (string list * typ option) -> context * (string list * typ option)
    80   val fix: (string list * string option) list -> context -> context
    80   val fix: (string list * string option) list -> context -> context
    81   val fix_i: (string list * typ option) list -> context -> context
    81   val fix_i: (string list * typ option) list -> context -> context
       
    82   val bind_skolem: context -> string list -> term -> term
    82   val get_case: context -> string -> RuleCases.T
    83   val get_case: context -> string -> RuleCases.T
    83   val add_cases: (string * RuleCases.T) list -> context -> context
    84   val add_cases: (string * RuleCases.T) list -> context -> context
    84   val setup: (theory -> theory) list
    85   val setup: (theory -> theory) list
    85 end;
    86 end;
    86 
    87 
   385 (* internalize Skolem constants *)
   386 (* internalize Skolem constants *)
   386 
   387 
   387 fun fixes_of (Context {asms = (_, (fixes, _)), ...}) = fixes;
   388 fun fixes_of (Context {asms = (_, (fixes, _)), ...}) = fixes;
   388 fun get_skolem ctxt x = assoc (fixes_of ctxt, x);
   389 fun get_skolem ctxt x = assoc (fixes_of ctxt, x);
   389 
   390 
   390 fun check_skolem ctxt check x =
   391 fun no_internal_skolem ctxt x =
   391   if check andalso can Syntax.dest_skolem x then
   392   if can Syntax.dest_skolem x then
   392     raise CONTEXT ("Illegal reference to internal Skolem constant: " ^ quote x, ctxt)
   393     raise CONTEXT ("Illegal reference to internal Skolem constant: " ^ quote x, ctxt)
       
   394   else if can Syntax.dest_internal x then
       
   395     raise CONTEXT ("Illegal reference to internal variable: " ^ quote x, ctxt)
   393   else x;
   396   else x;
   394 
   397 
   395 fun intern_skolem ctxt check =
   398 fun intern_skolem ctxt =
   396   let
   399   let
   397     fun intern (t as Free (x, T)) =
   400     fun intern (t as Free (x, T)) =
   398           (case get_skolem ctxt (check_skolem ctxt check x) of
   401           (case get_skolem ctxt (no_internal_skolem ctxt x) of
   399             Some x' => Free (x', T)
   402             Some x' => Free (x', T)
   400           | None => t)
   403           | None => t)
   401       | intern (t $ u) = intern t $ intern u
   404       | intern (t $ u) = intern t $ intern u
   402       | intern (Abs (x, T, t)) = Abs (x, T, intern t)
   405       | intern (Abs (x, T, t)) = Abs (x, T, intern t)
   403       | intern a = a;
   406       | intern a = a;
   404   in intern end;
   407   in intern end;
   405 
       
   406 fun cert_skolem ctxt x =
       
   407   (case get_skolem ctxt x of
       
   408     None => raise CONTEXT ("Undeclared variable: " ^ quote x, ctxt)
       
   409   | Some x' => x');
       
   410 
   408 
   411 
   409 
   412 (* externalize Skolem constants -- for printing purposes only *)
   410 (* externalize Skolem constants -- for printing purposes only *)
   413 
   411 
   414 fun extern_skolem ctxt =
   412 fun extern_skolem ctxt =
   516 
   514 
   517 fun gen_read read app is_pat (ctxt as Context {defs = (_, _, (used, _)), ...}) s =
   515 fun gen_read read app is_pat (ctxt as Context {defs = (_, _, (used, _)), ...}) s =
   518   (transform_error (read (sign_of ctxt) (def_type ctxt is_pat, def_sort ctxt, used)) s
   516   (transform_error (read (sign_of ctxt) (def_type ctxt is_pat, def_sort ctxt, used)) s
   519     handle TERM (msg, _) => raise CONTEXT (msg, ctxt)
   517     handle TERM (msg, _) => raise CONTEXT (msg, ctxt)
   520     | ERROR_MESSAGE msg => raise CONTEXT (msg, ctxt))
   518     | ERROR_MESSAGE msg => raise CONTEXT (msg, ctxt))
   521   |> app (intern_skolem ctxt true)
   519   |> app (intern_skolem ctxt)
   522   |> app (if is_pat then I else norm_term ctxt)
   520   |> app (if is_pat then I else norm_term ctxt)
   523   |> app (if is_pat then prepare_dummies else (reject_dummies ctxt));
   521   |> app (if is_pat then prepare_dummies else (reject_dummies ctxt));
   524 
   522 
   525 val read_termTs = gen_read (read_def_termTs false) (apfst o map) false;
   523 val read_termTs = gen_read (read_def_termTs false) (apfst o map) false;
   526 val read_termT_pats = #1 oo gen_read (read_def_termTs false) (apfst o map) true;
   524 val read_termT_pats = #1 oo gen_read (read_def_termTs false) (apfst o map) true;
   533 
   531 
   534 
   532 
   535 (* certify terms *)
   533 (* certify terms *)
   536 
   534 
   537 fun gen_cert cert is_pat ctxt t = t
   535 fun gen_cert cert is_pat ctxt t = t
   538   |> intern_skolem ctxt false
       
   539   |> (if is_pat then I else norm_term ctxt)
   536   |> (if is_pat then I else norm_term ctxt)
   540   |> (fn t' => cert (sign_of ctxt) t' handle TERM (msg, _) => raise CONTEXT (msg, ctxt));
   537   |> (fn t' => cert (sign_of ctxt) t' handle TERM (msg, _) => raise CONTEXT (msg, ctxt));
   541 
   538 
   542 val cert_term = gen_cert cert_term_sg false;
   539 val cert_term = gen_cert cert_term_sg false;
   543 val cert_prop = gen_cert cert_prop_sg false;
   540 val cert_prop = gen_cert cert_prop_sg false;
   584 fun declare_terms ts ctxt = foldl declare (ctxt, ts);
   581 fun declare_terms ts ctxt = foldl declare (ctxt, ts);
   585 
   582 
   586 
   583 
   587 
   584 
   588 (** Hindley-Milner polymorphism **)
   585 (** Hindley-Milner polymorphism **)
   589 
       
   590 
   586 
   591 (* warn_extra_tfrees *)
   587 (* warn_extra_tfrees *)
   592 
   588 
   593 local
   589 local
   594 
   590 
   917 end;
   913 end;
   918 
   914 
   919 
   915 
   920 (* variables *)
   916 (* variables *)
   921 
   917 
   922 fun prep_vars prep_typ check (ctxt, (xs, raw_T)) =
   918 fun prep_vars prep_typ (ctxt, (xs, raw_T)) =
   923   let
   919   let
   924     val _ = (case filter (not o Syntax.is_identifier) (map (check_skolem ctxt check) xs) of
   920     val _ = (case filter (not o Syntax.is_identifier) (map (no_internal_skolem ctxt) xs) of
   925       [] => () | bads => raise CONTEXT ("Bad variable name(s): " ^ commas_quote bads, ctxt));
   921       [] => () | bads => raise CONTEXT ("Bad variable name(s): " ^ commas_quote bads, ctxt));
   926 
   922 
   927     val opt_T = apsome (prep_typ ctxt) raw_T;
   923     val opt_T = apsome (prep_typ ctxt) raw_T;
   928     val T = if_none opt_T TypeInfer.logicT;
   924     val T = if_none opt_T TypeInfer.logicT;
   929     val ctxt' = ctxt |> declare_terms (map (fn x => Free (x, T)) xs);
   925     val ctxt' = ctxt |> declare_terms (map (fn x => Free (x, T)) xs);
   930   in (ctxt', (xs, opt_T)) end;
   926   in (ctxt', (xs, opt_T)) end;
   931 
   927 
   932 val read_vars = prep_vars read_typ true;
   928 val read_vars = prep_vars read_typ;
   933 val cert_vars = prep_vars cert_typ false;
   929 val cert_vars = prep_vars cert_typ;
   934 
   930 
   935 
   931 
   936 (* fix *)
   932 (* fix *)
   937 
   933 
   938 local
   934 local
   964 val fix_i = gen_fix cert_vars;
   960 val fix_i = gen_fix cert_vars;
   965 
   961 
   966 end;
   962 end;
   967 
   963 
   968 
   964 
       
   965 (*Note: improper use may result in variable capture / dynamic scoping!*)
       
   966 fun bind_skolem ctxt xs =
       
   967   let
       
   968     val ctxt' = ctxt |> fix_i [(xs, None)];
       
   969     fun bind (t as Free (x, T)) =
       
   970           if x mem_string xs then
       
   971             (case get_skolem ctxt' x of Some x' => Free (x', T) | None => t)
       
   972           else t
       
   973       | bind (t $ u) = bind t $ bind u
       
   974       | bind (Abs (x, T, t)) = Abs (x, T, bind t)
       
   975       | bind a = a;
       
   976   in bind end;
       
   977 
       
   978 
   969 
   979 
   970 (** cases **)
   980 (** cases **)
   971 
   981 
   972 fun check_case ctxt name (xs, ts) =
   982 fun check_case ctxt name (xs, ts) =
   973   if null (foldr Term.add_typ_tvars (map snd xs, [])) andalso
   983   if null (foldr Term.add_typ_tvars (map snd xs, [])) andalso