src/Tools/code/code_thingol.ML
changeset 27422 73d25d422124
parent 27103 d8549f4d900b
child 27609 b23c9ad0fe7d
equal deleted inserted replaced
27421:7e458bd56860 27422:73d25d422124
   296   | _ => [];
   296   | _ => [];
   297 
   297 
   298 
   298 
   299 (** translation kernel **)
   299 (** translation kernel **)
   300 
   300 
   301 type transaction = Graph.key option * program;
       
   302 
       
   303 fun ensure_stmt stmtgen name (dep, program) =
   301 fun ensure_stmt stmtgen name (dep, program) =
   304   let
   302   let
   305     val add_dep = case dep of NONE => I | SOME dep => Graph.add_edge (dep, name);
   303     val add_dep = case dep of NONE => I | SOME dep => Graph.add_edge (dep, name);
   306     fun add_stmt false =
   304     fun add_stmt false =
   307           Graph.default_node (name, NoStmt)
   305           Graph.default_node (name, NoStmt)
   336       fold_map (fn superclass => ensure_class thy algbr funcgr superclass
   334       fold_map (fn superclass => ensure_class thy algbr funcgr superclass
   337         ##>> ensure_classrel thy algbr funcgr (class, superclass)) superclasses
   335         ##>> ensure_classrel thy algbr funcgr (class, superclass)) superclasses
   338       ##>> fold_map (fn (c, ty) => ensure_const thy algbr funcgr c
   336       ##>> fold_map (fn (c, ty) => ensure_const thy algbr funcgr c
   339         ##>> exprgen_typ thy algbr funcgr ty) cs
   337         ##>> exprgen_typ thy algbr funcgr ty) cs
   340       #>> (fn info => Class (unprefix "'" Name.aT, info))
   338       #>> (fn info => Class (unprefix "'" Name.aT, info))
   341   in
   339   in ensure_stmt stmt_class class' end
   342     ensure_stmt stmt_class class'
       
   343   end
       
   344 and ensure_classrel thy algbr funcgr (subclass, superclass) =
   340 and ensure_classrel thy algbr funcgr (subclass, superclass) =
   345   let
   341   let
   346     val classrel' = CodeName.classrel thy (subclass, superclass);
   342     val classrel' = CodeName.classrel thy (subclass, superclass);
   347     val stmt_classrel =
   343     val stmt_classrel =
   348       ensure_class thy algbr funcgr subclass
   344       ensure_class thy algbr funcgr subclass
   349       ##>> ensure_class thy algbr funcgr superclass
   345       ##>> ensure_class thy algbr funcgr superclass
   350       #>> Classrel;
   346       #>> Classrel;
   351   in
   347   in ensure_stmt stmt_classrel classrel' end
   352     ensure_stmt stmt_classrel classrel'
       
   353   end
       
   354 and ensure_tyco thy algbr funcgr "fun" =
   348 and ensure_tyco thy algbr funcgr "fun" =
   355       pair "fun"
   349       pair "fun"
   356   | ensure_tyco thy algbr funcgr tyco =
   350   | ensure_tyco thy algbr funcgr tyco =
   357       let
   351       let
   358         val stmt_datatype =
   352         val stmt_datatype =
   364               ensure_const thy algbr funcgr c
   358               ensure_const thy algbr funcgr c
   365               ##>> fold_map (exprgen_typ thy algbr funcgr) tys) cos
   359               ##>> fold_map (exprgen_typ thy algbr funcgr) tys) cos
   366             #>> Datatype
   360             #>> Datatype
   367           end;
   361           end;
   368         val tyco' = CodeName.tyco thy tyco;
   362         val tyco' = CodeName.tyco thy tyco;
   369       in
   363       in ensure_stmt stmt_datatype tyco' end
   370         ensure_stmt stmt_datatype tyco'
       
   371       end
       
   372 and exprgen_tyvar_sort thy (algbr as (proj_sort, _)) funcgr (v, sort) =
   364 and exprgen_tyvar_sort thy (algbr as (proj_sort, _)) funcgr (v, sort) =
   373   fold_map (ensure_class thy algbr funcgr) (proj_sort sort)
   365   fold_map (ensure_class thy algbr funcgr) (proj_sort sort)
   374   #>> (fn sort => (unprefix "'" v, sort))
   366   #>> (fn sort => (unprefix "'" v, sort))
   375 and exprgen_typ thy algbr funcgr (TFree vs) =
   367 and exprgen_typ thy algbr funcgr (TFree v_sort) =
   376       exprgen_tyvar_sort thy algbr funcgr vs
   368       exprgen_tyvar_sort thy algbr funcgr v_sort
   377       #>> (fn (v, sort) => ITyVar v)
   369       #>> (fn (v, sort) => ITyVar v)
   378   | exprgen_typ thy algbr funcgr (Type (tyco, tys)) =
   370   | exprgen_typ thy algbr funcgr (Type (tyco, tys)) =
   379       ensure_tyco thy algbr funcgr tyco
   371       ensure_tyco thy algbr funcgr tyco
   380       ##>> fold_map (exprgen_typ thy algbr funcgr) tys
   372       ##>> fold_map (exprgen_typ thy algbr funcgr) tys
   381       #>> (fn (tyco, tys) => tyco `%% tys)
   373       #>> (fn (tyco, tys) => tyco `%% tys)
   404           ##>> (fold_map o fold_map) mk_dict yss
   396           ##>> (fold_map o fold_map) mk_dict yss
   405           #>> (fn (inst, dss) => DictConst (inst, dss))
   397           #>> (fn (inst, dss) => DictConst (inst, dss))
   406       | mk_dict (Local (classrels, (v, (k, sort)))) =
   398       | mk_dict (Local (classrels, (v, (k, sort)))) =
   407           fold_map (ensure_classrel thy algbr funcgr) classrels
   399           fold_map (ensure_classrel thy algbr funcgr) classrels
   408           #>> (fn classrels => DictVar (classrels, (unprefix "'" v, (k, length sort))))
   400           #>> (fn classrels => DictVar (classrels, (unprefix "'" v, (k, length sort))))
   409   in
   401   in fold_map mk_dict typargs end
   410     fold_map mk_dict typargs
       
   411   end
       
   412 and exprgen_eq thy algbr funcgr thm =
   402 and exprgen_eq thy algbr funcgr thm =
   413   let
   403   let
   414     val (args, rhs) = (apfst (snd o strip_comb) o Logic.dest_equals
   404     val (args, rhs) = (apfst (snd o strip_comb) o Logic.dest_equals
   415       o Logic.unvarify o prop_of) thm;
   405       o Logic.unvarify o prop_of) thm;
   416   in
   406   in
   452       ##>> fold_map exprgen_superarity superclasses
   442       ##>> fold_map exprgen_superarity superclasses
   453       ##>> fold_map exprgen_classparam_inst classparams
   443       ##>> fold_map exprgen_classparam_inst classparams
   454       #>> (fn ((((class, tyco), arity), superarities), classparams) =>
   444       #>> (fn ((((class, tyco), arity), superarities), classparams) =>
   455              Classinst ((class, (tyco, arity)), (superarities, classparams)));
   445              Classinst ((class, (tyco, arity)), (superarities, classparams)));
   456     val inst = CodeName.instance thy (class, tyco);
   446     val inst = CodeName.instance thy (class, tyco);
   457   in
   447   in ensure_stmt stmt_inst inst end
   458     ensure_stmt stmt_inst inst
       
   459   end
       
   460 and ensure_const thy algbr funcgr c =
   448 and ensure_const thy algbr funcgr c =
   461   let
   449   let
   462     val c' = CodeName.const thy c;
   450     val c' = CodeName.const thy c;
   463     fun stmt_datatypecons tyco =
   451     fun stmt_datatypecons tyco =
   464       ensure_tyco thy algbr funcgr tyco
   452       ensure_tyco thy algbr funcgr tyco
   484     val stmtgen = case Code.get_datatype_of_constr thy c
   472     val stmtgen = case Code.get_datatype_of_constr thy c
   485      of SOME tyco => stmt_datatypecons tyco
   473      of SOME tyco => stmt_datatypecons tyco
   486       | NONE => (case AxClass.class_of_param thy c
   474       | NONE => (case AxClass.class_of_param thy c
   487          of SOME class => stmt_classparam class
   475          of SOME class => stmt_classparam class
   488           | NONE => stmt_fun)
   476           | NONE => stmt_fun)
   489   in
   477   in ensure_stmt stmtgen c' end
   490     ensure_stmt stmtgen c'
       
   491   end
       
   492 and exprgen_term thy algbr funcgr thm (Const (c, ty)) =
   478 and exprgen_term thy algbr funcgr thm (Const (c, ty)) =
   493       exprgen_app thy algbr funcgr thm ((c, ty), [])
   479       exprgen_app thy algbr funcgr thm ((c, ty), [])
   494   | exprgen_term thy algbr funcgr thm (Free (v, _)) =
   480   | exprgen_term thy algbr funcgr thm (Free (v, _)) =
   495       pair (IVar v)
   481       pair (IVar v)
   496   | exprgen_term thy algbr funcgr thm (Abs (abs as (_, ty, _))) =
   482   | exprgen_term thy algbr funcgr thm (Abs (abs as (_, ty, _))) =