src/Pure/Syntax/syntax_phases.ML
changeset 81552 4717d3bf5752
parent 81545 6f8a56a6b391
child 81558 b57996a0688c
equal deleted inserted replaced
81551:a296642fa0a5 81552:4717d3bf5752
    62 fun markup_var xi = [Markup.name (Term.string_of_vname xi) Markup.var];
    62 fun markup_var xi = [Markup.name (Term.string_of_vname xi) Markup.var];
    63 
    63 
    64 fun markup_bound def ps (name, id) =
    64 fun markup_bound def ps (name, id) =
    65   Markup.bound :: map (fn pos => Position.make_entity_markup def id Markup.boundN (name, pos)) ps;
    65   Markup.bound :: map (fn pos => Position.make_entity_markup def id Markup.boundN (name, pos)) ps;
    66 
    66 
    67 fun markup_entity_cache ctxt =
    67 fun markup_entity ctxt c =
    68   Symtab.apply_unsynchronized_cache (fn c =>
    68   Syntax.get_consts (Proof_Context.syntax_of ctxt) c
    69     Syntax.get_consts (Proof_Context.syntax_of ctxt) c
    69   |> maps (Lexicon.unmark
    70     |> maps (Lexicon.unmark
    70      {case_class = markup_class ctxt,
    71        {case_class = markup_class ctxt,
    71       case_type = markup_type ctxt,
    72         case_type = markup_type ctxt,
    72       case_const = markup_const ctxt,
    73         case_const = markup_const ctxt,
    73       case_fixed = markup_free ctxt,
    74         case_fixed = markup_free ctxt,
    74       case_default = K []});
    75         case_default = K []}));
       
    76 
    75 
    77 
    76 
    78 
    77 
    79 (** reports of implicit variable scope **)
    78 (** reports of implicit variable scope **)
    80 
    79 
   164 
   163 
   165     fun report_pos tok =
   164     fun report_pos tok =
   166       if Lexicon.is_literal tok andalso null (Lexicon.literal_markup (Lexicon.str_of_token tok))
   165       if Lexicon.is_literal tok andalso null (Lexicon.literal_markup (Lexicon.str_of_token tok))
   167       then Position.none else Lexicon.pos_of_token tok;
   166       then Position.none else Lexicon.pos_of_token tok;
   168 
   167 
   169     val markup_cache = markup_entity_cache ctxt;
   168     val markup_cache = Symtab.apply_unsynchronized_cache (markup_entity ctxt);
   170 
   169 
   171     val ast_of_pos = Ast.Variable o Term_Position.encode;
   170     val ast_of_pos = Ast.Variable o Term_Position.encode;
   172     val ast_of_position = ast_of_pos o single o report_pos;
   171     val ast_of_position = ast_of_pos o single o report_pos;
   173     fun ast_of_position' a = Ast.constrain (Ast.Constant a) o ast_of_position;
   172     fun ast_of_position' a = Ast.constrain (Ast.Constant a) o ast_of_position;
   174 
   173 
   463     val syn = Proof_Context.syntax_of ctxt;
   462     val syn = Proof_Context.syntax_of ctxt;
   464 
   463 
   465     val reports = Unsynchronized.ref ([]: Position.report_text list);
   464     val reports = Unsynchronized.ref ([]: Position.report_text list);
   466     fun report ps = Position.store_reports reports ps;
   465     fun report ps = Position.store_reports reports ps;
   467 
   466 
   468     val markup_cache = markup_entity_cache ctxt;
   467     val markup_cache = Symtab.apply_unsynchronized_cache (markup_entity ctxt);
   469 
   468 
   470     fun decode_const ps c = (report ps markup_cache c; Ast.Constant c);
   469     fun decode_const ps c = (report ps markup_cache c; Ast.Constant c);
   471     fun decode_var ps x = (report ps (fn () => [Markup.name x Markup.free]) (); Ast.Variable x);
   470     fun decode_var ps x = (report ps (fn () => [Markup.name x Markup.free]) (); Ast.Variable x);
   472     fun decode_appl ps asts = Ast.Appl (map (decode ps) asts)
   471     fun decode_appl ps asts = Ast.Appl (map (decode ps) asts)
   473     and decode ps (Ast.Constant c) = decode_const ps c
   472     and decode ps (Ast.Constant c) = decode_const ps c
   710 
   709 
   711 (** unparse **)
   710 (** unparse **)
   712 
   711 
   713 local
   712 local
   714 
   713 
   715 fun extern_cache ctxt =
   714 fun extern ctxt c =
   716   Symtab.apply_unsynchronized_cache (fn c =>
   715   (case Syntax.get_consts (Proof_Context.syntax_of ctxt) c of
   717     (case Syntax.get_consts (Proof_Context.syntax_of ctxt) c of
   716     [b] => b
   718       [b] => b
   717   | bs =>
   719     | bs =>
   718       (case filter Lexicon.is_marked bs of
   720         (case filter Lexicon.is_marked bs of
   719         [] => c
   721           [] => c
   720       | [b] => b
   722         | [b] => b
   721       | _ => error ("Multiple logical entities for " ^ quote c ^ ": " ^ commas_quote bs)))
   723         | _ => error ("Multiple logical entities for " ^ quote c ^ ": " ^ commas_quote bs)))
   722   |> Proof_Context.extern_entity ctxt;
   724     |> Proof_Context.extern_entity ctxt);
   723 
   725 
   724 fun pretty_free ctxt x =
   726 val var_or_skolem_cache =
   725   Pretty.marks_str (markup_free ctxt x, Proof_Context.extern_fixed ctxt x);
   727   Symtab.apply_unsynchronized_cache (fn s =>
   726 
   728     (case Lexicon.read_variable s of
   727 fun pretty_var s =
   729       SOME (x, i) =>
   728   (case Lexicon.read_variable s of
   730         (case try Name.dest_skolem x of
   729     SOME (x, i) =>
   731           SOME x' => (Markup.skolem, Term.string_of_vname (x', i))
   730       (case try Name.dest_skolem x of
   732         | NONE => (Markup.var, s))
   731         SOME x' => (Markup.skolem, Term.string_of_vname (x', i))
   733     | NONE => (Markup.var, s)));
   732       | NONE => (Markup.var, s))
       
   733   | NONE => (Markup.var, s))
       
   734   |> Pretty.mark_str;
   734 
   735 
   735 val typing_elem = YXML.output_markup_elem Markup.typing;
   736 val typing_elem = YXML.output_markup_elem Markup.typing;
   736 val sorting_elem = YXML.output_markup_elem Markup.sorting;
   737 val sorting_elem = YXML.output_markup_elem Markup.sorting;
   737 
   738 
   738 fun exclude_consts ast ctxt =
   739 fun exclude_consts ast ctxt =
   759 
   760 
   760     val show_markup = Config.get ctxt show_markup;
   761     val show_markup = Config.get ctxt show_markup;
   761     val show_sorts = Config.get ctxt show_sorts;
   762     val show_sorts = Config.get ctxt show_sorts;
   762     val show_types = Config.get ctxt show_types orelse show_sorts;
   763     val show_types = Config.get ctxt show_types orelse show_sorts;
   763 
   764 
   764     val markup = markup_entity_cache ctxt;
   765     val pretty_free_cache = Symtab.apply_unsynchronized_cache (pretty_free ctxt);
   765     val extern = extern_cache ctxt;
   766     val pretty_var_cache = Symtab.apply_unsynchronized_cache pretty_var;
   766 
   767 
   767     val free_or_skolem_cache =
   768     val markup_syntax_cache = Symtab.apply_unsynchronized_cache (markup_entity ctxt);
   768       Symtab.apply_unsynchronized_cache (fn x =>
   769 
   769         (markup_free ctxt x, Proof_Context.extern_fixed ctxt x));
   770     val pretty_entity_cache =
       
   771       Symtab.apply_unsynchronized_cache (fn a =>
       
   772         Pretty.marks_str (markup_syntax_cache a, extern ctxt a));
   770 
   773 
   771     val cache1 = Unsynchronized.ref (Ast.Table.empty: Markup.output Pretty.block Ast.Table.table);
   774     val cache1 = Unsynchronized.ref (Ast.Table.empty: Markup.output Pretty.block Ast.Table.table);
   772     val cache2 = Unsynchronized.ref (Ast.Table.empty: Markup.output Pretty.block Ast.Table.table);
   775     val cache2 = Unsynchronized.ref (Ast.Table.empty: Markup.output Pretty.block Ast.Table.table);
   773 
   776 
   774     fun token_trans "_tfree" x = SOME (Pretty.mark_str (Markup.tfree, x))
   777     fun token_trans "_tfree" x = SOME (Pretty.mark_str (Markup.tfree, x))
   775       | token_trans "_tvar" x = SOME (Pretty.mark_str (Markup.tvar, x))
   778       | token_trans "_tvar" x = SOME (Pretty.mark_str (Markup.tvar, x))
   776       | token_trans "_free" x = SOME (Pretty.marks_str (free_or_skolem_cache x))
   779       | token_trans "_free" x = SOME (pretty_free_cache x)
   777       | token_trans "_bound" x = SOME (Pretty.mark_str (Markup.bound, x))
   780       | token_trans "_bound" x = SOME (Pretty.mark_str (Markup.bound, x))
   778       | token_trans "_loose" x = SOME (Pretty.mark_str (Markup.bad (), x))
   781       | token_trans "_loose" x = SOME (Pretty.mark_str (Markup.bad (), x))
   779       | token_trans "_var" x = SOME (Pretty.mark_str (var_or_skolem_cache x))
   782       | token_trans "_var" x = SOME (pretty_var_cache x)
   780       | token_trans "_numeral" x = SOME (Pretty.mark_str (Markup.numeral, x))
   783       | token_trans "_numeral" x = SOME (Pretty.mark_str (Markup.numeral, x))
   781       | token_trans "_inner_string" x = SOME (Pretty.mark_str (Markup.inner_string, x))
   784       | token_trans "_inner_string" x = SOME (Pretty.mark_str (Markup.inner_string, x))
   782       | token_trans _ _ = NONE;
   785       | token_trans _ _ = NONE;
   783 
   786 
   784     fun markup_trans a [Ast.Variable x] = token_trans a x
   787     fun markup_trans a [Ast.Variable x] = token_trans a x
   796       then SOME (markup_ast false ty s) else NONE
   799       then SOME (markup_ast false ty s) else NONE
   797 
   800 
   798     and pretty_ast flags m =
   801     and pretty_ast flags m =
   799       Printer.pretty flags ctxt prtabs
   802       Printer.pretty flags ctxt prtabs
   800         {trf = trf, constrain_block = constrain_block true, constrain_trans = constrain_trans,
   803         {trf = trf, constrain_block = constrain_block true, constrain_trans = constrain_trans,
   801           markup_trans = markup_trans, markup = markup, extern = extern}
   804           markup_trans = markup_trans, markup_syntax = markup_syntax_cache,
       
   805           pretty_entity = pretty_entity_cache}
   802       #> Pretty.markup m
   806       #> Pretty.markup m
   803 
   807 
   804     and markup_ast is_typing a A =
   808     and markup_ast is_typing a A =
   805       Pretty.make_block (constrain_block is_typing A)
   809       Pretty.make_block (constrain_block is_typing A)
   806         [pretty_ast (if is_typing then pretty_flags else Printer.type_mode_flags) Markup.empty a]
   810         [pretty_ast (if is_typing then pretty_flags else Printer.type_mode_flags) Markup.empty a]