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