src/Pure/Syntax/syntax_phases.ML
author wenzelm
Wed Oct 03 14:58:56 2012 +0200 (2012-10-03)
changeset 49685 4341e4d86872
parent 49674 dbadb4d03cbc
child 49686 678aa92e921c
permissions -rw-r--r--
allow position constraints to coexist with 0 or 1 sort constraints;
more position information in sorting error;
report sorting of all type variables;
     1 (*  Title:      Pure/Syntax/syntax_phases.ML
     2     Author:     Makarius
     3 
     4 Main phases of inner syntax processing, with standard implementations
     5 of parse/unparse operations.
     6 *)
     7 
     8 signature SYNTAX_PHASES =
     9 sig
    10   val decode_sort: term -> sort
    11   val decode_typ: term -> typ
    12   val decode_term: Proof.context ->
    13     Position.report list * term Exn.result -> Position.report list * term Exn.result
    14   val parse_ast_pattern: Proof.context -> string * string -> Ast.ast
    15   val term_of_typ: Proof.context -> typ -> term
    16   val print_checks: Proof.context -> unit
    17   val typ_check: int -> string -> (Proof.context -> typ list -> typ list) ->
    18     Context.generic -> Context.generic
    19   val term_check: int -> string -> (Proof.context -> term list -> term list) ->
    20     Context.generic -> Context.generic
    21   val typ_uncheck: int -> string -> (Proof.context -> typ list -> typ list) ->
    22     Context.generic -> Context.generic
    23   val term_uncheck: int -> string -> (Proof.context -> term list -> term list) ->
    24     Context.generic -> Context.generic
    25   val typ_check': int -> string ->
    26     (typ list -> Proof.context -> (typ list * Proof.context) option) ->
    27     Context.generic -> Context.generic
    28   val term_check': int -> string ->
    29     (term list -> Proof.context -> (term list * Proof.context) option) ->
    30     Context.generic -> Context.generic
    31   val typ_uncheck': int -> string ->
    32     (typ list -> Proof.context -> (typ list * Proof.context) option) ->
    33     Context.generic -> Context.generic
    34   val term_uncheck': int -> string ->
    35     (term list -> Proof.context -> (term list * Proof.context) option) ->
    36     Context.generic -> Context.generic
    37 end
    38 
    39 structure Syntax_Phases: SYNTAX_PHASES =
    40 struct
    41 
    42 (** markup logical entities **)
    43 
    44 fun markup_class ctxt c =
    45   [Name_Space.markup (Type.class_space (Proof_Context.tsig_of ctxt)) c];
    46 
    47 fun markup_type ctxt c =
    48   [Name_Space.markup (Type.type_space (Proof_Context.tsig_of ctxt)) c];
    49 
    50 fun markup_const ctxt c =
    51   [Name_Space.markup (Consts.space_of (Proof_Context.consts_of ctxt)) c];
    52 
    53 fun markup_free ctxt x =
    54   [if can Name.dest_skolem x then Isabelle_Markup.skolem else Isabelle_Markup.free] @
    55   (if Variable.is_body ctxt orelse Variable.is_fixed ctxt x
    56    then [Variable.markup_fixed ctxt x]
    57    else []);
    58 
    59 fun markup_var xi = [Markup.name (Term.string_of_vname xi) Isabelle_Markup.var];
    60 
    61 fun markup_bound def ps (name, id) =
    62   let val entity = Isabelle_Markup.entity Isabelle_Markup.boundN name in
    63     Isabelle_Markup.bound ::
    64       map (fn pos => Markup.properties (Position.entity_properties_of def id pos) entity) ps
    65   end;
    66 
    67 fun markup_entity ctxt c =
    68   (case Syntax.lookup_const (Proof_Context.syn_of ctxt) c of
    69     SOME "" => []
    70   | SOME b => markup_entity ctxt b
    71   | NONE => c |> Lexicon.unmark
    72      {case_class = markup_class ctxt,
    73       case_type = markup_type ctxt,
    74       case_const = markup_const ctxt,
    75       case_fixed = markup_free ctxt,
    76       case_default = K []});
    77 
    78 
    79 
    80 (** decode parse trees **)
    81 
    82 (* decode_sort *)
    83 
    84 fun decode_sort tm =
    85   let
    86     fun err () = raise TERM ("decode_sort: bad encoding of classes", [tm]);
    87 
    88     fun class s = Lexicon.unmark_class s handle Fail _ => err ();
    89 
    90     fun classes (Const (s, _)) = [class s]
    91       | classes (Const ("_classes", _) $ Const (s, _) $ cs) = class s :: classes cs
    92       | classes _ = err ();
    93 
    94     fun sort (Const ("_topsort", _)) = []
    95       | sort (Const ("_sort", _) $ cs) = classes cs
    96       | sort (Const (s, _)) = [class s]
    97       | sort _ = err ();
    98   in sort tm end;
    99 
   100 
   101 (* decode_typ *)
   102 
   103 fun decode_pos (Free (s, _)) =
   104       if is_some (Term_Position.decode s) then SOME s else NONE
   105   | decode_pos _ = NONE;
   106 
   107 fun decode_typ tm =
   108   let
   109     fun err () = raise TERM ("decode_typ: bad encoding of type", [tm]);
   110 
   111     fun typ ps sort tm =
   112       (case tm of
   113         Const ("_tfree", _) $ t => typ ps sort t
   114       | Const ("_tvar", _) $ t => typ ps sort t
   115       | Const ("_ofsort", _) $ t $ s =>
   116           (case decode_pos s of
   117             SOME p => typ (p :: ps) sort t
   118           | NONE =>
   119               if is_none sort then typ ps (SOME (decode_sort s)) t
   120               else err ())
   121       | Const ("_dummy_ofsort", _) $ s => TFree ("'_dummy_", decode_sort s)
   122       | Free (x, _) => TFree (x, ps @ the_default dummyS sort)
   123       | Var (xi, _) => TVar (xi, ps @ the_default dummyS sort)
   124       | _ =>
   125           if null ps andalso is_none sort then
   126             let
   127               val (head, args) = Term.strip_comb tm;
   128               val a =
   129                 (case head of
   130                   Const (c, _) => (Lexicon.unmark_type c handle Fail _ => err ())
   131                 | _ => err ());
   132             in Type (a, map (typ [] NONE) args) end
   133           else err ());
   134   in typ [] NONE tm end;
   135 
   136 
   137 (* parsetree_to_ast *)
   138 
   139 fun parsetree_to_ast ctxt raw trf parsetree =
   140   let
   141     val reports = Unsynchronized.ref ([]: Position.report list);
   142     fun report pos = Position.store_reports reports [pos];
   143 
   144     fun trans a args =
   145       (case trf a of
   146         NONE => Ast.mk_appl (Ast.Constant a) args
   147       | SOME f => f ctxt args);
   148 
   149     fun asts_of_token tok =
   150       if Lexicon.valued_token tok
   151       then [Ast.Variable (Lexicon.str_of_token tok)]
   152       else [];
   153 
   154     fun asts_of_position c tok =
   155       if raw then asts_of_token tok
   156       else
   157         [Ast.Appl [Ast.Constant c, ast_of (Parser.Tip tok),
   158           Ast.Variable (Term_Position.encode (Lexicon.pos_of_token tok))]]
   159 
   160     and asts_of (Parser.Node ("_class_name", [Parser.Tip tok])) =
   161           let
   162             val pos = Lexicon.pos_of_token tok;
   163             val c = Proof_Context.read_class ctxt (Lexicon.str_of_token tok)
   164               handle ERROR msg => error (msg ^ Position.here pos);
   165             val _ = report pos (markup_class ctxt) c;
   166           in [Ast.Constant (Lexicon.mark_class c)] end
   167       | asts_of (Parser.Node ("_type_name", [Parser.Tip tok])) =
   168           let
   169             val pos = Lexicon.pos_of_token tok;
   170             val Type (c, _) =
   171               Proof_Context.read_type_name_proper ctxt false (Lexicon.str_of_token tok)
   172                 handle ERROR msg => error (msg ^ Position.here pos);
   173             val _ = report pos (markup_type ctxt) c;
   174           in [Ast.Constant (Lexicon.mark_type c)] end
   175       | asts_of (Parser.Node ("_position", [Parser.Tip tok])) = asts_of_position "_constrain" tok
   176       | asts_of (Parser.Node ("_position_sort", [Parser.Tip tok])) = asts_of_position "_ofsort" tok
   177       | asts_of (Parser.Node (a, pts)) =
   178           let
   179             val _ = pts |> List.app
   180               (fn Parser.Node _ => () | Parser.Tip tok =>
   181                 if Lexicon.valued_token tok then ()
   182                 else report (Lexicon.pos_of_token tok) (markup_entity ctxt) a);
   183           in [trans a (maps asts_of pts)] end
   184       | asts_of (Parser.Tip tok) = asts_of_token tok
   185 
   186     and ast_of pt =
   187       (case asts_of pt of
   188         [ast] => ast
   189       | asts => raise Ast.AST ("parsetree_to_ast: malformed parsetree", asts));
   190 
   191     val ast = Exn.interruptible_capture ast_of parsetree;
   192   in (! reports, ast) end;
   193 
   194 
   195 (* ast_to_term *)
   196 
   197 fun ast_to_term ctxt trf =
   198   let
   199     fun trans a args =
   200       (case trf a of
   201         NONE => Term.list_comb (Syntax.const a, args)
   202       | SOME f => f ctxt args);
   203 
   204     fun term_of (Ast.Constant a) = trans a []
   205       | term_of (Ast.Variable x) = Lexicon.read_var x
   206       | term_of (Ast.Appl (Ast.Constant a :: (asts as _ :: _))) =
   207           trans a (map term_of asts)
   208       | term_of (Ast.Appl (ast :: (asts as _ :: _))) =
   209           Term.list_comb (term_of ast, map term_of asts)
   210       | term_of (ast as Ast.Appl _) = raise Ast.AST ("ast_to_term: malformed ast", [ast]);
   211   in term_of end;
   212 
   213 
   214 (* decode_term -- transform parse tree into raw term *)
   215 
   216 fun decode_term _ (result as (_: Position.report list, Exn.Exn _)) = result
   217   | decode_term ctxt (reports0, Exn.Res tm) =
   218       let
   219         fun get_const a =
   220           ((true, #1 (Term.dest_Const (Proof_Context.read_const_proper ctxt false a)))
   221             handle ERROR _ => (false, Consts.intern (Proof_Context.consts_of ctxt) a));
   222         val get_free = Proof_Context.intern_skolem ctxt;
   223 
   224         val reports = Unsynchronized.ref reports0;
   225         fun report ps = Position.store_reports reports ps;
   226 
   227         fun decode ps qs bs (Const ("_constrain", _) $ t $ typ) =
   228               (case Term_Position.decode_position typ of
   229                 SOME (p, T) => Type.constraint T (decode (p :: ps) qs bs t)
   230               | NONE => Type.constraint (decode_typ typ) (decode ps qs bs t))
   231           | decode ps qs bs (Const ("_constrainAbs", _) $ t $ typ) =
   232               (case Term_Position.decode_position typ of
   233                 SOME (q, T) => Type.constraint (T --> dummyT) (decode ps (q :: qs) bs t)
   234               | NONE => Type.constraint (decode_typ typ --> dummyT) (decode ps qs bs t))
   235           | decode _ qs bs (Abs (x, T, t)) =
   236               let
   237                 val id = serial ();
   238                 val _ = report qs (markup_bound true qs) (x, id);
   239               in Abs (x, T, decode [] [] ((qs, (x, id)) :: bs) t) end
   240           | decode _ _ bs (t $ u) = decode [] [] bs t $ decode [] [] bs u
   241           | decode ps _ _ (Const (a, T)) =
   242               (case try Lexicon.unmark_fixed a of
   243                 SOME x => (report ps (markup_free ctxt) x; Free (x, T))
   244               | NONE =>
   245                   let
   246                     val c =
   247                       (case try Lexicon.unmark_const a of
   248                         SOME c => c
   249                       | NONE => snd (get_const a));
   250                     val _ = report ps (markup_const ctxt) c;
   251                   in Const (c, T) end)
   252           | decode ps _ _ (Free (a, T)) =
   253               (case (get_free a, get_const a) of
   254                 (SOME x, _) => (report ps (markup_free ctxt) x; Free (x, T))
   255               | (_, (true, c)) => (report ps (markup_const ctxt) c; Const (c, T))
   256               | (_, (false, c)) =>
   257                   if Long_Name.is_qualified c
   258                   then (report ps (markup_const ctxt) c; Const (c, T))
   259                   else (report ps (markup_free ctxt) c; Free (c, T)))
   260           | decode ps _ _ (Var (xi, T)) = (report ps markup_var xi; Var (xi, T))
   261           | decode ps _ bs (t as Bound i) =
   262               (case try (nth bs) i of
   263                 SOME (qs, (x, id)) => (report ps (markup_bound false qs) (x, id); t)
   264               | NONE => t);
   265 
   266         val tm' = Exn.interruptible_capture (fn () => decode [] [] [] tm) ();
   267       in (! reports, tm') end;
   268 
   269 
   270 
   271 (** parse **)
   272 
   273 (* results *)
   274 
   275 fun proper_results results = map_filter (fn (y, Exn.Res x) => SOME (y, x) | _ => NONE) results;
   276 fun failed_results results = map_filter (fn (y, Exn.Exn e) => SOME (y, e) | _ => NONE) results;
   277 
   278 fun report_result ctxt pos ambig_msgs results =
   279   (case (proper_results results, failed_results results) of
   280     ([], (reports, exn) :: _) => (Context_Position.reports ctxt reports; reraise exn)
   281   | ([(reports, x)], _) => (Context_Position.reports ctxt reports; x)
   282   | _ =>
   283       if null ambig_msgs then
   284         error ("Parse error: ambiguous syntax" ^ Position.here pos)
   285       else error (cat_lines ambig_msgs));
   286 
   287 
   288 (* parse raw asts *)
   289 
   290 fun parse_asts ctxt raw root (syms, pos) =
   291   let
   292     val syn = Proof_Context.syn_of ctxt;
   293     val ast_tr = Syntax.parse_ast_translation syn;
   294 
   295     val toks = Syntax.tokenize syn raw syms;
   296     val _ = Context_Position.reports ctxt (map Lexicon.report_of_token toks);
   297 
   298     val pts = Syntax.parse syn root (filter Lexicon.is_proper toks)
   299       handle ERROR msg =>
   300         error (msg ^
   301           implode
   302             (map (Markup.markup Isabelle_Markup.report o Lexicon.reported_token_range ctxt) toks));
   303     val len = length pts;
   304 
   305     val limit = Config.get ctxt Syntax.ambiguity_limit;
   306     val ambig_msgs =
   307       if len <= 1 then []
   308       else
   309         [cat_lines
   310           (("Ambiguous input" ^ Position.here (Position.reset_range pos) ^
   311             "\nproduces " ^ string_of_int len ^ " parse trees" ^
   312             (if len <= limit then "" else " (" ^ string_of_int limit ^ " displayed)") ^ ":") ::
   313             map (Pretty.string_of o Parser.pretty_parsetree) (take limit pts))];
   314 
   315   in (ambig_msgs, map (parsetree_to_ast ctxt raw ast_tr) pts) end;
   316 
   317 fun parse_tree ctxt root input =
   318   let
   319     val syn = Proof_Context.syn_of ctxt;
   320     val tr = Syntax.parse_translation syn;
   321     val parse_rules = Syntax.parse_rules syn;
   322     val (ambig_msgs, asts) = parse_asts ctxt false root input;
   323     val results =
   324       (map o apsnd o Exn.maps_result)
   325         (Ast.normalize ctxt parse_rules #> Exn.interruptible_capture (ast_to_term ctxt tr)) asts;
   326   in (ambig_msgs, results) end;
   327 
   328 
   329 (* parse logical entities *)
   330 
   331 fun parse_failed ctxt pos msg kind =
   332   cat_error msg ("Failed to parse " ^ kind ^
   333     Markup.markup Isabelle_Markup.report
   334       (Context_Position.reported_text ctxt pos Isabelle_Markup.bad ""));
   335 
   336 fun parse_sort ctxt =
   337   Syntax.parse_token ctxt Term_XML.Decode.sort Isabelle_Markup.sort
   338     (fn (syms, pos) =>
   339       parse_tree ctxt "sort" (syms, pos)
   340       |> uncurry (report_result ctxt pos)
   341       |> decode_sort
   342       |> Type.minimize_sort (Proof_Context.tsig_of ctxt)
   343       handle ERROR msg => parse_failed ctxt pos msg "sort");
   344 
   345 fun parse_typ ctxt =
   346   Syntax.parse_token ctxt Term_XML.Decode.typ Isabelle_Markup.typ
   347     (fn (syms, pos) =>
   348       parse_tree ctxt "type" (syms, pos)
   349       |> uncurry (report_result ctxt pos)
   350       |> decode_typ
   351       handle ERROR msg => parse_failed ctxt pos msg "type");
   352 
   353 fun parse_term is_prop ctxt =
   354   let
   355     val (markup, kind, root, constrain) =
   356       if is_prop
   357       then (Isabelle_Markup.prop, "proposition", "prop", Type.constraint propT)
   358       else (Isabelle_Markup.term, "term", Config.get ctxt Syntax.root, I);
   359     val decode = constrain o Term_XML.Decode.term;
   360   in
   361     Syntax.parse_token ctxt decode markup
   362       (fn (syms, pos) =>
   363         let
   364           val (ambig_msgs, results) = parse_tree ctxt root (syms, pos) ||> map (decode_term ctxt);
   365           val parsed_len = length (proper_results results);
   366 
   367           val ambiguity_warning = Config.get ctxt Syntax.ambiguity_warning;
   368           val limit = Config.get ctxt Syntax.ambiguity_limit;
   369 
   370           (*brute-force disambiguation via type-inference*)
   371           fun check t = (Syntax.check_term ctxt (constrain t); Exn.Res t)
   372             handle exn as ERROR _ => Exn.Exn exn;
   373 
   374           val results' =
   375             if parsed_len > 1 then
   376               (grouped 10 (Par_List.map_name "Syntax_Phases.parse_term") o apsnd o Exn.maps_result)
   377                 check results
   378             else results;
   379           val reports' = fst (hd results');
   380 
   381           val errs = map snd (failed_results results');
   382           val checked = map snd (proper_results results');
   383           val checked_len = length checked;
   384 
   385           val show_term = Syntax.string_of_term (Config.put Printer.show_brackets true ctxt);
   386         in
   387           if checked_len = 0 then
   388             report_result ctxt pos []
   389               [(reports', Exn.Exn (Exn.EXCEPTIONS (map ERROR ambig_msgs @ errs)))]
   390           else if checked_len = 1 then
   391             (if parsed_len > 1 andalso ambiguity_warning then
   392               Context_Position.if_visible ctxt warning
   393                 (cat_lines (ambig_msgs @
   394                   ["Fortunately, only one parse tree is type correct" ^
   395                   Position.here (Position.reset_range pos) ^
   396                   ",\nbut you may still want to disambiguate your grammar or your input."]))
   397              else (); report_result ctxt pos [] results')
   398           else
   399             report_result ctxt pos []
   400               [(reports', Exn.Exn (ERROR (cat_lines (ambig_msgs @
   401                 (("Ambiguous input\n" ^ string_of_int checked_len ^ " terms are type correct" ^
   402                   (if checked_len <= limit then ""
   403                    else " (" ^ string_of_int limit ^ " displayed)") ^ ":") ::
   404                   map show_term (take limit checked))))))]
   405         end handle ERROR msg => parse_failed ctxt pos msg kind)
   406   end;
   407 
   408 
   409 (* parse_ast_pattern *)
   410 
   411 fun parse_ast_pattern ctxt (root, str) =
   412   let
   413     val syn = Proof_Context.syn_of ctxt;
   414 
   415     fun constify (ast as Ast.Constant _) = ast
   416       | constify (ast as Ast.Variable x) =
   417           if is_some (Syntax.lookup_const syn x) orelse Long_Name.is_qualified x
   418           then Ast.Constant x
   419           else ast
   420       | constify (Ast.Appl asts) = Ast.Appl (map constify asts);
   421 
   422     val (syms, pos) = Syntax.read_token str;
   423   in
   424     parse_asts ctxt true root (syms, pos)
   425     |> uncurry (report_result ctxt pos)
   426     |> constify
   427   end;
   428 
   429 
   430 
   431 (** encode parse trees **)
   432 
   433 (* term_of_sort *)
   434 
   435 fun term_of_sort S =
   436   let
   437     val class = Syntax.const o Lexicon.mark_class;
   438 
   439     fun classes [c] = class c
   440       | classes (c :: cs) = Syntax.const "_classes" $ class c $ classes cs;
   441   in
   442     (case S of
   443       [] => Syntax.const "_topsort"
   444     | [c] => class c
   445     | cs => Syntax.const "_sort" $ classes cs)
   446   end;
   447 
   448 
   449 (* term_of_typ *)
   450 
   451 fun term_of_typ ctxt ty =
   452   let
   453     val show_sorts = Config.get ctxt show_sorts;
   454 
   455     fun of_sort t S =
   456       if show_sorts then Syntax.const "_ofsort" $ t $ term_of_sort S
   457       else t;
   458 
   459     fun term_of (Type (a, Ts)) =
   460           Term.list_comb (Syntax.const (Lexicon.mark_type a), map term_of Ts)
   461       | term_of (TFree (x, S)) =
   462           if is_some (Term_Position.decode x) then Syntax.free x
   463           else of_sort (Syntax.const "_tfree" $ Syntax.free x) S
   464       | term_of (TVar (xi, S)) = of_sort (Syntax.const "_tvar" $ Syntax.var xi) S;
   465   in term_of ty end;
   466 
   467 
   468 (* simple_ast_of *)
   469 
   470 fun simple_ast_of ctxt =
   471   let
   472     val tune_var = if Config.get ctxt show_question_marks then I else unprefix "?";
   473     fun ast_of (Const (c, _)) = Ast.Constant c
   474       | ast_of (Free (x, _)) = Ast.Variable x
   475       | ast_of (Var (xi, _)) = Ast.Variable (tune_var (Term.string_of_vname xi))
   476       | ast_of (t as _ $ _) =
   477           let val (f, args) = strip_comb t
   478           in Ast.mk_appl (ast_of f) (map ast_of args) end
   479       | ast_of (Bound i) = Ast.Appl [Ast.Constant "_loose", Ast.Variable ("B." ^ string_of_int i)]
   480       | ast_of (Abs _) = raise Fail "simple_ast_of: Abs";
   481   in ast_of end;
   482 
   483 
   484 (* sort_to_ast and typ_to_ast *)
   485 
   486 fun ast_of_termT ctxt trf tm =
   487   let
   488     val ctxt' = Config.put show_sorts false ctxt;
   489     fun ast_of (t as Const ("_tfree", _) $ Free _) = simple_ast_of ctxt t
   490       | ast_of (t as Const ("_tvar", _) $ Var _) = simple_ast_of ctxt t
   491       | ast_of (Const (a, _)) = trans a []
   492       | ast_of (t as _ $ _) =
   493           (case strip_comb t of
   494             (Const (a, _), args) => trans a args
   495           | (f, args) => Ast.Appl (map ast_of (f :: args)))
   496       | ast_of t = simple_ast_of ctxt t
   497     and trans a args = ast_of (trf a ctxt' dummyT args)
   498       handle Match => Ast.mk_appl (Ast.Constant a) (map ast_of args);
   499   in ast_of tm end;
   500 
   501 fun sort_to_ast ctxt trf S = ast_of_termT ctxt trf (term_of_sort S);
   502 fun typ_to_ast ctxt trf T = ast_of_termT ctxt trf (term_of_typ ctxt T);
   503 
   504 
   505 (* term_to_ast *)
   506 
   507 fun term_to_ast idents is_syntax_const ctxt trf tm =
   508   let
   509     val show_types =
   510       Config.get ctxt show_types orelse Config.get ctxt show_sorts orelse
   511       Config.get ctxt show_all_types;
   512     val show_structs = Config.get ctxt show_structs;
   513     val show_free_types = Config.get ctxt show_free_types;
   514     val show_all_types = Config.get ctxt show_all_types;
   515     val show_markup = Config.get ctxt show_markup;
   516 
   517     val {structs, fixes} = idents;
   518 
   519     fun mark_atoms ((t as Const (c, _)) $ u) =
   520           if member (op =) Syntax.token_markers c
   521           then t $ u else mark_atoms t $ mark_atoms u
   522       | mark_atoms (t $ u) = mark_atoms t $ mark_atoms u
   523       | mark_atoms (Abs (x, T, t)) = Abs (x, T, mark_atoms t)
   524       | mark_atoms (t as Const (c, T)) =
   525           if is_syntax_const c then t
   526           else Const (Lexicon.mark_const c, T)
   527       | mark_atoms (t as Free (x, T)) =
   528           let val i = find_index (fn s => s = x) structs + 1 in
   529             if i = 0 andalso member (op =) fixes x then
   530               Const (Lexicon.mark_fixed x, T)
   531             else if i = 1 andalso not show_structs then
   532               Syntax.const "_struct" $ Syntax.const "_indexdefault"
   533             else Syntax.const "_free" $ t
   534           end
   535       | mark_atoms (t as Var (xi, T)) =
   536           if xi = Syntax_Ext.dddot_indexname then Const ("_DDDOT", T)
   537           else Syntax.const "_var" $ t
   538       | mark_atoms a = a;
   539 
   540     fun prune_typs (t_seen as (Const _, _)) = t_seen
   541       | prune_typs (t as Free (x, ty), seen) =
   542           if ty = dummyT then (t, seen)
   543           else if not show_free_types orelse member (op aconv) seen t then (Syntax.free x, seen)
   544           else (t, t :: seen)
   545       | prune_typs (t as Var (xi, ty), seen) =
   546           if ty = dummyT then (t, seen)
   547           else if not show_free_types orelse member (op aconv) seen t then (Syntax.var xi, seen)
   548           else (t, t :: seen)
   549       | prune_typs (t_seen as (Bound _, _)) = t_seen
   550       | prune_typs (Abs (x, ty, t), seen) =
   551           let val (t', seen') = prune_typs (t, seen);
   552           in (Abs (x, ty, t'), seen') end
   553       | prune_typs (t1 $ t2, seen) =
   554           let
   555             val (t1', seen') = prune_typs (t1, seen);
   556             val (t2', seen'') = prune_typs (t2, seen');
   557           in (t1' $ t2', seen'') end;
   558 
   559     fun ast_of tm =
   560       (case strip_comb tm of
   561         (t as Abs _, ts) => Ast.mk_appl (ast_of (Syntax_Trans.abs_tr' ctxt t)) (map ast_of ts)
   562       | ((c as Const ("_free", _)), Free (x, T) :: ts) =>
   563           Ast.mk_appl (constrain (c $ Syntax.free x) T) (map ast_of ts)
   564       | ((c as Const ("_var", _)), Var (xi, T) :: ts) =>
   565           Ast.mk_appl (constrain (c $ Syntax.var xi) T) (map ast_of ts)
   566       | ((c as Const ("_bound", B)), Free (x, T) :: ts) =>
   567           let
   568             val X =
   569               if show_markup andalso not show_types orelse B <> dummyT then T
   570               else dummyT;
   571           in Ast.mk_appl (constrain (c $ Syntax.free x) X) (map ast_of ts) end
   572       | (Const ("_idtdummy", T), ts) =>
   573           Ast.mk_appl (constrain (Syntax.const "_idtdummy") T) (map ast_of ts)
   574       | (const as Const (c, T), ts) =>
   575           if show_all_types
   576           then Ast.mk_appl (constrain const T) (map ast_of ts)
   577           else trans c T ts
   578       | (t, ts) => Ast.mk_appl (simple_ast_of ctxt t) (map ast_of ts))
   579 
   580     and trans a T args = ast_of (trf a ctxt T args)
   581       handle Match => Ast.mk_appl (Ast.Constant a) (map ast_of args)
   582 
   583     and constrain t T =
   584       if (show_types orelse show_markup) andalso T <> dummyT then
   585         Ast.Appl [Ast.Constant "_constrain", simple_ast_of ctxt t,
   586           ast_of_termT ctxt trf (term_of_typ ctxt T)]
   587       else simple_ast_of ctxt t;
   588   in
   589     tm
   590     |> Syntax_Trans.prop_tr'
   591     |> show_types ? (#1 o prune_typs o rpair [])
   592     |> mark_atoms
   593     |> ast_of
   594   end;
   595 
   596 
   597 
   598 (** unparse **)
   599 
   600 local
   601 
   602 fun free_or_skolem ctxt x =
   603   let
   604     val m =
   605       if Variable.is_fixed ctxt x orelse Syntax.is_pretty_global ctxt
   606       then Isabelle_Markup.fixed x
   607       else Isabelle_Markup.intensify;
   608   in
   609     if can Name.dest_skolem x
   610     then ([m, Isabelle_Markup.skolem], Variable.revert_fixed ctxt x)
   611     else ([m, Isabelle_Markup.free], x)
   612   end;
   613 
   614 fun var_or_skolem s =
   615   (case Lexicon.read_variable s of
   616     SOME (x, i) =>
   617       (case try Name.dest_skolem x of
   618         NONE => (Isabelle_Markup.var, s)
   619       | SOME x' => (Isabelle_Markup.skolem, Term.string_of_vname (x', i)))
   620   | NONE => (Isabelle_Markup.var, s));
   621 
   622 val typing_elem = YXML.output_markup_elem Isabelle_Markup.typing;
   623 
   624 fun unparse_t t_to_ast prt_t markup ctxt t =
   625   let
   626     val show_types =
   627       Config.get ctxt show_types orelse Config.get ctxt show_sorts orelse
   628       Config.get ctxt show_all_types;
   629     val show_markup = Config.get ctxt show_markup;
   630 
   631     val syn = Proof_Context.syn_of ctxt;
   632     val prtabs = Syntax.prtabs syn;
   633     val trf = Syntax.print_ast_translation syn;
   634 
   635     fun markup_extern c =
   636       (case Syntax.lookup_const syn c of
   637         SOME "" => ([], c)
   638       | SOME b => markup_extern b
   639       | NONE => c |> Lexicon.unmark
   640          {case_class = fn x => (markup_class ctxt x, Proof_Context.extern_class ctxt x),
   641           case_type = fn x => (markup_type ctxt x, Proof_Context.extern_type ctxt x),
   642           case_const = fn x => (markup_const ctxt x, Proof_Context.extern_const ctxt x),
   643           case_fixed = fn x => free_or_skolem ctxt x,
   644           case_default = fn x => ([], x)});
   645 
   646     fun token_trans "_tfree" x = SOME (Pretty.mark_str (Isabelle_Markup.tfree, x))
   647       | token_trans "_tvar" x = SOME (Pretty.mark_str (Isabelle_Markup.tvar, x))
   648       | token_trans "_free" x = SOME (Pretty.marks_str (free_or_skolem ctxt x))
   649       | token_trans "_bound" x = SOME (Pretty.mark_str (Isabelle_Markup.bound, x))
   650       | token_trans "_loose" x = SOME (Pretty.mark_str (Isabelle_Markup.bad, x))
   651       | token_trans "_var" x = SOME (Pretty.mark_str (var_or_skolem x))
   652       | token_trans "_numeral" x = SOME (Pretty.mark_str (Isabelle_Markup.numeral, x))
   653       | token_trans "_inner_string" x = SOME (Pretty.mark_str (Isabelle_Markup.inner_string, x))
   654       | token_trans _ _ = NONE;
   655 
   656     fun markup_trans a [Ast.Variable x] = token_trans a x
   657       | markup_trans "_constrain" [t, ty] = constrain_trans t ty
   658       | markup_trans "_idtyp" [t, ty] = constrain_trans t ty
   659       | markup_trans _ _ = NONE
   660 
   661     and constrain_trans t ty =
   662       if not show_types andalso show_markup then
   663         let
   664           val ((bg1, bg2), en) = typing_elem;
   665           val bg = bg1 ^ Pretty.symbolic_output (pretty_typ_ast Markup.empty ty) ^ bg2;
   666         in SOME (Pretty.raw_markup (bg, en) (0, [pretty_ast Markup.empty t])) end
   667       else NONE
   668 
   669     and pretty_typ_ast m ast = ast
   670       |> Printer.pretty_typ_ast ctxt prtabs trf markup_trans markup_extern
   671       |> Pretty.markup m
   672 
   673     and pretty_ast m ast = ast
   674       |> prt_t ctxt prtabs trf markup_trans markup_extern
   675       |> Pretty.markup m;
   676   in
   677     t_to_ast ctxt (Syntax.print_translation syn) t
   678     |> Ast.normalize ctxt (Syntax.print_rules syn)
   679     |> pretty_ast markup
   680   end;
   681 
   682 in
   683 
   684 val unparse_sort = unparse_t sort_to_ast Printer.pretty_typ_ast Isabelle_Markup.sort;
   685 val unparse_typ = unparse_t typ_to_ast Printer.pretty_typ_ast Isabelle_Markup.typ;
   686 
   687 fun unparse_term ctxt =
   688   let
   689     val thy = Proof_Context.theory_of ctxt;
   690     val syn = Proof_Context.syn_of ctxt;
   691     val idents = Local_Syntax.idents_of (Proof_Context.syntax_of ctxt);
   692   in
   693     unparse_t (term_to_ast idents (is_some o Syntax.lookup_const syn))
   694       (Printer.pretty_term_ast (not (Pure_Thy.old_appl_syntax thy)))
   695       Isabelle_Markup.term ctxt
   696   end;
   697 
   698 end;
   699 
   700 
   701 
   702 (** translations **)
   703 
   704 (* type propositions *)
   705 
   706 fun type_prop_tr' ctxt T [Const ("\\<^const>Pure.sort_constraint", _)] =
   707       Syntax.const "_sort_constraint" $ term_of_typ (Config.put show_sorts true ctxt) T
   708   | type_prop_tr' ctxt T [t] =
   709       Syntax.const "_ofclass" $ term_of_typ ctxt T $ t
   710   | type_prop_tr' _ T ts = raise TYPE ("type_prop_tr'", [T], ts);
   711 
   712 
   713 (* type reflection *)
   714 
   715 fun type_tr' ctxt (Type ("itself", [T])) ts =
   716       Term.list_comb (Syntax.const "_TYPE" $ term_of_typ ctxt T, ts)
   717   | type_tr' _ _ _ = raise Match;
   718 
   719 
   720 (* type constraints *)
   721 
   722 fun type_constraint_tr' ctxt (Type ("fun", [T, _])) (t :: ts) =
   723       Term.list_comb (Syntax.const "_constrain" $ t $ term_of_typ ctxt T, ts)
   724   | type_constraint_tr' _ _ _ = raise Match;
   725 
   726 
   727 (* authentic syntax *)
   728 
   729 fun const_ast_tr intern ctxt [Ast.Variable c] =
   730       let
   731         val Const (c', _) = Proof_Context.read_const_proper ctxt false c;
   732         val d = if intern then Lexicon.mark_const c' else c;
   733       in Ast.Constant d end
   734   | const_ast_tr intern ctxt [Ast.Appl [Ast.Constant "_constrain", x, T as Ast.Variable pos]] =
   735       (Ast.Appl [Ast.Constant "_constrain", const_ast_tr intern ctxt [x], T]
   736         handle ERROR msg =>
   737           error (msg ^ Position.here (the_default Position.none (Term_Position.decode pos))))
   738   | const_ast_tr _ _ asts = raise Ast.AST ("const_ast_tr", asts);
   739 
   740 
   741 (* setup translations *)
   742 
   743 val _ = Context.>> (Context.map_theory
   744  (Sign.add_advanced_trfuns
   745   ([("_context_const", const_ast_tr true),
   746     ("_context_xconst", const_ast_tr false)], [], [], []) #>
   747   Sign.add_advanced_trfunsT
   748    [("_type_prop", type_prop_tr'),
   749     ("\\<^const>TYPE", type_tr'),
   750     ("_type_constraint_", type_constraint_tr')]));
   751 
   752 
   753 
   754 (** check/uncheck **)
   755 
   756 (* context-sensitive (un)checking *)
   757 
   758 type key = int * bool;
   759 
   760 structure Checks = Generic_Data
   761 (
   762   type 'a check = 'a list -> Proof.context -> ('a list * Proof.context) option;
   763   type T =
   764     ((key * ((string * typ check) * stamp) list) list *
   765      (key * ((string * term check) * stamp) list) list);
   766   val empty = ([], []);
   767   val extend = I;
   768   fun merge ((typ_checks1, term_checks1), (typ_checks2, term_checks2)) : T =
   769     (AList.join (op =) (K (Library.merge (eq_snd (op =)))) (typ_checks1, typ_checks2),
   770      AList.join (op =) (K (Library.merge (eq_snd (op =)))) (term_checks1, term_checks2));
   771 );
   772 
   773 fun print_checks ctxt =
   774   let
   775     fun split_checks checks =
   776       List.partition (fn ((_, un), _) => not un) checks
   777       |> pairself (map (fn ((i, _), fs) => (i, map (fst o fst) fs))
   778           #> sort (int_ord o pairself fst));
   779     fun pretty_checks kind checks =
   780       checks |> map (fn (i, names) => Pretty.block
   781         [Pretty.str (kind ^ " (stage " ^ signed_string_of_int i ^ "):"),
   782           Pretty.brk 1, Pretty.strs names]);
   783 
   784     val (typs, terms) = Checks.get (Context.Proof ctxt);
   785     val (typ_checks, typ_unchecks) = split_checks typs;
   786     val (term_checks, term_unchecks) = split_checks terms;
   787   in
   788     pretty_checks "typ_checks" typ_checks @
   789     pretty_checks "term_checks" term_checks @
   790     pretty_checks "typ_unchecks" typ_unchecks @
   791     pretty_checks "term_unchecks" term_unchecks
   792   end |> Pretty.chunks |> Pretty.writeln;
   793 
   794 
   795 local
   796 
   797 fun context_check which (key: key) name f =
   798   Checks.map (which (AList.map_default op = (key, []) (cons ((name, f), stamp ()))));
   799 
   800 fun simple_check eq f xs ctxt =
   801   let val xs' = f ctxt xs
   802   in if eq_list eq (xs, xs') then NONE else SOME (xs', ctxt) end;
   803 
   804 in
   805 
   806 fun typ_check' stage = context_check apfst (stage, false);
   807 fun term_check' stage = context_check apsnd (stage, false);
   808 fun typ_uncheck' stage = context_check apfst (stage, true);
   809 fun term_uncheck' stage = context_check apsnd (stage, true);
   810 
   811 fun typ_check key name f = typ_check' key name (simple_check (op =) f);
   812 fun term_check key name f = term_check' key name (simple_check (op aconv) f);
   813 fun typ_uncheck key name f = typ_uncheck' key name (simple_check (op =) f);
   814 fun term_uncheck key name f = term_uncheck' key name (simple_check (op aconv) f);
   815 
   816 end;
   817 
   818 
   819 local
   820 
   821 fun check_stage fs = perhaps_loop (perhaps_apply (map uncurry fs));
   822 fun check_all fs = perhaps_apply (map check_stage fs);
   823 
   824 fun check which uncheck ctxt0 xs0 =
   825   let
   826     val funs = which (Checks.get (Context.Proof ctxt0))
   827       |> map_filter (fn ((i, u), fs) => if uncheck = u then SOME (i, map (snd o fst) fs) else NONE)
   828       |> Library.sort (int_ord o pairself fst) |> map snd
   829       |> not uncheck ? map rev;
   830   in #1 (perhaps (check_all funs) (xs0, ctxt0)) end;
   831 
   832 val apply_typ_check = check fst false;
   833 val apply_term_check = check snd false;
   834 val apply_typ_uncheck = check fst true;
   835 val apply_term_uncheck = check snd true;
   836 
   837 in
   838 
   839 fun check_typs ctxt raw_tys =
   840   let
   841     val (sorting_report, tys) = Proof_Context.prepare_sortsT ctxt raw_tys;
   842     val _ = Context_Position.if_visible ctxt Output.report sorting_report;
   843   in
   844     tys
   845     |> apply_typ_check ctxt
   846     |> Term_Sharing.typs (Proof_Context.theory_of ctxt)
   847   end;
   848 
   849 fun check_terms ctxt raw_ts =
   850   let
   851     val (sorting_report, raw_ts') = Proof_Context.prepare_sorts ctxt raw_ts;
   852     val (ts, ps) = Type_Infer_Context.prepare_positions ctxt raw_ts';
   853 
   854     val tys = map (Logic.mk_type o snd) ps;
   855     val (ts', tys') = ts @ tys
   856       |> apply_term_check ctxt
   857       |> chop (length ts);
   858     val typing_report =
   859       fold2 (fn (pos, _) => fn ty =>
   860         if Position.is_reported pos then
   861           cons (Position.reported_text pos Isabelle_Markup.typing
   862             (Syntax.string_of_typ ctxt (Logic.dest_type ty)))
   863         else I) ps tys' []
   864       |> implode;
   865 
   866     val _ = Context_Position.if_visible ctxt Output.report (sorting_report ^ typing_report);
   867   in Term_Sharing.terms (Proof_Context.theory_of ctxt) ts' end;
   868 
   869 fun check_props ctxt = map (Type.constraint propT) #> check_terms ctxt;
   870 
   871 val uncheck_typs = apply_typ_uncheck;
   872 val uncheck_terms = apply_term_uncheck;
   873 
   874 end;
   875 
   876 
   877 (* standard phases *)
   878 
   879 val _ = Context.>>
   880  (typ_check 0 "standard" Proof_Context.standard_typ_check #>
   881   term_check 0 "standard"
   882     (fn ctxt => Type_Infer_Context.infer_types ctxt #> map (Proof_Context.expand_abbrevs ctxt)) #>
   883   term_check 100 "standard_finish" Proof_Context.standard_term_check_finish #>
   884   term_uncheck 0 "standard" Proof_Context.standard_term_uncheck);
   885 
   886 
   887 
   888 (** install operations **)
   889 
   890 val _ = Syntax.install_operations
   891   {parse_sort = parse_sort,
   892    parse_typ = parse_typ,
   893    parse_term = parse_term false,
   894    parse_prop = parse_term true,
   895    unparse_sort = unparse_sort,
   896    unparse_typ = unparse_typ,
   897    unparse_term = unparse_term,
   898    check_typs = check_typs,
   899    check_terms = check_terms,
   900    check_props = check_props,
   901    uncheck_typs = uncheck_typs,
   902    uncheck_terms = uncheck_terms};
   903 
   904 end;