Datatype.ML
changeset 92 bcd0ee8d71aa
parent 91 a94029edb01f
child 96 d94d0b324b4b
equal deleted inserted replaced
91:a94029edb01f 92:bcd0ee8d71aa
     4     Copyright    1994 TU Muenchen
     4     Copyright    1994 TU Muenchen
     5 *)
     5 *)
     6 
     6 
     7 
     7 
     8 (*choice between Ci_neg1 and Ci_neg2 axioms depends on number of constructors*)
     8 (*choice between Ci_neg1 and Ci_neg2 axioms depends on number of constructors*)
     9 val dtK = 5;
     9 local val dtK = 5
       
    10 in
    10 
    11 
    11 local open ThyParse in
    12 local open ThyParse in
    12 val datatype_decls =
    13 val datatype_decls =
    13   let fun cat s1 s2 = s1 ^ " " ^ s2;
    14   let fun cat s1 s2 = s1 ^ " " ^ s2;
    14 
    15 
    23         tvar >> (fn s => [s]) || "(" $$-- list1 tvar --$$ ")";
    24         tvar >> (fn s => [s]) || "(" $$-- list1 tvar --$$ ")";
    24     
    25     
    25       val typ =
    26       val typ =
    26          ident                  >> (cat "dtId" o quote)
    27          ident                  >> (cat "dtId" o quote)
    27         ||
    28         ||
    28          type_var_list -- ident >> (fn (ts, id) => "Comp (" ^ mk_list ts ^
    29          type_var_list -- ident >> (fn (ts, id) => "dtComp (" ^ mk_list ts ^
    29   				  ", " ^ quote id ^ ")")
    30   				  ", " ^ quote id ^ ")")
    30         ||
    31         ||
    31          tvar;
    32          tvar;
    32     
    33     
    33       val typ_list = "(" $$-- list1 typ --$$ ")" || empty;
    34       val typ_list = "(" $$-- list1 typ --$$ ")" || empty;
    88 end;
    89 end;
    89 
    90 
    90 (*used for constructor parameters*)
    91 (*used for constructor parameters*)
    91 datatype dt_type = dtVar of string |
    92 datatype dt_type = dtVar of string |
    92                    dtId  of string |
    93                    dtId  of string |
    93                    Comp of dt_type list * string |
    94                    dtComp of dt_type list * string |
    94                    Rek of dt_type list * string;
    95                    dtRek of dt_type list * string;
    95 
    96 
    96 exception Impossible;
    97 local open Syntax.Mixfix
    97 
    98       exception Impossible
    98 local open Syntax.Mixfix in
    99 in
    99 fun add_datatype (typevars, tname, cons_list') thy = 
   100 fun add_datatype (typevars, tname, cons_list') thy = 
   100   let fun cat s1 s2 = s1 ^ " " ^ s2;
   101   let fun cat s1 s2 = s1 ^ " " ^ s2;
   101 
   102 
   102       val pars = parents "(" ")";
   103       val pars = parents "(" ")";
   103       val brackets = parents "[" "]";
   104       val brackets = parents "[" "]";
   117                        (dtVar v) :: analyse typlist
   118                        (dtVar v) :: analyse typlist
   118                      else error ("Variable " ^ v ^ " is free.")
   119                      else error ("Variable " ^ v ^ " is free.")
   119                   | analyse ((dtId s) :: typlist) =
   120                   | analyse ((dtId s) :: typlist) =
   120                      if tname<>s then (dtId s) :: analyse typlist
   121                      if tname<>s then (dtId s) :: analyse typlist
   121                      else if null typevars then 
   122                      else if null typevars then 
   122                        Rek ([], tname) :: analyse typlist
   123                        dtRek ([], tname) :: analyse typlist
   123                      else error (s ^ " used in different ways")
   124                      else error (s ^ " used in different ways")
   124                   | analyse (Comp (typl,s) :: typlist) =
   125                   | analyse (dtComp (typl,s) :: typlist) =
   125                      if tname <> s then Comp (analyse typl, s)
   126                      if tname <> s then dtComp (analyse typl, s)
   126                                      :: analyse typlist
   127                                      :: analyse typlist
   127                      else if typevars = typl then
   128                      else if typevars = typl then
   128                        Rek (typl, s) :: analyse typlist
   129                        dtRek (typl, s) :: analyse typlist
   129                      else 
   130                      else 
   130                        error (s ^ " used in different ways")
   131                        error (s ^ " used in different ways")
   131                   | analyse [] = []
   132                   | analyse [] = []
   132                   | analyse ((Rek _) :: _) = raise Impossible;
   133                   | analyse ((dtRek _) :: _) = raise Impossible;
   133             in (cons, analyse typlist, syn) end;
   134             in (cons, analyse typlist, syn) end;
   134 
   135 
   135       (*test if there are elements that are not recursive, i.e. if the type is
   136       (*test if there are elements that are not recursive, i.e. if the type is
   136         not empty*)
   137         not empty*)
   137       fun one_not_rek (cs : ('a * dt_type list * 'c) list) = 
   138       fun one_not_rek (cs : ('a * dt_type list * 'c) list) = 
   138         let val contains_no_rek = forall (fn Rek _ => false | _ => true);
   139         let val contains_no_rek = forall (fn dtRek _ => false | _ => true);
   139         in exists (contains_no_rek o #2) cs orelse
   140         in exists (contains_no_rek o #2) cs orelse
   140            error("Empty type not allowed!") end;
   141            error("Empty type not allowed!") end;
   141 
   142 
   142       val dummy = check_cons cons_list';
   143       val dummy = check_cons cons_list';
   143       val cons_list = map analyse_types cons_list';
   144       val cons_list = map analyse_types cons_list';
   145 
   146 
   146       (*Pretty printers for type lists;
   147       (*Pretty printers for type lists;
   147         pp_typlist1: parentheses, pp_typlist2: brackets*)
   148         pp_typlist1: parentheses, pp_typlist2: brackets*)
   148       fun pp_typ (dtVar s) = s
   149       fun pp_typ (dtVar s) = s
   149         | pp_typ (dtId s) = s
   150         | pp_typ (dtId s) = s
   150         | pp_typ (Comp (typvars, id)) = (pp_typlist1 typvars) ^ id
   151         | pp_typ (dtComp (typvars, id)) = (pp_typlist1 typvars) ^ id
   151         | pp_typ (Rek (typvars, id)) = (pp_typlist1 typvars) ^ id
   152         | pp_typ (dtRek (typvars, id)) = (pp_typlist1 typvars) ^ id
   152       and
   153       and
   153           pp_typlist' ts = commas (map pp_typ ts)
   154           pp_typlist' ts = commas (map pp_typ ts)
   154       and
   155       and
   155           pp_typlist1 ts = if null ts then "" else pars (pp_typlist' ts);
   156           pp_typlist1 ts = if null ts then "" else pars (pp_typlist' ts);
   156 
   157 
   195             if (dtVar s) mem typlist then 
   196             if (dtVar s) mem typlist then 
   196 	      create_typevar (dtVar (s ^ "'")) typlist 
   197 	      create_typevar (dtVar (s ^ "'")) typlist 
   197             else s
   198             else s
   198         | create_typevar _ _ = raise Impossible;
   199         | create_typevar _ _ = raise Impossible;
   199 
   200 
   200       fun assumpt (Rek _ :: ts, v :: vs ,found) =
   201       fun assumpt (dtRek _ :: ts, v :: vs ,found) =
   201             let val h = if found then ";P(" ^ v ^ ")"
   202             let val h = if found then ";P(" ^ v ^ ")"
   202                                  else "[| P(" ^ v ^ ")"
   203                                  else "[| P(" ^ v ^ ")"
   203             in h ^ (assumpt (ts, vs, true)) end
   204             in h ^ (assumpt (ts, vs, true)) end
   204         | assumpt (t :: ts, v :: vs, found) = assumpt (ts, vs, found)
   205         | assumpt (t :: ts, v :: vs, found) = assumpt (ts, vs, found)
   205         | assumpt ([], [], found) = if found then "|] ==>" else ""
   206         | assumpt ([], [], found) = if found then "|] ==>" else ""
   210             if typ = t then (t, s, n+1) :: xs
   211             if typ = t then (t, s, n+1) :: xs
   211             else if varname = s then (t,s,n) :: (insert typ (varname ^ "'") xs)
   212             else if varname = s then (t,s,n) :: (insert typ (varname ^ "'") xs)
   212                                 else (t,s,n) :: (insert typ varname xs)
   213                                 else (t,s,n) :: (insert typ varname xs)
   213         | insert typ varname [] = [(typ, varname, 1)];
   214         | insert typ varname [] = [(typ, varname, 1)];
   214 
   215 
   215       fun insert_types (Rek (l,id) :: ts) tab =
   216       fun insert_types (dtRek (l,id) :: ts) tab =
   216             insert_types ts (insert (Rek(l,id)) id tab)
   217             insert_types ts (insert (dtRek(l,id)) id tab)
   217         | insert_types ((dtVar s) :: ts) tab =
   218         | insert_types ((dtVar s) :: ts) tab =
   218             insert_types ts (insert (dtVar s) (implode (tl (explode s))) tab)
   219             insert_types ts (insert (dtVar s) (implode (tl (explode s))) tab)
   219         | insert_types ((dtId s) :: ts) tab =
   220         | insert_types ((dtId s) :: ts) tab =
   220             insert_types ts (insert (dtId s) s tab)
   221             insert_types ts (insert (dtId s) s tab)
   221         | insert_types (Comp (l,id) :: ts) tab =
   222         | insert_types (dtComp (l,id) :: ts) tab =
   222             insert_types ts (insert (Comp(l,id)) id tab)
   223             insert_types ts (insert (dtComp(l,id)) id tab)
   223         | insert_types [] tab = tab;
   224         | insert_types [] tab = tab;
   224 
   225 
   225       fun update(Rek _, s, v :: vs, (Rek _) :: ts) = s :: vs
   226       fun update(dtRek _, s, v :: vs, (dtRek _) :: ts) = s :: vs
   226         | update(t, s, v :: vs, t1 :: ts) = 
   227         | update(t, s, v :: vs, t1 :: ts) = 
   227             if t=t1 then s :: vs
   228             if t=t1 then s :: vs
   228                     else v :: (update (t, s, vs, ts))
   229                     else v :: (update (t, s, vs, ts))
   229         | update _ = raise Impossible;
   230         | update _ = raise Impossible;
   230       
   231       
   231       fun update_n (Rek r1, s, v :: vs, (Rek r2) :: ts, n) =
   232       fun update_n (dtRek r1, s, v :: vs, (dtRek r2) :: ts, n) =
   232             if r1 = r2 then (s ^ string_of_int n) :: 
   233             if r1 = r2 then (s ^ string_of_int n) :: 
   233                             (update_n (Rek r1, s, vs, ts, n+1))
   234                             (update_n (dtRek r1, s, vs, ts, n+1))
   234                        else v :: (update_n (Rek r1, s, vs, ts, n))
   235                        else v :: (update_n (dtRek r1, s, vs, ts, n))
   235         | update_n (t, s, v :: vs, t1 :: ts, n) = 
   236         | update_n (t, s, v :: vs, t1 :: ts, n) = 
   236             if t = t1 then (s ^ string_of_int n) :: 
   237             if t = t1 then (s ^ string_of_int n) :: 
   237                            (update_n (t, s, vs, ts, n+1))
   238                            (update_n (t, s, vs, ts, n+1))
   238                       else v :: (update_n (t, s, vs, ts, n))
   239                       else v :: (update_n (t, s, vs, ts, n))
   239         | update_n (_,_,[],[],_) = []
   240         | update_n (_,_,[],[],_) = []
   382      |> add_arities arities
   383      |> add_arities arities
   383      |> add_consts consts
   384      |> add_consts consts
   384      |> add_trrules xrules
   385      |> add_trrules xrules
   385      |> add_axioms rules
   386      |> add_axioms rules
   386   end
   387   end
       
   388 end
   387 end;
   389 end;