src/Pure/sign.ML
changeset 14987 699239c7632c
parent 14981 e73f8140af78
child 14993 802f3732a54e
equal deleted inserted replaced
14986:c97190ae13bd 14987:699239c7632c
   669 fun read_raw_typ (sg, def_sort) = read_raw_typ' (syn_of sg) (sg, def_sort);
   669 fun read_raw_typ (sg, def_sort) = read_raw_typ' (syn_of sg) (sg, def_sort);
   670 
   670 
   671 (*read and certify typ wrt a signature*)
   671 (*read and certify typ wrt a signature*)
   672 local
   672 local
   673   fun read_typ_aux rd cert (sg, def_sort) str =
   673   fun read_typ_aux rd cert (sg, def_sort) str =
   674     (cert (tsig_of sg) (rd (sg, def_sort) str)
   674     (#1 (cert (tsig_of sg) (rd (sg, def_sort) str))
   675       handle TYPE (msg, _, _) => (error_msg msg; err_in_type str));
   675       handle TYPE (msg, _, _) => (error_msg msg; err_in_type str));
   676 in
   676 in
   677   val read_typ          = read_typ_aux read_raw_typ Type.cert_typ;
   677   val read_typ          = read_typ_aux read_raw_typ Type.cert_typ;
   678   val read_typ_raw      = read_typ_aux read_raw_typ Type.cert_typ_raw;
   678   val read_typ_raw      = read_typ_aux read_raw_typ Type.cert_typ_raw;
   679   fun read_typ' syn     = read_typ_aux (read_raw_typ' syn) Type.cert_typ;
   679   fun read_typ' syn     = read_typ_aux (read_raw_typ' syn) Type.cert_typ;
   684 
   684 
   685 (** certify classes, sorts, types and terms **)   (*exception TYPE*)
   685 (** certify classes, sorts, types and terms **)   (*exception TYPE*)
   686 
   686 
   687 val certify_class = Type.cert_class o tsig_of;
   687 val certify_class = Type.cert_class o tsig_of;
   688 val certify_sort = Type.cert_sort o tsig_of;
   688 val certify_sort = Type.cert_sort o tsig_of;
   689 val certify_typ = Type.cert_typ o tsig_of;
   689 val certify_typ = #1 oo (Type.cert_typ o tsig_of);
   690 val certify_typ_raw = Type.cert_typ_raw o tsig_of;
   690 val certify_typ_raw = #1 oo (Type.cert_typ_raw o tsig_of);
   691 
   691 
   692 fun certify_tyname sg c =
   692 fun certify_tyname sg c =
   693   if is_some (Symtab.lookup (#types (Type.rep_tsig (tsig_of sg)), c)) then c
   693   if is_some (Symtab.lookup (#types (Type.rep_tsig (tsig_of sg)), c)) then c
   694   else raise TYPE ("Undeclared type constructor: " ^ quote c, [], []);
   694   else raise TYPE ("Undeclared type constructor: " ^ quote c, [], []);
   695 
   695 
   697   if is_some (const_type sg c) then c
   697   if is_some (const_type sg c) then c
   698   else raise TYPE ("Undeclared constant: " ^ quote c, [], []);
   698   else raise TYPE ("Undeclared constant: " ^ quote c, [], []);
   699 
   699 
   700 
   700 
   701 (* certify_term *)
   701 (* certify_term *)
       
   702 
       
   703 local
       
   704 
       
   705 (*certify types of term and compute maxidx*)
       
   706 fun cert_term_types certT =
       
   707   let
       
   708     fun cert (Const (a, T)) = let val (T', i) = certT T in (Const (a, T'), i) end
       
   709       | cert (Free (a, T)) = let val (T', i) = certT T in (Free (a, T'), i) end
       
   710       | cert (Var (xi as (_, i), T)) =
       
   711           let val (T', j) = certT T in (Var (xi, T'), Int.max (i, j)) end
       
   712       | cert (t as Bound _) = (t, ~1)
       
   713       | cert (Abs (a, T, t)) =
       
   714           let val (T', i) = certT T and (t', j) = cert t
       
   715           in (Abs (a, T', t'), Int.max (i, j)) end
       
   716       | cert (t $ u) =
       
   717           let val (t', i) = cert t and (u', j) =  cert u
       
   718           in (t' $ u', Int.max (i, j)) end;
       
   719   in cert end;
       
   720 
       
   721 (*compute and check type of the term*)
       
   722 fun type_check pp sg tm =
       
   723   let
       
   724     fun err_appl why bs t T u U =
       
   725       let
       
   726         val xs = map Free bs;           (*we do not rename here*)
       
   727         val t' = subst_bounds (xs, t);
       
   728         val u' = subst_bounds (xs, u);
       
   729         val text = cat_lines
       
   730           (TypeInfer.appl_error (Syntax.pp_show_brackets pp) why t' T u' U);
       
   731       in raise TYPE (text, [T, U], [t', u']) end;
       
   732 
       
   733     fun typ_of (_, Const (_, T)) = T
       
   734       | typ_of (_, Free  (_, T)) = T
       
   735       | typ_of (_, Var (_, T)) = T
       
   736       | typ_of (bs, Bound i) = snd (nth_elem (i, bs) handle LIST _ =>
       
   737           raise TYPE ("Loose bound variable: B." ^ string_of_int i, [], [Bound i]))
       
   738       | typ_of (bs, Abs (x, T, body)) = T --> typ_of ((x, T) :: bs, body)
       
   739       | typ_of (bs, t $ u) =
       
   740           let val T = typ_of (bs, t) and U = typ_of (bs, u) in
       
   741             (case T of
       
   742               Type ("fun", [T1, T2]) =>
       
   743                 if T1 = U then T2 else err_appl "Incompatible operand type" bs t T u U
       
   744             | _ => err_appl "Operator not of function type" bs t T u U)
       
   745           end;
       
   746 
       
   747   in typ_of ([], tm) end;
   702 
   748 
   703 (*check for duplicate occurrences of TFree/TVar with distinct sorts*)
   749 (*check for duplicate occurrences of TFree/TVar with distinct sorts*)
   704 fun nodup_tvars (env, Type (_, Ts)) = nodup_tvars_list (env, Ts)
   750 fun nodup_tvars (env, Type (_, Ts)) = nodup_tvars_list (env, Ts)
   705   | nodup_tvars (env as (tfrees, tvars), T as TFree (v as (a, S))) =
   751   | nodup_tvars (env as (tfrees, tvars), T as TFree (v as (a, S))) =
   706       (case assoc_string (tfrees, a) of
   752       (case assoc_string (tfrees, a) of
   717             " has two distinct sorts", [TVar (a, S'), T], [])
   763             " has two distinct sorts", [TVar (a, S'), T], [])
   718       | None => (tfrees, v :: tvars))
   764       | None => (tfrees, v :: tvars))
   719 (*equivalent to foldl nodup_tvars_list, but 3X faster under Poly/ML*)
   765 (*equivalent to foldl nodup_tvars_list, but 3X faster under Poly/ML*)
   720 and nodup_tvars_list (env, []) = env
   766 and nodup_tvars_list (env, []) = env
   721   | nodup_tvars_list (env, T :: Ts) = nodup_tvars_list (nodup_tvars (env, T), Ts);
   767   | nodup_tvars_list (env, T :: Ts) = nodup_tvars_list (nodup_tvars (env, T), Ts);
       
   768 
       
   769 in
   722 
   770 
   723 (*check for duplicate occurrences of Free/Var with distinct types*)
   771 (*check for duplicate occurrences of Free/Var with distinct types*)
   724 fun nodup_vars tm =
   772 fun nodup_vars tm =
   725   let
   773   let
   726     fun nodups (envs as (env as (frees, vars), envT)) tm =
   774     fun nodups (envs as (env as (frees, vars), envT)) tm =
   743       | Bound _ => envs
   791       | Bound _ => envs
   744       | Abs (_, T, t) => nodups (env, nodup_tvars (envT, T)) t
   792       | Abs (_, T, t) => nodups (env, nodup_tvars (envT, T)) t
   745       | s $ t => nodups (nodups envs s) t)
   793       | s $ t => nodups (nodups envs s) t)
   746   in nodups (([], []), ([], [])) tm; tm end;
   794   in nodups (([], []), ([], [])) tm; tm end;
   747 
   795 
   748 (*compute and check type of the term*)
       
   749 fun type_check pp sg tm =
       
   750   let
       
   751     fun err_appl why bs t T u U =
       
   752       let
       
   753         val xs = map Free bs;           (*we do not rename here*)
       
   754         val t' = subst_bounds (xs, t);
       
   755         val u' = subst_bounds (xs, u);
       
   756         val text = cat_lines
       
   757           (TypeInfer.appl_error (Syntax.pp_show_brackets pp) why t' T u' U);
       
   758       in raise TYPE (text, [T, U], [t', u']) end;
       
   759 
       
   760     fun typ_of (_, Const (_, T)) = T
       
   761       | typ_of (_, Free  (_, T)) = T
       
   762       | typ_of (_, Var (_, T)) = T
       
   763       | typ_of (bs, Bound i) = snd (nth_elem (i, bs) handle LIST _ =>
       
   764           raise TYPE ("Loose bound variable: B." ^ string_of_int i, [], [Bound i]))
       
   765       | typ_of (bs, Abs (x, T, body)) = T --> typ_of ((x, T) :: bs, body)
       
   766       | typ_of (bs, t $ u) =
       
   767           let val T = typ_of (bs, t) and U = typ_of (bs, u) in
       
   768             (case T of
       
   769               Type ("fun", [T1, T2]) =>
       
   770                 if T1 = U then T2 else err_appl "Incompatible operand type" bs t T u U
       
   771             | _ => err_appl "Operator not of function type" bs t T u U)
       
   772           end;
       
   773 
       
   774   in typ_of ([], tm) end;
       
   775 
       
   776 fun certify_term pp sg tm =
   796 fun certify_term pp sg tm =
   777   let
   797   let
   778     val _ = check_stale sg;
   798     val _ = check_stale sg;
   779 
   799 
   780     val tm' = Term.map_term_types (Type.cert_typ (tsig_of sg)) tm;
   800     val (tm', maxidx) = cert_term_types (Type.cert_typ (tsig_of sg)) tm;
   781     val tm' = if tm = tm' then tm else tm';  (*avoid copying of already normal term*)
   801     val tm' = if tm = tm' then tm else tm';  (*avoid copying of already normal term*)
   782 
   802 
   783     fun err msg = raise TYPE (msg, [], [tm']);
   803     fun err msg = raise TYPE (msg, [], [tm']);
   784 
   804 
   785     fun show_const a T = quote a ^ " :: " ^ Pretty.string_of_typ pp T;
   805     fun show_const a T = quote a ^ " :: " ^ Pretty.string_of_typ pp T;
   796           if i < 0 then err ("Malformed variable: " ^ quote x) else ()
   816           if i < 0 then err ("Malformed variable: " ^ quote x) else ()
   797       | check_atoms _ = ();
   817       | check_atoms _ = ();
   798   in
   818   in
   799     check_atoms tm';
   819     check_atoms tm';
   800     nodup_vars tm';
   820     nodup_vars tm';
   801     (tm', type_check pp sg tm', maxidx_of_term tm')
   821     (tm', type_check pp sg tm', maxidx)
   802   end;
   822   end;
       
   823 
       
   824 end;
   803 
   825 
   804 
   826 
   805 
   827 
   806 (** instantiation **)
   828 (** instantiation **)
   807 
   829 
   977 
   999 
   978 fun ext_cnsts rd_const syn_only prmode sg (syn, tsig, ctab, (path, spaces), data)
  1000 fun ext_cnsts rd_const syn_only prmode sg (syn, tsig, ctab, (path, spaces), data)
   979     raw_consts =
  1001     raw_consts =
   980   let
  1002   let
   981     val prep_typ = compress_type o Type.varifyT o Type.no_tvars o
  1003     val prep_typ = compress_type o Type.varifyT o Type.no_tvars o
   982       (if syn_only then Type.cert_typ_syntax tsig else Term.no_dummyT o Type.cert_typ tsig);
  1004      (if syn_only then #1 o Type.cert_typ_syntax tsig
       
  1005       else Term.no_dummyT o #1 o Type.cert_typ tsig);
   983     fun prep_const (c, ty, mx) = (c, prep_typ ty, mx) handle TYPE (msg, _, _) =>
  1006     fun prep_const (c, ty, mx) = (c, prep_typ ty, mx) handle TYPE (msg, _, _) =>
   984       (error_msg msg; err_in_const (const_name path c mx));
  1007       (error_msg msg; err_in_const (const_name path c mx));
   985 
  1008 
   986     val consts = map (prep_const o rd_const sg syn tsig (path, spaces)) raw_consts;
  1009     val consts = map (prep_const o rd_const sg syn tsig (path, spaces)) raw_consts;
   987     val decls =
  1010     val decls =