lean4-htt/lean4-mode/lean4-syntax.el
Leonardo de Moura 0b7d987699 feat(frontends/lean, library/init/lean): opaque constants
@kha I have added support for opaque constants to the old C++ frontend,
and made sure the new frontend can still parse `library/init/core.lean`.
The kernel should enforce that opaque constants are really opaque, and
the following example should fail
```
constant x : nat := 0
theorem foo : x = 0 := rfl
```
If it doesn't, it is a bug.

Here are some remaining issues:
1- `environment.mk_empty` is currently an axiom because we cannot create
an inhabitant of an opaque type. A possible solution is to use
`option environment` instead of `environment`.

2- There is no support for opaque constants in the new
frontend. However, I modified it to handle axioms, and fixed the literal
values with decl_cmd_kind. I tried to mark some of my changes with
comments, but it is probably much easier for you to just check the
commit change list.

3- I did not add any support for automatically constructing `e`
at `constant x : t := e`. I think we can do this later
after we replace the old frontend with the new one. BTW, it took only a
few minutes to provide the inhabitants manually.
2019-03-15 17:41:44 -07:00

194 lines
9.2 KiB
EmacsLisp
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

;; Copyright (c) 2013, 2014 Microsoft Corporation. All rights reserved.
;; Released under Apache 2.0 license as described in the file LICENSE.
;;
;; Author: Leonardo de Moura
;; Soonho Kong
;;
(require 'rx)
(defconst lean4-keywords1
'("import" "prelude" "protected" "private" "noncomputable" "definition" "unsafe" "renaming"
"hiding" "exposing" "parameter" "parameters" "begin" "constant"
"lemma" "variable" "variables" "theorem" "example" "abbreviation" "abbrev"
"open" "export" "axiom" "inductive" "coinductive" "with" "without"
"structure" "universe" "universes" "hide"
"precedence" "reserve" "declare_trace" "add_key_equivalence"
"match" "infix" "infixl" "infixr" "notation" "postfix" "prefix" "instance"
"end" "this" "using" "using_well_founded" "namespace" "section"
"attribute" "local" "set_option" "extends" "include" "omit" "classes" "class"
"attributes" "raw" "replacing"
"calc" "have" "show" "suffices" "by" "in" "at" "do" "let" "forall" "Pi" "fun"
"exists" "if" "then" "else" "assume" "from"
"mutual" "def" "run_cmd")
"lean keywords ending with 'word' (not symbol)")
(defconst lean4-keywords1-regexp
(eval `(rx word-start (or ,@lean4-keywords1) word-end)))
(defconst lean4-constants
'("#" "@" "!" "$" "->" "" "" "/" "==" "=" ":=" "<->" "/\\" "\\/" "" ""
"" "<" ">" "" "" "¬" "<=" ">=" "⁻¹" "" "" "+" "*" "-" "/" "λ"
"" "" "" "" "×" "Σ" "Π" "~" "||" "&&" "" "" ""
"" "" "" "" "" "𝔸"
"⬝e" "⬝i" "⬝o" "⬝op" "⬝po" "⬝h" "⬝v" "⬝hp" "⬝vp" "⬝ph" "⬝pv" "⬝r" "" "◾o"
"∘n" "∘f" "∘fi" "∘nf" "∘fn" "∘n1f" "∘1nf" "∘f1n" "∘fn1"
"^c" "≃c" "≅c" "×c" "×f" "×n" "+c" "+f" "+n" "ℕ₋₂")
"lean constants")
(defconst lean4-constants-regexp (regexp-opt lean4-constants))
(defconst lean4-numerals-regexp
(eval `(rx word-start
(one-or-more digit) (optional (and "." (zero-or-more digit)))
word-end)))
(defconst lean4-warnings '("sorry" "exit") "lean warnings")
(defconst lean4-warnings-regexp
(eval `(rx word-start (or ,@lean4-warnings) word-end)))
(defconst lean4-syntax-table
(let ((st (make-syntax-table)))
;; Matching parens
(modify-syntax-entry ?\[ "(]" st)
(modify-syntax-entry ?\] ")[" st)
(modify-syntax-entry ?\{ "(}" st)
(modify-syntax-entry ?\} "){" st)
;; comment
(modify-syntax-entry ?/ ". 14nb" st)
(modify-syntax-entry ?- ". 123" st)
(modify-syntax-entry ?\n ">" st)
(modify-syntax-entry "<" st)
(modify-syntax-entry ">" st)
;; Word constituent
(--map (modify-syntax-entry it "w" st)
(list ?a ?b ?c ?d ?e ?f ?g ?h ?i ?j ?k ?l ?m
?n ?o ?p ?q ?r ?s ?t ?u ?v ?w ?x ?y ?z
?A ?B ?C ?D ?E ?F ?G ?H ?I ?J ?K ?L ?M
?N ?O ?P ?Q ?R ?S ?T ?U ?V ?W ?X ?Y ?Z))
(--map (modify-syntax-entry it "w" st)
(list ?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9))
(--map (modify-syntax-entry it "w" st)
(list ?α ?γ ?ι ;;?λ
?ν ?ο ?ρ ?σ ?υ
))
(--map (modify-syntax-entry it "w" st)
(list ?ϒ
?Ϝ
?Ϩ
?ϱ ?ϲ ?ϳ ?Ϲ ?Ϻ ))
(--map (modify-syntax-entry it "w" st)
(list ?ἀ ?ἁ ?ἂ ?ἃ ?ἄ ?ἅ ?ἆ ?ἇ ?Ἀ ?Ἁ ?Ἂ ?Ἃ ?Ἄ
?Ἅ ?Ἆ ?Ἇ ?ἐ ?ἑ ?ἒ ?ἓ ?ἔ ?ἕ ?἖ ?἗ ?Ἐ ?Ἑ
?Ἒ ?Ἓ ?Ἔ ?Ἕ ?἞ ?἟ ?ἠ ?ἡ ?ἢ ?ἣ ?ἤ ?ἥ
?ἦ ?ἧ ?Ἠ ?Ἡ ?Ἢ ?Ἣ ?Ἤ ?Ἥ ?Ἦ ?Ἧ ?ἰ ?ἱ
?ἲ ?ἳ ?ἴ ?ἵ ?ἶ ?ἷ ?Ἰ ?Ἱ ?Ἲ ?Ἳ ?Ἴ ?Ἵ ?Ἶ ?Ἷ
?ὀ ?ὁ ?ὂ ?ὃ ?ὄ ?ὅ ?὆ ?὇ ?Ὀ ?Ὁ ?Ὂ ?Ὃ
?Ὄ ?Ὅ ?὎ ?὏ ?ὐ ?ὑ ?ὒ ?ὓ ?ὔ ?ὕ ?ὖ ?ὗ
?὘ ?Ὑ ?὚ ?Ὓ ?὜ ?Ὕ ?὞ ?Ὗ ?ὠ ?ὡ ?ὢ
?ὣ ?ὤ ?ὥ ?ὦ ?ὧ ?Ὠ ?Ὡ ?Ὢ ?Ὣ ?Ὤ ?Ὥ ?Ὦ
?Ὧ ?ὰ ?ά ?ὲ ?έ ?ὴ ?ή ?ὶ ?ί ?ὸ ?ό ?ὺ ?ύ ?ὼ
?ώ ?὾ ?὿ ?ᾀ ?ᾁ ?ᾂ ?ᾃ ?ᾄ ?ᾅ ?ᾆ ?ᾇ ?ᾈ
?ᾉ ?ᾊ ?ᾋ ?ᾌ ?ᾍ ?ᾎ ?ᾏ ?ᾐ ?ᾑ ?ᾒ ?ᾓ ?ᾔ
?ᾕ ?ᾖ ?ᾗ ?ᾘ ?ᾙ ?ᾚ ?ᾛ ?ᾜ ?ᾝ ?ᾞ ?ᾟ ?ᾠ ?ᾡ ?ᾢ
?ᾣ ?ᾤ ?ᾥ ?ᾦ ?ᾧ ?ᾨ ?ᾩ ?ᾪ ?ᾫ ?ᾬ ?ᾭ ?ᾮ ?ᾯ ?ᾰ
?ᾱ ?ᾲ ?ᾳ ?ᾴ ?᾵ ?ᾶ ?ᾷ ?Ᾰ ?Ᾱ ?Ὰ ?Ά ?ᾼ ?
? ?᾿ ? ?῁ ?ῂ ?ῃ ?ῄ ?῅ ?ῆ ?ῇ ?Ὲ ?Έ ?Ὴ
?Ή ?ῌ ?῍ ?῎ ?῏ ?ῐ ?ῑ ?ῒ ?ΐ ?῔ ?῕ ?ῖ ?ῗ
?Ῐ ?Ῑ ?Ὶ ?Ί ?῜ ?῝ ?῞ ?῟ ?ῠ ?ῡ ?ῢ ?ΰ ?ῤ ?ῥ
?ῦ ?ῧ ?Ῠ ?Ῡ ?Ὺ ?Ύ ?Ῥ ?῭ ?΅ ? ?῰ ?῱ ?ῲ ?ῳ
?ῴ ?῵ ?ῶ ?ῷ ?Ὸ ?Ό ?Ὼ ?Ώ ?ῼ ? ?))
(--map (modify-syntax-entry it "w" st)
(list ?℀ ?℁ ? ?℃ ?℄ ?℅ ?℆ ?ℇ ?℈ ?℉ ? ? ? ? ?
?ℏ ? ? ? ? ?℔ ? ?№ ?℗ ?℘ ? ? ? ? ?
?℞ ?℟ ?℠ ?℡ ?™ ?℣ ? ?℥ ?Ω ?℧ ? ?℩ ? ?Å ?
? ? ? ? ? ?Ⅎ ? ? ?ℵ ?ℶ ?ℷ ?ℸ ? ?℺ ?℻
?ℼ ? ?ℾ ?ℿ ?⅀ ?⅁ ?⅂ ?⅃ ?⅄ ? ? ? ? ? ?⅊
?⅋ ?⅌ ?⅍ ?ⅎ ?⅏))
(modify-syntax-entry ?' "w" st)
(modify-syntax-entry ?_ "w" st)
(modify-syntax-entry ?\. "w" st)
;; Lean operator chars
(mapc #'(lambda (ch) (modify-syntax-entry ch "_" st))
"!#$%&*+<=>@^|~:")
;; Whitespace is whitespace
(modify-syntax-entry ?\ " " st)
(modify-syntax-entry ?\t " " st)
;; Strings
(modify-syntax-entry ?\" "\"" st)
(modify-syntax-entry ?\\ "/" st)
st))
(defconst lean4-font-lock-defaults
`((;; attributes
(,(rx word-start "attribute" word-end (zero-or-more whitespace) (group (one-or-more "[" (zero-or-more (not (any "]"))) "]" (zero-or-more whitespace))))
(1 'font-lock-doc-face))
(,(rx (group "@[" (zero-or-more (not (any "]"))) "]"))
(1 'font-lock-doc-face))
(,(rx (group "#" (or "eval" "print" "reduce" "help" "check")))
(1 'font-lock-keyword-face))
;; mutual definitions "names"
(,(rx word-start
"mutual"
word-end
(zero-or-more whitespace)
word-start
(or "inductive" "definition" "def")
word-end
(group (zero-or-more (not (any " \t\n\r{([,"))) (zero-or-more (zero-or-more whitespace) "," (zero-or-more whitespace) (not (any " \t\n\r{([,")))))
(1 'font-lock-function-name-face))
;; declarations
(,(rx word-start
(group (or "inductive" (group "class" (zero-or-more whitespace) "inductive") "instance" "structure" "class" "theorem" "axiom" "lemma" "definition" "def" "constant"))
word-end (zero-or-more whitespace)
(group (zero-or-more "{" (zero-or-more (not (any "}"))) "}" (zero-or-more whitespace)))
(zero-or-more whitespace)
(group (zero-or-more (not (any " \t\n\r{([")))))
(4 'font-lock-function-name-face))
;; Constants which have a keyword as subterm
(,(rx (or "∘if")) . 'font-lock-constant-face)
;; Keywords
("\\(set_option\\)[ \t]*\\([^ \t\n]*\\)" (2 'font-lock-constant-face))
(,lean4-keywords1-regexp . 'font-lock-keyword-face)
(,(rx word-start (group "example") ".") (1 'font-lock-keyword-face))
(,(rx (or "")) . 'font-lock-keyword-face)
;; Types
(,(rx word-start (or "Prop" "Type" "Type*" "Sort" "Sort*") symbol-end) . 'font-lock-type-face)
(,(rx word-start (group (or "Prop" "Type" "Sort")) ".") (1 'font-lock-type-face))
;; String
("\"[^\"]*\"" . 'font-lock-string-face)
;; ;; Constants
(,lean4-constants-regexp . 'font-lock-constant-face)
(,lean4-numerals-regexp . 'font-lock-constant-face)
;; place holder
(,(rx symbol-start "_" symbol-end) . 'font-lock-preprocessor-face)
;; warnings
(,lean4-warnings-regexp . 'font-lock-warning-face)
;; escaped identifiers
(,(rx (and (group "«") (group (one-or-more (not (any "»")))) (group "»")))
(1 font-lock-comment-face t)
(2 nil t)
(3 font-lock-comment-face t))
)))
;; Syntax Highlighting for Lean Info Mode
(defconst lean4-info-font-lock-defaults
(let ((new-entries
`(;; Please add more after this:
(,(rx (group (+ symbol-start (+ (or word (char ?₁ ?₂ ?₃ ?₄ ?₅ ?₆ ?₇ ?₈ ?₉ ?₀))) symbol-end (* white))) ":")
(1 'font-lock-variable-name-face))
(,(rx white ":" white)
. 'font-lock-keyword-face)
(,(rx "" white)
. 'font-lock-keyword-face)
(,(rx "[" (group "stale") "]")
(1 'font-lock-warning-face))
(,(rx line-start "No Goal" line-end)
. 'font-lock-constant-face)))
(inherited-entries (car lean4-font-lock-defaults)))
`(,(-concat new-entries inherited-entries))))
(provide 'lean4-syntax)