neuper@38025: (* Title: extends Isabelle/src/Pure/term.ML e0726734@41962: Author: Walther Neuper 1999, Mathias Lehnfeld neuper@38025: (c) due to copyright terms neuper@38025: *) wneuper@59507: infix contains_one_of wneuper@59507: wneuper@59577: (* TERM_C extends Isabelle's naming conventions: "C" indicates Isac add-ons to an Isabelle module *) walther@59861: signature TERM_ISAC = walther@59861: sig walther@59868: type as_string walther@59861: val empty: term walther@59878: val typ_empty: typ walther@59865: walther@59861: datatype lrd = D | L | R walther@59861: type path walther@59861: val string_of_path: path -> string walther@60023: val sub_at: path -> term -> term walther@59861: val go_up: path -> term -> term walther@59767: Walther@60564: val perm: term -> term -> bool (*old original Isabelle code*) walther@59861: val contains_Var: term -> bool walther@59861: val dest_binop_typ: typ -> typ * typ * typ walther@59861: val dest_equals: term -> term * term walther@59861: val free2str: term -> string walther@59861: val ids2str: term -> string list walther@59861: val ins_concl: term -> term -> term walther@59861: val inst_abs: term -> term Walther@60477: val inst_bdv: LibraryC.subst -> term -> term wneuper@59390: walther@60391: val lhs: term -> term walther@60391: val rhs: term -> term walther@60391: walther@60317: val mk_frac: typ -> int * (int * int) -> term walther@60324: val numerals_to_Free: term -> term walther@59861: val term_of_num: typ -> int -> term walther@59861: val num_of_term: term -> int walther@60322: val to_string: term -> string walther@59861: val int_of_str: string -> int walther@59861: val isastr_of_int: int -> string walther@60317: val int_opt_of_string: string -> int option wneuper@59390: walther@59861: val isalist2list: term -> term list walther@59861: val list2isalist: typ -> term list -> term walther@59861: val isapair2pair: term -> term * term (* rename to dest_pair, compare HOLogic.dest_string *) wneuper@59390: walther@59861: val is_atom: term -> bool walther@60322: val string_of_atom: term -> string walther@59861: val is_variable: term -> bool walther@59861: val is_bdv: string -> bool walther@59861: val is_bdv_subst: term -> bool walther@59861: val guess_bdv_typ: term -> typ walther@59861: val is_equality: term -> bool walther@59861: val is_expliceq: term -> bool walther@59861: val is_f_x: term -> bool walther@59861: val is_list: term -> bool walther@59861: val is_bool_list: term -> bool walther@59861: val dest_listT: typ -> typ walther@59861: val is_num: term -> bool walther@59861: val is_num': string -> bool walther@60317: val string_of_num: term -> string walther@59861: val variable_constant_pair: term * term -> bool wneuper@59390: walther@59861: val mk_add: term -> term -> term walther@59861: val mk_free: typ -> string -> term walther@59861: val mk_equality: term * term -> term walther@59861: val mk_factroot: string -> typ -> int -> int -> term walther@59861: val mk_Free: string * typ -> term walther@59861: val mk_thmid: string -> string -> string -> string walther@59861: val mk_num_op_num: typ -> typ -> string * typ -> int -> int -> term walther@59861: val mk_num_op_var: term -> string -> typ -> typ -> int -> term walther@59861: val mk_var_op_num: term -> string -> typ -> typ -> int -> term wneuper@59390: walther@59861: val matches: theory -> term -> term -> bool walther@60414: Walther@60564: val parseNEW: Proof.context -> string -> term option(*old version to be eliminated*) Walther@60564: val parseNEW': Proof.context -> string -> term (*old version to be eliminated*) Walther@60564: val parseNEW'': theory -> string -> term (*old version to be eliminated*) Walther@60564: (*goal*)val parse: Proof.context -> string -> term Walther@60584: (*goal*)val parse_patt: theory -> string -> term Walther@60564: (*for test/* *) Walther@60564: (*goal*)val parse_test: Proof.context -> string -> term Walther@60564: (*goal*)val parse_patt_test: theory -> string -> term wneuper@59390: walther@59861: val str_of_free_opt: term -> string option walther@59861: val str_of_int: int -> string walther@59861: val strip_imp_prems': term -> term option Walther@60477: val subst_atomic_all: LibraryC.subst -> term -> bool * term walther@59861: walther@59861: val pairt: term -> term -> term walther@59861: val pairT: typ -> typ -> typ walther@59861: val raise_type_conflicts: term list -> unit walther@59861: val strip_trueprop: term -> term walther@59861: walther@59861: val var2free: term -> term walther@60317: val vars: term -> term list (* recognises numerals, should replace "fun vars_of" TODOO*) walther@59998: val vars': term list -> term list walther@60317: val vars_of: term -> term list (* deprecated TODOO: see differences in test/../termC.sml*) walther@59861: val dest_list': term -> term list walther@59861: val negates: term -> term -> bool wneuper@59532: Walther@60649: val contains_one_of: thm * (string * typ) list -> bool Walther@60649: val contains_Const_typeless: term list -> term -> bool Walther@60649: val sym_trm : term -> term (* unused code, kept as hints to design ideas *) Walther@60649: Walther@60650: val string_of_detail: Proof.context -> term -> string Walther@60650: Walther@60650: \<^isac_test>\ walther@60317: val mk_negative: typ -> term -> term Walther@60650: val mk_Var: term -> term walther@59861: val scala_of_term: term -> string Walther@60649: Walther@60650: val atom_typ: Proof.context -> typ -> unit Walther@60650: val atom_write: Proof.context -> term -> unit Walther@60650: val atom_trace: Proof.context -> term -> unit Walther@60649: Walther@60650: val atom_write_detail: Proof.context -> term -> unit Walther@60650: val atom_trace_detail: Proof.context -> term -> unit wenzelm@60223: \ walther@59861: end wneuper@59389: wneuper@59389: (**) walther@59861: structure TermC(**): TERM_ISAC(**) = wneuper@59389: struct wneuper@59389: (**) neuper@38025: walther@59865: type as_string = UnparseC.term_as_string walther@59868: val empty = UnparseC.term_empty walther@59878: val typ_empty = UnparseC.typ_empty walther@59861: walther@59767: datatype lrd = L (*t1 in "t1$t2"*) walther@59767: | R (*t2 in "t1$t2"*) | D; (*b in Abs(_,_,b*) walther@59767: type path = lrd list; walther@59767: fun ldr2str L = "L" walther@59767: | ldr2str R = "R" walther@59767: | ldr2str D = "D"; walther@59774: fun string_of_path k = (strs2str' o (map ldr2str)) k; walther@59768: (*go to a location in a term and fetch the resective sub-term*) walther@60023: fun sub_at [] t = t walther@60023: | sub_at (D :: p) (Abs(_, _, body)) = sub_at p body walther@60023: | sub_at (L :: p) (t1 $ _) = sub_at p t1 walther@60023: | sub_at (R :: p) (_ $ t2) = sub_at p t2 walther@60023: | sub_at l t = raise TERM ("sub_at: no " ^ string_of_path l ^ " for ", [t]); walther@59768: fun go_up l t = walther@60023: if length l > 1 then sub_at (drop_last l) t else raise ERROR ("go_up [] " ^ UnparseC.term t) walther@59767: neuper@52087: fun isastr_of_int i = if i >= 0 then string_of_int i else "-" ^ string_of_int (abs i) neuper@52087: neuper@38025: fun matches thy tm pa = neuper@38025: (Pattern.match thy (pa, tm) (Vartab.empty, Vartab.empty); true) wneuper@59392: handle Pattern.MATCH => false neuper@38025: wneuper@59392: (** transform typ / term to a String to be parsed by Scala after transport via libisabelle **) Walther@60650: fun string_of_detail ctxt t = Walther@60650: let Walther@60650: fun ato (Const (a, T)) n = "\n*** " ^ indent n ^ "Const (" ^ a ^ ", " ^ UnparseC.typ_in_ctxt ctxt T ^ ")" Walther@60650: | ato (Free (a, T)) n = "\n*** " ^ indent n ^ "Free (" ^ a ^ ", " ^ UnparseC.typ_in_ctxt ctxt T ^ ")" Walther@60650: | ato (Var ((a, i), T)) n = Walther@60650: "\n*** " ^ indent n ^ "Var ((" ^ a ^ ", " ^ string_of_int i ^ "), " ^ UnparseC.typ_in_ctxt ctxt T ^ ")" Walther@60650: | ato (Bound i) n = "\n*** " ^ indent n ^ "Bound " ^ string_of_int i Walther@60650: | ato (Abs(a, T, body)) n = Walther@60650: "\n*** " ^ indent n ^ "Abs (" ^ a ^ ", " ^ UnparseC.typ_in_ctxt ctxt T ^ ",.." ^ ato body (n + 1) Walther@60650: | ato (f $ t) n = ato f n ^ ato t (n + 1) Walther@60650: in "\n*** " ^ ato t 0 ^ "\n***" end; Walther@60650: Walther@60650: Walther@60650: \<^isac_test>\ wneuper@59318: fun scala_of_typ (Type (s, typs)) = wneuper@59318: enclose "Type(" ")" (quote s ^ ", " ^ wneuper@59318: (typs |> map scala_of_typ |> commas |> enclose "List(" ")")) wneuper@59318: | scala_of_typ (TFree (s, sort)) = wneuper@59318: enclose "TFree(" ")" (quote s ^ ", " ^ (sort |> map quote |> commas |> enclose "List(" ")")) wneuper@59318: | scala_of_typ (TVar ((s, i), sort)) = wneuper@59318: enclose "TVar(" ")" ( walther@59997: enclose "(" ")," (quote s ^ ", " ^ quote (string_of_int i)) ^ wneuper@59318: (sort |> map quote |> commas |> enclose "List(" ")")) wenzelm@60223: wneuper@59318: fun scala_of_term (Const (s, T)) = walther@60335: enclose "Const (" ")" (quote s ^ ", " ^ scala_of_typ T) wneuper@59318: | scala_of_term (Free (s, T)) = wneuper@59318: enclose "Free(" ")" (quote s ^ ", " ^ scala_of_typ T) wneuper@59318: | scala_of_term (Var ((s, i), T)) = wneuper@59318: enclose "TVar(" ")" ( walther@59997: enclose "(" ")," (quote s ^ ", " ^ quote (string_of_int i)) ^ wneuper@59318: scala_of_typ T) wneuper@59318: | scala_of_term (Bound i) = enclose "Bound(" ")" (string_of_int i) wneuper@59318: | scala_of_term (Abs (s, T, t)) = wneuper@59318: enclose "Abs(" ")" ( wneuper@59318: quote s ^ ", " ^ wneuper@59318: scala_of_typ T ^ ", " ^ wneuper@59318: scala_of_term t) wneuper@59318: | scala_of_term (t1 $ t2) = wneuper@59318: enclose "App(" ")" (scala_of_term t1 ^ ", " ^ scala_of_term t2) wneuper@59318: wneuper@59392: (* see structure's bare bones. wneuper@59392: for Isabelle standard output compare 2017 "structure ML_PP" *) Walther@60650: fun atom_typ ctxt t = neuper@38025: let wneuper@59392: fun ato n (Type (s, [])) = "\n*** " ^ indent n ^ "Type (" ^ s ^",[])" wneuper@59392: | ato n (Type (s, Ts)) = "\n*** " ^ indent n ^ "Type (" ^ s ^ ",[" ^ atol (n + 1) Ts walther@59997: | ato n (TFree (s, sort)) = "\n*** " ^ indent n ^ "TFree (" ^ s ^ ", " ^ strs2str' sort wneuper@59392: | ato n (TVar ((s, i), sort)) = walther@59997: "\n*** " ^ indent n ^ "TVar ((" ^ s ^ ", " ^ string_of_int i ^ strs2str' sort wneuper@59392: and atol n [] = "\n*** " ^ indent n ^ "]" wneuper@59392: | atol n (T :: Ts) = (ato n T ^ atol n Ts) neuper@38048: in tracing (ato 0 t ^ "\n") end; neuper@38025: neuper@38067: local wneuper@59392: fun ato (Const (a, _)) n = "\n*** " ^ indent n ^ "Const (" ^ a ^ ", _)" wneuper@59392: | ato (Free (a, _)) n = "\n*** " ^ indent n ^ "Free (" ^ a ^ ", _)" neuper@38025: | ato (Var ((a, i), _)) n = wneuper@59392: "\n*** " ^ indent n ^ "Var (" ^ a ^ ", " ^ string_of_int i ^ "), _)" wneuper@59392: | ato (Bound i) n = "\n*** " ^ indent n ^ "Bound " ^ string_of_int i wneuper@59392: | ato (Abs (a, _, body)) n = "\n*** " ^ indent n ^ "Abs(" ^ a ^ ", _" ^ ato body (n+1) neuper@38025: | ato (f $ t) n = (ato f n ^ ato t (n + 1)) neuper@38067: in Walther@60650: fun atom_write _ t = writeln ("\n*** -------------" ^ ato t 0 ^ "\n***"); Walther@60650: fun atom_trace _ t = tracing ("\n*** -------------" ^ ato t 0 ^ "\n***"); neuper@38067: end; Walther@60649: Walther@60650: fun atom_write_detail ctxt t = (writeln o (string_of_detail ctxt)) t; Walther@60650: fun atom_trace_detail ctxt t = (tracing o (string_of_detail ctxt)) t; wenzelm@60223: \ neuper@38025: wneuper@59392: (* contains the term a VAR(("*",_),_) ? *) neuper@38025: fun contains_Var (Abs(_,_,body)) = contains_Var body neuper@38025: | contains_Var (f $ f') = contains_Var f orelse contains_Var f' neuper@38025: | contains_Var (Var _) = true neuper@38025: | contains_Var _ = false; neuper@38025: wneuper@59392: fun str_of_int n = wneuper@59392: if n < 0 then "-" ^ ((string_of_int o abs) n) wneuper@59392: else string_of_int n; wneuper@59457: val int_of_str = Value.parse_int; walther@60278: walther@60317: val int_opt_of_string = ThmC_Def.int_opt_of_string walther@60317: fun is_num' str = case int_opt_of_string str of SOME _ => true | NONE => false; walther@59871: walther@60335: fun is_num (Const (\<^const_name>\numeral\, _) $ _) = true walther@60335: | is_num (Const (\<^const_name>\uminus\, _) $ walther@60335: (Const (\<^const_name>\numeral\, _) $ _)) = true walther@60335: | is_num (Const (\<^const_name>\one_class.one\, _)) = true walther@60335: | is_num (Const (\<^const_name>\uminus\, _) $ Const (\<^const_name>\one_class.one\, _)) = true walther@60335: | is_num (Const (\<^const_name>\zero_class.zero\, _)) = true walther@60335: | is_num (Const (\<^const_name>\uminus\, _) $ Const (\<^const_name>\zero_class.zero\, _)) = true walther@60317: | is_num _ = false; walther@59871: walther@60317: fun string_of_num n = (n |> HOLogic.dest_number |> snd |> string_of_int) walther@60317: walther@60391: (* refer to Thm.lhs_of *) walther@60391: fun lhs (Const (\<^const_name>\HOL.eq\,_) $ l $ _) = l walther@60391: | lhs t = raise ERROR("lhs called with (" ^ UnparseC.term t ^ ")"); walther@60391: fun rhs (Const (\<^const_name>\HOL.eq\,_) $ _ $ r) = r walther@60391: | rhs t = raise ERROR("rhs called with (" ^ UnparseC.term t ^ ")"); walther@60391: walther@60391: walther@60335: fun mk_negative T t = Const (\<^const_name>\uminus\, T --> T) $ t walther@60317: fun mk_frac T (sg, (i1, i2)) = walther@60317: if sg = 1 then walther@60317: if i2 = 1 then HOLogic.mk_number T i1 walther@60335: else Const (\<^const_name>\divide_class.divide\, T --> T --> T) $ walther@60317: HOLogic.mk_number T i1 $ HOLogic.mk_number T i2 walther@60317: else (*take negative*) walther@60317: if i2 = 1 then mk_negative T (HOLogic.mk_number T i1) walther@60335: else Const (\<^const_name>\divide_class.divide\, T --> T --> T) $ walther@60317: mk_negative T (HOLogic.mk_number T i1) $ HOLogic.mk_number T i2 walther@60317: walther@60324: val numerals_to_Free = (* Makarius 100308 *) walther@60324: let walther@60324: fun dest_num t = walther@60324: (case try HOLogic.dest_number t of walther@60324: SOME (T, i) => SOME (Free (signed_string_of_int i, T)) walther@60324: | NONE => NONE); walther@60324: fun to_str (Abs (x, T, b)) = Abs (x, T, to_str b) walther@60324: | to_str (t as (u1 $ u2)) = walther@60324: (case dest_num t of SOME t' => t' | NONE => to_str u1 $ to_str u2) walther@60324: | to_str t = perhaps dest_num t; walther@60324: in to_str end walther@60324: walther@60317: val term_of_num = HOLogic.mk_number; walther@60317: fun num_of_term t = t |> HOLogic.dest_number |> snd; walther@60324: (* accomodate string-representation for int to term-orders *) wenzelm@60383: fun to_string t = t |> num_of_term |> signed_string_of_int neuper@52064: walther@59841: fun is_variable (t as Free _) = not (is_num t) walther@59841: | is_variable _ = false; wneuper@59392: fun is_Free (Free _) = true | is_Free _ = false; neuper@38025: fun is_fun_id (Const _) = true neuper@38025: | is_fun_id (Free _) = true neuper@38025: | is_fun_id _ = false; neuper@38025: fun is_f_x (f $ x) = is_fun_id f andalso is_Free x neuper@38025: | is_f_x _ = false; walther@59841: (* precondition: TermC.is_atom v andalso TermC.is_atom c *) walther@60400: fun is_const (Const _) = true | is_const _ = false; walther@59841: fun variable_constant_pair (v, c) = walther@59841: if (is_variable v andalso (is_const c orelse is_num c)) orelse walther@59841: (is_variable c andalso (is_const v orelse is_num v)) walther@59841: then true walther@59841: else false wneuper@59392: wneuper@59392: fun vars t = neuper@38025: let wneuper@59392: fun scan vs (Const _) = vs walther@60317: | scan vs (t as Free _) = (*if is_num' s then vs else*) t :: vs wneuper@59392: | scan vs (t as Var _) = t :: vs wneuper@59392: | scan vs (Bound _) = vs wneuper@59392: | scan vs (Abs (_, _, t)) = scan vs t neuper@38025: | scan vs (t1 $ t2) = (scan vs t1) @ (scan vs t2) walther@60017: in ((distinct op =) o (scan [])) t end; walther@60017: fun vars' ts = ts |> map vars |> flat |> distinct op = walther@59998: wneuper@59392: (* bypass Isabelle's Pretty, which requires ctxt *) neuper@38025: fun ids2str t = neuper@38025: let walther@60333: fun scan vs (t as Const (s, _) $ arg) = walther@60333: if is_num t then vs else scan (s :: vs) arg walther@60333: | scan vs (Const (s as "Partial_Fractions.AA", _)) = s :: vs (*how get rid of spec.case?*) walther@60356: | scan vs (Const (s as "Partial_Fractions.BB", _)) = s :: vs (*how get rid of spec.case?*) walther@60333: | scan vs (Const _) = vs wneuper@59392: | scan vs (Free (s, _)) = if is_num' s then vs else s :: vs wneuper@59392: | scan vs (Var ((s, i), _)) = (s ^ "_" ^ string_of_int i) :: vs wneuper@59392: | scan vs (Bound _) = vs wneuper@59392: | scan vs (Abs (s, _, t)) = scan (s :: vs) t neuper@38025: | scan vs (t1 $ t2) = (scan vs t1) @ (scan vs t2) walther@60333: in ((distinct op =) o (scan [])) t walther@60333: end; wneuper@59392: fun is_bdv str = case Symbol.explode str of "b"::"d"::"v"::_ => true | _ => false; Walther@60477: (* instantiate #prop thm with bound vars_of (as Free) *) wneuper@59394: fun inst_bdv [] t = t wneuper@59394: | inst_bdv (instl: (term*term) list) t = wneuper@59394: let wneuper@59394: fun subst (v as Var((s, _), T)) = wneuper@59394: (case Symbol.explode s of wneuper@59394: "b"::"d"::"v"::_ => if_none (assoc(instl,Free(s,T))) (Free(s,T)) wneuper@59394: | _ => v) wneuper@59394: | subst (Abs(a, T, body)) = Abs(a, T, subst body) wneuper@59394: | subst (f $ t') = subst f $ subst t' wneuper@59394: | subst t = if_none (assoc (instl, t)) t wneuper@59394: in subst t end; neuper@38025: wneuper@59494: (* is a term a substitution for a bdv as found in programs and tactics *) wenzelm@60309: fun is_bdv_subst (Const (\<^const_name>\Cons\, _) $ wenzelm@60309: (Const (\<^const_name>\Pair\, _) $ str $ _) $ _) = is_bdv (HOLogic.dest_string str) neuper@42426: | is_bdv_subst _ = false; neuper@42426: walther@59636: (* this shall be improved due to future requirements *) walther@59636: fun guess_bdv_typ t = t |> vars |> hd |> type_of walther@59636: wneuper@59392: fun free2str (Free (s, _)) = s walther@59962: | free2str t = raise ERROR ("free2str not for " ^ UnparseC.term t); wneuper@59392: fun str_of_free_opt (Free (s, _)) = SOME s neuper@52103: | str_of_free_opt _ = NONE neuper@38025: wneuper@59392: (* compare Logic.unvarify_global, which rejects Free *) wneuper@59392: fun var2free (t as Const _) = t wneuper@59392: | var2free (t as Free _) = t wneuper@59392: | var2free (Var((s, _), T)) = Free (s,T) wneuper@59392: | var2free (t as Bound _) = t wneuper@59392: | var2free (Abs(s, T, t)) = Abs(s, T, var2free t) neuper@38025: | var2free (t1 $ t2) = (var2free t1) $ (var2free t2); neuper@38025: Walther@60650: Walther@60650: \<^isac_test>\ (*TODO: check with new numerals --vv*) wneuper@59392: (* Logic.varify does NOT take care of 'Free ("1", _)'*) Walther@60650: fun mk_Var (t as Const _) = t Walther@60650: | mk_Var (t as Free (s, T)) = if is_num' s then t else Var ((s, 0), T) Walther@60650: | mk_Var (t as Var _) = t Walther@60650: | mk_Var (t as Bound _) = t Walther@60650: | mk_Var (Abs (s, T, t)) = Abs (s, T, mk_Var t) Walther@60650: | mk_Var (t1 $ t2) = (mk_Var t1) $ (mk_Var t2); wenzelm@60223: \ neuper@38025: wenzelm@60309: fun mk_listT T = Type (\<^type_name>\list\, [T]); wenzelm@60309: fun list_const T = Const (\<^const_name>\Cons\, [T, mk_listT T] ---> mk_listT T); wenzelm@60309: fun list2isalist T [] = Const (\<^const_name>\Nil\, mk_listT T) wneuper@59392: | list2isalist T (t :: ts) = (list_const T) $ t $ (list2isalist T ts); neuper@38025: wenzelm@60309: fun isapair2pair (Const (\<^const_name>\Pair\,_) $ a $ b) = (a, b) neuper@38025: | isapair2pair t = walther@59962: raise ERROR ("isapair2pair called with " ^ UnparseC.term t); neuper@38025: fun isalist2list ls = neuper@38025: let wenzelm@60309: fun get es (Const(\<^const_name>\Cons\, _) $ t $ ls) = get (t :: es) ls wenzelm@60309: | get es (Const(\<^const_name>\Nil\, _)) = es wneuper@59403: | get _ t = raise TERM ("isalist2list applied to NON-list: ", [t]) neuper@38025: in (rev o (get [])) ls end; neuper@38025: wenzelm@60309: fun is_list ((Const (\<^const_name>\Cons\, _)) $ _ $ _) = true wneuper@59392: | is_list _ = false; wenzelm@60309: fun dest_listT (Type (\<^type_name>\list\, [T])) = T walther@59842: | dest_listT T = raise TYPE ("dest_listT: list type expected", [T], []); walther@59842: fun is_bool_list t = walther@59842: (if dest_listT (Term.type_of t) = HOLogic.boolT then true else false) walther@59842: handle TYPE _ => false walther@59842: walther@59842: wenzelm@60309: fun dest_binop_typ (Type (\<^type_name>\fun\, [range, Type (\<^type_name>\fun\, [arg2, arg1])])) = (arg1, arg2, range) wneuper@59392: | dest_binop_typ _ = raise ERROR "dest_binop_typ: not binary"; wenzelm@60309: fun dest_equals (Const(\<^const_name>\HOL.eq\, _) $ t $ u) = (t, u) (* Pure/logic.ML: Const ("==", ..*) wneuper@59390: | dest_equals t = raise TERM ("dest_equals'", [t]); walther@60335: fun is_equality (Const(\<^const_name>\HOL.eq\,_) $ _ $ _) = true (* logic.ML: Const ("=="*) neuper@38025: | is_equality _ = false; wenzelm@60309: fun mk_equality (t, u) = (Const(\<^const_name>\HOL.eq\, [type_of t, type_of u] ---> HOLogic.boolT) $ t $ u); wenzelm@60309: fun is_expliceq (Const(\<^const_name>\HOL.eq\,_) $ (Free _) $ _) = true neuper@38025: | is_expliceq _ = false; wenzelm@60309: fun strip_trueprop (Const (\<^const_name>\Trueprop\, _) $ t) = t neuper@38025: | strip_trueprop t = t; neuper@38025: wneuper@59392: (* (A1==>...An==>B) goes to (A1==>...An==>) Pure/logic.ML: term -> term list*) wenzelm@60309: fun strip_imp_prems' (Const (\<^const_name>\Pure.imp\, _) $ A $ t) = wneuper@59392: let wenzelm@60309: fun coll_prems As (Const(\<^const_name>\Pure.imp\, _) $ A $ t) = wneuper@59392: coll_prems (As $ (Logic.implies $ A)) t wneuper@59392: | coll_prems As _ = SOME As neuper@38025: in coll_prems (Logic.implies $ A) t end wneuper@59392: | strip_imp_prems' _ = NONE; (* *) neuper@38025: wneuper@59392: (* (A1==>...An==>) (B) goes to (A1==>...An==>B), where B is lowest branch, 2002 Pure/thm.ML *) wenzelm@60309: fun ins_concl (Const (\<^const_name>\Pure.imp\, _) $ A $ t) B = Logic.implies $ A $ (ins_concl t B) wenzelm@60309: | ins_concl (Const (\<^const_name>\Pure.imp\, _) $ A ) B = Logic.implies $ A $ B wneuper@59390: | ins_concl t B = raise TERM ("ins_concl", [t, B]); neuper@38025: wneuper@59392: fun vperm (Var _, Var _) = true (* 2002 Pure/thm.ML *) neuper@38025: | vperm (Abs (_, _, s), Abs (_, _, t)) = vperm (s, t) neuper@38025: | vperm (t1 $ t2, u1 $ u2) = vperm (t1, u1) andalso vperm (t2, u2) neuper@38025: | vperm (t, u) = (t = u); neuper@38025: neuper@38025: (*2002 cp from Pure/term.ML --- since 2009 in Pure/old_term.ML*) neuper@38025: fun mem_term (_, []) = false wneuper@59392: | mem_term (t, t' :: ts) = t aconv t' orelse mem_term (t, ts); wneuper@59390: fun subset_term ([], _) = true wneuper@59390: | subset_term (x :: xs, ys) = mem_term (x, ys) andalso subset_term (xs, ys); neuper@38025: fun eq_set_term (xs, ys) = neuper@38025: xs = ys orelse (subset_term (xs, ys) andalso subset_term (ys, xs)); neuper@38025: (*a total, irreflexive ordering on index names*) wneuper@59392: fun xless ((a, i), (b, j): indexname) = i insert_aterm (t, vars) wneuper@59392: | Abs (_, _, body) => add_term_vars (body, vars) wneuper@59392: | f$t => add_term_vars (f, add_term_vars (t, vars)) neuper@38025: | _ => vars; wneuper@59390: fun term_vars t = add_term_vars (t, []); neuper@38025: wneuper@59392: (*2002 Pure/thm.ML *) wneuper@59392: fun var_perm (t, u) = vperm (t, u) andalso eq_set_term (term_vars t, term_vars u); neuper@38025: (*2002 fun decomp_simp, Pure/thm.ML *) wneuper@59390: fun perm lhs rhs = var_perm (lhs, rhs) andalso not (lhs aconv rhs) andalso not (is_Var lhs); neuper@38025: neuper@38025: wenzelm@60310: fun pairT T1 T2 = Type (\<^type_name>\prod\, [T1, T2]); wenzelm@60310: fun PairT T1 T2 = ([T1, T2] ---> Type (\<^type_name>\prod\, [T1, T2])); wenzelm@60309: fun pairt t1 t2 = Const (\<^const_name>\Pair\, PairT (type_of t1) (type_of t2)) $ t1 $ t2; neuper@38025: neuper@38025: fun mk_factroot op_(*=thy.sqrt*) T fact root = wenzelm@60309: Const (\<^const_name>\times\, [T, T] ---> T) $ (term_of_num T fact) $ wneuper@59392: (Const (op_, T --> T) $ term_of_num T root); walther@60317: fun mk_var_op_num v op_ optype ntyp n = Const (op_, optype) $ v $ HOLogic.mk_number ntyp n; walther@60317: fun mk_num_op_var v op_ optype ntyp n = Const (op_, optype) $ HOLogic.mk_number ntyp n $ v; wneuper@59390: fun mk_num_op_num T1 T2 (op_, Top) n1 n2 = walther@60317: Const (op_, Top) $ HOLogic.mk_number T1 n1 $ HOLogic.mk_number T2 n2; wneuper@59392: fun mk_thmid thmid n1 n2 = wneuper@59392: thmid ^ (strip_thy n1) ^ "_" ^ (strip_thy n2); wneuper@59392: fun mk_add t1 t2 = wneuper@59392: let wneuper@59392: val (T1, T2) = (type_of t1, type_of t2) wneuper@59392: in wneuper@59392: if T1 <> T2 then raise TYPE ("mk_add gets ", [T1, T2], [t1,t2]) wenzelm@60309: else (Const (\<^const_name>\plus\, [T1, T2] ---> T1) $ t1 $ t2) wneuper@59392: end; neuper@38025: neuper@38025: (** transform binary numeralsstrings **) wneuper@59392: fun mk_Free (s,T) = Free (s, T); wneuper@59392: fun mk_free T s = Free (s, T); neuper@38025: neuper@38025: (*Special case: one argument cp from Isabelle2002/src/Pure/term.ML*) wneuper@59392: fun subst_bound (arg, t) = wneuper@59392: let wneuper@59392: fun subst (t as Bound i, lev) = wneuper@59392: if i < lev then t (*var is locally bound*) wneuper@59392: else if i = lev then incr_boundvars lev arg wneuper@59392: else Bound (i - 1) (*loose: change it*) wneuper@59392: | subst (Abs(a, T, body), lev) = Abs (a, T, subst (body, lev + 1)) wneuper@59392: | subst (f$t, lev) = subst(f, lev) $ subst(t, lev) wneuper@59392: | subst (t, _) = t wneuper@59392: in subst (t, 0) end; neuper@38025: walther@59691: (* instantiate let; necessary for scan_up1 *) wneuper@59394: fun inst_abs (Const sT) = Const sT wneuper@59393: | inst_abs (Free sT) = Free sT wneuper@59393: | inst_abs (Bound n) = Bound n wneuper@59393: | inst_abs (Var iT) = Var iT wenzelm@60309: | inst_abs (Const (\<^const_name>\Let\,T1) $ e $ (Abs (v, T2, b))) = wneuper@59394: let val b' = subst_bound (Free (v, T2), b); (*fun variant_abs: term.ML*) wenzelm@60309: in Const (\<^const_name>\Let\, T1) $ inst_abs e $ (Abs (v, T2, inst_abs b')) end wneuper@59393: | inst_abs (t1 $ t2) = inst_abs t1 $ inst_abs t2 wneuper@59393: | inst_abs t = t; neuper@38025: Walther@60564: Walther@60564: (*** parse ***) Walther@60564: Walther@60564: (*/----- old versions to be eliminated -------------------------------------------------------\*) walther@60414: fun parseNEW ctxt str = \<^try>\Syntax.read_term ctxt str\; walther@60414: fun parseNEW' ctxt str = walther@60414: case parseNEW ctxt str of walther@60414: SOME t => t walther@60414: | NONE => raise TERM ("NO parseNEW' for " ^ str, []) walther@60414: fun parseNEW'' thy str = walther@60414: case parseNEW (Proof_Context.init_global thy) str of walther@60414: SOME t => t walther@60414: | NONE => raise TERM ("NO parseNEW'' for " ^ str, []) Walther@60564: (*\----- old versions to be eliminated -------------------------------------------------------/*) walther@60414: Walther@60564: (* to be replaced by parse..*) Walther@60564: fun parse ctxt str = Syntax.read_term_global (Proof_Context.theory_of ctxt) str; Walther@60564: (* parse term patterns; Var ("v",_), i.e. "?v", are required for instantiation *) walther@59875: fun parse_patt thy str = (thy, str) walther@60360: |>> Proof_Context.init_global walther@59875: |-> Proof_Context.read_term_pattern Walther@60564: Walther@60564: (** parse in test/* **) Walther@60564: (* Walther@60564: This bypasses building ctxt at the begin of a calculation Walther@60586: and thus borrows adapt_to_type (used for where_-parsed terms from Know_Store). Walther@60564: *) Walther@60564: fun parse_test ctxt str = str Walther@60564: |> Syntax.read_term_global (Proof_Context.theory_of ctxt) Walther@60564: |> Model_Pattern.adapt_term_to_type ctxt Walther@60564: fun parse_patt_test thy str = (thy, str) Walther@60556: |>> Proof_Context.init_global Walther@60556: |-> Proof_Context.read_term_pattern Walther@60565: |> Model_Pattern.adapt_term_to_type (Proof_Context.init_global thy) neuper@38025: Walther@60564: walther@60343: fun is_atom (Const _) = true neuper@38025: | is_atom (Free _) = true neuper@38025: | is_atom (Var _) = true walther@60343: | is_atom t = is_num t; walther@60335: fun string_of_atom (t as Const (\<^const_name>\numeral\, _) $ _) = to_string t walther@60335: | string_of_atom (t as Const (\<^const_name>\one_class.one\, _)) = to_string t walther@60335: | string_of_atom (t as Const (\<^const_name>\zero_class.zero\, _)) = to_string t walther@60333: | string_of_atom (Const (str, _)) = str walther@60333: | string_of_atom (Free (str, _)) = str walther@60333: | string_of_atom (Var ((str, int), _)) = str ^ "_" ^ string_of_int int walther@60333: | string_of_atom _ = "DUMMY-string_of_atom"; neuper@38025: wneuper@59394: (* from Pure/term.ML; reports if ALL Free's have found a substitution wneuper@59394: (required for evaluating the preconditions of _incomplete_ models) *) wneuper@59394: fun subst_atomic_all [] t = (false (*TODO may be 'true' for some terms ?*), t) wneuper@59394: | subst_atomic_all instl t = wneuper@59394: let wneuper@59394: fun subst (Abs (a, T, body)) = wneuper@59394: let wneuper@59394: val (all, body') = subst body wneuper@59394: in (all, Abs(a, T, body')) end wneuper@59394: | subst (f$tt) = wneuper@59394: let wneuper@59394: val (all1, f') = subst f wneuper@59394: val (all2, tt') = subst tt wneuper@59394: in (all1 andalso all2, f' $ tt') end wneuper@59394: | subst (t as Free _) = wneuper@59394: if is_num t then (true, t) (*numerals cannot be subst*) wneuper@59394: else (case assoc (instl, t) of wneuper@59394: SOME t' => (true, t') wneuper@59394: | NONE => (false, t)) wneuper@59394: | subst t = (true, if_none (assoc(instl,t)) t) wneuper@59394: in subst t end; wneuper@59389: wneuper@59507: fun op contains_one_of (thm, ids) = wneuper@59507: Term.exists_Const (fn id => member op= ids id) (Thm.prop_of thm) wneuper@59507: walther@60333: fun var_for vs (t as Const (str, _)) id = walther@60333: if is_num t then vs walther@60333: else if id = strip_thy str then t :: vs else vs wneuper@59532: | var_for vs (t as Free (str, _)) id = if id = str then t :: vs else vs wneuper@59532: | var_for vs (t as Var (idn, _)) id = if id = Term.string_of_vname idn then t :: vs else vs wneuper@59532: | var_for vs (Bound _) _ = vs wneuper@59532: | var_for vs (Abs (_, _, t)) id = var_for vs t id wneuper@59532: | var_for vs (t1 $ t2) id = (var_for vs t1 id) @ (var_for vs t2 id) wneuper@59532: wneuper@59532: val poly_consts = (* TODO: adopt syntax-const from Isabelle*) wenzelm@60309: [\<^const_name>\plus\, \<^const_name>\minus\, wenzelm@60309: \<^const_name>\divide\, \<^const_name>\times\, wenzelm@60405: \<^const_name>\realpow\]; Walther@60477: (* treat Free, Const, Var as vars_of in polynomials *) wneuper@59532: fun vars_of t = wneuper@59532: let wneuper@59532: val var_ids = t |> ids2str |> subtract op = poly_consts |> map strip_thy |> sort string_ord walther@60017: in (map (var_for [] t) var_ids) |> flat |> distinct op = end wneuper@59532: wneuper@59577: (* this may decompose an object-language isa-list; wneuper@59577: use only, if description is not available, eg. not input ?WN:14.5.03 ??!?*) wneuper@59577: fun dest_list' t = if is_list t then isalist2list t else [t]; wneuper@59577: wenzelm@60309: fun negat (Const (\<^const_name>\Not\, _) $ P, P') = P = P' walther@59842: | negat _ = false walther@59842: fun negates p1 p2 = negat (p1, p2) orelse negat (swap (p1, p2)); walther@59842: wneuper@59580: fun raise_type_conflicts ts = wneuper@59580: let wneuper@59580: val dups = duplicates (op =) (map (fst o dest_Free) ts) walther@60266: val confl = filter (fn Free (str, _) => member op = dups str walther@60266: | _ => false) ts wneuper@59580: in wneuper@59580: if confl = [] wneuper@59580: then () wneuper@59582: else raise TYPE ("formalisation inconsistent w.r.t. type inference: ", wneuper@59580: map (snd o dest_Free)confl, confl) wneuper@59580: end wneuper@59580: walther@59716: (* expects t as Const *) walther@59717: fun contains_Const_typeless ts t = (t walther@59716: |> strip_comb |> fst walther@59716: |> member (fn (t1, t2) => fst (dest_Const t1) = fst (dest_Const t2)) ts walther@59717: ) handle TERM("dest_Const", _) => raise TERM ("contains_Const_typeless", [t]) walther@59716: walther@59916: (* WN100910 weaker than fun sym_thm for Theory.axioms_of in isa02 *) walther@59916: fun sym_trm trm = walther@59916: let walther@59916: val (lhs, rhs) = (dest_equals o strip_trueprop o Logic.strip_imp_concl) trm walther@59916: val trm' = case strip_imp_prems' trm of walther@59916: NONE => mk_equality (rhs, lhs) walther@59916: | SOME cs => ins_concl cs (mk_equality (rhs, lhs)) walther@59916: in trm' end walther@59916: walther@59716: walther@60317: end