src/Pure/Syntax/syntax_phases.ML
author wenzelm
Wed Apr 27 20:37:56 2011 +0200 (2011-04-27)
changeset 42493 01430341fc79
parent 42488 4638622bcaa1
child 43548 f231a7594e54
permissions -rw-r--r--
more informative markup for fixed variables (via name space entry);
uniform markup for undeclared entities;
tuned;
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@42242
    10
  val term_sorts: term -> (indexname * sort) list
wenzelm@42242
    11
  val typ_of_term: (indexname -> sort) -> term -> typ
wenzelm@42242
    12
  val decode_term: Proof.context ->
wenzelm@42242
    13
    Position.reports * term Exn.result -> Position.reports * 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@42242
    16
end
wenzelm@42242
    17
wenzelm@42243
    18
structure Syntax_Phases: SYNTAX_PHASES =
wenzelm@42241
    19
struct
wenzelm@42241
    20
wenzelm@42298
    21
(** markup logical entities **)
wenzelm@42298
    22
wenzelm@42298
    23
fun markup_class ctxt c =
wenzelm@42379
    24
  [Name_Space.markup (Type.class_space (Proof_Context.tsig_of ctxt)) c];
wenzelm@42298
    25
wenzelm@42298
    26
fun markup_type ctxt c =
wenzelm@42379
    27
  [Name_Space.markup (Type.type_space (Proof_Context.tsig_of ctxt)) c];
wenzelm@42298
    28
wenzelm@42298
    29
fun markup_const ctxt c =
wenzelm@42379
    30
  [Name_Space.markup (Consts.space_of (Proof_Context.consts_of ctxt)) c];
wenzelm@42298
    31
wenzelm@42298
    32
fun markup_free ctxt x =
wenzelm@42298
    33
  [if can Name.dest_skolem x then Markup.skolem else Markup.free] @
wenzelm@42493
    34
  (if Variable.is_body ctxt orelse Variable.is_fixed ctxt x
wenzelm@42493
    35
   then [Variable.markup_fixed ctxt x]
wenzelm@42493
    36
   else []);
wenzelm@42298
    37
wenzelm@42298
    38
fun markup_var xi = [Markup.name (Term.string_of_vname xi) Markup.var];
wenzelm@42298
    39
wenzelm@42298
    40
fun markup_bound def id =
wenzelm@42298
    41
  [Markup.properties [(if def then Markup.defN else Markup.refN, id)] Markup.bound];
wenzelm@42298
    42
wenzelm@42298
    43
fun markup_entity ctxt c =
wenzelm@42360
    44
  (case Syntax.lookup_const (Proof_Context.syn_of ctxt) c of
wenzelm@42298
    45
    SOME "" => []
wenzelm@42298
    46
  | SOME b => markup_entity ctxt b
wenzelm@42298
    47
  | NONE => c |> Lexicon.unmark
wenzelm@42298
    48
     {case_class = markup_class ctxt,
wenzelm@42298
    49
      case_type = markup_type ctxt,
wenzelm@42298
    50
      case_const = markup_const ctxt,
wenzelm@42298
    51
      case_fixed = markup_free ctxt,
wenzelm@42298
    52
      case_default = K []});
wenzelm@42298
    53
wenzelm@42298
    54
wenzelm@42298
    55
wenzelm@42242
    56
(** decode parse trees **)
wenzelm@42242
    57
wenzelm@42242
    58
(* sort_of_term *)
wenzelm@42242
    59
wenzelm@42242
    60
fun sort_of_term tm =
wenzelm@42242
    61
  let
wenzelm@42242
    62
    fun err () = raise TERM ("sort_of_term: bad encoding of classes", [tm]);
wenzelm@42242
    63
wenzelm@42242
    64
    fun class s = Lexicon.unmark_class s handle Fail _ => err ();
wenzelm@42242
    65
wenzelm@42242
    66
    fun classes (Const (s, _)) = [class s]
wenzelm@42242
    67
      | classes (Const ("_classes", _) $ Const (s, _) $ cs) = class s :: classes cs
wenzelm@42242
    68
      | classes _ = err ();
wenzelm@42242
    69
wenzelm@42242
    70
    fun sort (Const ("_topsort", _)) = []
wenzelm@42242
    71
      | sort (Const (s, _)) = [class s]
wenzelm@42242
    72
      | sort (Const ("_sort", _) $ cs) = classes cs
wenzelm@42242
    73
      | sort _ = err ();
wenzelm@42242
    74
  in sort tm end;
wenzelm@42242
    75
wenzelm@42242
    76
wenzelm@42242
    77
(* term_sorts *)
wenzelm@42242
    78
wenzelm@42242
    79
fun term_sorts tm =
wenzelm@42242
    80
  let
wenzelm@42242
    81
    val sort_of = sort_of_term;
wenzelm@42242
    82
wenzelm@42242
    83
    fun add_env (Const ("_ofsort", _) $ Free (x, _) $ cs) =
wenzelm@42242
    84
          insert (op =) ((x, ~1), sort_of cs)
wenzelm@42242
    85
      | add_env (Const ("_ofsort", _) $ (Const ("_tfree", _) $ Free (x, _)) $ cs) =
wenzelm@42242
    86
          insert (op =) ((x, ~1), sort_of cs)
wenzelm@42242
    87
      | add_env (Const ("_ofsort", _) $ Var (xi, _) $ cs) =
wenzelm@42242
    88
          insert (op =) (xi, sort_of cs)
wenzelm@42242
    89
      | add_env (Const ("_ofsort", _) $ (Const ("_tvar", _) $ Var (xi, _)) $ cs) =
wenzelm@42242
    90
          insert (op =) (xi, sort_of cs)
wenzelm@42242
    91
      | add_env (Abs (_, _, t)) = add_env t
wenzelm@42242
    92
      | add_env (t1 $ t2) = add_env t1 #> add_env t2
wenzelm@42242
    93
      | add_env _ = I;
wenzelm@42242
    94
  in add_env tm [] end;
wenzelm@42242
    95
wenzelm@42242
    96
wenzelm@42242
    97
(* typ_of_term *)
wenzelm@42242
    98
wenzelm@42242
    99
fun typ_of_term get_sort tm =
wenzelm@42242
   100
  let
wenzelm@42242
   101
    fun err () = raise TERM ("typ_of_term: bad encoding of type", [tm]);
wenzelm@42242
   102
wenzelm@42242
   103
    fun typ_of (Free (x, _)) = TFree (x, get_sort (x, ~1))
wenzelm@42242
   104
      | typ_of (Var (xi, _)) = TVar (xi, get_sort xi)
wenzelm@42242
   105
      | typ_of (Const ("_tfree",_) $ (t as Free _)) = typ_of t
wenzelm@42242
   106
      | typ_of (Const ("_tvar",_) $ (t as Var _)) = typ_of t
wenzelm@42242
   107
      | typ_of (Const ("_ofsort", _) $ Free (x, _) $ _) = TFree (x, get_sort (x, ~1))
wenzelm@42242
   108
      | typ_of (Const ("_ofsort", _) $ (Const ("_tfree",_) $ Free (x, _)) $ _) =
wenzelm@42242
   109
          TFree (x, get_sort (x, ~1))
wenzelm@42242
   110
      | typ_of (Const ("_ofsort", _) $ Var (xi, _) $ _) = TVar (xi, get_sort xi)
wenzelm@42242
   111
      | typ_of (Const ("_ofsort", _) $ (Const ("_tvar",_) $ Var (xi, _)) $ _) =
wenzelm@42242
   112
          TVar (xi, get_sort xi)
wenzelm@42242
   113
      | typ_of (Const ("_dummy_ofsort", _) $ t) = TFree ("'_dummy_", sort_of_term t)
wenzelm@42242
   114
      | typ_of t =
wenzelm@42242
   115
          let
wenzelm@42242
   116
            val (head, args) = Term.strip_comb t;
wenzelm@42242
   117
            val a =
wenzelm@42242
   118
              (case head of
wenzelm@42242
   119
                Const (c, _) => (Lexicon.unmark_type c handle Fail _ => err ())
wenzelm@42242
   120
              | _ => err ());
wenzelm@42242
   121
          in Type (a, map typ_of args) end;
wenzelm@42242
   122
  in typ_of tm end;
wenzelm@42242
   123
wenzelm@42242
   124
wenzelm@42242
   125
(* parsetree_to_ast *)
wenzelm@42242
   126
wenzelm@42242
   127
fun parsetree_to_ast ctxt constrain_pos trf parsetree =
wenzelm@42242
   128
  let
wenzelm@42242
   129
    val reports = Unsynchronized.ref ([]: Position.reports);
wenzelm@42242
   130
    fun report pos = Position.reports reports [pos];
wenzelm@42242
   131
wenzelm@42242
   132
    fun trans a args =
wenzelm@42242
   133
      (case trf a of
wenzelm@42242
   134
        NONE => Ast.mk_appl (Ast.Constant a) args
wenzelm@42242
   135
      | SOME f => f ctxt args);
wenzelm@42242
   136
wenzelm@42282
   137
    fun asts_of (Parser.Node ("_class_name", [Parser.Tip tok])) =
wenzelm@42242
   138
          let
wenzelm@42470
   139
            val pos = Lexicon.pos_of_token tok;
wenzelm@42470
   140
            val c = Proof_Context.read_class ctxt (Lexicon.str_of_token tok)
wenzelm@42470
   141
              handle ERROR msg => error (msg ^ Position.str_of pos);
wenzelm@42470
   142
            val _ = report pos (markup_class ctxt) c;
wenzelm@42282
   143
          in [Ast.Constant (Lexicon.mark_class c)] end
wenzelm@42282
   144
      | asts_of (Parser.Node ("_type_name", [Parser.Tip tok])) =
wenzelm@42242
   145
          let
wenzelm@42470
   146
            val pos = Lexicon.pos_of_token tok;
wenzelm@42470
   147
            val Type (c, _) =
wenzelm@42470
   148
              Proof_Context.read_type_name_proper ctxt false (Lexicon.str_of_token tok)
wenzelm@42470
   149
                handle ERROR msg => error (msg ^ Position.str_of pos);
wenzelm@42470
   150
            val _ = report pos (markup_type ctxt) c;
wenzelm@42282
   151
          in [Ast.Constant (Lexicon.mark_type c)] end
wenzelm@42410
   152
      | asts_of (Parser.Node ("_position", [pt as Parser.Tip tok])) =
wenzelm@42242
   153
          if constrain_pos then
wenzelm@42282
   154
            [Ast.Appl [Ast.Constant "_constrain", ast_of pt,
wenzelm@42282
   155
              Ast.Variable (Term_Position.encode (Lexicon.pos_of_token tok))]]
wenzelm@42282
   156
          else [ast_of pt]
wenzelm@42282
   157
      | asts_of (Parser.Node (a, pts)) =
wenzelm@42282
   158
          let
wenzelm@42282
   159
            val _ = pts |> List.app
wenzelm@42282
   160
              (fn Parser.Node _ => () | Parser.Tip tok =>
wenzelm@42282
   161
                if Lexicon.valued_token tok then ()
wenzelm@42298
   162
                else report (Lexicon.pos_of_token tok) (markup_entity ctxt) a);
wenzelm@42282
   163
          in [trans a (maps asts_of pts)] end
wenzelm@42282
   164
      | asts_of (Parser.Tip tok) =
wenzelm@42282
   165
          if Lexicon.valued_token tok
wenzelm@42282
   166
          then [Ast.Variable (Lexicon.str_of_token tok)]
wenzelm@42282
   167
          else []
wenzelm@42282
   168
wenzelm@42282
   169
    and ast_of pt =
wenzelm@42282
   170
      (case asts_of pt of
wenzelm@42282
   171
        [ast] => ast
wenzelm@42282
   172
      | asts => raise Ast.AST ("parsetree_to_ast: malformed parsetree", asts));
wenzelm@42242
   173
wenzelm@42242
   174
    val ast = Exn.interruptible_capture ast_of parsetree;
wenzelm@42242
   175
  in (! reports, ast) end;
wenzelm@42242
   176
wenzelm@42242
   177
wenzelm@42242
   178
(* ast_to_term *)
wenzelm@42242
   179
wenzelm@42242
   180
fun ast_to_term ctxt trf =
wenzelm@42242
   181
  let
wenzelm@42242
   182
    fun trans a args =
wenzelm@42242
   183
      (case trf a of
wenzelm@42476
   184
        NONE => Term.list_comb (Syntax.const a, args)
wenzelm@42242
   185
      | SOME f => f ctxt args);
wenzelm@42242
   186
wenzelm@42242
   187
    fun term_of (Ast.Constant a) = trans a []
wenzelm@42242
   188
      | term_of (Ast.Variable x) = Lexicon.read_var x
wenzelm@42242
   189
      | term_of (Ast.Appl (Ast.Constant a :: (asts as _ :: _))) =
wenzelm@42242
   190
          trans a (map term_of asts)
wenzelm@42242
   191
      | term_of (Ast.Appl (ast :: (asts as _ :: _))) =
wenzelm@42242
   192
          Term.list_comb (term_of ast, map term_of asts)
wenzelm@42242
   193
      | term_of (ast as Ast.Appl _) = raise Ast.AST ("ast_to_term: malformed ast", [ast]);
wenzelm@42242
   194
  in term_of end;
wenzelm@42242
   195
wenzelm@42242
   196
wenzelm@42242
   197
(* decode_term -- transform parse tree into raw term *)
wenzelm@42242
   198
wenzelm@42242
   199
fun decode_term _ (result as (_: Position.reports, Exn.Exn _)) = result
wenzelm@42242
   200
  | decode_term ctxt (reports0, Exn.Result tm) =
wenzelm@42242
   201
      let
wenzelm@42250
   202
        fun get_const a =
wenzelm@42360
   203
          ((true, #1 (Term.dest_Const (Proof_Context.read_const_proper ctxt false a)))
wenzelm@42360
   204
            handle ERROR _ => (false, Consts.intern (Proof_Context.consts_of ctxt) a));
wenzelm@42360
   205
        val get_free = Proof_Context.intern_skolem ctxt;
wenzelm@42250
   206
wenzelm@42360
   207
        val decodeT = typ_of_term (Proof_Context.get_sort ctxt (term_sorts tm));
wenzelm@42242
   208
wenzelm@42242
   209
        val reports = Unsynchronized.ref reports0;
wenzelm@42242
   210
        fun report ps = Position.reports reports ps;
wenzelm@42242
   211
wenzelm@42242
   212
        fun decode ps qs bs (Const ("_constrain", _) $ t $ typ) =
wenzelm@42264
   213
              (case Term_Position.decode_position typ of
wenzelm@42242
   214
                SOME p => decode (p :: ps) qs bs t
wenzelm@42242
   215
              | NONE => Type.constraint (decodeT typ) (decode ps qs bs t))
wenzelm@42242
   216
          | decode ps qs bs (Const ("_constrainAbs", _) $ t $ typ) =
wenzelm@42264
   217
              (case Term_Position.decode_position typ of
wenzelm@42242
   218
                SOME q => decode ps (q :: qs) bs t
wenzelm@42242
   219
              | NONE => Type.constraint (decodeT typ --> dummyT) (decode ps qs bs t))
wenzelm@42242
   220
          | decode _ qs bs (Abs (x, T, t)) =
wenzelm@42242
   221
              let
wenzelm@42242
   222
                val id = serial_string ();
wenzelm@42242
   223
                val _ = report qs (markup_bound true) id;
wenzelm@42242
   224
              in Abs (x, T, decode [] [] (id :: bs) t) end
wenzelm@42242
   225
          | decode _ _ bs (t $ u) = decode [] [] bs t $ decode [] [] bs u
wenzelm@42242
   226
          | decode ps _ _ (Const (a, T)) =
wenzelm@42242
   227
              (case try Lexicon.unmark_fixed a of
wenzelm@42282
   228
                SOME x => (report ps (markup_free ctxt) x; Free (x, T))
wenzelm@42242
   229
              | NONE =>
wenzelm@42242
   230
                  let
wenzelm@42242
   231
                    val c =
wenzelm@42242
   232
                      (case try Lexicon.unmark_const a of
wenzelm@42242
   233
                        SOME c => c
wenzelm@42242
   234
                      | NONE => snd (get_const a));
wenzelm@42282
   235
                    val _ = report ps (markup_const ctxt) c;
wenzelm@42242
   236
                  in Const (c, T) end)
wenzelm@42242
   237
          | decode ps _ _ (Free (a, T)) =
wenzelm@42242
   238
              (case (get_free a, get_const a) of
wenzelm@42282
   239
                (SOME x, _) => (report ps (markup_free ctxt) x; Free (x, T))
wenzelm@42282
   240
              | (_, (true, c)) => (report ps (markup_const ctxt) c; Const (c, T))
wenzelm@42242
   241
              | (_, (false, c)) =>
wenzelm@42242
   242
                  if Long_Name.is_qualified c
wenzelm@42282
   243
                  then (report ps (markup_const ctxt) c; Const (c, T))
wenzelm@42282
   244
                  else (report ps (markup_free ctxt) c; Free (c, T)))
wenzelm@42242
   245
          | decode ps _ _ (Var (xi, T)) = (report ps markup_var xi; Var (xi, T))
wenzelm@42242
   246
          | decode ps _ bs (t as Bound i) =
wenzelm@42242
   247
              (case try (nth bs) i of
wenzelm@42242
   248
                SOME id => (report ps (markup_bound false) id; t)
wenzelm@42242
   249
              | NONE => t);
wenzelm@42242
   250
wenzelm@42242
   251
        val tm' = Exn.interruptible_capture (fn () => decode [] [] [] tm) ();
wenzelm@42242
   252
      in (! reports, tm') end;
wenzelm@42242
   253
wenzelm@42242
   254
wenzelm@42242
   255
wenzelm@42242
   256
(** parse **)
wenzelm@42242
   257
wenzelm@42242
   258
(* results *)
wenzelm@42242
   259
wenzelm@42242
   260
fun ambiguity_msg pos = "Parse error: ambiguous syntax" ^ Position.str_of pos;
wenzelm@42242
   261
wenzelm@42242
   262
fun proper_results results = map_filter (fn (y, Exn.Result x) => SOME (y, x) | _ => NONE) results;
wenzelm@42242
   263
fun failed_results results = map_filter (fn (y, Exn.Exn e) => SOME (y, e) | _ => NONE) results;
wenzelm@42242
   264
wenzelm@42242
   265
fun report ctxt = List.app (fn (pos, m) => Context_Position.report ctxt pos m);
wenzelm@42242
   266
wenzelm@42242
   267
fun report_result ctxt pos results =
wenzelm@42242
   268
  (case (proper_results results, failed_results results) of
wenzelm@42242
   269
    ([], (reports, exn) :: _) => (report ctxt reports; reraise exn)
wenzelm@42242
   270
  | ([(reports, x)], _) => (report ctxt reports; x)
wenzelm@42242
   271
  | _ => error (ambiguity_msg pos));
wenzelm@42242
   272
wenzelm@42242
   273
wenzelm@42249
   274
(* parse raw asts *)
wenzelm@42242
   275
wenzelm@42242
   276
fun parse_asts ctxt raw root (syms, pos) =
wenzelm@42242
   277
  let
wenzelm@42360
   278
    val syn = Proof_Context.syn_of ctxt;
wenzelm@42253
   279
    val ast_tr = Syntax.parse_ast_translation syn;
wenzelm@42242
   280
wenzelm@42251
   281
    val toks = Syntax.tokenize syn raw syms;
wenzelm@42242
   282
    val _ = List.app (Lexicon.report_token ctxt) toks;
wenzelm@42242
   283
wenzelm@42251
   284
    val pts = Syntax.parse ctxt syn root (filter Lexicon.is_proper toks)
wenzelm@42242
   285
      handle ERROR msg =>
wenzelm@42242
   286
        error (msg ^
wenzelm@42242
   287
          implode (map (Markup.markup Markup.report o Lexicon.reported_token_range ctxt) toks));
wenzelm@42242
   288
    val len = length pts;
wenzelm@42242
   289
wenzelm@42242
   290
    val limit = Config.get ctxt Syntax.ambiguity_limit;
wenzelm@42242
   291
    val _ =
wenzelm@42242
   292
      if len <= Config.get ctxt Syntax.ambiguity_level then ()
wenzelm@42242
   293
      else if not (Config.get ctxt Syntax.ambiguity_enabled) then error (ambiguity_msg pos)
wenzelm@42242
   294
      else
wenzelm@42242
   295
        (Context_Position.if_visible ctxt warning (cat_lines
wenzelm@42242
   296
          (("Ambiguous input" ^ Position.str_of pos ^
wenzelm@42242
   297
            "\nproduces " ^ string_of_int len ^ " parse trees" ^
wenzelm@42242
   298
            (if len <= limit then "" else " (" ^ string_of_int limit ^ " displayed)") ^ ":") ::
wenzelm@42242
   299
            map (Pretty.string_of o Parser.pretty_parsetree) (take limit pts))));
wenzelm@42242
   300
wenzelm@42242
   301
    val constrain_pos = not raw andalso Config.get ctxt Syntax.positions;
wenzelm@42253
   302
    val parsetree_to_ast = parsetree_to_ast ctxt constrain_pos ast_tr;
wenzelm@42242
   303
  in map parsetree_to_ast pts end;
wenzelm@42242
   304
wenzelm@42249
   305
fun parse_raw ctxt root input =
wenzelm@42242
   306
  let
wenzelm@42360
   307
    val syn = Proof_Context.syn_of ctxt;
wenzelm@42253
   308
    val tr = Syntax.parse_translation syn;
wenzelm@42255
   309
    val parse_rules = Syntax.parse_rules syn;
wenzelm@42242
   310
  in
wenzelm@42242
   311
    parse_asts ctxt false root input
wenzelm@42255
   312
    |> (map o apsnd o Exn.maps_result)
wenzelm@42255
   313
        (Ast.normalize ctxt parse_rules #> Exn.interruptible_capture (ast_to_term ctxt tr))
wenzelm@42242
   314
  end;
wenzelm@42242
   315
wenzelm@42242
   316
wenzelm@42249
   317
(* parse logical entities *)
wenzelm@42242
   318
wenzelm@42241
   319
fun parse_failed ctxt pos msg kind =
wenzelm@42241
   320
  cat_error msg ("Failed to parse " ^ kind ^
wenzelm@42241
   321
    Markup.markup Markup.report (Context_Position.reported_text ctxt pos Markup.bad ""));
wenzelm@42241
   322
wenzelm@42241
   323
fun parse_sort ctxt text =
wenzelm@42241
   324
  let
wenzelm@42241
   325
    val (syms, pos) = Syntax.parse_token ctxt Markup.sort text;
wenzelm@42249
   326
    val S =
wenzelm@42249
   327
      parse_raw ctxt "sort" (syms, pos)
wenzelm@42249
   328
      |> report_result ctxt pos
wenzelm@42249
   329
      |> sort_of_term
wenzelm@42242
   330
      handle ERROR msg => parse_failed ctxt pos msg "sort";
wenzelm@42360
   331
  in Type.minimize_sort (Proof_Context.tsig_of ctxt) S end;
wenzelm@42241
   332
wenzelm@42241
   333
fun parse_typ ctxt text =
wenzelm@42241
   334
  let
wenzelm@42241
   335
    val (syms, pos) = Syntax.parse_token ctxt Markup.typ text;
wenzelm@42249
   336
    val T =
wenzelm@42249
   337
      parse_raw ctxt "type" (syms, pos)
wenzelm@42249
   338
      |> report_result ctxt pos
wenzelm@42360
   339
      |> (fn t => typ_of_term (Proof_Context.get_sort ctxt (term_sorts t)) t)
wenzelm@42242
   340
      handle ERROR msg => parse_failed ctxt pos msg "type";
wenzelm@42241
   341
  in T end;
wenzelm@42241
   342
wenzelm@42281
   343
fun parse_term is_prop ctxt text =
wenzelm@42241
   344
  let
wenzelm@42281
   345
    val (markup, kind, root, constrain) =
wenzelm@42281
   346
      if is_prop
wenzelm@42281
   347
      then (Markup.prop, "proposition", "prop", Type.constraint propT)
wenzelm@42288
   348
      else (Markup.term, "term", Config.get ctxt Syntax.root, I);
wenzelm@42241
   349
    val (syms, pos) = Syntax.parse_token ctxt markup text;
wenzelm@42249
   350
  in
wenzelm@42249
   351
    let
wenzelm@42249
   352
      val results = parse_raw ctxt root (syms, pos) |> map (decode_term ctxt);
wenzelm@42249
   353
      val ambiguity = length (proper_results results);
wenzelm@42241
   354
wenzelm@42249
   355
      val level = Config.get ctxt Syntax.ambiguity_level;
wenzelm@42249
   356
      val limit = Config.get ctxt Syntax.ambiguity_limit;
wenzelm@42249
   357
wenzelm@42470
   358
      val ambig_msg =
wenzelm@42249
   359
        if ambiguity > 1 andalso ambiguity <= level then
wenzelm@42470
   360
          ["Got more than one parse tree.\n\
wenzelm@42470
   361
          \Retry with smaller syntax_ambiguity_level for more information."]
wenzelm@42470
   362
        else [];
wenzelm@42249
   363
wenzelm@42249
   364
      (*brute-force disambiguation via type-inference*)
wenzelm@42281
   365
      fun check t = (Syntax.check_term ctxt (constrain t); Exn.Result t)
wenzelm@42249
   366
        handle exn as ERROR _ => Exn.Exn exn;
wenzelm@42249
   367
wenzelm@42249
   368
      val results' =
wenzelm@42249
   369
        if ambiguity > 1 then
wenzelm@42249
   370
          (Par_List.map_name "Syntax_Phases.parse_term" o apsnd o Exn.maps_result)
wenzelm@42249
   371
            check results
wenzelm@42249
   372
        else results;
wenzelm@42249
   373
      val reports' = fst (hd results');
wenzelm@42249
   374
wenzelm@42249
   375
      val errs = map snd (failed_results results');
wenzelm@42249
   376
      val checked = map snd (proper_results results');
wenzelm@42249
   377
      val len = length checked;
wenzelm@42249
   378
wenzelm@42289
   379
      val show_term = Syntax.string_of_term (Config.put Printer.show_brackets true ctxt);
wenzelm@42249
   380
    in
wenzelm@42249
   381
      if len = 0 then
wenzelm@42249
   382
        report_result ctxt pos
wenzelm@42470
   383
          [(reports', Exn.Exn (Exn.EXCEPTIONS (map ERROR ambig_msg @ errs)))]
wenzelm@42249
   384
      else if len = 1 then
wenzelm@42249
   385
        (if ambiguity > level then
wenzelm@42249
   386
          Context_Position.if_visible ctxt warning
wenzelm@42249
   387
            "Fortunately, only one parse tree is type correct.\n\
wenzelm@42249
   388
            \You may still want to disambiguate your grammar or your input."
wenzelm@42249
   389
        else (); report_result ctxt pos results')
wenzelm@42249
   390
      else
wenzelm@42249
   391
        report_result ctxt pos
wenzelm@42470
   392
          [(reports', Exn.Exn (ERROR (cat_lines (ambig_msg @
wenzelm@42249
   393
            (("Ambiguous input, " ^ string_of_int len ^ " terms are type correct" ^
wenzelm@42249
   394
              (if len <= limit then "" else " (" ^ string_of_int limit ^ " displayed)") ^ ":") ::
wenzelm@42249
   395
              map show_term (take limit checked))))))]
wenzelm@42249
   396
    end handle ERROR msg => parse_failed ctxt pos msg kind
wenzelm@42249
   397
  end;
wenzelm@42241
   398
wenzelm@42241
   399
wenzelm@42242
   400
(* parse_ast_pattern *)
wenzelm@42242
   401
wenzelm@42242
   402
fun parse_ast_pattern ctxt (root, str) =
wenzelm@42242
   403
  let
wenzelm@42360
   404
    val syn = Proof_Context.syn_of ctxt;
wenzelm@42242
   405
wenzelm@42242
   406
    fun constify (ast as Ast.Constant _) = ast
wenzelm@42242
   407
      | constify (ast as Ast.Variable x) =
wenzelm@42298
   408
          if is_some (Syntax.lookup_const syn x) orelse Long_Name.is_qualified x
wenzelm@42298
   409
          then Ast.Constant x
wenzelm@42242
   410
          else ast
wenzelm@42242
   411
      | constify (Ast.Appl asts) = Ast.Appl (map constify asts);
wenzelm@42242
   412
wenzelm@42242
   413
    val (syms, pos) = Syntax.read_token str;
wenzelm@42242
   414
  in
wenzelm@42242
   415
    parse_asts ctxt true root (syms, pos)
wenzelm@42242
   416
    |> report_result ctxt pos
wenzelm@42242
   417
    |> constify
wenzelm@42242
   418
  end;
wenzelm@42242
   419
wenzelm@42242
   420
wenzelm@42242
   421
wenzelm@42245
   422
(** encode parse trees **)
wenzelm@42245
   423
wenzelm@42245
   424
(* term_of_sort *)
wenzelm@42245
   425
wenzelm@42245
   426
fun term_of_sort S =
wenzelm@42245
   427
  let
wenzelm@42476
   428
    val class = Syntax.const o Lexicon.mark_class;
wenzelm@42245
   429
wenzelm@42245
   430
    fun classes [c] = class c
wenzelm@42476
   431
      | classes (c :: cs) = Syntax.const "_classes" $ class c $ classes cs;
wenzelm@42245
   432
  in
wenzelm@42245
   433
    (case S of
wenzelm@42476
   434
      [] => Syntax.const "_topsort"
wenzelm@42245
   435
    | [c] => class c
wenzelm@42476
   436
    | cs => Syntax.const "_sort" $ classes cs)
wenzelm@42245
   437
  end;
wenzelm@42245
   438
wenzelm@42245
   439
wenzelm@42245
   440
(* term_of_typ *)
wenzelm@42245
   441
wenzelm@42247
   442
fun term_of_typ ctxt ty =
wenzelm@42245
   443
  let
wenzelm@42247
   444
    val show_sorts = Config.get ctxt show_sorts;
wenzelm@42247
   445
wenzelm@42245
   446
    fun of_sort t S =
wenzelm@42476
   447
      if show_sorts then Syntax.const "_ofsort" $ t $ term_of_sort S
wenzelm@42245
   448
      else t;
wenzelm@42245
   449
wenzelm@42245
   450
    fun term_of (Type (a, Ts)) =
wenzelm@42476
   451
          Term.list_comb (Syntax.const (Lexicon.mark_type a), map term_of Ts)
wenzelm@42245
   452
      | term_of (TFree (x, S)) =
wenzelm@42290
   453
          if is_some (Term_Position.decode x) then Syntax.free x
wenzelm@42476
   454
          else of_sort (Syntax.const "_tfree" $ Syntax.free x) S
wenzelm@42476
   455
      | term_of (TVar (xi, S)) = of_sort (Syntax.const "_tvar" $ Syntax.var xi) S;
wenzelm@42245
   456
  in term_of ty end;
wenzelm@42245
   457
wenzelm@42245
   458
wenzelm@42245
   459
(* simple_ast_of *)
wenzelm@42245
   460
wenzelm@42245
   461
fun simple_ast_of ctxt =
wenzelm@42245
   462
  let
wenzelm@42245
   463
    val tune_var = if Config.get ctxt show_question_marks then I else unprefix "?";
wenzelm@42245
   464
    fun ast_of (Const (c, _)) = Ast.Constant c
wenzelm@42245
   465
      | ast_of (Free (x, _)) = Ast.Variable x
wenzelm@42245
   466
      | ast_of (Var (xi, _)) = Ast.Variable (tune_var (Term.string_of_vname xi))
wenzelm@42245
   467
      | ast_of (t as _ $ _) =
wenzelm@42245
   468
          let val (f, args) = strip_comb t
wenzelm@42245
   469
          in Ast.mk_appl (ast_of f) (map ast_of args) end
wenzelm@42408
   470
      | ast_of (Bound i) = Ast.Appl [Ast.Constant "_loose", Ast.Variable ("B." ^ string_of_int i)]
wenzelm@42245
   471
      | ast_of (Abs _) = raise Fail "simple_ast_of: Abs";
wenzelm@42245
   472
  in ast_of end;
wenzelm@42245
   473
wenzelm@42245
   474
wenzelm@42245
   475
(* sort_to_ast and typ_to_ast *)
wenzelm@42245
   476
wenzelm@42245
   477
fun ast_of_termT ctxt trf tm =
wenzelm@42245
   478
  let
wenzelm@42245
   479
    val ctxt' = Config.put show_sorts false ctxt;
wenzelm@42245
   480
    fun ast_of (t as Const ("_tfree", _) $ Free _) = simple_ast_of ctxt t
wenzelm@42245
   481
      | ast_of (t as Const ("_tvar", _) $ Var _) = simple_ast_of ctxt t
wenzelm@42245
   482
      | ast_of (Const (a, _)) = trans a []
wenzelm@42245
   483
      | ast_of (t as _ $ _) =
wenzelm@42245
   484
          (case strip_comb t of
wenzelm@42245
   485
            (Const (a, _), args) => trans a args
wenzelm@42245
   486
          | (f, args) => Ast.Appl (map ast_of (f :: args)))
wenzelm@42245
   487
      | ast_of t = simple_ast_of ctxt t
wenzelm@42254
   488
    and trans a args = ast_of (trf a ctxt' dummyT args)
wenzelm@42254
   489
      handle Match => Ast.mk_appl (Ast.Constant a) (map ast_of args);
wenzelm@42245
   490
  in ast_of tm end;
wenzelm@42245
   491
wenzelm@42245
   492
fun sort_to_ast ctxt trf S = ast_of_termT ctxt trf (term_of_sort S);
wenzelm@42247
   493
fun typ_to_ast ctxt trf T = ast_of_termT ctxt trf (term_of_typ ctxt T);
wenzelm@42245
   494
wenzelm@42245
   495
wenzelm@42245
   496
(* term_to_ast *)
wenzelm@42245
   497
wenzelm@42252
   498
fun term_to_ast idents is_syntax_const ctxt trf tm =
wenzelm@42245
   499
  let
wenzelm@42245
   500
    val show_types =
wenzelm@42245
   501
      Config.get ctxt show_types orelse Config.get ctxt show_sorts orelse
wenzelm@42245
   502
      Config.get ctxt show_all_types;
wenzelm@42245
   503
    val show_structs = Config.get ctxt show_structs;
wenzelm@42245
   504
    val show_free_types = Config.get ctxt show_free_types;
wenzelm@42245
   505
    val show_all_types = Config.get ctxt show_all_types;
wenzelm@42245
   506
wenzelm@42245
   507
    val {structs, fixes} = idents;
wenzelm@42245
   508
wenzelm@42249
   509
    fun mark_atoms ((t as Const (c, _)) $ u) =
wenzelm@42294
   510
          if member (op =) Syntax.token_markers c
wenzelm@42245
   511
          then t $ u else mark_atoms t $ mark_atoms u
wenzelm@42245
   512
      | mark_atoms (t $ u) = mark_atoms t $ mark_atoms u
wenzelm@42245
   513
      | mark_atoms (Abs (x, T, t)) = Abs (x, T, mark_atoms t)
wenzelm@42245
   514
      | mark_atoms (t as Const (c, T)) =
wenzelm@42252
   515
          if is_syntax_const c then t
wenzelm@42245
   516
          else Const (Lexicon.mark_const c, T)
wenzelm@42245
   517
      | mark_atoms (t as Free (x, T)) =
wenzelm@42245
   518
          let val i = find_index (fn s => s = x) structs + 1 in
wenzelm@42245
   519
            if i = 0 andalso member (op =) fixes x then
wenzelm@42245
   520
              Const (Lexicon.mark_fixed x, T)
wenzelm@42245
   521
            else if i = 1 andalso not show_structs then
wenzelm@42476
   522
              Syntax.const "_struct" $ Syntax.const "_indexdefault"
wenzelm@42476
   523
            else Syntax.const "_free" $ t
wenzelm@42245
   524
          end
wenzelm@42245
   525
      | mark_atoms (t as Var (xi, T)) =
wenzelm@42288
   526
          if xi = Syntax_Ext.dddot_indexname then Const ("_DDDOT", T)
wenzelm@42476
   527
          else Syntax.const "_var" $ t
wenzelm@42245
   528
      | mark_atoms a = a;
wenzelm@42245
   529
wenzelm@42245
   530
    fun prune_typs (t_seen as (Const _, _)) = t_seen
wenzelm@42245
   531
      | prune_typs (t as Free (x, ty), seen) =
wenzelm@42245
   532
          if ty = dummyT then (t, seen)
wenzelm@42290
   533
          else if not show_free_types orelse member (op aconv) seen t then (Syntax.free x, seen)
wenzelm@42245
   534
          else (t, t :: seen)
wenzelm@42245
   535
      | prune_typs (t as Var (xi, ty), seen) =
wenzelm@42245
   536
          if ty = dummyT then (t, seen)
wenzelm@42290
   537
          else if not show_free_types orelse member (op aconv) seen t then (Syntax.var xi, seen)
wenzelm@42245
   538
          else (t, t :: seen)
wenzelm@42245
   539
      | prune_typs (t_seen as (Bound _, _)) = t_seen
wenzelm@42245
   540
      | prune_typs (Abs (x, ty, t), seen) =
wenzelm@42245
   541
          let val (t', seen') = prune_typs (t, seen);
wenzelm@42245
   542
          in (Abs (x, ty, t'), seen') end
wenzelm@42245
   543
      | prune_typs (t1 $ t2, seen) =
wenzelm@42245
   544
          let
wenzelm@42245
   545
            val (t1', seen') = prune_typs (t1, seen);
wenzelm@42245
   546
            val (t2', seen'') = prune_typs (t2, seen');
wenzelm@42245
   547
          in (t1' $ t2', seen'') end;
wenzelm@42245
   548
wenzelm@42245
   549
    fun ast_of tm =
wenzelm@42245
   550
      (case strip_comb tm of
wenzelm@42284
   551
        (t as Abs _, ts) => Ast.mk_appl (ast_of (Syntax_Trans.abs_tr' ctxt t)) (map ast_of ts)
wenzelm@42245
   552
      | ((c as Const ("_free", _)), Free (x, T) :: ts) =>
wenzelm@42290
   553
          Ast.mk_appl (constrain (c $ Syntax.free x) T) (map ast_of ts)
wenzelm@42245
   554
      | ((c as Const ("_var", _)), Var (xi, T) :: ts) =>
wenzelm@42290
   555
          Ast.mk_appl (constrain (c $ Syntax.var xi) T) (map ast_of ts)
wenzelm@42245
   556
      | ((c as Const ("_bound", _)), Free (x, T) :: ts) =>
wenzelm@42290
   557
          Ast.mk_appl (constrain (c $ Syntax.free x) T) (map ast_of ts)
wenzelm@42245
   558
      | (Const ("_idtdummy", T), ts) =>
wenzelm@42476
   559
          Ast.mk_appl (constrain (Syntax.const "_idtdummy") T) (map ast_of ts)
wenzelm@42245
   560
      | (const as Const (c, T), ts) =>
wenzelm@42245
   561
          if show_all_types
wenzelm@42245
   562
          then Ast.mk_appl (constrain const T) (map ast_of ts)
wenzelm@42245
   563
          else trans c T ts
wenzelm@42245
   564
      | (t, ts) => Ast.mk_appl (simple_ast_of ctxt t) (map ast_of ts))
wenzelm@42245
   565
wenzelm@42254
   566
    and trans a T args = ast_of (trf a ctxt T args)
wenzelm@42254
   567
      handle Match => Ast.mk_appl (Ast.Constant a) (map ast_of args)
wenzelm@42245
   568
wenzelm@42245
   569
    and constrain t T =
wenzelm@42245
   570
      if show_types andalso T <> dummyT then
wenzelm@42248
   571
        Ast.Appl [Ast.Constant "_constrain", simple_ast_of ctxt t,
wenzelm@42247
   572
          ast_of_termT ctxt trf (term_of_typ ctxt T)]
wenzelm@42245
   573
      else simple_ast_of ctxt t;
wenzelm@42245
   574
  in
wenzelm@42245
   575
    tm
wenzelm@42284
   576
    |> Syntax_Trans.prop_tr'
wenzelm@42245
   577
    |> show_types ? (#1 o prune_typs o rpair [])
wenzelm@42245
   578
    |> mark_atoms
wenzelm@42245
   579
    |> ast_of
wenzelm@42245
   580
  end;
wenzelm@42245
   581
wenzelm@42245
   582
wenzelm@42245
   583
wenzelm@42242
   584
(** unparse **)
wenzelm@42242
   585
wenzelm@42245
   586
local
wenzelm@42245
   587
wenzelm@42267
   588
fun free_or_skolem ctxt x =
wenzelm@42267
   589
  let
wenzelm@42267
   590
    val m =
wenzelm@42267
   591
      if Variable.is_fixed ctxt x orelse Syntax.is_pretty_global ctxt
wenzelm@42267
   592
      then Markup.fixed x
wenzelm@42267
   593
      else Markup.hilite;
wenzelm@42267
   594
  in
wenzelm@42267
   595
    if can Name.dest_skolem x
wenzelm@42488
   596
    then ([m, Markup.skolem], Variable.revert_fixed ctxt x)
wenzelm@42267
   597
    else ([m, Markup.free], x)
wenzelm@42267
   598
  end;
wenzelm@42267
   599
wenzelm@42267
   600
fun var_or_skolem s =
wenzelm@42267
   601
  (case Lexicon.read_variable s of
wenzelm@42267
   602
    SOME (x, i) =>
wenzelm@42267
   603
      (case try Name.dest_skolem x of
wenzelm@42267
   604
        NONE => (Markup.var, s)
wenzelm@42267
   605
      | SOME x' => (Markup.skolem, Term.string_of_vname (x', i)))
wenzelm@42267
   606
  | NONE => (Markup.var, s));
wenzelm@42267
   607
wenzelm@42267
   608
fun unparse_t t_to_ast prt_t markup ctxt t =
wenzelm@42245
   609
  let
wenzelm@42360
   610
    val syn = Proof_Context.syn_of ctxt;
wenzelm@42267
   611
wenzelm@42267
   612
    fun token_trans "_tfree" x = SOME (Pretty.mark_str (Markup.tfree, x))
wenzelm@42267
   613
      | token_trans "_tvar" x = SOME (Pretty.mark_str (Markup.tvar, x))
wenzelm@42267
   614
      | token_trans "_free" x = SOME (Pretty.marks_str (free_or_skolem ctxt x))
wenzelm@42267
   615
      | token_trans "_bound" x = SOME (Pretty.mark_str (Markup.bound, x))
wenzelm@42408
   616
      | token_trans "_loose" x = SOME (Pretty.mark_str (Markup.malformed, x))
wenzelm@42267
   617
      | token_trans "_var" x = SOME (Pretty.mark_str (var_or_skolem x))
wenzelm@42267
   618
      | token_trans "_numeral" x = SOME (Pretty.mark_str (Markup.numeral, x))
wenzelm@42267
   619
      | token_trans "_inner_string" x = SOME (Pretty.mark_str (Markup.inner_string, x))
wenzelm@42267
   620
      | token_trans _ _ = NONE;
wenzelm@42267
   621
wenzelm@42301
   622
    fun markup_extern c =
wenzelm@42301
   623
      (case Syntax.lookup_const syn c of
wenzelm@42301
   624
        SOME "" => ([], c)
wenzelm@42301
   625
      | SOME b => markup_extern b
wenzelm@42301
   626
      | NONE => c |> Lexicon.unmark
wenzelm@42360
   627
         {case_class = fn x => ([Markup.tclass x], Proof_Context.extern_class ctxt x),
wenzelm@42360
   628
          case_type = fn x => ([Markup.tycon x], Proof_Context.extern_type ctxt x),
wenzelm@42360
   629
          case_const = fn x => ([Markup.const x], Proof_Context.extern_const ctxt x),
wenzelm@42301
   630
          case_fixed = fn x => free_or_skolem ctxt x,
wenzelm@42301
   631
          case_default = fn x => ([], x)});
wenzelm@42245
   632
  in
wenzelm@42255
   633
    t_to_ast ctxt (Syntax.print_translation syn) t
wenzelm@42255
   634
    |> Ast.normalize ctxt (Syntax.print_rules syn)
wenzelm@42267
   635
    |> prt_t ctxt (Syntax.prtabs syn) (Syntax.print_ast_translation syn) token_trans markup_extern
wenzelm@42253
   636
    |> Pretty.markup markup
wenzelm@42245
   637
  end;
wenzelm@42245
   638
wenzelm@42245
   639
in
wenzelm@42245
   640
wenzelm@42267
   641
val unparse_sort = unparse_t sort_to_ast Printer.pretty_typ_ast Markup.sort;
wenzelm@42267
   642
val unparse_typ = unparse_t typ_to_ast Printer.pretty_typ_ast Markup.typ;
wenzelm@42241
   643
wenzelm@42241
   644
fun unparse_term ctxt =
wenzelm@42241
   645
  let
wenzelm@42360
   646
    val thy = Proof_Context.theory_of ctxt;
wenzelm@42360
   647
    val syn = Proof_Context.syn_of ctxt;
wenzelm@42360
   648
    val idents = Local_Syntax.idents_of (Proof_Context.syntax_of ctxt);
wenzelm@42241
   649
  in
wenzelm@42298
   650
    unparse_t (term_to_ast idents (is_some o Syntax.lookup_const syn))
wenzelm@42267
   651
      (Printer.pretty_term_ast (not (Pure_Thy.old_appl_syntax thy)))
wenzelm@42267
   652
      Markup.term ctxt
wenzelm@42241
   653
  end;
wenzelm@42241
   654
wenzelm@42249
   655
end;
wenzelm@42249
   656
wenzelm@42241
   657
wenzelm@42245
   658
wenzelm@42245
   659
(** translations **)
wenzelm@42245
   660
wenzelm@42245
   661
(* type propositions *)
wenzelm@42245
   662
wenzelm@42247
   663
fun type_prop_tr' ctxt T [Const ("\\<^const>Pure.sort_constraint", _)] =
wenzelm@42476
   664
      Syntax.const "_sort_constraint" $ term_of_typ (Config.put show_sorts true ctxt) T
wenzelm@42247
   665
  | type_prop_tr' ctxt T [t] =
wenzelm@42476
   666
      Syntax.const "_ofclass" $ term_of_typ ctxt T $ t
wenzelm@42245
   667
  | type_prop_tr' _ T ts = raise TYPE ("type_prop_tr'", [T], ts);
wenzelm@42245
   668
wenzelm@42245
   669
wenzelm@42245
   670
(* type reflection *)
wenzelm@42245
   671
wenzelm@42247
   672
fun type_tr' ctxt (Type ("itself", [T])) ts =
wenzelm@42476
   673
      Term.list_comb (Syntax.const "_TYPE" $ term_of_typ ctxt T, ts)
wenzelm@42245
   674
  | type_tr' _ _ _ = raise Match;
wenzelm@42245
   675
wenzelm@42245
   676
wenzelm@42245
   677
(* type constraints *)
wenzelm@42245
   678
wenzelm@42247
   679
fun type_constraint_tr' ctxt (Type ("fun", [T, _])) (t :: ts) =
wenzelm@42476
   680
      Term.list_comb (Syntax.const "_constrain" $ t $ term_of_typ ctxt T, ts)
wenzelm@42245
   681
  | type_constraint_tr' _ _ _ = raise Match;
wenzelm@42245
   682
wenzelm@42245
   683
wenzelm@42295
   684
(* authentic syntax *)
wenzelm@42295
   685
wenzelm@42295
   686
fun const_ast_tr intern ctxt [Ast.Variable c] =
wenzelm@42295
   687
      let
wenzelm@42360
   688
        val Const (c', _) = Proof_Context.read_const_proper ctxt false c;
wenzelm@42295
   689
        val d = if intern then Lexicon.mark_const c' else c;
wenzelm@42295
   690
      in Ast.Constant d end
wenzelm@42470
   691
  | const_ast_tr intern ctxt [Ast.Appl [Ast.Constant "_constrain", x, T as Ast.Variable pos]] =
wenzelm@42470
   692
      (Ast.Appl [Ast.Constant "_constrain", const_ast_tr intern ctxt [x], T]
wenzelm@42470
   693
        handle ERROR msg =>
wenzelm@42470
   694
          error (msg ^ Position.str_of (the_default Position.none (Term_Position.decode pos))))
wenzelm@42295
   695
  | const_ast_tr _ _ asts = raise Ast.AST ("const_ast_tr", asts);
wenzelm@42295
   696
wenzelm@42295
   697
wenzelm@42245
   698
(* setup translations *)
wenzelm@42245
   699
wenzelm@42245
   700
val _ = Context.>> (Context.map_theory
wenzelm@42295
   701
 (Sign.add_advanced_trfuns
wenzelm@42295
   702
  ([("_context_const", const_ast_tr true),
wenzelm@42295
   703
    ("_context_xconst", const_ast_tr false)], [], [], []) #>
wenzelm@42295
   704
  Sign.add_advanced_trfunsT
wenzelm@42245
   705
   [("_type_prop", type_prop_tr'),
wenzelm@42245
   706
    ("\\<^const>TYPE", type_tr'),
wenzelm@42245
   707
    ("_type_constraint_", type_constraint_tr')]));
wenzelm@42245
   708
wenzelm@42245
   709
wenzelm@42245
   710
wenzelm@42382
   711
(** check/uncheck **)
wenzelm@42382
   712
wenzelm@42382
   713
val check_typs = Syntax.typ_check;
wenzelm@42382
   714
val check_terms = Syntax.term_check;
wenzelm@42382
   715
fun check_props ctxt = map (Type.constraint propT) #> check_terms ctxt;
wenzelm@42382
   716
wenzelm@42382
   717
val uncheck_typs = Syntax.typ_uncheck;
wenzelm@42382
   718
val uncheck_terms = Syntax.term_uncheck;
wenzelm@42382
   719
wenzelm@42382
   720
wenzelm@42382
   721
wenzelm@42242
   722
(** install operations **)
wenzelm@42242
   723
wenzelm@42241
   724
val _ = Syntax.install_operations
wenzelm@42241
   725
  {parse_sort = parse_sort,
wenzelm@42241
   726
   parse_typ = parse_typ,
wenzelm@42281
   727
   parse_term = parse_term false,
wenzelm@42281
   728
   parse_prop = parse_term true,
wenzelm@42241
   729
   unparse_sort = unparse_sort,
wenzelm@42241
   730
   unparse_typ = unparse_typ,
wenzelm@42382
   731
   unparse_term = unparse_term,
wenzelm@42382
   732
   check_typs = check_typs,
wenzelm@42382
   733
   check_terms = check_terms,
wenzelm@42382
   734
   check_props = check_props,
wenzelm@42382
   735
   uncheck_typs = uncheck_typs,
wenzelm@42382
   736
   uncheck_terms = uncheck_terms};
wenzelm@42241
   737
wenzelm@42241
   738
end;