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