src/Pure/Syntax/type_ext.ML
changeset 16614 a493a50e6c0a
parent 15833 78109c7012ed
child 19305 5c16895d548b
equal deleted inserted replaced
16613:76e57e08dcb5 16614:a493a50e6c0a
    11   val sort_of_term: term -> sort
    11   val sort_of_term: term -> sort
    12   val raw_term_sorts: term -> (indexname * sort) list
    12   val raw_term_sorts: term -> (indexname * sort) list
    13   val typ_of_term: (indexname -> sort) -> (sort -> sort) -> term -> typ
    13   val typ_of_term: (indexname -> sort) -> (sort -> sort) -> term -> typ
    14   val term_of_typ: bool -> typ -> term
    14   val term_of_typ: bool -> typ -> term
    15   val no_brackets: unit -> bool
    15   val no_brackets: unit -> bool
       
    16   val no_type_brackets: unit -> bool
    16 end;
    17 end;
    17 
    18 
    18 signature TYPE_EXT =
    19 signature TYPE_EXT =
    19 sig
    20 sig
    20   include TYPE_EXT0
    21   include TYPE_EXT0
    51 
    52 
    52 (* raw_term_sorts *)
    53 (* raw_term_sorts *)
    53 
    54 
    54 fun raw_term_sorts tm =
    55 fun raw_term_sorts tm =
    55   let
    56   let
    56     fun add_env (Const ("_ofsort", _) $ Free (x, _) $ cs) env = ((x, ~1), sort_of_term cs) ins env
    57     fun add_env (Const ("_ofsort", _) $ Free (x, _) $ cs) env =
    57       | add_env (Const ("_ofsort", _) $ (Const ("_tfree",_) $ Free (x, _)) $ cs) env 
    58           ((x, ~1), sort_of_term cs) ins env
    58                 = ((x, ~1), sort_of_term cs) ins env
    59       | add_env (Const ("_ofsort", _) $ (Const ("_tfree",_) $ Free (x, _)) $ cs) env =
    59       | add_env (Const ("_ofsort", _) $ Var (xi, _) $ cs) env = (xi, sort_of_term cs) ins env
    60           ((x, ~1), sort_of_term cs) ins env
    60       | add_env (Const ("_ofsort", _) $ (Const ("_tvar",_) $ Var (xi, _)) $ cs) env 
    61       | add_env (Const ("_ofsort", _) $ Var (xi, _) $ cs) env =
    61                 = (xi, sort_of_term cs) ins env
    62           (xi, sort_of_term cs) ins env
       
    63       | add_env (Const ("_ofsort", _) $ (Const ("_tvar",_) $ Var (xi, _)) $ cs) env =
       
    64           (xi, sort_of_term cs) ins env
    62       | add_env (Abs (_, _, t)) env = add_env t env
    65       | add_env (Abs (_, _, t)) env = add_env t env
    63       | add_env (t1 $ t2) env = add_env t1 (add_env t2 env)
    66       | add_env (t1 $ t2) env = add_env t1 (add_env t2 env)
    64       | add_env t env = env;
    67       | add_env _ env = env;
    65   in add_env tm [] end;
    68   in add_env tm [] end;
    66 
    69 
    67 
    70 
    68 (* typ_of_term *)
    71 (* typ_of_term *)
    69 
    72 
    74           else Type (x, [])
    77           else Type (x, [])
    75       | typ_of (Var (xi, _)) = TVar (xi, get_sort xi)
    78       | typ_of (Var (xi, _)) = TVar (xi, get_sort xi)
    76       | typ_of (Const ("_tfree",_) $ (t as Free x)) = typ_of t
    79       | typ_of (Const ("_tfree",_) $ (t as Free x)) = typ_of t
    77       | typ_of (Const ("_tvar",_) $ (t as Var x)) = typ_of t
    80       | typ_of (Const ("_tvar",_) $ (t as Var x)) = typ_of t
    78       | typ_of (Const ("_ofsort", _) $ Free (x, _) $ _) = TFree (x, get_sort (x, ~1))
    81       | typ_of (Const ("_ofsort", _) $ Free (x, _) $ _) = TFree (x, get_sort (x, ~1))
    79       | typ_of (Const ("_ofsort", _) $ (Const ("_tfree",_) $ Free (x, _)) $ _) 
    82       | typ_of (Const ("_ofsort", _) $ (Const ("_tfree",_) $ Free (x, _)) $ _) =
    80                = TFree (x, get_sort (x, ~1))
    83           TFree (x, get_sort (x, ~1))
    81       | typ_of (Const ("_ofsort", _) $ Var (xi, _) $ _) = TVar (xi, get_sort xi)
    84       | typ_of (Const ("_ofsort", _) $ Var (xi, _) $ _) = TVar (xi, get_sort xi)
    82       | typ_of (Const ("_ofsort", _) $ (Const ("_tvar",_) $ Var (xi, _)) $ _) 
    85       | typ_of (Const ("_ofsort", _) $ (Const ("_tvar",_) $ Var (xi, _)) $ _) =
    83                = TVar (xi, get_sort xi)
    86           TVar (xi, get_sort xi)
    84       | typ_of (Const ("_dummy_ofsort", _) $ t) = TFree ("'_dummy_", map_sort (sort_of_term t))
    87       | typ_of (Const ("_dummy_ofsort", _) $ t) = TFree ("'_dummy_", map_sort (sort_of_term t))
    85      
       
    86       | typ_of tm =
    88       | typ_of tm =
    87           let
    89           let
    88             val (t, ts) = strip_comb tm;
    90             val (t, ts) = Term.strip_comb tm;
    89             val a =
    91             val a =
    90               (case t of
    92               (case t of
    91                 Const (x, _) => x
    93                 Const (x, _) => x
    92               | Free (x, _) => x
    94               | Free (x, _) => x
    93               | _ => raise TERM ("typ_of_term: bad encoding of type", [tm]));
    95               | _ => raise TERM ("typ_of_term: bad encoding of type", [tm]));
   121   let
   123   let
   122     fun of_sort t S =
   124     fun of_sort t S =
   123       if show_sorts then Lexicon.const "_ofsort" $ t $ term_of_sort S
   125       if show_sorts then Lexicon.const "_ofsort" $ t $ term_of_sort S
   124       else t;
   126       else t;
   125 
   127 
   126     fun term_of (Type (a, Ts)) = list_comb (Lexicon.const a, map term_of Ts)
   128     fun term_of (Type (a, Ts)) = Term.list_comb (Lexicon.const a, map term_of Ts)
   127       | term_of (TFree (x, S)) = of_sort (Lexicon.const "_tfree" $ Lexicon.free x) S
   129       | term_of (TFree (x, S)) = of_sort (Lexicon.const "_tfree" $ Lexicon.free x) S
   128       | term_of (TVar (xi, S)) = of_sort (Lexicon.const "_tvar" $ Lexicon.var xi) S;
   130       | term_of (TVar (xi, S)) = of_sort (Lexicon.const "_tvar" $ Lexicon.var xi) S;
   129   in term_of ty end;
   131   in term_of ty end;
   130 
   132 
   131 
   133 
   136 
   138 
   137 val bracketsN = "brackets";
   139 val bracketsN = "brackets";
   138 val no_bracketsN = "no_brackets";
   140 val no_bracketsN = "no_brackets";
   139 
   141 
   140 fun no_brackets () =
   142 fun no_brackets () =
   141   Library.find_first (equal bracketsN orf equal no_bracketsN) (! print_mode)
   143   find_first (equal bracketsN orf equal no_bracketsN) (! print_mode)
   142   = SOME no_bracketsN;
   144     = SOME no_bracketsN;
   143 
   145 
   144 val type_bracketsN = "type_brackets";
   146 val type_bracketsN = "type_brackets";
   145 val no_type_bracketsN = "no_type_brackets";
   147 val no_type_bracketsN = "no_type_brackets";
   146 
   148 
   147 fun no_type_brackets () =
   149 fun no_type_brackets () =
   148   Library.find_first (equal type_bracketsN orf equal no_type_bracketsN)
   150   Library.find_first (equal type_bracketsN orf equal no_type_bracketsN) (! print_mode)
   149                      (! print_mode)
   151     <> SOME type_bracketsN;
   150   <> SOME type_bracketsN;
       
   151 
   152 
   152 
   153 
   153 (* parse ast translations *)
   154 (* parse ast translations *)
   154 
   155 
   155 fun tapp_ast_tr (*"_tapp"*) [ty, f] = Ast.Appl [f, ty]
   156 fun tapp_ast_tr (*"_tapp"*) [ty, f] = Ast.Appl [f, ty]
   170   | tappl_ast_tr' (f, [ty]) = Ast.Appl [Ast.Constant "_tapp", ty, f]
   171   | tappl_ast_tr' (f, [ty]) = Ast.Appl [Ast.Constant "_tapp", ty, f]
   171   | tappl_ast_tr' (f, ty :: tys) =
   172   | tappl_ast_tr' (f, ty :: tys) =
   172       Ast.Appl [Ast.Constant "_tappl", ty, Ast.fold_ast "_types" tys, f];
   173       Ast.Appl [Ast.Constant "_tappl", ty, Ast.fold_ast "_types" tys, f];
   173 
   174 
   174 fun fun_ast_tr' (*"fun"*) asts =
   175 fun fun_ast_tr' (*"fun"*) asts =
   175   if no_brackets() orelse no_type_brackets() then raise Match
   176   if no_brackets () orelse no_type_brackets () then raise Match
   176   else
   177   else
   177     (case Ast.unfold_ast_p "fun" (Ast.Appl (Ast.Constant "fun" :: asts)) of
   178     (case Ast.unfold_ast_p "fun" (Ast.Appl (Ast.Constant "fun" :: asts)) of
   178       (dom as _ :: _ :: _, cod)
   179       (dom as _ :: _ :: _, cod)
   179         => Ast.Appl [Ast.Constant "_bracket", Ast.fold_ast "_types" dom, cod]
   180         => Ast.Appl [Ast.Constant "_bracket", Ast.fold_ast "_types" dom, cod]
   180     | _ => raise Match);
   181     | _ => raise Match);
   214    Mfix ("([_]/ => _)", [typesT, typeT] ---> typeT,    "_bracket", [0, 0], 0),
   215    Mfix ("([_]/ => _)", [typesT, typeT] ---> typeT,    "_bracket", [0, 0], 0),
   215    Mfix ("'(_')",       typeT --> typeT,               "", [0], max_pri),
   216    Mfix ("'(_')",       typeT --> typeT,               "", [0], max_pri),
   216    Mfix ("'_",          typeT,                         "dummy", [], max_pri)]
   217    Mfix ("'_",          typeT,                         "dummy", [], max_pri)]
   217   []
   218   []
   218   (map SynExt.mk_trfun
   219   (map SynExt.mk_trfun
   219    [("_tapp", tapp_ast_tr), ("_tappl", tappl_ast_tr), ("_bracket", bracket_ast_tr)],
   220    [("_tapp", K tapp_ast_tr), ("_tappl", K tappl_ast_tr), ("_bracket", K bracket_ast_tr)],
   220    [],
   221    [],
   221    [],
   222    [],
   222    map SynExt.mk_trfun [("fun", fun_ast_tr')])
   223    map SynExt.mk_trfun [("fun", K fun_ast_tr')])
   223   (map (fn s => ("", s, Output.output_width)) SynExt.standard_token_classes)
   224   (map (fn s => ("", s, Output.output_width)) SynExt.standard_token_classes)
   224   ([], []);
   225   ([], []);
   225 
   226 
   226 end;
   227 end;
   227 
   228