src/Tools/Code/code_thingol.ML
author haftmann
Fri Jul 02 16:15:49 2010 +0200 (2010-07-02)
changeset 37697 db7b5f2e19cd
parent 37640 fc27be4c6b1c
child 37698 e38abf437c20
permissions -rw-r--r--
traceback for error messages
     1 (*  Title:      Tools/code/code_thingol.ML
     2     Author:     Florian Haftmann, TU Muenchen
     3 
     4 Intermediate language ("Thin-gol") representing executable code.
     5 Representation and translation.
     6 *)
     7 
     8 infix 8 `%%;
     9 infix 4 `$;
    10 infix 4 `$$;
    11 infixr 3 `|=>;
    12 infixr 3 `|==>;
    13 
    14 signature BASIC_CODE_THINGOL =
    15 sig
    16   type vname = string;
    17   datatype dict =
    18       DictConst of string * dict list list
    19     | DictVar of string list * (vname * (int * int));
    20   datatype itype =
    21       `%% of string * itype list
    22     | ITyVar of vname;
    23   type const = string * ((itype list * dict list list) * itype list (*types of arguments*))
    24   datatype iterm =
    25       IConst of const
    26     | IVar of vname option
    27     | `$ of iterm * iterm
    28     | `|=> of (vname option * itype) * iterm
    29     | ICase of ((iterm * itype) * (iterm * iterm) list) * iterm;
    30         (*((term, type), [(selector pattern, body term )]), primitive term)*)
    31   val `$$ : iterm * iterm list -> iterm;
    32   val `|==> : (vname option * itype) list * iterm -> iterm;
    33   type typscheme = (vname * sort) list * itype;
    34 end;
    35 
    36 signature CODE_THINGOL =
    37 sig
    38   include BASIC_CODE_THINGOL
    39   val fun_tyco: string
    40   val unfoldl: ('a -> ('a * 'b) option) -> 'a -> 'a * 'b list
    41   val unfoldr: ('a -> ('b * 'a) option) -> 'a -> 'b list * 'a
    42   val unfold_fun: itype -> itype list * itype
    43   val unfold_fun_n: int -> itype -> itype list * itype
    44   val unfold_app: iterm -> iterm * iterm list
    45   val unfold_abs: iterm -> (vname option * itype) list * iterm
    46   val split_let: iterm -> (((iterm * itype) * iterm) * iterm) option
    47   val unfold_let: iterm -> ((iterm * itype) * iterm) list * iterm
    48   val split_pat_abs: iterm -> ((iterm * itype) * iterm) option
    49   val unfold_pat_abs: iterm -> (iterm * itype) list * iterm
    50   val unfold_const_app: iterm -> (const * iterm list) option
    51   val is_IVar: iterm -> bool
    52   val eta_expand: int -> const * iterm list -> iterm
    53   val contains_dictvar: iterm -> bool
    54   val locally_monomorphic: iterm -> bool
    55   val add_constnames: iterm -> string list -> string list
    56   val add_tyconames: iterm -> string list -> string list
    57   val fold_varnames: (string -> 'a -> 'a) -> iterm -> 'a -> 'a
    58 
    59   type naming
    60   val empty_naming: naming
    61   val lookup_class: naming -> class -> string option
    62   val lookup_classrel: naming -> class * class -> string option
    63   val lookup_tyco: naming -> string -> string option
    64   val lookup_instance: naming -> class * string -> string option
    65   val lookup_const: naming -> string -> string option
    66   val ensure_declared_const: theory -> string -> naming -> string * naming
    67 
    68   datatype stmt =
    69       NoStmt
    70     | Fun of string * ((typscheme * ((iterm list * iterm) * (thm option * bool)) list) * thm option)
    71     | Datatype of string * ((vname * sort) list *
    72         ((string * vname list (*type argument wrt. canonical order*)) * itype list) list)
    73     | Datatypecons of string * string
    74     | Class of class * (vname * ((class * string) list * (string * itype) list))
    75     | Classrel of class * class
    76     | Classparam of string * class
    77     | Classinst of (class * (string * (vname * sort) list) (*class and arity*))
    78           * ((class * (string * (string * dict list list))) list (*super instances*)
    79         * (((string * const) * (thm * bool)) list (*class parameter instances*)
    80           * ((string * const) * (thm * bool)) list (*super class parameter instances*)))
    81   type program = stmt Graph.T
    82   val empty_funs: program -> string list
    83   val map_terms_bottom_up: (iterm -> iterm) -> iterm -> iterm
    84   val map_terms_stmt: (iterm -> iterm) -> stmt -> stmt
    85   val is_cons: program -> string -> bool
    86   val is_case: stmt -> bool
    87   val contr_classparam_typs: program -> string -> itype option list
    88   val labelled_name: theory -> program -> string -> string
    89   val labelled_traceback: theory -> program -> string -> string
    90   val group_stmts: theory -> program
    91     -> ((string * stmt) list * (string * stmt) list
    92       * ((string * stmt) list * (string * stmt) list)) list
    93 
    94   val read_const_exprs: theory -> string list -> string list * string list
    95   val consts_program: theory -> bool -> string list -> string list * (naming * program)
    96   val eval_conv: theory
    97     -> (naming -> program -> ((string * sort) list * typscheme) * iterm -> string list -> cterm -> thm)
    98     -> cterm -> thm
    99   val eval: theory -> ((term -> term) -> 'a -> 'a)
   100     -> (naming -> program -> ((string * sort) list * typscheme) * iterm -> string list -> 'a)
   101     -> term -> 'a
   102 end;
   103 
   104 structure Code_Thingol: CODE_THINGOL =
   105 struct
   106 
   107 (** auxiliary **)
   108 
   109 fun unfoldl dest x =
   110   case dest x
   111    of NONE => (x, [])
   112     | SOME (x1, x2) =>
   113         let val (x', xs') = unfoldl dest x1 in (x', xs' @ [x2]) end;
   114 
   115 fun unfoldr dest x =
   116   case dest x
   117    of NONE => ([], x)
   118     | SOME (x1, x2) =>
   119         let val (xs', x') = unfoldr dest x2 in (x1::xs', x') end;
   120 
   121 
   122 (** language core - types, terms **)
   123 
   124 type vname = string;
   125 
   126 datatype dict =
   127     DictConst of string * dict list list
   128   | DictVar of string list * (vname * (int * int));
   129 
   130 datatype itype =
   131     `%% of string * itype list
   132   | ITyVar of vname;
   133 
   134 type const = string * ((itype list * dict list list) * itype list (*types of arguments*))
   135 
   136 datatype iterm =
   137     IConst of const
   138   | IVar of vname option
   139   | `$ of iterm * iterm
   140   | `|=> of (vname option * itype) * iterm
   141   | ICase of ((iterm * itype) * (iterm * iterm) list) * iterm;
   142     (*see also signature*)
   143 
   144 fun is_IVar (IVar _) = true
   145   | is_IVar _ = false;
   146 
   147 val op `$$ = Library.foldl (op `$);
   148 val op `|==> = Library.foldr (op `|=>);
   149 
   150 val unfold_app = unfoldl
   151   (fn op `$ t => SOME t
   152     | _ => NONE);
   153 
   154 val unfold_abs = unfoldr
   155   (fn op `|=> t => SOME t
   156     | _ => NONE);
   157 
   158 val split_let = 
   159   (fn ICase (((td, ty), [(p, t)]), _) => SOME (((p, ty), td), t)
   160     | _ => NONE);
   161 
   162 val unfold_let = unfoldr split_let;
   163 
   164 fun unfold_const_app t =
   165  case unfold_app t
   166   of (IConst c, ts) => SOME (c, ts)
   167    | _ => NONE;
   168 
   169 fun fold_constexprs f =
   170   let
   171     fun fold' (IConst c) = f c
   172       | fold' (IVar _) = I
   173       | fold' (t1 `$ t2) = fold' t1 #> fold' t2
   174       | fold' (_ `|=> t) = fold' t
   175       | fold' (ICase (((t, _), ds), _)) = fold' t
   176           #> fold (fn (pat, body) => fold' pat #> fold' body) ds
   177   in fold' end;
   178 
   179 val add_constnames = fold_constexprs (fn (c, _) => insert (op =) c);
   180 
   181 fun add_tycos (tyco `%% tys) = insert (op =) tyco #> fold add_tycos tys
   182   | add_tycos (ITyVar _) = I;
   183 
   184 val add_tyconames = fold_constexprs (fn (_, ((tys, _), _)) => fold add_tycos tys);
   185 
   186 fun fold_varnames f =
   187   let
   188     fun fold_aux add f =
   189       let
   190         fun fold_term _ (IConst _) = I
   191           | fold_term vs (IVar (SOME v)) = if member (op =) vs v then I else f v
   192           | fold_term _ (IVar NONE) = I
   193           | fold_term vs (t1 `$ t2) = fold_term vs t1 #> fold_term vs t2
   194           | fold_term vs ((SOME v, _) `|=> t) = fold_term (insert (op =) v vs) t
   195           | fold_term vs ((NONE, _) `|=> t) = fold_term vs t
   196           | fold_term vs (ICase (((t, _), ds), _)) = fold_term vs t #> fold (fold_case vs) ds
   197         and fold_case vs (p, t) = fold_term (add p vs) t;
   198       in fold_term [] end;
   199     fun add t = fold_aux add (insert (op =)) t;
   200   in fold_aux add f end;
   201 
   202 fun exists_var t v = fold_varnames (fn w => fn b => v = w orelse b) t false;
   203 
   204 fun split_pat_abs ((NONE, ty) `|=> t) = SOME ((IVar NONE, ty), t)
   205   | split_pat_abs ((SOME v, ty) `|=> t) = SOME (case t
   206      of ICase (((IVar (SOME w), _), [(p, t')]), _) =>
   207           if v = w andalso (exists_var p v orelse not (exists_var t' v))
   208           then ((p, ty), t')
   209           else ((IVar (SOME v), ty), t)
   210       | _ => ((IVar (SOME v), ty), t))
   211   | split_pat_abs _ = NONE;
   212 
   213 val unfold_pat_abs = unfoldr split_pat_abs;
   214 
   215 fun unfold_abs_eta [] t = ([], t)
   216   | unfold_abs_eta (_ :: tys) (v_ty `|=> t) =
   217       let
   218         val (vs_tys, t') = unfold_abs_eta tys t;
   219       in (v_ty :: vs_tys, t') end
   220   | unfold_abs_eta tys t =
   221       let
   222         val ctxt = fold_varnames Name.declare t Name.context;
   223         val vs_tys = (map o apfst) SOME (Name.names ctxt "a" tys);
   224       in (vs_tys, t `$$ map (IVar o fst) vs_tys) end;
   225 
   226 fun eta_expand k (c as (_, (_, tys)), ts) =
   227   let
   228     val j = length ts;
   229     val l = k - j;
   230     val ctxt = (fold o fold_varnames) Name.declare ts Name.context;
   231     val vs_tys = (map o apfst) SOME
   232       (Name.names ctxt "a" ((take l o drop j) tys));
   233   in vs_tys `|==> IConst c `$$ ts @ map (IVar o fst) vs_tys end;
   234 
   235 fun contains_dictvar t =
   236   let
   237     fun cont_dict (DictConst (_, dss)) = (exists o exists) cont_dict dss
   238       | cont_dict (DictVar _) = true;
   239     fun cont_term (IConst (_, ((_, dss), _))) = (exists o exists) cont_dict dss
   240       | cont_term (IVar _) = false
   241       | cont_term (t1 `$ t2) = cont_term t1 orelse cont_term t2
   242       | cont_term (_ `|=> t) = cont_term t
   243       | cont_term (ICase (_, t)) = cont_term t;
   244   in cont_term t end;
   245   
   246 fun locally_monomorphic (IConst _) = false
   247   | locally_monomorphic (IVar _) = true
   248   | locally_monomorphic (t `$ _) = locally_monomorphic t
   249   | locally_monomorphic (_ `|=> t) = locally_monomorphic t
   250   | locally_monomorphic (ICase ((_, ds), _)) = exists (locally_monomorphic o snd) ds;
   251 
   252 
   253 (** namings **)
   254 
   255 (* policies *)
   256 
   257 local
   258   fun thyname_of_class thy = #theory_name o Name_Space.the_entry (Sign.class_space thy);
   259   fun thyname_of_instance thy inst = case AxClass.thynames_of_arity thy inst
   260    of [] => error ("No such instance: " ^ quote (snd inst ^ " :: " ^ fst inst))
   261     | thyname :: _ => thyname;
   262   fun thyname_of_const thy c = case AxClass.class_of_param thy c
   263    of SOME class => thyname_of_class thy class
   264     | NONE => (case Code.get_type_of_constr_or_abstr thy c
   265        of SOME (tyco, _) => Codegen.thyname_of_type thy tyco
   266         | NONE => Codegen.thyname_of_const thy c);
   267   fun purify_base "==>" = "follows"
   268     | purify_base "op &" = "and"
   269     | purify_base "op |" = "or"
   270     | purify_base "op -->" = "implies"
   271     | purify_base "op :" = "member"
   272     | purify_base "op =" = "eq"
   273     | purify_base "*" = "product"
   274     | purify_base "+" = "sum"
   275     | purify_base s = Name.desymbolize false s;
   276   fun namify thy get_basename get_thyname name =
   277     let
   278       val prefix = get_thyname thy name;
   279       val base = (purify_base o get_basename) name;
   280     in Long_Name.append prefix base end;
   281 in
   282 
   283 fun namify_class thy = namify thy Long_Name.base_name thyname_of_class;
   284 fun namify_classrel thy = namify thy (fn (sub_class, super_class) => 
   285     Long_Name.base_name super_class ^ "_" ^ Long_Name.base_name sub_class)
   286   (fn thy => thyname_of_class thy o fst);
   287   (*order fits nicely with composed projections*)
   288 fun namify_tyco thy "fun" = "Pure.fun"
   289   | namify_tyco thy tyco = namify thy Long_Name.base_name Codegen.thyname_of_type tyco;
   290 fun namify_instance thy = namify thy (fn (class, tyco) => 
   291   Long_Name.base_name class ^ "_" ^ Long_Name.base_name tyco) thyname_of_instance;
   292 fun namify_const thy = namify thy Long_Name.base_name thyname_of_const;
   293 
   294 end; (* local *)
   295 
   296 
   297 (* data *)
   298 
   299 datatype naming = Naming of {
   300   class: class Symtab.table * Name.context,
   301   classrel: string Symreltab.table * Name.context,
   302   tyco: string Symtab.table * Name.context,
   303   instance: string Symreltab.table * Name.context,
   304   const: string Symtab.table * Name.context
   305 }
   306 
   307 fun dest_Naming (Naming naming) = naming;
   308 
   309 val empty_naming = Naming {
   310   class = (Symtab.empty, Name.context),
   311   classrel = (Symreltab.empty, Name.context),
   312   tyco = (Symtab.empty, Name.context),
   313   instance = (Symreltab.empty, Name.context),
   314   const = (Symtab.empty, Name.context)
   315 };
   316 
   317 local
   318   fun mk_naming (class, classrel, tyco, instance, const) =
   319     Naming { class = class, classrel = classrel,
   320       tyco = tyco, instance = instance, const = const };
   321   fun map_naming f (Naming { class, classrel, tyco, instance, const }) =
   322     mk_naming (f (class, classrel, tyco, instance, const));
   323 in
   324   fun map_class f = map_naming
   325     (fn (class, classrel, tyco, inst, const) =>
   326       (f class, classrel, tyco, inst, const));
   327   fun map_classrel f = map_naming
   328     (fn (class, classrel, tyco, inst, const) =>
   329       (class, f classrel, tyco, inst, const));
   330   fun map_tyco f = map_naming
   331     (fn (class, classrel, tyco, inst, const) =>
   332       (class, classrel, f tyco, inst, const));
   333   fun map_instance f = map_naming
   334     (fn (class, classrel, tyco, inst, const) =>
   335       (class, classrel, tyco, f inst, const));
   336   fun map_const f = map_naming
   337     (fn (class, classrel, tyco, inst, const) =>
   338       (class, classrel, tyco, inst, f const));
   339 end; (*local*)
   340 
   341 fun add_variant update (thing, name) (tab, used) =
   342   let
   343     val (name', used') = yield_singleton Name.variants name used;
   344     val tab' = update (thing, name') tab;
   345   in (tab', used') end;
   346 
   347 fun declare thy mapp lookup update namify thing =
   348   mapp (add_variant update (thing, namify thy thing))
   349   #> `(fn naming => the (lookup naming thing));
   350 
   351 
   352 (* lookup and declare *)
   353 
   354 local
   355 
   356 val suffix_class = "class";
   357 val suffix_classrel = "classrel"
   358 val suffix_tyco = "tyco";
   359 val suffix_instance = "inst";
   360 val suffix_const = "const";
   361 
   362 fun add_suffix nsp NONE = NONE
   363   | add_suffix nsp (SOME name) = SOME (Long_Name.append name nsp);
   364 
   365 in
   366 
   367 val lookup_class = add_suffix suffix_class
   368   oo Symtab.lookup o fst o #class o dest_Naming;
   369 val lookup_classrel = add_suffix suffix_classrel
   370   oo Symreltab.lookup o fst o #classrel o dest_Naming;
   371 val lookup_tyco = add_suffix suffix_tyco
   372   oo Symtab.lookup o fst o #tyco o dest_Naming;
   373 val lookup_instance = add_suffix suffix_instance
   374   oo Symreltab.lookup o fst o #instance o dest_Naming;
   375 val lookup_const = add_suffix suffix_const
   376   oo Symtab.lookup o fst o #const o dest_Naming;
   377 
   378 fun declare_class thy = declare thy map_class
   379   lookup_class Symtab.update_new namify_class;
   380 fun declare_classrel thy = declare thy map_classrel
   381   lookup_classrel Symreltab.update_new namify_classrel;
   382 fun declare_tyco thy = declare thy map_tyco
   383   lookup_tyco Symtab.update_new namify_tyco;
   384 fun declare_instance thy = declare thy map_instance
   385   lookup_instance Symreltab.update_new namify_instance;
   386 fun declare_const thy = declare thy map_const
   387   lookup_const Symtab.update_new namify_const;
   388 
   389 fun ensure_declared_const thy const naming =
   390   case lookup_const naming const
   391    of SOME const' => (const', naming)
   392     | NONE => declare_const thy const naming;
   393 
   394 val fun_tyco = Long_Name.append (namify_tyco Pure.thy "fun") suffix_tyco
   395   (*depends on add_suffix*);
   396 
   397 val unfold_fun = unfoldr
   398   (fn tyco `%% [ty1, ty2] => if tyco = fun_tyco then SOME (ty1, ty2) else NONE
   399     | _ => NONE);
   400 
   401 fun unfold_fun_n n ty =
   402   let
   403     val (tys1, ty1) = unfold_fun ty;
   404     val (tys3, tys2) = chop n tys1;
   405     val ty3 = Library.foldr (fn (ty1, ty2) => fun_tyco `%% [ty1, ty2]) (tys2, ty1);
   406   in (tys3, ty3) end;
   407 
   408 end; (* local *)
   409 
   410 
   411 (** statements, abstract programs **)
   412 
   413 type typscheme = (vname * sort) list * itype;
   414 datatype stmt =
   415     NoStmt
   416   | Fun of string * ((typscheme * ((iterm list * iterm) * (thm option * bool)) list) * thm option)
   417   | Datatype of string * ((vname * sort) list * ((string * vname list) * itype list) list)
   418   | Datatypecons of string * string
   419   | Class of class * (vname * ((class * string) list * (string * itype) list))
   420   | Classrel of class * class
   421   | Classparam of string * class
   422   | Classinst of (class * (string * (vname * sort) list))
   423         * ((class * (string * (string * dict list list))) list
   424       * (((string * const) * (thm * bool)) list
   425         * ((string * const) * (thm * bool)) list))
   426       (*see also signature*);
   427 
   428 type program = stmt Graph.T;
   429 
   430 fun empty_funs program =
   431   Graph.fold (fn (name, (Fun (c, ((_, []), _)), _)) => cons c
   432                | _ => I) program [];
   433 
   434 fun map_terms_bottom_up f (t as IConst _) = f t
   435   | map_terms_bottom_up f (t as IVar _) = f t
   436   | map_terms_bottom_up f (t1 `$ t2) = f
   437       (map_terms_bottom_up f t1 `$ map_terms_bottom_up f t2)
   438   | map_terms_bottom_up f ((v, ty) `|=> t) = f
   439       ((v, ty) `|=> map_terms_bottom_up f t)
   440   | map_terms_bottom_up f (ICase (((t, ty), ps), t0)) = f
   441       (ICase (((map_terms_bottom_up f t, ty), (map o pairself)
   442         (map_terms_bottom_up f) ps), map_terms_bottom_up f t0));
   443 
   444 fun map_classparam_instances_as_term f =
   445   (map o apfst o apsnd) (fn const => case f (IConst const) of IConst const' => const')
   446 
   447 fun map_terms_stmt f NoStmt = NoStmt
   448   | map_terms_stmt f (Fun (c, ((tysm, eqs), case_cong))) = Fun (c, ((tysm, (map o apfst)
   449       (fn (ts, t) => (map f ts, f t)) eqs), case_cong))
   450   | map_terms_stmt f (stmt as Datatype _) = stmt
   451   | map_terms_stmt f (stmt as Datatypecons _) = stmt
   452   | map_terms_stmt f (stmt as Class _) = stmt
   453   | map_terms_stmt f (stmt as Classrel _) = stmt
   454   | map_terms_stmt f (stmt as Classparam _) = stmt
   455   | map_terms_stmt f (Classinst (arity, (super_instances, classparam_instances))) =
   456       Classinst (arity, (super_instances, (pairself o map_classparam_instances_as_term) f classparam_instances));
   457 
   458 fun is_cons program name = case Graph.get_node program name
   459  of Datatypecons _ => true
   460   | _ => false;
   461 
   462 fun is_case (Fun (_, (_, SOME _))) = true
   463   | is_case _ = false;
   464 
   465 fun contr_classparam_typs program name = case Graph.get_node program name
   466  of Classparam (_, class) => let
   467         val Class (_, (_, (_, params))) = Graph.get_node program class;
   468         val SOME ty = AList.lookup (op =) params name;
   469         val (tys, res_ty) = unfold_fun ty;
   470         fun no_tyvar (_ `%% tys) = forall no_tyvar tys
   471           | no_tyvar (ITyVar _) = false;
   472       in if no_tyvar res_ty
   473         then map (fn ty => if no_tyvar ty then NONE else SOME ty) tys
   474         else []
   475       end
   476   | _ => [];
   477 
   478 fun labelled_name thy program name = case Graph.get_node program name
   479  of Fun (c, _) => quote (Code.string_of_const thy c)
   480   | Datatype (tyco, _) => "type " ^ quote (Sign.extern_type thy tyco)
   481   | Datatypecons (c, _) => quote (Code.string_of_const thy c)
   482   | Class (class, _) => "class " ^ quote (Sign.extern_class thy class)
   483   | Classrel (sub, super) => let
   484         val Class (sub, _) = Graph.get_node program sub
   485         val Class (super, _) = Graph.get_node program super
   486       in quote (Sign.extern_class thy sub ^ " < " ^ Sign.extern_class thy super) end
   487   | Classparam (c, _) => quote (Code.string_of_const thy c)
   488   | Classinst ((class, (tyco, _)), _) => let
   489         val Class (class, _) = Graph.get_node program class
   490         val Datatype (tyco, _) = Graph.get_node program tyco
   491       in quote (Sign.extern_type thy tyco ^ " :: " ^ Sign.extern_class thy class) end
   492 
   493 fun labelled_traceback thy program name =
   494   let
   495     val minimals = Graph.minimals program;
   496     val traceback = these (get_first
   497       (fn minimal => case Graph.irreducible_paths program (minimal, name)
   498         of [] => NONE
   499          | p :: ps => SOME p) minimals);
   500   in space_implode " -> " (map (labelled_name thy program) traceback) end;
   501 
   502 fun linear_stmts program =
   503   rev (Graph.strong_conn program)
   504   |> map (AList.make (Graph.get_node program));
   505 
   506 fun group_stmts thy program =
   507   let
   508     fun is_fun (_, Fun _) = true | is_fun _ = false;
   509     fun is_datatypecons (_, Datatypecons _) = true | is_datatypecons _ = false;
   510     fun is_datatype (_, Datatype _) = true | is_datatype _ = false;
   511     fun is_class (_, Class _) = true | is_class _ = false;
   512     fun is_classrel (_, Classrel _) = true | is_classrel _ = false;
   513     fun is_classparam (_, Classparam _) = true | is_classparam _ = false;
   514     fun is_classinst (_, Classinst _) = true | is_classinst _ = false;
   515     fun group stmts =
   516       if forall (is_datatypecons orf is_datatype) stmts
   517       then (filter is_datatype stmts, [], ([], []))
   518       else if forall (is_class orf is_classrel orf is_classparam) stmts
   519       then ([], filter is_class stmts, ([], []))
   520       else if forall (is_fun orf is_classinst) stmts
   521       then ([], [], List.partition is_fun stmts)
   522       else error ("Illegal mutual dependencies: " ^
   523         (commas o map (labelled_name thy program o fst)) stmts)
   524   in
   525     linear_stmts program
   526     |> map group
   527   end;
   528 
   529 
   530 (** translation kernel **)
   531 
   532 (* generic mechanisms *)
   533 
   534 fun ensure_stmt lookup declare generate thing (dep, (naming, program)) =
   535   let
   536     fun add_dep name = case dep of NONE => I
   537       | SOME dep => Graph.add_edge (dep, name);
   538     val (name, naming') = case lookup naming thing
   539      of SOME name => (name, naming)
   540       | NONE => declare thing naming;
   541   in case try (Graph.get_node program) name
   542    of SOME stmt => program
   543         |> add_dep name
   544         |> pair naming'
   545         |> pair dep
   546         |> pair name
   547     | NONE => program
   548         |> Graph.default_node (name, NoStmt)
   549         |> add_dep name
   550         |> pair naming'
   551         |> curry generate (SOME name)
   552         ||> snd
   553         |-> (fn stmt => (apsnd o Graph.map_node name) (K stmt))
   554         |> pair dep
   555         |> pair name
   556   end;
   557 
   558 exception PERMISSIVE of unit;
   559 
   560 fun translation_error thy permissive some_thm msg sub_msg (dep, (_, program)) =
   561   if permissive
   562   then raise PERMISSIVE ()
   563   else let
   564     val err_thm = case some_thm
   565      of SOME thm => "\n(in code equation " ^ Display.string_of_thm_global thy thm ^ ")"
   566       | NONE => "";
   567     val traceback = case dep
   568      of SOME name => labelled_traceback thy program name
   569       | NONE => "";
   570     val err_traceback = if traceback = ""
   571       then "" else "\n(dependencies " ^ traceback ^ ")";
   572   in error (msg ^ err_thm ^ err_traceback ^ ":\n" ^ sub_msg) end;
   573 
   574 
   575 (* translation *)
   576 
   577 fun ensure_tyco thy algbr eqngr permissive tyco =
   578   let
   579     val (vs, cos) = Code.get_type thy tyco;
   580     val stmt_datatype =
   581       fold_map (translate_tyvar_sort thy algbr eqngr permissive) vs
   582       ##>> fold_map (fn ((c, vs), tys) =>
   583         ensure_const thy algbr eqngr permissive c
   584         ##>> pair (map (unprefix "'") vs)
   585         ##>> fold_map (translate_typ thy algbr eqngr permissive) tys) cos
   586       #>> (fn info => Datatype (tyco, info));
   587   in ensure_stmt lookup_tyco (declare_tyco thy) stmt_datatype tyco end
   588 and ensure_const thy algbr eqngr permissive c =
   589   let
   590     fun stmt_datatypecons tyco =
   591       ensure_tyco thy algbr eqngr permissive tyco
   592       #>> (fn tyco => Datatypecons (c, tyco));
   593     fun stmt_classparam class =
   594       ensure_class thy algbr eqngr permissive class
   595       #>> (fn class => Classparam (c, class));
   596     fun stmt_fun cert =
   597       let
   598         val ((vs, ty), eqns) = Code.equations_of_cert thy cert;
   599         val some_case_cong = Code.get_case_cong thy c;
   600       in
   601         fold_map (translate_tyvar_sort thy algbr eqngr permissive) vs
   602         ##>> translate_typ thy algbr eqngr permissive ty
   603         ##>> translate_eqns thy algbr eqngr permissive eqns
   604         #>> (fn info => Fun (c, (info, some_case_cong)))
   605       end;
   606     val stmt_const = case Code.get_type_of_constr_or_abstr thy c
   607      of SOME (tyco, _) => stmt_datatypecons tyco
   608       | NONE => (case AxClass.class_of_param thy c
   609          of SOME class => stmt_classparam class
   610           | NONE => stmt_fun (Code_Preproc.cert eqngr c))
   611   in ensure_stmt lookup_const (declare_const thy) stmt_const c end
   612 and ensure_class thy (algbr as (_, algebra)) eqngr permissive class =
   613   let
   614     val super_classes = (Sorts.minimize_sort algebra o Sorts.super_classes algebra) class;
   615     val cs = #params (AxClass.get_info thy class);
   616     val stmt_class =
   617       fold_map (fn super_class => ensure_class thy algbr eqngr permissive super_class
   618         ##>> ensure_classrel thy algbr eqngr permissive (class, super_class)) super_classes
   619       ##>> fold_map (fn (c, ty) => ensure_const thy algbr eqngr permissive c
   620         ##>> translate_typ thy algbr eqngr permissive ty) cs
   621       #>> (fn info => Class (class, (unprefix "'" Name.aT, info)))
   622   in ensure_stmt lookup_class (declare_class thy) stmt_class class end
   623 and ensure_classrel thy algbr eqngr permissive (sub_class, super_class) =
   624   let
   625     val stmt_classrel =
   626       ensure_class thy algbr eqngr permissive sub_class
   627       ##>> ensure_class thy algbr eqngr permissive super_class
   628       #>> Classrel;
   629   in ensure_stmt lookup_classrel (declare_classrel thy) stmt_classrel (sub_class, super_class) end
   630 and ensure_inst thy (algbr as (_, algebra)) eqngr permissive (class, tyco) =
   631   let
   632     val super_classes = (Sorts.minimize_sort algebra o Sorts.super_classes algebra) class;
   633     val these_classparams = these o try (#params o AxClass.get_info thy);
   634     val classparams = these_classparams class;
   635     val further_classparams = maps these_classparams
   636       ((Sorts.complete_sort algebra o Sorts.super_classes algebra) class);
   637     val vs = Name.names Name.context "'a" (Sorts.mg_domain algebra tyco [class]);
   638     val sorts' = Sorts.mg_domain (Sign.classes_of thy) tyco [class];
   639     val vs' = map2 (fn (v, sort1) => fn sort2 => (v,
   640       Sorts.inter_sort (Sign.classes_of thy) (sort1, sort2))) vs sorts';
   641     val arity_typ = Type (tyco, map TFree vs);
   642     val arity_typ' = Type (tyco, map (fn (v, sort) => TVar ((v, 0), sort)) vs');
   643     fun translate_super_instance super_class =
   644       ensure_class thy algbr eqngr permissive super_class
   645       ##>> ensure_classrel thy algbr eqngr permissive (class, super_class)
   646       ##>> translate_dicts thy algbr eqngr permissive NONE (arity_typ, [super_class])
   647       #>> (fn ((super_class, classrel), [DictConst (inst, dss)]) =>
   648             (super_class, (classrel, (inst, dss))));
   649     fun translate_classparam_instance (c, ty) =
   650       let
   651         val raw_const = Const (c, map_type_tfree (K arity_typ') ty);
   652         val thm = AxClass.unoverload_conv thy (Thm.cterm_of thy raw_const);
   653         val const = (apsnd Logic.unvarifyT_global o dest_Const o snd
   654           o Logic.dest_equals o Thm.prop_of) thm;
   655       in
   656         ensure_const thy algbr eqngr permissive c
   657         ##>> translate_const thy algbr eqngr permissive (SOME thm) (const, NONE)
   658         #>> (fn (c, IConst const') => ((c, const'), (thm, true)))
   659       end;
   660     val stmt_inst =
   661       ensure_class thy algbr eqngr permissive class
   662       ##>> ensure_tyco thy algbr eqngr permissive tyco
   663       ##>> fold_map (translate_tyvar_sort thy algbr eqngr permissive) vs
   664       ##>> fold_map translate_super_instance super_classes
   665       ##>> fold_map translate_classparam_instance classparams
   666       ##>> fold_map translate_classparam_instance further_classparams
   667       #>> (fn (((((class, tyco), arity_args), super_instances),
   668         classparam_instances), further_classparam_instances) =>
   669           Classinst ((class, (tyco, arity_args)), (super_instances,
   670             (classparam_instances, further_classparam_instances))));
   671   in ensure_stmt lookup_instance (declare_instance thy) stmt_inst (class, tyco) end
   672 and translate_typ thy algbr eqngr permissive (TFree (v, _)) =
   673       pair (ITyVar (unprefix "'" v))
   674   | translate_typ thy algbr eqngr permissive (Type (tyco, tys)) =
   675       ensure_tyco thy algbr eqngr permissive tyco
   676       ##>> fold_map (translate_typ thy algbr eqngr permissive) tys
   677       #>> (fn (tyco, tys) => tyco `%% tys)
   678 and translate_term thy algbr eqngr permissive some_thm (Const (c, ty), some_abs) =
   679       translate_app thy algbr eqngr permissive some_thm (((c, ty), []), some_abs)
   680   | translate_term thy algbr eqngr permissive some_thm (Free (v, _), some_abs) =
   681       pair (IVar (SOME v))
   682   | translate_term thy algbr eqngr permissive some_thm (Abs (v, ty, t), some_abs) =
   683       let
   684         val (v', t') = Syntax.variant_abs (Name.desymbolize false v, ty, t);
   685         val v'' = if member (op =) (Term.add_free_names t' []) v'
   686           then SOME v' else NONE
   687       in
   688         translate_typ thy algbr eqngr permissive ty
   689         ##>> translate_term thy algbr eqngr permissive some_thm (t', some_abs)
   690         #>> (fn (ty, t) => (v'', ty) `|=> t)
   691       end
   692   | translate_term thy algbr eqngr permissive some_thm (t as _ $ _, some_abs) =
   693       case strip_comb t
   694        of (Const (c, ty), ts) =>
   695             translate_app thy algbr eqngr permissive some_thm (((c, ty), ts), some_abs)
   696         | (t', ts) =>
   697             translate_term thy algbr eqngr permissive some_thm (t', some_abs)
   698             ##>> fold_map (translate_term thy algbr eqngr permissive some_thm o rpair NONE) ts
   699             #>> (fn (t, ts) => t `$$ ts)
   700 and translate_eqn thy algbr eqngr permissive ((args, (rhs, some_abs)), (some_thm, proper)) =
   701   fold_map (translate_term thy algbr eqngr permissive some_thm) args
   702   ##>> translate_term thy algbr eqngr permissive some_thm (rhs, some_abs)
   703   #>> rpair (some_thm, proper)
   704 and translate_eqns thy algbr eqngr permissive eqns prgrm =
   705   prgrm |> fold_map (translate_eqn thy algbr eqngr permissive) eqns
   706     handle PERMISSIVE () => ([], prgrm)
   707 and translate_const thy algbr eqngr permissive some_thm ((c, ty), some_abs) =
   708   let
   709     val abstraction_violation = (case some_abs of NONE => true | SOME abs => not (c = abs))
   710         andalso Code.is_abstr thy c
   711     val arg_typs = Sign.const_typargs thy (c, ty);
   712     val sorts = Code_Preproc.sortargs eqngr c;
   713     val function_typs = (fst o Term.strip_type) ty;
   714   in
   715     abstraction_violation ? translation_error thy permissive some_thm
   716       "Abstraction violation" ("constant " ^ Code.string_of_const thy c)
   717     #> ensure_const thy algbr eqngr permissive c
   718     ##>> fold_map (translate_typ thy algbr eqngr permissive) arg_typs
   719     ##>> fold_map (translate_dicts thy algbr eqngr permissive some_thm) (arg_typs ~~ sorts)
   720     ##>> fold_map (translate_typ thy algbr eqngr permissive) function_typs
   721     #>> (fn (((c, arg_typs), dss), function_typs) => IConst (c, ((arg_typs, dss), function_typs)))
   722   end
   723 and translate_app_const thy algbr eqngr permissive some_thm ((c_ty, ts), some_abs) =
   724   translate_const thy algbr eqngr permissive some_thm (c_ty, some_abs)
   725   ##>> fold_map (translate_term thy algbr eqngr permissive some_thm o rpair NONE) ts
   726   #>> (fn (t, ts) => t `$$ ts)
   727 and translate_case thy algbr eqngr permissive some_thm (num_args, (t_pos, case_pats)) (c_ty, ts) =
   728   let
   729     fun arg_types num_args ty = (fst o chop num_args o fst o strip_type) ty;
   730     val tys = arg_types num_args (snd c_ty);
   731     val ty = nth tys t_pos;
   732     fun mk_constr c t = let val n = Code.args_number thy c
   733       in ((c, arg_types n (fastype_of t) ---> ty), n) end;
   734     val constrs = if null case_pats then []
   735       else map2 mk_constr case_pats (nth_drop t_pos ts);
   736     fun casify naming constrs ty ts =
   737       let
   738         val undefineds = map_filter (lookup_const naming) (Code.undefineds thy);
   739         fun collapse_clause vs_map ts body =
   740           let
   741           in case body
   742            of IConst (c, _) => if member (op =) undefineds c
   743                 then []
   744                 else [(ts, body)]
   745             | ICase (((IVar (SOME v), _), subclauses), _) =>
   746                 if forall (fn (pat', body') => exists_var pat' v
   747                   orelse not (exists_var body' v)) subclauses
   748                 then case AList.lookup (op =) vs_map v
   749                  of SOME i => maps (fn (pat', body') =>
   750                       collapse_clause (AList.delete (op =) v vs_map)
   751                         (nth_map i (K pat') ts) body') subclauses
   752                   | NONE => [(ts, body)]
   753                 else [(ts, body)]
   754             | _ => [(ts, body)]
   755           end;
   756         fun mk_clause mk tys t =
   757           let
   758             val (vs, body) = unfold_abs_eta tys t;
   759             val vs_map = fold_index (fn (i, (SOME v, _)) => cons (v, i) | _ => I) vs [];
   760             val ts = map (IVar o fst) vs;
   761           in map mk (collapse_clause vs_map ts body) end;
   762         val t = nth ts t_pos;
   763         val ts_clause = nth_drop t_pos ts;
   764         val clauses = if null case_pats
   765           then mk_clause (fn ([t], body) => (t, body)) [ty] (the_single ts_clause)
   766           else maps (fn ((constr as IConst (_, (_, tys)), n), t) =>
   767             mk_clause (fn (ts, body) => (constr `$$ ts, body)) (take n tys) t)
   768               (constrs ~~ ts_clause);
   769       in ((t, ty), clauses) end;
   770   in
   771     translate_const thy algbr eqngr permissive some_thm (c_ty, NONE)
   772     ##>> fold_map (fn (constr, n) => translate_const thy algbr eqngr permissive some_thm (constr, NONE)
   773       #>> rpair n) constrs
   774     ##>> translate_typ thy algbr eqngr permissive ty
   775     ##>> fold_map (translate_term thy algbr eqngr permissive some_thm o rpair NONE) ts
   776     #-> (fn (((t, constrs), ty), ts) =>
   777       `(fn (_, (naming, _)) => ICase (casify naming constrs ty ts, t `$$ ts)))
   778   end
   779 and translate_app_case thy algbr eqngr permissive some_thm (case_scheme as (num_args, _)) ((c, ty), ts) =
   780   if length ts < num_args then
   781     let
   782       val k = length ts;
   783       val tys = (take (num_args - k) o drop k o fst o strip_type) ty;
   784       val ctxt = (fold o fold_aterms) Term.declare_term_frees ts Name.context;
   785       val vs = Name.names ctxt "a" tys;
   786     in
   787       fold_map (translate_typ thy algbr eqngr permissive) tys
   788       ##>> translate_case thy algbr eqngr permissive some_thm case_scheme ((c, ty), ts @ map Free vs)
   789       #>> (fn (tys, t) => map2 (fn (v, _) => pair (SOME v)) vs tys `|==> t)
   790     end
   791   else if length ts > num_args then
   792     translate_case thy algbr eqngr permissive some_thm case_scheme ((c, ty), take num_args ts)
   793     ##>> fold_map (translate_term thy algbr eqngr permissive some_thm o rpair NONE) (drop num_args ts)
   794     #>> (fn (t, ts) => t `$$ ts)
   795   else
   796     translate_case thy algbr eqngr permissive some_thm case_scheme ((c, ty), ts)
   797 and translate_app thy algbr eqngr permissive some_thm (c_ty_ts as ((c, _), _), some_abs) =
   798   case Code.get_case_scheme thy c
   799    of SOME case_scheme => translate_app_case thy algbr eqngr permissive some_thm case_scheme c_ty_ts
   800     | NONE => translate_app_const thy algbr eqngr permissive some_thm (c_ty_ts, some_abs)
   801 and translate_tyvar_sort thy (algbr as (proj_sort, _)) eqngr permissive (v, sort) =
   802   fold_map (ensure_class thy algbr eqngr permissive) (proj_sort sort)
   803   #>> (fn sort => (unprefix "'" v, sort))
   804 and translate_dicts thy (algbr as (proj_sort, algebra)) eqngr permissive some_thm (ty, sort) =
   805   let
   806     fun format_class_error e =
   807       let
   808         val err_class = Sorts.class_error (Syntax.pp_global thy) e;
   809         val err_typ = "Type " ^ Syntax.string_of_typ_global thy ty ^ " not of sort "
   810           ^ Syntax.string_of_sort_global thy sort;
   811       in err_typ ^ "\n" ^ err_class end;
   812     datatype typarg =
   813         Global of (class * string) * typarg list list
   814       | Local of (class * class) list * (string * (int * sort));
   815     fun class_relation (Global ((_, tyco), yss), _) class =
   816           Global ((class, tyco), yss)
   817       | class_relation (Local (classrels, v), sub_class) super_class =
   818           Local ((sub_class, super_class) :: classrels, v);
   819     fun type_constructor (tyco, _) yss class =
   820       Global ((class, tyco), (map o map) fst yss);
   821     fun type_variable (TFree (v, sort)) =
   822       let
   823         val sort' = proj_sort sort;
   824       in map_index (fn (n, class) => (Local ([], (v, (n, sort'))), class)) sort' end;
   825     val (class_error, typargs) = ("", Sorts.of_sort_derivation algebra
   826       {class_relation = K (Sorts.classrel_derivation algebra class_relation),
   827        type_constructor = type_constructor,
   828        type_variable = type_variable} (ty, proj_sort sort))
   829       handle Sorts.CLASS_ERROR e => (format_class_error e, []);
   830     fun mk_dict (Global (inst, yss)) =
   831           ensure_inst thy algbr eqngr permissive inst
   832           ##>> (fold_map o fold_map) mk_dict yss
   833           #>> (fn (inst, dss) => DictConst (inst, dss))
   834       | mk_dict (Local (classrels, (v, (n, sort)))) =
   835           fold_map (ensure_classrel thy algbr eqngr permissive) classrels
   836           #>> (fn classrels => DictVar (classrels, (unprefix "'" v, (n, length sort))))
   837   in
   838     not (class_error = "")
   839       ? translation_error thy permissive some_thm "Wellsortedness error" class_error
   840     #> fold_map mk_dict typargs
   841   end;
   842 
   843 
   844 (* store *)
   845 
   846 structure Program = Code_Data
   847 (
   848   type T = naming * program;
   849   val empty = (empty_naming, Graph.empty);
   850 );
   851 
   852 fun cache_generation thy (algebra, eqngr) f name =
   853   Program.change_yield thy (fn naming_program => (NONE, naming_program)
   854     |> f thy algebra eqngr name
   855     |-> (fn name => fn (_, naming_program) => (name, naming_program)));
   856 
   857 fun transient_generation thy (algebra, eqngr) f name =
   858   (NONE, (empty_naming, Graph.empty))
   859   |> f thy algebra eqngr name
   860   |-> (fn name => fn (_, naming_program) => (name, naming_program));
   861 
   862 
   863 (* program generation *)
   864 
   865 fun consts_program thy permissive cs =
   866   let
   867     fun project_consts cs (naming, program) =
   868       let
   869         val cs_all = Graph.all_succs program cs;
   870       in (cs, (naming, Graph.subgraph (member (op =) cs_all) program)) end;
   871     fun generate_consts thy algebra eqngr =
   872       fold_map (ensure_const thy algebra eqngr permissive);
   873     val invoke_generation = if permissive
   874       then transient_generation else cache_generation
   875   in
   876     invoke_generation thy (Code_Preproc.obtain thy cs []) generate_consts cs
   877     |-> project_consts
   878   end;
   879 
   880 
   881 (* value evaluation *)
   882 
   883 fun ensure_value thy algbr eqngr t =
   884   let
   885     val ty = fastype_of t;
   886     val vs = fold_term_types (K (fold_atyps (insert (eq_fst op =)
   887       o dest_TFree))) t [];
   888     val stmt_value =
   889       fold_map (translate_tyvar_sort thy algbr eqngr false) vs
   890       ##>> translate_typ thy algbr eqngr false ty
   891       ##>> translate_term thy algbr eqngr false NONE (Code.subst_signatures thy t, NONE)
   892       #>> (fn ((vs, ty), t) => Fun
   893         (Term.dummy_patternN, (((vs, ty), [(([], t), (NONE, true))]), NONE)));
   894     fun term_value (dep, (naming, program1)) =
   895       let
   896         val Fun (_, ((vs_ty, [(([], t), _)]), _)) =
   897           Graph.get_node program1 Term.dummy_patternN;
   898         val deps = Graph.imm_succs program1 Term.dummy_patternN;
   899         val program2 = Graph.del_nodes [Term.dummy_patternN] program1;
   900         val deps_all = Graph.all_succs program2 deps;
   901         val program3 = Graph.subgraph (member (op =) deps_all) program2;
   902       in (((naming, program3), ((vs_ty, t), deps)), (dep, (naming, program2))) end;
   903   in
   904     ensure_stmt ((K o K) NONE) pair stmt_value Term.dummy_patternN
   905     #> snd
   906     #> term_value
   907   end;
   908 
   909 fun base_evaluator thy evaluator algebra eqngr vs t =
   910   let
   911     val (((naming, program), (((vs', ty'), t'), deps)), _) =
   912       cache_generation thy (algebra, eqngr) ensure_value t;
   913     val vs'' = map (fn (v, _) => (v, (the o AList.lookup (op =) vs o prefix "'") v)) vs';
   914   in evaluator naming program ((vs'', (vs', ty')), t') deps end;
   915 
   916 fun eval_conv thy = Code_Preproc.eval_conv thy o base_evaluator thy;
   917 fun eval thy postproc = Code_Preproc.eval thy postproc o base_evaluator thy;
   918 
   919 
   920 (** diagnostic commands **)
   921 
   922 fun read_const_exprs thy =
   923   let
   924     fun consts_of thy' = Symtab.fold (fn (c, (_, NONE)) => cons c | _ => I)
   925       ((snd o #constants o Consts.dest o #consts o Sign.rep_sg) thy') [];
   926     fun belongs_here thy' c = forall
   927       (fn thy'' => not (Sign.declared_const thy'' c)) (Theory.parents_of thy');
   928     fun consts_of_select thy' = filter (belongs_here thy') (consts_of thy');
   929     fun read_const_expr "*" = ([], consts_of thy)
   930       | read_const_expr s = if String.isSuffix ".*" s
   931           then ([], consts_of_select (Thy_Info.the_theory (unsuffix ".*" s) thy))
   932           else ([Code.read_const thy s], []);
   933   in pairself flat o split_list o map read_const_expr end;
   934 
   935 fun code_depgr thy consts =
   936   let
   937     val (_, eqngr) = Code_Preproc.obtain thy consts [];
   938     val all_consts = Graph.all_succs eqngr consts;
   939   in Graph.subgraph (member (op =) all_consts) eqngr end;
   940 
   941 fun code_thms thy = Pretty.writeln o Code_Preproc.pretty thy o code_depgr thy;
   942 
   943 fun code_deps thy consts =
   944   let
   945     val eqngr = code_depgr thy consts;
   946     val constss = Graph.strong_conn eqngr;
   947     val mapping = Symtab.empty |> fold (fn consts => fold (fn const =>
   948       Symtab.update (const, consts)) consts) constss;
   949     fun succs consts = consts
   950       |> maps (Graph.imm_succs eqngr)
   951       |> subtract (op =) consts
   952       |> map (the o Symtab.lookup mapping)
   953       |> distinct (op =);
   954     val conn = [] |> fold (fn consts => cons (consts, succs consts)) constss;
   955     fun namify consts = map (Code.string_of_const thy) consts
   956       |> commas;
   957     val prgr = map (fn (consts, constss) =>
   958       { name = namify consts, ID = namify consts, dir = "", unfold = true,
   959         path = "", parents = map namify constss }) conn;
   960   in Present.display_graph prgr end;
   961 
   962 local
   963 
   964 fun code_thms_cmd thy = code_thms thy o op @ o read_const_exprs thy;
   965 fun code_deps_cmd thy = code_deps thy o op @ o read_const_exprs thy;
   966 
   967 in
   968 
   969 val _ =
   970   Outer_Syntax.improper_command "code_thms" "print system of code equations for code" Keyword.diag
   971     (Scan.repeat1 Parse.term_group
   972       >> (fn cs => Toplevel.no_timing o Toplevel.unknown_theory
   973         o Toplevel.keep ((fn thy => code_thms_cmd thy cs) o Toplevel.theory_of)));
   974 
   975 val _ =
   976   Outer_Syntax.improper_command "code_deps" "visualize dependencies of code equations for code"
   977     Keyword.diag
   978     (Scan.repeat1 Parse.term_group
   979       >> (fn cs => Toplevel.no_timing o Toplevel.unknown_theory
   980         o Toplevel.keep ((fn thy => code_deps_cmd thy cs) o Toplevel.theory_of)));
   981 
   982 end;
   983 
   984 end; (*struct*)
   985 
   986 
   987 structure Basic_Code_Thingol: BASIC_CODE_THINGOL = Code_Thingol;