theorem.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. """
  2. pygments.lexers.theorem
  3. ~~~~~~~~~~~~~~~~~~~~~~~
  4. Lexers for theorem-proving languages.
  5. See also :mod:`pygments.lexers.lean`
  6. :copyright: Copyright 2006-2024 by the Pygments team, see AUTHORS.
  7. :license: BSD, see LICENSE for details.
  8. """
  9. from pygments.lexer import RegexLexer, bygroups, default, words
  10. from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
  11. Number, Punctuation, Generic, Whitespace
  12. # compatibility import
  13. from pygments.lexers.lean import LeanLexer # noqa: F401
  14. __all__ = ['CoqLexer', 'IsabelleLexer']
  15. class CoqLexer(RegexLexer):
  16. """
  17. For the Coq theorem prover.
  18. """
  19. name = 'Coq'
  20. url = 'http://coq.inria.fr/'
  21. aliases = ['coq']
  22. filenames = ['*.v']
  23. mimetypes = ['text/x-coq']
  24. version_added = '1.5'
  25. flags = 0 # no re.MULTILINE
  26. keywords1 = (
  27. # Vernacular commands
  28. 'Section', 'Module', 'End', 'Require', 'Import', 'Export', 'Include', 'Variable',
  29. 'Variables', 'Parameter', 'Parameters', 'Axiom', 'Axioms', 'Hypothesis',
  30. 'Hypotheses', 'Notation', 'Local', 'Tactic', 'Reserved', 'Scope',
  31. 'Open', 'Close', 'Bind', 'Declare', 'Delimit', 'Definition', 'Example', 'Let',
  32. 'Ltac', 'Ltac2', 'Fixpoint', 'CoFixpoint', 'Morphism', 'Relation', 'Implicit',
  33. 'Arguments', 'Types', 'Contextual', 'Strict', 'Prenex',
  34. 'Implicits', 'Inductive', 'CoInductive', 'Record', 'Structure',
  35. 'Variant', 'Canonical', 'Coercion', 'Theorem', 'Lemma', 'Fact',
  36. 'Remark', 'Corollary', 'Proposition', 'Property', 'Goal',
  37. 'Proof', 'Restart', 'Save', 'Qed', 'Defined', 'Abort', 'Admitted',
  38. 'Hint', 'Resolve', 'Rewrite', 'View', 'Search', 'Compute', 'Eval',
  39. 'Show', 'Print', 'Printing', 'All', 'Graph', 'Projections', 'inside',
  40. 'outside', 'Check', 'Global', 'Instance', 'Class', 'Existing',
  41. 'Universe', 'Polymorphic', 'Monomorphic', 'Context', 'Scheme', 'From',
  42. 'Undo', 'Fail', 'Function', 'Program', 'Elpi', 'Extract', 'Opaque',
  43. 'Transparent', 'Unshelve', 'Next Obligation',
  44. )
  45. keywords2 = (
  46. # Gallina
  47. 'forall', 'exists', 'exists2', 'fun', 'fix', 'cofix', 'struct',
  48. 'match', 'end', 'in', 'return', 'let', 'if', 'is', 'then', 'else',
  49. 'for', 'of', 'nosimpl', 'with', 'as',
  50. )
  51. keywords3 = (
  52. # Sorts
  53. 'Type', 'Prop', 'SProp', 'Set',
  54. )
  55. keywords4 = (
  56. # Tactics
  57. 'pose', 'set', 'move', 'case', 'elim', 'apply', 'clear', 'hnf', 'intro',
  58. 'intros', 'generalize', 'rename', 'pattern', 'after', 'destruct',
  59. 'induction', 'using', 'refine', 'inversion', 'injection', 'rewrite',
  60. 'congr', 'unlock', 'compute', 'ring', 'field', 'replace', 'fold',
  61. 'unfold', 'change', 'cutrewrite', 'simpl', 'have', 'suff', 'wlog',
  62. 'suffices', 'without', 'loss', 'nat_norm', 'assert', 'cut', 'trivial',
  63. 'revert', 'bool_congr', 'nat_congr', 'symmetry', 'transitivity', 'auto',
  64. 'split', 'left', 'right', 'autorewrite', 'tauto', 'setoid_rewrite',
  65. 'intuition', 'eauto', 'eapply', 'econstructor', 'etransitivity',
  66. 'constructor', 'erewrite', 'red', 'cbv', 'lazy', 'vm_compute',
  67. 'native_compute', 'subst',
  68. )
  69. keywords5 = (
  70. # Terminators
  71. 'by', 'now', 'done', 'exact', 'reflexivity',
  72. 'tauto', 'romega', 'omega', 'lia', 'nia', 'lra', 'nra', 'psatz',
  73. 'assumption', 'solve', 'contradiction', 'discriminate',
  74. 'congruence', 'admit'
  75. )
  76. keywords6 = (
  77. # Control
  78. 'do', 'last', 'first', 'try', 'idtac', 'repeat',
  79. )
  80. # 'as', 'assert', 'begin', 'class', 'constraint', 'do', 'done',
  81. # 'downto', 'else', 'end', 'exception', 'external', 'false',
  82. # 'for', 'fun', 'function', 'functor', 'if', 'in', 'include',
  83. # 'inherit', 'initializer', 'lazy', 'let', 'match', 'method',
  84. # 'module', 'mutable', 'new', 'object', 'of', 'open', 'private',
  85. # 'raise', 'rec', 'sig', 'struct', 'then', 'to', 'true', 'try',
  86. # 'type', 'val', 'virtual', 'when', 'while', 'with'
  87. keyopts = (
  88. '!=', '#', '&', '&&', r'\(', r'\)', r'\*', r'\+', ',', '-', r'-\.',
  89. '->', r'\.', r'\.\.', ':', '::', ':=', ':>', ';', ';;', '<', '<-',
  90. '<->', '=', '>', '>]', r'>\}', r'\?', r'\?\?', r'\[', r'\[<', r'\[>',
  91. r'\[\|', ']', '_', '`', r'\{', r'\{<', r'lp:\{\{', r'\|', r'\|]', r'\}', '~', '=>',
  92. r'/\\', r'\\/', r'\{\|', r'\|\}',
  93. # 'Π', 'Σ', # Not defined in the standard library
  94. 'λ', '¬', '∧', '∨', '∀', '∃', '→', '↔', '≠', '≤', '≥',
  95. )
  96. operators = r'[!$%&*+\./:<=>?@^|~-]'
  97. prefix_syms = r'[!?~]'
  98. infix_syms = r'[=<>@^|&+\*/$%-]'
  99. tokens = {
  100. 'root': [
  101. (r'\s+', Text),
  102. (r'false|true|\(\)|\[\]', Name.Builtin.Pseudo),
  103. (r'\(\*', Comment, 'comment'),
  104. (r'\b(?:[^\W\d][\w\']*\.)+[^\W\d][\w\']*\b', Name),
  105. (r'\bEquations\b\??', Keyword.Namespace),
  106. (r'\b(Elpi)(\s+)(Program|Query|Accumulate|Command|Typecheck|Db|Export|Tactic)?\b', bygroups(Keyword.Namespace,Text,Keyword.Namespace)),
  107. # Very weak heuristic to distinguish the Set vernacular from the Set sort
  108. (r'\bUnset\b|\bSet(?=[ \t]+[A-Z][a-z][^\n]*?\.)', Keyword.Namespace, 'set-options'),
  109. (r'\b(?:String|Number)\s+Notation', Keyword.Namespace, 'sn-notation'),
  110. (words(keywords1, prefix=r'\b', suffix=r'\b'), Keyword.Namespace),
  111. (words(keywords2, prefix=r'\b', suffix=r'\b'), Keyword),
  112. (words(keywords3, prefix=r'\b', suffix=r'\b'), Keyword.Type),
  113. (words(keywords4, prefix=r'\b', suffix=r'\b'), Keyword),
  114. (words(keywords5, prefix=r'\b', suffix=r'\b'), Keyword.Pseudo),
  115. (words(keywords6, prefix=r'\b', suffix=r'\b'), Keyword.Reserved),
  116. # (r'\b([A-Z][\w\']*)(\.)', Name.Namespace, 'dotted'),
  117. (r'\b([A-Z][\w\']*)', Name),
  118. (r'({})'.format('|'.join(keyopts[::-1])), Operator),
  119. (rf'({infix_syms}|{prefix_syms})?{operators}', Operator),
  120. (r"[^\W\d][\w']*", Name),
  121. (r'\d[\d_]*', Number.Integer),
  122. (r'0[xX][\da-fA-F][\da-fA-F_]*', Number.Hex),
  123. (r'0[oO][0-7][0-7_]*', Number.Oct),
  124. (r'0[bB][01][01_]*', Number.Bin),
  125. (r'-?\d[\d_]*(.[\d_]*)?([eE][+\-]?\d[\d_]*)', Number.Float),
  126. (r"'(?:(\\[\\\"'ntbr ])|(\\[0-9]{3})|(\\x[0-9a-fA-F]{2}))'", String.Char),
  127. (r"'.'", String.Char),
  128. (r"'", Keyword), # a stray quote is another syntax element
  129. (r'"', String.Double, 'string'),
  130. (r'[~?][a-z][\w\']*:', Name),
  131. (r'\S', Name.Builtin.Pseudo),
  132. ],
  133. 'set-options': [
  134. (r'\s+', Text),
  135. (r'[A-Z]\w*', Keyword.Namespace),
  136. (r'"', String.Double, 'string'),
  137. (r'\d+', Number.Integer),
  138. (r'\.', Punctuation, '#pop'),
  139. ],
  140. 'sn-notation': [
  141. (r'\s+', Text),
  142. # Extra keywords to highlight only in this scope
  143. (r'\b(?:via|mapping|abstract|warning|after)\b', Keyword),
  144. (r'=>|[()\[\]:,]', Operator),
  145. (r'\b[^\W\d][\w\']*(?:\.[^\W\d][\w\']*)*\b', Name),
  146. (r'\d[\d_]*', Number.Integer),
  147. (r'0[xX][\da-fA-F][\da-fA-F_]*', Number.Hex),
  148. (r'\(\*', Comment, 'comment'),
  149. (r'\.', Punctuation, '#pop'),
  150. ],
  151. 'comment': [
  152. # Consume comments like ***** as one token
  153. (r'([^(*)]+|\*+(?!\)))+', Comment),
  154. (r'\(\*', Comment, '#push'),
  155. (r'\*\)', Comment, '#pop'),
  156. (r'[(*)]', Comment),
  157. ],
  158. 'string': [
  159. (r'[^"]+', String.Double),
  160. (r'""', String.Double),
  161. (r'"', String.Double, '#pop'),
  162. ],
  163. 'dotted': [
  164. (r'\s+', Text),
  165. (r'\.', Punctuation),
  166. (r'[A-Z][\w\']*(?=\s*\.)', Name.Namespace),
  167. (r'[A-Z][\w\']*', Name.Class, '#pop'),
  168. (r'[a-z][a-z0-9_\']*', Name, '#pop'),
  169. default('#pop')
  170. ],
  171. }
  172. def analyse_text(text):
  173. if 'Qed' in text and 'Proof' in text:
  174. return 1
  175. class IsabelleLexer(RegexLexer):
  176. """
  177. For the Isabelle proof assistant.
  178. """
  179. name = 'Isabelle'
  180. url = 'https://isabelle.in.tum.de/'
  181. aliases = ['isabelle']
  182. filenames = ['*.thy']
  183. mimetypes = ['text/x-isabelle']
  184. version_added = '2.0'
  185. keyword_minor = (
  186. 'and', 'assumes', 'attach', 'avoids', 'binder', 'checking',
  187. 'class_instance', 'class_relation', 'code_module', 'congs',
  188. 'constant', 'constrains', 'datatypes', 'defines', 'file', 'fixes',
  189. 'for', 'functions', 'hints', 'identifier', 'if', 'imports', 'in',
  190. 'includes', 'infix', 'infixl', 'infixr', 'is', 'keywords', 'lazy',
  191. 'module_name', 'monos', 'morphisms', 'no_discs_sels', 'notes',
  192. 'obtains', 'open', 'output', 'overloaded', 'parametric', 'permissive',
  193. 'pervasive', 'rep_compat', 'shows', 'structure', 'type_class',
  194. 'type_constructor', 'unchecked', 'unsafe', 'where',
  195. )
  196. keyword_diag = (
  197. 'ML_command', 'ML_val', 'class_deps', 'code_deps', 'code_thms',
  198. 'display_drafts', 'find_consts', 'find_theorems', 'find_unused_assms',
  199. 'full_prf', 'help', 'locale_deps', 'nitpick', 'pr', 'prf',
  200. 'print_abbrevs', 'print_antiquotations', 'print_attributes',
  201. 'print_binds', 'print_bnfs', 'print_bundles',
  202. 'print_case_translations', 'print_cases', 'print_claset',
  203. 'print_classes', 'print_codeproc', 'print_codesetup',
  204. 'print_coercions', 'print_commands', 'print_context',
  205. 'print_defn_rules', 'print_dependencies', 'print_facts',
  206. 'print_induct_rules', 'print_inductives', 'print_interps',
  207. 'print_locale', 'print_locales', 'print_methods', 'print_options',
  208. 'print_orders', 'print_quot_maps', 'print_quotconsts',
  209. 'print_quotients', 'print_quotientsQ3', 'print_quotmapsQ3',
  210. 'print_rules', 'print_simpset', 'print_state', 'print_statement',
  211. 'print_syntax', 'print_theorems', 'print_theory', 'print_trans_rules',
  212. 'prop', 'pwd', 'quickcheck', 'refute', 'sledgehammer', 'smt_status',
  213. 'solve_direct', 'spark_status', 'term', 'thm', 'thm_deps', 'thy_deps',
  214. 'try', 'try0', 'typ', 'unused_thms', 'value', 'values', 'welcome',
  215. 'print_ML_antiquotations', 'print_term_bindings', 'values_prolog',
  216. )
  217. keyword_thy = ('theory', 'begin', 'end')
  218. keyword_section = ('header', 'chapter')
  219. keyword_subsection = (
  220. 'section', 'subsection', 'subsubsection', 'sect', 'subsect',
  221. 'subsubsect',
  222. )
  223. keyword_theory_decl = (
  224. 'ML', 'ML_file', 'abbreviation', 'adhoc_overloading', 'arities',
  225. 'atom_decl', 'attribute_setup', 'axiomatization', 'bundle',
  226. 'case_of_simps', 'class', 'classes', 'classrel', 'codatatype',
  227. 'code_abort', 'code_class', 'code_const', 'code_datatype',
  228. 'code_identifier', 'code_include', 'code_instance', 'code_modulename',
  229. 'code_monad', 'code_printing', 'code_reflect', 'code_reserved',
  230. 'code_type', 'coinductive', 'coinductive_set', 'consts', 'context',
  231. 'datatype', 'datatype_new', 'datatype_new_compat', 'declaration',
  232. 'declare', 'default_sort', 'defer_recdef', 'definition', 'defs',
  233. 'domain', 'domain_isomorphism', 'domaindef', 'equivariance',
  234. 'export_code', 'extract', 'extract_type', 'fixrec', 'fun',
  235. 'fun_cases', 'hide_class', 'hide_const', 'hide_fact', 'hide_type',
  236. 'import_const_map', 'import_file', 'import_tptp', 'import_type_map',
  237. 'inductive', 'inductive_set', 'instantiation', 'judgment', 'lemmas',
  238. 'lifting_forget', 'lifting_update', 'local_setup', 'locale',
  239. 'method_setup', 'nitpick_params', 'no_adhoc_overloading',
  240. 'no_notation', 'no_syntax', 'no_translations', 'no_type_notation',
  241. 'nominal_datatype', 'nonterminal', 'notation', 'notepad', 'oracle',
  242. 'overloading', 'parse_ast_translation', 'parse_translation',
  243. 'partial_function', 'primcorec', 'primrec', 'primrec_new',
  244. 'print_ast_translation', 'print_translation', 'quickcheck_generator',
  245. 'quickcheck_params', 'realizability', 'realizers', 'recdef', 'record',
  246. 'refute_params', 'setup', 'setup_lifting', 'simproc_setup',
  247. 'simps_of_case', 'sledgehammer_params', 'spark_end', 'spark_open',
  248. 'spark_open_siv', 'spark_open_vcg', 'spark_proof_functions',
  249. 'spark_types', 'statespace', 'syntax', 'syntax_declaration', 'text',
  250. 'text_raw', 'theorems', 'translations', 'type_notation',
  251. 'type_synonym', 'typed_print_translation', 'typedecl', 'hoarestate',
  252. 'install_C_file', 'install_C_types', 'wpc_setup', 'c_defs', 'c_types',
  253. 'memsafe', 'SML_export', 'SML_file', 'SML_import', 'approximate',
  254. 'bnf_axiomatization', 'cartouche', 'datatype_compat',
  255. 'free_constructors', 'functor', 'nominal_function',
  256. 'nominal_termination', 'permanent_interpretation',
  257. 'binds', 'defining', 'smt2_status', 'term_cartouche',
  258. 'boogie_file', 'text_cartouche',
  259. )
  260. keyword_theory_script = ('inductive_cases', 'inductive_simps')
  261. keyword_theory_goal = (
  262. 'ax_specification', 'bnf', 'code_pred', 'corollary', 'cpodef',
  263. 'crunch', 'crunch_ignore',
  264. 'enriched_type', 'function', 'instance', 'interpretation', 'lemma',
  265. 'lift_definition', 'nominal_inductive', 'nominal_inductive2',
  266. 'nominal_primrec', 'pcpodef', 'primcorecursive',
  267. 'quotient_definition', 'quotient_type', 'recdef_tc', 'rep_datatype',
  268. 'schematic_corollary', 'schematic_lemma', 'schematic_theorem',
  269. 'spark_vc', 'specification', 'subclass', 'sublocale', 'termination',
  270. 'theorem', 'typedef', 'wrap_free_constructors',
  271. )
  272. keyword_qed = ('by', 'done', 'qed')
  273. keyword_abandon_proof = ('sorry', 'oops')
  274. keyword_proof_goal = ('have', 'hence', 'interpret')
  275. keyword_proof_block = ('next', 'proof')
  276. keyword_proof_chain = (
  277. 'finally', 'from', 'then', 'ultimately', 'with',
  278. )
  279. keyword_proof_decl = (
  280. 'ML_prf', 'also', 'include', 'including', 'let', 'moreover', 'note',
  281. 'txt', 'txt_raw', 'unfolding', 'using', 'write',
  282. )
  283. keyword_proof_asm = ('assume', 'case', 'def', 'fix', 'presume')
  284. keyword_proof_asm_goal = ('guess', 'obtain', 'show', 'thus')
  285. keyword_proof_script = (
  286. 'apply', 'apply_end', 'apply_trace', 'back', 'defer', 'prefer',
  287. )
  288. operators = (
  289. '::', ':', '(', ')', '[', ']', '_', '=', ',', '|',
  290. '+', '-', '!', '?',
  291. )
  292. proof_operators = ('{', '}', '.', '..')
  293. tokens = {
  294. 'root': [
  295. (r'\s+', Whitespace),
  296. (r'\(\*', Comment, 'comment'),
  297. (r'\\<open>', String.Symbol, 'cartouche'),
  298. (r'\{\*|‹', String, 'cartouche'),
  299. (words(operators), Operator),
  300. (words(proof_operators), Operator.Word),
  301. (words(keyword_minor, prefix=r'\b', suffix=r'\b'), Keyword.Pseudo),
  302. (words(keyword_diag, prefix=r'\b', suffix=r'\b'), Keyword.Type),
  303. (words(keyword_thy, prefix=r'\b', suffix=r'\b'), Keyword),
  304. (words(keyword_theory_decl, prefix=r'\b', suffix=r'\b'), Keyword),
  305. (words(keyword_section, prefix=r'\b', suffix=r'\b'), Generic.Heading),
  306. (words(keyword_subsection, prefix=r'\b', suffix=r'\b'), Generic.Subheading),
  307. (words(keyword_theory_goal, prefix=r'\b', suffix=r'\b'), Keyword.Namespace),
  308. (words(keyword_theory_script, prefix=r'\b', suffix=r'\b'), Keyword.Namespace),
  309. (words(keyword_abandon_proof, prefix=r'\b', suffix=r'\b'), Generic.Error),
  310. (words(keyword_qed, prefix=r'\b', suffix=r'\b'), Keyword),
  311. (words(keyword_proof_goal, prefix=r'\b', suffix=r'\b'), Keyword),
  312. (words(keyword_proof_block, prefix=r'\b', suffix=r'\b'), Keyword),
  313. (words(keyword_proof_decl, prefix=r'\b', suffix=r'\b'), Keyword),
  314. (words(keyword_proof_chain, prefix=r'\b', suffix=r'\b'), Keyword),
  315. (words(keyword_proof_asm, prefix=r'\b', suffix=r'\b'), Keyword),
  316. (words(keyword_proof_asm_goal, prefix=r'\b', suffix=r'\b'), Keyword),
  317. (words(keyword_proof_script, prefix=r'\b', suffix=r'\b'), Keyword.Pseudo),
  318. (r'\\<(\w|\^)*>', Text.Symbol),
  319. (r"'[^\W\d][.\w']*", Name.Type),
  320. (r'0[xX][\da-fA-F][\da-fA-F_]*', Number.Hex),
  321. (r'0[oO][0-7][0-7_]*', Number.Oct),
  322. (r'0[bB][01][01_]*', Number.Bin),
  323. (r'"', String, 'string'),
  324. (r'`', String.Other, 'fact'),
  325. (r'[^\s:|\[\]\-()=,+!?{}._][^\s:|\[\]\-()=,+!?{}]*', Name),
  326. ],
  327. 'comment': [
  328. (r'[^(*)]+', Comment),
  329. (r'\(\*', Comment, '#push'),
  330. (r'\*\)', Comment, '#pop'),
  331. (r'[(*)]', Comment),
  332. ],
  333. 'cartouche': [
  334. (r'[^{*}\\‹›]+', String),
  335. (r'\\<open>', String.Symbol, '#push'),
  336. (r'\{\*|‹', String, '#push'),
  337. (r'\\<close>', String.Symbol, '#pop'),
  338. (r'\*\}|›', String, '#pop'),
  339. (r'\\<(\w|\^)*>', String.Symbol),
  340. (r'[{*}\\]', String),
  341. ],
  342. 'string': [
  343. (r'[^"\\]+', String),
  344. (r'\\<(\w|\^)*>', String.Symbol),
  345. (r'\\"', String),
  346. (r'\\', String),
  347. (r'"', String, '#pop'),
  348. ],
  349. 'fact': [
  350. (r'[^`\\]+', String.Other),
  351. (r'\\<(\w|\^)*>', String.Symbol),
  352. (r'\\`', String.Other),
  353. (r'\\', String.Other),
  354. (r'`', String.Other, '#pop'),
  355. ],
  356. }