theory TPTP imports Main begin (*Removes colliding notation (Warning: only valid for Isabelle2025 onwards)*) no_notation (ASCII) Not (\(\open_block notation=\prefix ~\\~ _)\ [40] 40) and conj (infixr \&\ 35) and disj (infixr \|\ 30) and Set.member (\(\notation=\infix :\\_/ : _)\ [51, 51] 50) no_notation List.append (infixr \@\ 65) (*Introduces explicit application connective (useful later for generating THF)*) definition thf_app :: "('a \ 'b) \ 'a \ 'b" (infixl "@" 70) where "f @ x \ f x" declare thf_app_def[simp] (*add as simplification rule (in case we forget to manually unfold it before proving)*) syntax "_thf_arrow" :: "type \ type \ type" (infixr ">" 0) "_thf_bool" :: "type" ("$o") "_thf_true" :: "logic" ("$true") "_thf_false" :: "logic" ("$false") "_thf_not" :: "logic \ logic" ("~") "_thf_impl" :: "logic \ logic \ logic" (infixr "=>" 25) "_thf_dimpl" :: "logic \ logic \ logic" (infixr "<=>" 25) "_thf_xor" :: "logic \ logic \ logic" (infixr "<~>" 25) "_thf_diseq" :: "logic \ logic \ logic" (infix "!=" 50) "_thf_and" :: "logic \ logic \ logic" (infixl "&" 35) "_thf_or" :: "logic \ logic \ logic" (infixl "|" 30) "_thf_lam" :: "args \ logic \ logic" ("(3^ [(_)] :/ _)" [0, 10] 10) "_thf_all" :: "args \ logic \ logic" ("(3! [(_)] :/ _)" [0, 10] 10) "_thf_ex" :: "args \ logic \ logic" ("(3? [(_)] :/ _)" [0, 10] 10) "_thf_idtyp" :: "id \ type \ idt" ("(1_ :/ _)" [4, 0] 0) "_thf_constrain" :: "logic \ type \ logic" ("_ :/ _" [4, 0] 3) "_thf_All" :: "logic \ logic" ("!!") "_thf_Ex" :: "logic \ logic" ("??") (*Other TPTP interpreted symbol types*) typedecl i (*... add others on-demand*) bundle from_TPTP begin translations "~a" \ "\a" "a & b" \ "a \ b" "a | b" \ "a \ b" "a => b" \ "a \ b" "a <=> b" \ "a \ b" "a <~> b" \ "\(a \ b)" "a != b" \ "a \ b" "$true" \ "CONST True" "$false" \ "CONST False" (type) "$o" \ (type) "bool" (type) "'a > 'b" \ (type) "'a \ 'b" "_thf_idtyp x T" \ "_idtyp x T" "_thf_constrain t T" \ "_constrain t T" "^ [x, xs] : t" \ "\x. (_thf_lam xs t)" "! [x, xs] : t" \ "\x. (_thf_all xs t)" "? [x, xs] : t" \ "\x. (_thf_ex xs t)" "^ [x] : t" \ "(\x. t)" "! [x] : t" \ "(\x. t)" "? [x] : t" \ "(\x. t)" "f @ x" \ "f x" "!! t" \ "CONST All t" "?? t" \ "CONST Ex t" type_notation(input) i ("$i") (*... add others*) end bundle to_TPTP begin declare [[eta_contract = false]] (*important, otherwise quantifiers like "\x y. f x y" break*) no_notation (output) All (binder "\" 10) no_notation (output) Ex (binder \\\ 10) translations "~a" \ "\a" "a & b" \ "a \ b" "a | b" \ "a \ b" "a => b" \ "a \ b" "a <=> b" \ "a \ b" "a <~> b" \ "\(a \ b)" "a != b" \ "a \ b" "$true" \ "CONST True" "$false" \ "CONST False" (type) "$o" \ (type) "bool" (type) "'a > 'b" \ (type) "'a \ 'b" "_thf_idtyp x T" \ "_idtyp x T" "_thf_constrain t T" \ "_constrain t T" "! [x] : t" \ "CONST All (\x. t)" "? [x] : t" \ "CONST Ex (\x. t)" "^ [x] : t" \ "(\x. t)" "!! t" \ "CONST All t" "?? t" \ "CONST Ex t" type_notation(output) i ("$i") (*... add others*) end (* Reifying @s in THF *) ML \ (* A list of "protected" constants that we don't want to convert:*) val protected = Symtab.make_set [@{const_name HOL.conj}, (* otherwise "a \ b" becomes "(\) @ a @ b" and so on ...*) @{const_name HOL.Not}, @{const_name HOL.disj}, @{const_name HOL.implies}, @{const_name HOL.eq}, @{const_name HOL.All}, @{const_name HOL.Ex}, @{const_name HOL.Trueprop} (*...*) ] (* Detects heads that should keep their normal notation*) fun protected_head (Const (d, _)) = d = @{const_name thf_app} orelse Symtab.defined protected d | protected_head _ = false (* Builds a well-typed explicit application node ("thf_app f x")*) fun app Ts f x = let val A = Term.fastype_of1 (Ts, x) val B = Term.fastype_of1 (Ts, f $ x) in Const (@{const_name thf_app}, (A --> B) --> A --> B) $ f $ x end (* Converts ordinary application chains into @-chains*) fun reify Ts (Abs (x, T, t)) = Abs (x, T, reify (T :: Ts) t) | reify Ts t = let val (h, xs) = Term.strip_comb t in if null xs orelse protected_head h then Term.list_comb (h, map (reify Ts) xs) else fold (fn x => fn f => app Ts f x) (map (reify Ts) xs) (reify Ts h) end (*Introduces a new configuration flag "show_thf_app" to enable/disable @-reification*) val show = Attrib.setup_config_bool @{binding show_thf_app} (K true) (* Runs @-reification before pretty-printing (only if the flag is set)*) fun uncheck ctxt = if Config.get ctxt show then map (reify []) else I \ (* Register "uncheck" as term-uncheck phase*) setup \Context.theory_map (Syntax_Phases.term_uncheck 100 "thf_app" uncheck)\ end