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