src/Pure/Syntax/syntax_phases.ML
author wenzelm
Thu Mar 06 13:44:01 2014 +0100 (2014-03-06)
changeset 55954 a29aefc88c8d
parent 55953 b19373bd7ea8
child 55955 e8f1bf005661
permissions -rw-r--r--
more uniform check_const/read_const;
     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_text list * term Exn.result -> Position.report_text 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 Name.is_skolem x then Markup.skolem else 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) Markup.var];
    60 
    61 fun markup_bound def ps (name, id) =
    62   let val entity = Markup.entity Markup.boundN name in
    63     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 trf parsetree =
   140   let
   141     val reports = Unsynchronized.ref ([]: Position.report_text 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 ast_of_position tok =
   155       Ast.Variable (Term_Position.encode (Lexicon.pos_of_token tok));
   156 
   157     fun ast_of_dummy a tok =
   158       Ast.Appl [Ast.Constant "_constrain", Ast.Constant a, ast_of_position tok];
   159 
   160     fun asts_of_position c tok =
   161       [Ast.Appl [Ast.Constant c, ast_of (Parser.Tip tok), ast_of_position tok]]
   162 
   163     and asts_of (Parser.Node ("_class_name", [Parser.Tip tok])) =
   164           let
   165             val pos = Lexicon.pos_of_token tok;
   166             val (c, rs) = Proof_Context.check_class ctxt (Lexicon.str_of_token tok, pos);
   167             val _ = Unsynchronized.change reports (append (map (rpair "") rs));
   168           in [Ast.Constant (Lexicon.mark_class c)] end
   169       | asts_of (Parser.Node ("_type_name", [Parser.Tip tok])) =
   170           let
   171             val pos = Lexicon.pos_of_token tok;
   172             val (Type (c, _), rs) =
   173               Proof_Context.check_type_name ctxt {proper = true, strict = false}
   174                 (Lexicon.str_of_token tok, pos);
   175             val _ = Unsynchronized.change reports (append (map (rpair "") rs));
   176           in [Ast.Constant (Lexicon.mark_type c)] end
   177       | asts_of (Parser.Node ("_position", [Parser.Tip tok])) = asts_of_position "_constrain" tok
   178       | asts_of (Parser.Node ("_position_sort", [Parser.Tip tok])) = asts_of_position "_ofsort" tok
   179       | asts_of (Parser.Node (a as "\\<^const>dummy_pattern", [Parser.Tip tok])) =
   180           [ast_of_dummy a tok]
   181       | asts_of (Parser.Node (a as "_idtdummy", [Parser.Tip tok])) =
   182           [ast_of_dummy a tok]
   183       | asts_of (Parser.Node ("_idtypdummy", pts as [Parser.Tip tok, _, _])) =
   184           [Ast.Appl (Ast.Constant "_constrain" :: ast_of_dummy "_idtdummy" tok :: maps asts_of pts)]
   185       | asts_of (Parser.Node (a, pts)) =
   186           let
   187             val _ = pts |> List.app
   188               (fn Parser.Node _ => () | Parser.Tip tok =>
   189                 if Lexicon.valued_token tok then ()
   190                 else report (Lexicon.pos_of_token tok) (markup_entity ctxt) a);
   191           in [trans a (maps asts_of pts)] end
   192       | asts_of (Parser.Tip tok) = asts_of_token tok
   193 
   194     and ast_of pt =
   195       (case asts_of pt of
   196         [ast] => ast
   197       | asts => raise Ast.AST ("parsetree_to_ast: malformed parsetree", asts));
   198 
   199     val ast = Exn.interruptible_capture ast_of parsetree;
   200   in (! reports, ast) end;
   201 
   202 
   203 (* ast_to_term *)
   204 
   205 fun ast_to_term ctxt trf =
   206   let
   207     fun trans a args =
   208       (case trf a of
   209         NONE => Term.list_comb (Syntax.const a, args)
   210       | SOME f => f ctxt args);
   211 
   212     fun term_of (Ast.Constant a) = trans a []
   213       | term_of (Ast.Variable x) = Lexicon.read_var x
   214       | term_of (Ast.Appl (Ast.Constant a :: (asts as _ :: _))) =
   215           trans a (map term_of asts)
   216       | term_of (Ast.Appl (ast :: (asts as _ :: _))) =
   217           Term.list_comb (term_of ast, map term_of asts)
   218       | term_of (ast as Ast.Appl _) = raise Ast.AST ("ast_to_term: malformed ast", [ast]);
   219   in term_of end;
   220 
   221 
   222 (* decode_term -- transform parse tree into raw term *)
   223 
   224 fun decode_const ctxt c =
   225   let
   226     val (Const (c', _), _) =
   227       Proof_Context.check_const ctxt {proper = true, strict = false} dummyT (c, Position.none);
   228   in c' end;
   229 
   230 fun decode_term _ (result as (_: Position.report_text list, Exn.Exn _)) = result
   231   | decode_term ctxt (reports0, Exn.Res tm) =
   232       let
   233         fun get_const c =
   234           (true, decode_const ctxt c) handle ERROR _ =>
   235             (false, Consts.intern (Proof_Context.consts_of ctxt) c);
   236         fun get_free x =
   237           let
   238             val fixed = Variable.lookup_fixed ctxt x;
   239             val is_const = can (decode_const ctxt) x orelse Long_Name.is_qualified x;
   240             val is_declared = is_some (Variable.def_type ctxt false (x, ~1));
   241           in
   242             if Variable.is_const ctxt x then NONE
   243             else if is_some fixed then fixed
   244             else if not is_const orelse is_declared then SOME x
   245             else NONE
   246           end;
   247 
   248         val reports = Unsynchronized.ref reports0;
   249         fun report ps = Position.store_reports reports ps;
   250 
   251         fun decode ps qs bs (Const ("_constrain", _) $ t $ typ) =
   252               (case Term_Position.decode_position typ of
   253                 SOME (p, T) => Type.constraint T (decode (p :: ps) qs bs t)
   254               | NONE => Type.constraint (decode_typ typ) (decode ps qs bs t))
   255           | decode ps qs bs (Const ("_constrainAbs", _) $ t $ typ) =
   256               (case Term_Position.decode_position typ of
   257                 SOME (q, T) => Type.constraint (T --> dummyT) (decode ps (q :: qs) bs t)
   258               | NONE => Type.constraint (decode_typ typ --> dummyT) (decode ps qs bs t))
   259           | decode _ qs bs (Abs (x, T, t)) =
   260               let
   261                 val id = serial ();
   262                 val _ = report qs (markup_bound true qs) (x, id);
   263               in Abs (x, T, decode [] [] ((qs, (x, id)) :: bs) t) end
   264           | decode _ _ bs (t $ u) = decode [] [] bs t $ decode [] [] bs u
   265           | decode ps _ _ (Const (a, T)) =
   266               (case try Lexicon.unmark_fixed a of
   267                 SOME x => (report ps (markup_free ctxt) x; Free (x, T))
   268               | NONE =>
   269                   let
   270                     val c =
   271                       (case try Lexicon.unmark_const a of
   272                         SOME c => c
   273                       | NONE => snd (get_const a));
   274                     val _ = report ps (markup_const ctxt) c;
   275                   in Const (c, T) end)
   276           | decode ps _ _ (Free (a, T)) =
   277               (Name.reject_internal (a, ps);
   278                case (get_free a, get_const a) of
   279                 (SOME x, _) => (report ps (markup_free ctxt) x; Free (x, T))
   280               | (_, (true, c)) => (report ps (markup_const ctxt) c; Const (c, T))
   281               | (_, (false, c)) =>
   282                   if Long_Name.is_qualified c
   283                   then (report ps (markup_const ctxt) c; Const (c, T))
   284                   else (report ps (markup_free ctxt) c; Free (c, T)))
   285           | decode ps _ _ (Var (xi, T)) = (report ps markup_var xi; Var (xi, T))
   286           | decode ps _ bs (t as Bound i) =
   287               (case try (nth bs) i of
   288                 SOME (qs, (x, id)) => (report ps (markup_bound false qs) (x, id); t)
   289               | NONE => t);
   290 
   291         val tm' = Exn.interruptible_capture (fn () => decode [] [] [] tm) ();
   292       in (! reports, tm') end;
   293 
   294 
   295 
   296 (** parse **)
   297 
   298 (* results *)
   299 
   300 fun proper_results results = map_filter (fn (y, Exn.Res x) => SOME (y, x) | _ => NONE) results;
   301 fun failed_results results = map_filter (fn (y, Exn.Exn e) => SOME (y, e) | _ => NONE) results;
   302 
   303 fun report_result ctxt pos ambig_msgs results =
   304   (case (proper_results results, failed_results results) of
   305     ([], (reports, exn) :: _) => (Context_Position.reports_text ctxt reports; reraise exn)
   306   | ([(reports, x)], _) => (Context_Position.reports_text ctxt reports; x)
   307   | _ =>
   308       if null ambig_msgs then
   309         error ("Parse error: ambiguous syntax" ^ Position.here pos)
   310       else error (cat_lines ambig_msgs));
   311 
   312 
   313 (* parse raw asts *)
   314 
   315 fun parse_asts ctxt raw root (syms, pos) =
   316   let
   317     val syn = Proof_Context.syn_of ctxt;
   318     val ast_tr = Syntax.parse_ast_translation syn;
   319 
   320     val toks = Syntax.tokenize syn raw syms;
   321     val _ = Context_Position.reports ctxt (map Lexicon.report_of_token toks);
   322 
   323     val pts = Syntax.parse syn root (filter Lexicon.is_proper toks)
   324       handle ERROR msg =>
   325         error (msg ^
   326           implode (map (Markup.markup Markup.report o Lexicon.reported_token_range ctxt) toks));
   327     val len = length pts;
   328 
   329     val limit = Config.get ctxt Syntax.ambiguity_limit;
   330     val ambig_msgs =
   331       if len <= 1 then []
   332       else
   333         [cat_lines
   334           (("Ambiguous input" ^ Position.here (Position.reset_range pos) ^
   335             "\nproduces " ^ string_of_int len ^ " parse trees" ^
   336             (if len <= limit then "" else " (" ^ string_of_int limit ^ " displayed)") ^ ":") ::
   337             map (Pretty.string_of o Pretty.item o single o Parser.pretty_parsetree)
   338               (take limit pts))];
   339 
   340   in (ambig_msgs, map (parsetree_to_ast ctxt ast_tr) pts) end;
   341 
   342 fun parse_tree ctxt root input =
   343   let
   344     val syn = Proof_Context.syn_of ctxt;
   345     val tr = Syntax.parse_translation syn;
   346     val parse_rules = Syntax.parse_rules syn;
   347     val (ambig_msgs, asts) = parse_asts ctxt false root input;
   348     val results =
   349       (map o apsnd o Exn.maps_result)
   350         (Ast.normalize ctxt parse_rules #> Exn.interruptible_capture (ast_to_term ctxt tr)) asts;
   351   in (ambig_msgs, results) end;
   352 
   353 
   354 (* parse logical entities *)
   355 
   356 fun parse_failed ctxt pos msg kind =
   357   cat_error msg ("Failed to parse " ^ kind ^
   358     Markup.markup Markup.report (Context_Position.reported_text ctxt pos Markup.bad ""));
   359 
   360 fun parse_sort ctxt =
   361   Syntax.parse_token ctxt Term_XML.Decode.sort Markup.language_sort
   362     (fn (syms, pos) =>
   363       parse_tree ctxt "sort" (syms, pos)
   364       |> uncurry (report_result ctxt pos)
   365       |> decode_sort
   366       |> Type.minimize_sort (Proof_Context.tsig_of ctxt)
   367       handle ERROR msg => parse_failed ctxt pos msg "sort");
   368 
   369 fun parse_typ ctxt =
   370   Syntax.parse_token ctxt Term_XML.Decode.typ Markup.language_type
   371     (fn (syms, pos) =>
   372       parse_tree ctxt "type" (syms, pos)
   373       |> uncurry (report_result ctxt pos)
   374       |> decode_typ
   375       handle ERROR msg => parse_failed ctxt pos msg "type");
   376 
   377 fun parse_term is_prop ctxt =
   378   let
   379     val (markup, kind, root, constrain) =
   380       if is_prop
   381       then (Markup.language_prop, "prop", "prop", Type.constraint propT)
   382       else (Markup.language_term, "term", Config.get ctxt Syntax.root, I);
   383     val decode = constrain o Term_XML.Decode.term;
   384   in
   385     Syntax.parse_token ctxt decode markup
   386       (fn (syms, pos) =>
   387         let
   388           val (ambig_msgs, results) = parse_tree ctxt root (syms, pos) ||> map (decode_term ctxt);
   389           val parsed_len = length (proper_results results);
   390 
   391           val ambiguity_warning = Config.get ctxt Syntax.ambiguity_warning;
   392           val limit = Config.get ctxt Syntax.ambiguity_limit;
   393 
   394           (*brute-force disambiguation via type-inference*)
   395           fun check t = (Syntax.check_term ctxt (constrain t); Exn.Res t)
   396             handle exn as ERROR _ => Exn.Exn exn;
   397 
   398           val results' =
   399             if parsed_len > 1 then
   400               (grouped 10 (Par_List.map_name "Syntax_Phases.parse_term") o apsnd o Exn.maps_result)
   401                 check results
   402             else results;
   403           val reports' = fst (hd results');
   404 
   405           val errs = map snd (failed_results results');
   406           val checked = map snd (proper_results results');
   407           val checked_len = length checked;
   408 
   409           val pretty_term = Syntax.pretty_term (Config.put Printer.show_brackets true ctxt);
   410         in
   411           if checked_len = 0 then
   412             report_result ctxt pos []
   413               [(reports', Exn.Exn (Exn.EXCEPTIONS (map ERROR ambig_msgs @ errs)))]
   414           else if checked_len = 1 then
   415             (if parsed_len > 1 andalso ambiguity_warning then
   416               Context_Position.if_visible ctxt warning
   417                 (cat_lines (ambig_msgs @
   418                   ["Fortunately, only one parse tree is type correct" ^
   419                   Position.here (Position.reset_range pos) ^
   420                   ",\nbut you may still want to disambiguate your grammar or your input."]))
   421              else (); report_result ctxt pos [] results')
   422           else
   423             report_result ctxt pos []
   424               [(reports', Exn.Exn (ERROR (cat_lines (ambig_msgs @
   425                 (("Ambiguous input\n" ^ string_of_int checked_len ^ " terms are type correct" ^
   426                   (if checked_len <= limit then ""
   427                    else " (" ^ string_of_int limit ^ " displayed)") ^ ":") ::
   428                   map (Pretty.string_of o Pretty.item o single o pretty_term)
   429                     (take limit checked))))))]
   430         end handle ERROR msg => parse_failed ctxt pos msg kind)
   431   end;
   432 
   433 
   434 (* parse_ast_pattern *)
   435 
   436 fun parse_ast_pattern ctxt (root, str) =
   437   let
   438     val syn = Proof_Context.syn_of ctxt;
   439 
   440     val reports = Unsynchronized.ref ([]: Position.report_text list);
   441     fun report ps = Position.store_reports reports ps;
   442 
   443     fun decode_const ps c = (report ps (markup_entity ctxt) c; Ast.Constant c);
   444     fun decode_var ps x = (report ps (fn () => [Markup.name x Markup.free]) (); Ast.Variable x);
   445     fun decode_appl ps asts = Ast.Appl (map (decode ps) asts)
   446     and decode ps (Ast.Constant c) = decode_const ps c
   447       | decode ps (Ast.Variable x) =
   448           if is_some (Syntax.lookup_const syn x) orelse Long_Name.is_qualified x
   449           then decode_const ps x
   450           else decode_var ps x
   451       | decode ps (Ast.Appl (asts as (Ast.Constant c :: ast :: Ast.Variable x :: args))) =
   452           if member (op =) Term_Position.markers c then
   453             (case Term_Position.decode x of
   454               SOME p => Ast.mk_appl (decode (p :: ps) ast) (map (decode ps) args)
   455             | NONE => decode_appl ps asts)
   456           else decode_appl ps asts
   457       | decode ps (Ast.Appl asts) = decode_appl ps asts;
   458 
   459     val {text, pos, ...} = Syntax.read_token str;
   460     val syms = Symbol_Pos.explode (text, pos);
   461     val ast =
   462       parse_asts ctxt true root (syms, pos)
   463       |> uncurry (report_result ctxt pos)
   464       |> decode [];
   465     val _ = Context_Position.reports_text ctxt (! reports);
   466   in ast end;
   467 
   468 
   469 
   470 (** encode parse trees **)
   471 
   472 (* term_of_sort *)
   473 
   474 fun term_of_sort S =
   475   let
   476     val class = Syntax.const o Lexicon.mark_class;
   477 
   478     fun classes [c] = class c
   479       | classes (c :: cs) = Syntax.const "_classes" $ class c $ classes cs;
   480   in
   481     (case S of
   482       [] => Syntax.const "_topsort"
   483     | [c] => class c
   484     | cs => Syntax.const "_sort" $ classes cs)
   485   end;
   486 
   487 
   488 (* term_of_typ *)
   489 
   490 fun term_of_typ ctxt ty =
   491   let
   492     val show_sorts = Config.get ctxt show_sorts orelse Config.get ctxt show_markup;
   493 
   494     fun ofsort t raw_S =
   495       if show_sorts then
   496         let val S = #2 (Term_Position.decode_positionS raw_S)
   497         in if S = dummyS then t else Syntax.const "_ofsort" $ t $ term_of_sort S end
   498       else t;
   499 
   500     fun term_of (Type (a, Ts)) =
   501           Term.list_comb (Syntax.const (Lexicon.mark_type a), map term_of Ts)
   502       | term_of (TFree (x, S)) =
   503           if is_some (Term_Position.decode x) then Syntax.free x
   504           else ofsort (Syntax.const "_tfree" $ Syntax.free x) S
   505       | term_of (TVar (xi, S)) = ofsort (Syntax.const "_tvar" $ Syntax.var xi) S;
   506   in term_of ty end;
   507 
   508 
   509 (* simple_ast_of *)
   510 
   511 fun simple_ast_of ctxt =
   512   let
   513     val tune_var = if Config.get ctxt show_question_marks then I else unprefix "?";
   514     fun ast_of (Const (c, _)) = Ast.Constant c
   515       | ast_of (Free (x, _)) = Ast.Variable x
   516       | ast_of (Var (xi, _)) = Ast.Variable (tune_var (Term.string_of_vname xi))
   517       | ast_of (t as _ $ _) =
   518           let val (f, args) = strip_comb t
   519           in Ast.mk_appl (ast_of f) (map ast_of args) end
   520       | ast_of (Bound i) = Ast.Appl [Ast.Constant "_loose", Ast.Variable ("B." ^ string_of_int i)]
   521       | ast_of (Abs _) = raise Fail "simple_ast_of: Abs";
   522   in ast_of end;
   523 
   524 
   525 (* sort_to_ast and typ_to_ast *)
   526 
   527 fun ast_of_termT ctxt trf tm =
   528   let
   529     val ctxt' = Config.put show_sorts false ctxt;
   530     fun ast_of (t as Const ("_tfree", _) $ Free _) = simple_ast_of ctxt t
   531       | ast_of (t as Const ("_tvar", _) $ Var _) = simple_ast_of ctxt t
   532       | ast_of (Const (a, _)) = trans a []
   533       | ast_of (t as _ $ _) =
   534           (case strip_comb t of
   535             (Const (a, _), args) => trans a args
   536           | (f, args) => Ast.Appl (map ast_of (f :: args)))
   537       | ast_of t = simple_ast_of ctxt t
   538     and trans a args = ast_of (trf a ctxt' dummyT args)
   539       handle Match => Ast.mk_appl (Ast.Constant a) (map ast_of args);
   540   in ast_of tm end;
   541 
   542 fun sort_to_ast ctxt trf S = ast_of_termT ctxt trf (term_of_sort S);
   543 fun typ_to_ast ctxt trf T = ast_of_termT ctxt trf (term_of_typ ctxt T);
   544 
   545 
   546 (* term_to_ast *)
   547 
   548 local
   549 
   550 fun mark_aprop tm =
   551   let
   552     fun aprop t = Syntax.const "_aprop" $ t;
   553 
   554     fun is_prop Ts t =
   555       Type_Annotation.clean (Type_Annotation.fastype_of Ts t) = propT
   556         handle TERM _ => false;
   557 
   558     fun is_term (Const ("Pure.term", _) $ _) = true
   559       | is_term _ = false;
   560 
   561     fun mark _ (t as Const _) = t
   562       | mark Ts (t as Const ("_bound", _) $ u) = if is_prop Ts u then aprop t else t
   563       | mark Ts (t as Free _) = if is_prop Ts t then aprop t else t
   564       | mark Ts (t as Var _) = if is_prop Ts t then aprop t else t
   565       | mark Ts (t as Bound _) = if is_prop Ts t then aprop t else t
   566       | mark Ts (Abs (x, T, t)) = Abs (x, T, mark (T :: Ts) t)
   567       | mark Ts (t as t1 $ (t2 as Const ("TYPE", Type ("itself", [T])))) =
   568           if is_prop Ts t andalso not (is_term t) then Const ("_type_prop", T) $ mark Ts t1
   569           else mark Ts t1 $ mark Ts t2
   570       | mark Ts (t as t1 $ t2) =
   571           (if is_Const (Term.head_of t) orelse not (is_prop Ts t) then I else aprop)
   572             (mark Ts t1 $ mark Ts t2);
   573   in mark [] tm end;
   574 
   575 fun prune_types ctxt tm =
   576   let
   577     fun regard t t' seen =
   578       if Type_Annotation.is_omitted (Type_Annotation.fastype_of [] t) then (t, seen)
   579       else if member (op aconv) seen t then (t', seen)
   580       else (t, t :: seen);
   581 
   582     fun prune (t as Const _, seen) = (t, seen)
   583       | prune (t as Free (x, T), seen) = regard t (Free (x, Type_Annotation.ignore_type T)) seen
   584       | prune (t as Var (xi, T), seen) = regard t (Var (xi, Type_Annotation.ignore_type T)) seen
   585       | prune (t as Bound _, seen) = (t, seen)
   586       | prune (Abs (x, T, t), seen) =
   587           let val (t', seen') = prune (t, seen);
   588           in (Abs (x, T, t'), seen') end
   589       | prune (t1 $ t2, seen) =
   590           let
   591             val (t1', seen') = prune (t1, seen);
   592             val (t2', seen'') = prune (t2, seen');
   593           in (t1' $ t2', seen'') end;
   594   in #1 (prune (tm, [])) end;
   595 
   596 fun mark_atoms {structs, fixes} is_syntax_const ctxt tm =
   597   let
   598     val show_structs = Config.get ctxt show_structs;
   599 
   600     fun mark ((t as Const (c, _)) $ u) =
   601           if member (op =) Pure_Thy.token_markers c
   602           then t $ u else mark t $ mark u
   603       | mark (t $ u) = mark t $ mark u
   604       | mark (Abs (x, T, t)) = Abs (x, T, mark t)
   605       | mark (t as Const (c, T)) =
   606           if is_syntax_const c then t
   607           else Const (Lexicon.mark_const c, T)
   608       | mark (t as Free (x, T)) =
   609           let val i = find_index (fn s => s = x) structs + 1 in
   610             if i = 0 andalso member (op =) fixes x then
   611               Const (Lexicon.mark_fixed x, T)
   612             else if i = 1 andalso not show_structs then
   613               Syntax.const "_struct" $ Syntax.const "_indexdefault"
   614             else Syntax.const "_free" $ t
   615           end
   616       | mark (t as Var (xi, T)) =
   617           if xi = Syntax_Ext.dddot_indexname then Const ("_DDDOT", T)
   618           else Syntax.const "_var" $ t
   619       | mark a = a;
   620   in mark tm end;
   621 
   622 in
   623 
   624 fun term_to_ast idents is_syntax_const ctxt trf tm =
   625   let
   626     val show_types = Config.get ctxt show_types orelse Config.get ctxt show_sorts;
   627     val show_markup = Config.get ctxt show_markup;
   628 
   629     fun ast_of tm =
   630       (case strip_comb tm of
   631         (t as Abs _, ts) => Ast.mk_appl (ast_of (Syntax_Trans.abs_tr' ctxt t)) (map ast_of ts)
   632       | ((c as Const ("_free", _)), Free (x, T) :: ts) =>
   633           Ast.mk_appl (constrain (c $ Syntax.free x) T) (map ast_of ts)
   634       | ((c as Const ("_var", _)), Var (xi, T) :: ts) =>
   635           Ast.mk_appl (constrain (c $ Syntax.var xi) T) (map ast_of ts)
   636       | ((c as Const ("_bound", B)), Free (x, T) :: ts) =>
   637           let
   638             val X =
   639               if show_markup andalso not show_types orelse B <> dummyT then T
   640               else dummyT;
   641           in Ast.mk_appl (constrain (c $ Syntax.free x) X) (map ast_of ts) end
   642       | (Const ("_idtdummy", T), ts) =>
   643           Ast.mk_appl (constrain (Syntax.const "_idtdummy") T) (map ast_of ts)
   644       | (const as Const (c, T), ts) => trans c (Type_Annotation.smash T) ts
   645       | (t, ts) => Ast.mk_appl (simple_ast_of ctxt t) (map ast_of ts))
   646 
   647     and trans a T args = ast_of (trf a ctxt T args)
   648       handle Match => Ast.mk_appl (Ast.Constant a) (map ast_of args)
   649 
   650     and constrain t T0 =
   651       let
   652         val T =
   653           if show_markup andalso not show_types
   654           then Type_Annotation.clean T0
   655           else Type_Annotation.smash T0;
   656       in
   657         if (show_types orelse show_markup) andalso T <> dummyT then
   658           Ast.Appl [Ast.Constant "_constrain", simple_ast_of ctxt t,
   659             ast_of_termT ctxt trf (term_of_typ ctxt T)]
   660         else simple_ast_of ctxt t
   661       end;
   662   in
   663     tm
   664     |> mark_aprop
   665     |> show_types ? prune_types ctxt
   666     |> Variable.revert_bounds ctxt
   667     |> mark_atoms idents is_syntax_const ctxt
   668     |> ast_of
   669   end;
   670 
   671 end;
   672 
   673 
   674 
   675 (** unparse **)
   676 
   677 local
   678 
   679 fun free_or_skolem ctxt x =
   680   let
   681     val m =
   682       if Variable.is_fixed ctxt x orelse Syntax.is_pretty_global ctxt
   683       then Markup.fixed x else Markup.intensify;
   684   in
   685     if Name.is_skolem x
   686     then ([m, Markup.skolem], Variable.revert_fixed ctxt x)
   687     else ([m, Markup.free], x)
   688   end;
   689 
   690 fun var_or_skolem s =
   691   (case Lexicon.read_variable s of
   692     SOME (x, i) =>
   693       (case try Name.dest_skolem x of
   694         NONE => (Markup.var, s)
   695       | SOME x' => (Markup.skolem, Term.string_of_vname (x', i)))
   696   | NONE => (Markup.var, s));
   697 
   698 val typing_elem = YXML.output_markup_elem Markup.typing;
   699 val sorting_elem = YXML.output_markup_elem Markup.sorting;
   700 
   701 fun unparse_t t_to_ast prt_t markup ctxt t =
   702   let
   703     val show_markup = Config.get ctxt show_markup;
   704     val show_sorts = Config.get ctxt show_sorts;
   705     val show_types = Config.get ctxt show_types orelse show_sorts;
   706 
   707     val syn = Proof_Context.syn_of ctxt;
   708     val prtabs = Syntax.prtabs syn;
   709     val trf = Syntax.print_ast_translation syn;
   710 
   711     fun markup_extern c =
   712       (case Syntax.lookup_const syn c of
   713         SOME "" => ([], c)
   714       | SOME b => markup_extern b
   715       | NONE => c |> Lexicon.unmark
   716          {case_class = fn x => (markup_class ctxt x, Proof_Context.extern_class ctxt x),
   717           case_type = fn x => (markup_type ctxt x, Proof_Context.extern_type ctxt x),
   718           case_const = fn x => (markup_const ctxt x, Proof_Context.extern_const ctxt x),
   719           case_fixed = fn x => free_or_skolem ctxt x,
   720           case_default = fn x => ([], x)});
   721 
   722     fun token_trans "_tfree" x = SOME (Pretty.mark_str (Markup.tfree, x))
   723       | token_trans "_tvar" x = SOME (Pretty.mark_str (Markup.tvar, x))
   724       | token_trans "_free" x = SOME (Pretty.marks_str (free_or_skolem ctxt x))
   725       | token_trans "_bound" x = SOME (Pretty.mark_str (Markup.bound, x))
   726       | token_trans "_loose" x = SOME (Pretty.mark_str (Markup.bad, x))
   727       | token_trans "_var" x = SOME (Pretty.mark_str (var_or_skolem x))
   728       | token_trans "_numeral" x = SOME (Pretty.mark_str (Markup.numeral, x))
   729       | token_trans "_inner_string" x = SOME (Pretty.mark_str (Markup.inner_string, x))
   730       | token_trans _ _ = NONE;
   731 
   732     fun markup_trans a [Ast.Variable x] = token_trans a x
   733       | markup_trans "_constrain" [t, ty] = constrain_trans t ty
   734       | markup_trans "_idtyp" [t, ty] = constrain_trans t ty
   735       | markup_trans "_ofsort" [ty, s] = ofsort_trans ty s
   736       | markup_trans _ _ = NONE
   737 
   738     and constrain_trans t ty =
   739       if show_markup andalso not show_types then
   740         let
   741           val ((bg1, bg2), en) = typing_elem;
   742           val bg = bg1 ^ Pretty.symbolic_output (pretty_typ_ast Markup.empty ty) ^ bg2;
   743         in SOME (Pretty.raw_markup (bg, en) (0, [pretty_ast Markup.empty t])) end
   744       else NONE
   745 
   746     and ofsort_trans ty s =
   747       if show_markup andalso not show_sorts then
   748         let
   749           val ((bg1, bg2), en) = sorting_elem;
   750           val bg = bg1 ^ Pretty.symbolic_output (pretty_typ_ast Markup.empty s) ^ bg2;
   751         in SOME (Pretty.raw_markup (bg, en) (0, [pretty_typ_ast Markup.empty ty])) end
   752       else NONE
   753 
   754     and pretty_typ_ast m ast = ast
   755       |> Printer.pretty_typ_ast ctxt prtabs trf markup_trans markup_extern
   756       |> Pretty.markup m
   757 
   758     and pretty_ast m ast = ast
   759       |> prt_t ctxt prtabs trf markup_trans markup_extern
   760       |> Pretty.markup m;
   761   in
   762     t_to_ast ctxt (Syntax.print_translation syn) t
   763     |> Ast.normalize ctxt (Syntax.print_rules syn)
   764     |> pretty_ast markup
   765   end;
   766 
   767 in
   768 
   769 val unparse_sort = unparse_t sort_to_ast Printer.pretty_typ_ast (Markup.language_sort false);
   770 val unparse_typ = unparse_t typ_to_ast Printer.pretty_typ_ast (Markup.language_type false);
   771 
   772 fun unparse_term ctxt =
   773   let
   774     val thy = Proof_Context.theory_of ctxt;
   775     val syn = Proof_Context.syn_of ctxt;
   776     val idents = Local_Syntax.idents_of (Proof_Context.syntax_of ctxt);
   777   in
   778     unparse_t (term_to_ast idents (is_some o Syntax.lookup_const syn))
   779       (Printer.pretty_term_ast (not (Pure_Thy.old_appl_syntax thy)))
   780       (Markup.language_term false) ctxt
   781   end;
   782 
   783 end;
   784 
   785 
   786 
   787 (** translations **)
   788 
   789 (* type propositions *)
   790 
   791 fun type_prop_tr' ctxt T [Const ("\\<^const>Pure.sort_constraint", _)] =
   792       Syntax.const "_sort_constraint" $ term_of_typ (Config.put show_sorts true ctxt) T
   793   | type_prop_tr' ctxt T [t] =
   794       Syntax.const "_ofclass" $ term_of_typ ctxt T $ t
   795   | type_prop_tr' _ T ts = raise TYPE ("type_prop_tr'", [T], ts);
   796 
   797 
   798 (* type reflection *)
   799 
   800 fun type_tr' ctxt (Type ("itself", [T])) ts =
   801       Term.list_comb (Syntax.const "_TYPE" $ term_of_typ ctxt T, ts)
   802   | type_tr' _ _ _ = raise Match;
   803 
   804 
   805 (* type constraints *)
   806 
   807 fun type_constraint_tr' ctxt (Type ("fun", [T, _])) (t :: ts) =
   808       Term.list_comb (Syntax.const "_constrain" $ t $ term_of_typ ctxt T, ts)
   809   | type_constraint_tr' _ _ _ = raise Match;
   810 
   811 
   812 (* authentic syntax *)
   813 
   814 fun const_ast_tr intern ctxt [Ast.Variable c] =
   815       let
   816         val c' = decode_const ctxt c;
   817         val d = if intern then Lexicon.mark_const c' else c;
   818       in Ast.Constant d end
   819   | const_ast_tr intern ctxt [Ast.Appl [Ast.Constant "_constrain", x, T as Ast.Variable pos]] =
   820       (Ast.Appl [Ast.Constant "_constrain", const_ast_tr intern ctxt [x], T]
   821         handle ERROR msg =>
   822           error (msg ^ Position.here (the_default Position.none (Term_Position.decode pos))))
   823   | const_ast_tr _ _ asts = raise Ast.AST ("const_ast_tr", asts);
   824 
   825 
   826 (* setup translations *)
   827 
   828 val _ = Theory.setup
   829  (Sign.parse_ast_translation
   830    [("_context_const", const_ast_tr true),
   831     ("_context_xconst", const_ast_tr false)] #>
   832   Sign.typed_print_translation
   833    [("_type_prop", type_prop_tr'),
   834     ("\\<^const>TYPE", type_tr'),
   835     ("_type_constraint_", type_constraint_tr')]);
   836 
   837 
   838 
   839 (** check/uncheck **)
   840 
   841 (* context-sensitive (un)checking *)
   842 
   843 type key = int * bool;
   844 
   845 structure Checks = Generic_Data
   846 (
   847   type 'a check = 'a list -> Proof.context -> ('a list * Proof.context) option;
   848   type T =
   849     ((key * ((string * typ check) * stamp) list) list *
   850      (key * ((string * term check) * stamp) list) list);
   851   val empty = ([], []);
   852   val extend = I;
   853   fun merge ((typ_checks1, term_checks1), (typ_checks2, term_checks2)) : T =
   854     (AList.join (op =) (K (Library.merge (eq_snd (op =)))) (typ_checks1, typ_checks2),
   855      AList.join (op =) (K (Library.merge (eq_snd (op =)))) (term_checks1, term_checks2));
   856 );
   857 
   858 fun print_checks ctxt =
   859   let
   860     fun split_checks checks =
   861       List.partition (fn ((_, un), _) => not un) checks
   862       |> pairself (map (fn ((i, _), fs) => (i, map (fst o fst) fs))
   863           #> sort (int_ord o pairself fst));
   864     fun pretty_checks kind checks =
   865       checks |> map (fn (i, names) => Pretty.block
   866         [Pretty.str (kind ^ " (stage " ^ signed_string_of_int i ^ "):"),
   867           Pretty.brk 1, Pretty.strs names]);
   868 
   869     val (typs, terms) = Checks.get (Context.Proof ctxt);
   870     val (typ_checks, typ_unchecks) = split_checks typs;
   871     val (term_checks, term_unchecks) = split_checks terms;
   872   in
   873     pretty_checks "typ_checks" typ_checks @
   874     pretty_checks "term_checks" term_checks @
   875     pretty_checks "typ_unchecks" typ_unchecks @
   876     pretty_checks "term_unchecks" term_unchecks
   877   end |> Pretty.chunks |> Pretty.writeln;
   878 
   879 
   880 local
   881 
   882 fun context_check which (key: key) name f =
   883   Checks.map (which (AList.map_default op = (key, []) (cons ((name, f), stamp ()))));
   884 
   885 fun simple_check eq f xs ctxt =
   886   let val xs' = f ctxt xs
   887   in if eq_list eq (xs, xs') then NONE else SOME (xs', ctxt) end;
   888 
   889 in
   890 
   891 fun typ_check' stage = context_check apfst (stage, false);
   892 fun term_check' stage = context_check apsnd (stage, false);
   893 fun typ_uncheck' stage = context_check apfst (stage, true);
   894 fun term_uncheck' stage = context_check apsnd (stage, true);
   895 
   896 fun typ_check key name f = typ_check' key name (simple_check (op =) f);
   897 fun term_check key name f = term_check' key name (simple_check (op aconv) f);
   898 fun typ_uncheck key name f = typ_uncheck' key name (simple_check (op =) f);
   899 fun term_uncheck key name f = term_uncheck' key name (simple_check (op aconv) f);
   900 
   901 end;
   902 
   903 
   904 local
   905 
   906 fun check_stage fs = perhaps_loop (perhaps_apply (map uncurry fs));
   907 fun check_all fs = perhaps_apply (map check_stage fs);
   908 
   909 fun check which uncheck ctxt0 xs0 =
   910   let
   911     val funs = which (Checks.get (Context.Proof ctxt0))
   912       |> map_filter (fn ((i, u), fs) => if uncheck = u then SOME (i, map (snd o fst) fs) else NONE)
   913       |> Library.sort (int_ord o pairself fst) |> map snd
   914       |> not uncheck ? map rev;
   915   in #1 (perhaps (check_all funs) (xs0, ctxt0)) end;
   916 
   917 val apply_typ_check = check fst false;
   918 val apply_term_check = check snd false;
   919 val apply_typ_uncheck = check fst true;
   920 val apply_term_uncheck = check snd true;
   921 
   922 in
   923 
   924 fun check_typs ctxt raw_tys =
   925   let
   926     val (sorting_report, tys) = Proof_Context.prepare_sortsT ctxt raw_tys;
   927     val _ = Context_Position.if_visible ctxt Output.report sorting_report;
   928   in
   929     tys
   930     |> apply_typ_check ctxt
   931     |> Term_Sharing.typs (Proof_Context.theory_of ctxt)
   932   end;
   933 
   934 fun check_terms ctxt raw_ts =
   935   let
   936     val (sorting_report, raw_ts') = Proof_Context.prepare_sorts ctxt raw_ts;
   937     val (ts, ps) = Type_Infer_Context.prepare_positions ctxt raw_ts';
   938 
   939     val tys = map (Logic.mk_type o snd) ps;
   940     val (ts', tys') = ts @ tys
   941       |> apply_term_check ctxt
   942       |> chop (length ts);
   943     val typing_report =
   944       fold2 (fn (pos, _) => fn ty =>
   945         if Position.is_reported pos then
   946           cons (Position.reported_text pos Markup.typing
   947             (Syntax.string_of_typ ctxt (Logic.dest_type ty)))
   948         else I) ps tys' []
   949       |> implode;
   950 
   951     val _ = Context_Position.if_visible ctxt Output.report (sorting_report ^ typing_report);
   952   in Term_Sharing.terms (Proof_Context.theory_of ctxt) ts' end;
   953 
   954 fun check_props ctxt = map (Type.constraint propT) #> check_terms ctxt;
   955 
   956 val uncheck_typs = apply_typ_uncheck;
   957 val uncheck_terms = apply_term_uncheck;
   958 
   959 end;
   960 
   961 
   962 (* standard phases *)
   963 
   964 val _ = Context.>>
   965  (typ_check 0 "standard" Proof_Context.standard_typ_check #>
   966   term_check 0 "standard"
   967     (fn ctxt => Type_Infer_Context.infer_types ctxt #> map (Proof_Context.expand_abbrevs ctxt)) #>
   968   term_check 100 "standard_finish" Proof_Context.standard_term_check_finish #>
   969   term_uncheck 0 "standard" Proof_Context.standard_term_uncheck);
   970 
   971 
   972 
   973 (** install operations **)
   974 
   975 val _ = Syntax.install_operations
   976   {parse_sort = parse_sort,
   977    parse_typ = parse_typ,
   978    parse_term = parse_term false,
   979    parse_prop = parse_term true,
   980    unparse_sort = unparse_sort,
   981    unparse_typ = unparse_typ,
   982    unparse_term = unparse_term,
   983    check_typs = check_typs,
   984    check_terms = check_terms,
   985    check_props = check_props,
   986    uncheck_typs = uncheck_typs,
   987    uncheck_terms = uncheck_terms};
   988 
   989 end;