diff --git a/library/init/lean/format.lean b/library/init/lean/format.lean index 18752e9f74..b76d912c9f 100644 --- a/library/init/lean/format.lean +++ b/library/init/lean/format.lean @@ -98,6 +98,9 @@ class has_to_format (α : Type u) := export lean.has_to_format (to_format) +def to_fmt {α : Type u} [has_to_format α] : α → format := +to_format + instance to_string_to_format {α : Type u} [has_to_string α] : has_to_format α := ⟨text ∘ to_string⟩ @@ -113,4 +116,10 @@ instance list_has_to_format {α : Type u} [has_to_format α] : has_to_format (li instance {α : Type u} {β : Type v} [has_to_format α] [has_to_format β] : has_to_format (prod α β) := ⟨λ ⟨a, b⟩, paren $ to_format a ++ "," ++ line ++ to_format b⟩ +instance nat_has_to_format : has_to_format nat := +⟨λ n, to_string n⟩ + +instance format_has_to_string : has_to_string format := +⟨pretty⟩ + end lean diff --git a/library/init/lean/parser/macro.lean b/library/init/lean/parser/macro.lean index 3313a79314..5bd8296aa7 100644 --- a/library/init/lean/parser/macro.lean +++ b/library/init/lean/parser/macro.lean @@ -4,10 +4,10 @@ Released under Apache 2.0 license as described in the file LICENSE. Author: Sebastian Ullrich -/ prelude -import init.lean.parser.move -import init.lean.parser.syntax +import init.data.rbmap init.lean.parser.syntax init.control.combinators -namespace lean.parser +namespace lean +namespace parser local attribute [instance] name.has_lt_quick @@ -17,13 +17,13 @@ namespace parse_m section local attribute [reducible] parse_m -instance (r σ) : monad (parse_m r σ) := by apply_instance +instance (r σ) : monad (parse_m r σ) := infer_instance --instance (r σ) : monad_run _ (parse_m r σ) := by apply_instance -instance (r σ) : monad_except string (parse_m r σ) := by apply_instance -instance (r σ) : monad_reader r (parse_m r σ) := by apply_instance -instance (r σ) : monad_state σ (parse_m r σ) := by apply_instance -instance (r σ σ') : monad_state_adapter σ σ' (parse_m r σ) (parse_m r σ') := by apply_instance -instance (r r' σ) : monad_reader_adapter r r' (parse_m r σ) (parse_m r' σ) := by apply_instance +instance (r σ) : monad_except string (parse_m r σ) := infer_instance +instance (r σ) : monad_reader r (parse_m r σ) := infer_instance +instance (r σ) : monad_state σ (parse_m r σ) := infer_instance +instance (r σ σ') : monad_state_adapter σ σ' (parse_m r σ) (parse_m r σ') := infer_instance +instance (r r' σ) : monad_reader_adapter r r' (parse_m r σ) (parse_m r' σ) := infer_instance def run {r σ α} (cfg : r) (st : σ) (x : parse_m r σ α) := (x.run.run cfg).run st @@ -83,7 +83,7 @@ def flip_tag (tag : ℕ) : syntax → syntax | (syntax.node node) := syntax.node {node with args := (node.args.map -- flip_tag (λ s, flip_tag s))} -| (syntax.list ls) := syntax.list (ls.map +| (syntax.lst ls) := syntax.lst (ls.map -- flip_tag (λ s, flip_tag s)) | (syntax.ident ident@{msc := none, ..}) := syntax.ident {ident with msc := some tag} @@ -126,4 +126,5 @@ let sc : scope := mk_rbmap _ _ _, rsm ← get, pure (stx, rsm) -end lean.parser +end parser +end lean diff --git a/library/init/lean/parser/move.lean b/library/init/lean/parser/move.lean deleted file mode 100644 index a2af48875f..0000000000 --- a/library/init/lean/parser/move.lean +++ /dev/null @@ -1,37 +0,0 @@ -/- -Copyright (c) 2018 Microsoft Corporation. All rights reserved. -Released under Apache 2.0 license as described in the file LICENSE. -Author: Sebastian Ullrich --/ -prelude -import init.data - --- non-meta instance -instance : decidable_eq name := -sorry -- TODO(Leo): define instance after we implement new `decidable` type class - -universes u v w - -namespace name - @[simp] protected def quick_lt : name → name → Prop - | anonymous anonymous := false - | anonymous _ := true - | (mk_numeral v n) (mk_numeral v' n') := v < v' ∨ v = v' ∧ n.quick_lt n' - | (mk_numeral _ _) (mk_string _ _) := true - | (mk_string s n) (mk_string s' n') := s < s' ∨ s = s' ∧ n.quick_lt n' - | _ _ := false - - instance decidable_rel_quick_lt : decidable_rel name.quick_lt := - begin - intros n n', - induction n generalizing n', - case anonymous { - by_cases n' = anonymous; simp *; apply_instance - }, - all_goals { cases n'; { - tactic.unfreeze_local_instances, -- use recursive instance - simp; apply_instance } } - end - - protected def has_lt_quick : has_lt name := ⟨name.quick_lt⟩ -end name diff --git a/library/init/lean/parser/syntax.lean b/library/init/lean/parser/syntax.lean index 91d0a3e50b..2216558bb8 100644 --- a/library/init/lean/parser/syntax.lean +++ b/library/init/lean/parser/syntax.lean @@ -4,9 +4,10 @@ Released under Apache 2.0 license as described in the file LICENSE. Author: Sebastian Ullrich -/ prelude -import init.data +import init.lean.name -namespace lean.parser +namespace lean +namespace parser @[reducible] def syntax_id := ℕ @[reducible] def macro_scope_id := ℕ @@ -32,22 +33,29 @@ inductive syntax | ident (val : syntax_ident) /- any non-ident atom -/ | atom (val : syntax_atom) -| list (ls : list syntax) +| lst (ls : list syntax) | node (val : syntax_node syntax) -protected meta def syntax.to_format : syntax → format := -λ s, format.group $ format.nest 2 $ match s with -| (syntax.ident ident@{msc := none, ..}) := format!"({ident.id}: ident `{ident.name})" -| (syntax.ident ident@{msc := some sc, ..}) := format!"({ident.id}: ident `{ident.name} from {sc})" -| (syntax.atom atom) := format!"({atom.id}: atom {atom.val})" -| (syntax.list ls) := format!"[{format.join $ ls.map syntax.to_format}]" -| (syntax.node node) := - let args := format.join $ node.args.map (λ arg, format!"\n{arg.to_format}") in - format!"({node.id}: node `{node.m} {args})" -end +namespace syntax +open format -meta instance : has_to_format syntax := ⟨syntax.to_format⟩ -meta instance : has_to_string syntax := ⟨to_string ∘ to_fmt⟩ -meta instance : has_repr syntax := ⟨to_string ∘ to_fmt⟩ +protected mutual def to_format, to_format_lst +with to_format : syntax → format +| (ident {id := id, name := n, msc := none, ..}) := + paren (to_fmt id ++ ": ident " ++ to_fmt n) +| (ident {id := id, name := n, msc := some sc, ..}) := + paren (to_fmt id ++ ": ident " ++ to_fmt n ++ " from " ++ to_fmt sc) +| (atom a) := paren (to_fmt a.id ++ ": atom " ++ to_fmt a.val) +| (lst ls) := paren $ join $ to_format_lst ls +| (node {id := id, m := n, args := args, ..}) := + paren (to_fmt id ++ ": node " ++ to_fmt n ++ join (to_format_lst args)) +with to_format_lst : list syntax → list format +| [] := [] +| (s::ss) := (line ++ to_format s) :: to_format_lst ss +end syntax -end lean.parser +instance : has_to_format syntax := ⟨syntax.to_format⟩ +instance : has_to_string syntax := ⟨to_string ∘ to_fmt⟩ + +end parser +end lean