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