src/Pure/Syntax/type_ext.ML
changeset 22704 f67607c3e56e
parent 20854 f9cf9e62d11c
child 22764 ccbd31bc1ef7
equal deleted inserted replaced
22703:d9fbdfe22543 22704:f67607c3e56e
     6 types, which is required to bootstrap Pure.
     6 types, which is required to bootstrap Pure.
     7 *)
     7 *)
     8 
     8 
     9 signature TYPE_EXT0 =
     9 signature TYPE_EXT0 =
    10 sig
    10 sig
    11   val sort_of_term: term -> sort
    11   val sort_of_term: (sort -> sort) -> term -> sort
    12   val raw_term_sorts: term -> (indexname * sort) list
    12   val term_sorts: (sort -> sort) -> 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 decode_types: (((string * int) * sort) list -> string * int -> sort) ->
       
    15     (string -> string option) -> (string -> string option) ->
       
    16     (typ -> typ) -> (sort -> sort) -> term -> term
    14   val term_of_typ: bool -> typ -> term
    17   val term_of_typ: bool -> typ -> term
    15   val no_brackets: unit -> bool
    18   val no_brackets: unit -> bool
    16   val no_type_brackets: unit -> bool
    19   val no_type_brackets: unit -> bool
    17 end;
    20 end;
    18 
    21 
    26 end;
    29 end;
    27 
    30 
    28 structure TypeExt: TYPE_EXT =
    31 structure TypeExt: TYPE_EXT =
    29 struct
    32 struct
    30 
    33 
    31 
       
    32 (** input utils **)
    34 (** input utils **)
    33 
    35 
    34 (* sort_of_term *)
    36 (* sort_of_term *)
    35 
    37 
    36 fun sort_of_term tm =
    38 fun sort_of_term (map_sort: sort -> sort) tm =
    37   let
    39   let
    38     fun classes (Const (c, _)) = [c]
    40     fun classes (Const (c, _)) = [c]
    39       | classes (Free (c, _)) = [c]
    41       | classes (Free (c, _)) = [c]
    40       | classes (Const ("_classes", _) $ Const (c, _) $ cs) = c :: classes cs
    42       | classes (Const ("_classes", _) $ Const (c, _) $ cs) = c :: classes cs
    41       | classes (Const ("_classes", _) $ Free (c, _) $ cs) = c :: classes cs
    43       | classes (Const ("_classes", _) $ Free (c, _) $ cs) = c :: classes cs
    45       | sort (Const (c, _)) = [c]
    47       | sort (Const (c, _)) = [c]
    46       | sort (Free (c, _)) = [c]
    48       | sort (Free (c, _)) = [c]
    47       | sort (Const ("_class",_) $ Free (c, _)) = [c]
    49       | sort (Const ("_class",_) $ Free (c, _)) = [c]
    48       | sort (Const ("_sort", _) $ cs) = classes cs
    50       | sort (Const ("_sort", _) $ cs) = classes cs
    49       | sort tm = raise TERM ("sort_of_term: bad encoding of sort", [tm]);
    51       | sort tm = raise TERM ("sort_of_term: bad encoding of sort", [tm]);
    50   in sort tm end;
    52   in map_sort (sort tm) end;
    51 
    53 
    52 
    54 
    53 (* raw_term_sorts *)
    55 (* term_sorts *)
    54 
    56 
    55 fun raw_term_sorts tm =
    57 fun term_sorts map_sort tm =
    56   let
    58   let
    57     fun add_env (Const ("_ofsort", _) $ Free (x, _) $ cs) env =
    59     val sort_of = sort_of_term map_sort;
    58           insert (op =) ((x, ~1), sort_of_term cs) env
    60 
    59       | add_env (Const ("_ofsort", _) $ (Const ("_tfree",_) $ Free (x, _)) $ cs) env =
    61     fun add_env (Const ("_ofsort", _) $ Free (x, _) $ cs) =
    60           insert (op =) ((x, ~1), sort_of_term cs) env
    62           insert (op =) ((x, ~1), sort_of cs)
    61       | add_env (Const ("_ofsort", _) $ Var (xi, _) $ cs) env =
    63       | add_env (Const ("_ofsort", _) $ (Const ("_tfree", _) $ Free (x, _)) $ cs) =
    62           insert (op =) (xi, sort_of_term cs) env
    64           insert (op =) ((x, ~1), sort_of cs)
    63       | add_env (Const ("_ofsort", _) $ (Const ("_tvar",_) $ Var (xi, _)) $ cs) env =
    65       | add_env (Const ("_ofsort", _) $ Var (xi, _) $ cs) =
    64           insert (op =) (xi, sort_of_term cs) env
    66           insert (op =) (xi, sort_of cs)
    65       | add_env (Abs (_, _, t)) env = add_env t env
    67       | add_env (Const ("_ofsort", _) $ (Const ("_tvar", _) $ Var (xi, _)) $ cs) =
    66       | add_env (t1 $ t2) env = add_env t1 (add_env t2 env)
    68           insert (op =) (xi, sort_of cs)
    67       | add_env _ env = env;
    69       | add_env (Abs (_, _, t)) = add_env t
       
    70       | add_env (t1 $ t2) = add_env t1 #> add_env t2
       
    71       | add_env _ = I;
    68   in add_env tm [] end;
    72   in add_env tm [] end;
    69 
    73 
    70 
    74 
    71 (* typ_of_term *)
    75 (* typ_of_term *)
    72 
    76 
    82       | typ_of (Const ("_ofsort", _) $ (Const ("_tfree",_) $ Free (x, _)) $ _) =
    86       | typ_of (Const ("_ofsort", _) $ (Const ("_tfree",_) $ Free (x, _)) $ _) =
    83           TFree (x, get_sort (x, ~1))
    87           TFree (x, get_sort (x, ~1))
    84       | typ_of (Const ("_ofsort", _) $ Var (xi, _) $ _) = TVar (xi, get_sort xi)
    88       | typ_of (Const ("_ofsort", _) $ Var (xi, _) $ _) = TVar (xi, get_sort xi)
    85       | typ_of (Const ("_ofsort", _) $ (Const ("_tvar",_) $ Var (xi, _)) $ _) =
    89       | typ_of (Const ("_ofsort", _) $ (Const ("_tvar",_) $ Var (xi, _)) $ _) =
    86           TVar (xi, get_sort xi)
    90           TVar (xi, get_sort xi)
    87       | typ_of (Const ("_dummy_ofsort", _) $ t) = TFree ("'_dummy_", map_sort (sort_of_term t))
    91       | typ_of (Const ("_dummy_ofsort", _) $ t) = TFree ("'_dummy_", sort_of_term map_sort t)
    88       | typ_of tm =
    92       | typ_of tm =
    89           let
    93           let
    90             val (t, ts) = Term.strip_comb tm;
    94             val (t, ts) = Term.strip_comb tm;
    91             val a =
    95             val a =
    92               (case t of
    96               (case t of
    93                 Const (x, _) => x
    97                 Const (x, _) => x
    94               | Free (x, _) => x
    98               | Free (x, _) => x
    95               | _ => raise TERM ("typ_of_term: bad encoding of type", [tm]));
    99               | _ => raise TERM ("typ_of_term: bad encoding of type", [tm]));
    96           in Type (a, map typ_of ts) end;
   100           in Type (a, map typ_of ts) end;
    97   in typ_of t end;
   101   in typ_of t end;
       
   102 
       
   103 
       
   104 (* decode_types -- transform parse tree into raw term *)
       
   105 
       
   106 fun decode_types get_sort map_const map_free map_type map_sort tm =
       
   107   let
       
   108     val sort_env = term_sorts map_sort tm;
       
   109     val decodeT = map_type o typ_of_term (get_sort sort_env) map_sort;
       
   110 
       
   111     fun decode (Const ("_constrain", _) $ t $ typ) =
       
   112           TypeInfer.constrain (decode t) (decodeT typ)
       
   113       | decode (Const ("_constrainAbs", _) $ (Abs (x, T, t)) $ typ) =
       
   114           if T = dummyT then Abs (x, decodeT typ, decode t)
       
   115           else TypeInfer.constrain (Abs (x, map_type T, decode t)) (decodeT typ --> dummyT)
       
   116       | decode (Abs (x, T, t)) = Abs (x, map_type T, decode t)
       
   117       | decode (t $ u) = decode t $ decode u
       
   118       | decode (Const (a, T)) =
       
   119           let val c =
       
   120             (case try (unprefix Lexicon.constN) a of SOME c => c | NONE => perhaps map_const a)
       
   121           in Const (c, map_type T) end
       
   122       | decode (Free (a, T)) =
       
   123           (case (map_free a, map_const a) of
       
   124             (SOME x, _) => Free (x, map_type T)
       
   125           | (_, SOME c) => Const (c, map_type T)
       
   126           | _ => Free (a, map_type T))
       
   127       | decode (Var (xi, T)) = Var (xi, map_type T)
       
   128       | decode (t as Bound _) = t;
       
   129   in decode tm end;
    98 
   130 
    99 
   131 
   100 
   132 
   101 (** output utils **)
   133 (** output utils **)
   102 
   134