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