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