src/Tools/code/code_thingol.ML
changeset 28706 3fef773ae6b1
parent 28688 1a9fabb515cd
child 28708 a1a436f09ec6
equal deleted inserted replaced
28705:c77a9f5672f8 28706:3fef773ae6b1
    55   val fold_constnames: (string -> 'a -> 'a) -> iterm -> 'a -> 'a
    55   val fold_constnames: (string -> 'a -> 'a) -> iterm -> 'a -> 'a
    56   val fold_varnames: (string -> 'a -> 'a) -> iterm -> 'a -> 'a
    56   val fold_varnames: (string -> 'a -> 'a) -> iterm -> 'a -> 'a
    57   val fold_unbound_varnames: (string -> 'a -> 'a) -> iterm -> 'a -> 'a
    57   val fold_unbound_varnames: (string -> 'a -> 'a) -> iterm -> 'a -> 'a
    58 
    58 
    59   type naming
    59   type naming
       
    60   val empty_naming: naming
    60   val lookup_class: naming -> class -> string option
    61   val lookup_class: naming -> class -> string option
    61   val lookup_classrel: naming -> class * class -> string option
    62   val lookup_classrel: naming -> class * class -> string option
    62   val lookup_tyco: naming -> string -> string option
    63   val lookup_tyco: naming -> string -> string option
    63   val lookup_instance: naming -> class * string -> string option
    64   val lookup_instance: naming -> class * string -> string option
    64   val lookup_const: naming -> string -> string option
    65   val lookup_const: naming -> string -> string option
   257   fun thyname_of_tyco thy =
   258   fun thyname_of_tyco thy =
   258     thyname_of thy (Type.the_tags (Sign.tsig_of thy));
   259     thyname_of thy (Type.the_tags (Sign.tsig_of thy));
   259   fun thyname_of_instance thy inst = case AxClass.arity_property thy inst Markup.theory_nameN
   260   fun thyname_of_instance thy inst = case AxClass.arity_property thy inst Markup.theory_nameN
   260    of [] => error ("no such instance: " ^ quote (snd inst ^ " :: " ^ fst inst))
   261    of [] => error ("no such instance: " ^ quote (snd inst ^ " :: " ^ fst inst))
   261     | thyname :: _ => thyname;
   262     | thyname :: _ => thyname;
   262   fun thyname_of_const thy c = case Code.get_datatype_of_constr thy c
   263   fun thyname_of_const thy c = case AxClass.class_of_param thy c
   263      of SOME dtco => thyname_of_tyco thy dtco
   264    of SOME class => thyname_of_class thy class
   264       | NONE => (case AxClass.class_of_param thy c
   265     | NONE => (case Code.get_datatype_of_constr thy c
   265          of SOME class => thyname_of_class thy class
   266        of SOME dtco => thyname_of_tyco thy dtco
   266           | NONE => (case AxClass.inst_of_param thy c
   267         | NONE => thyname_of thy (Consts.the_tags (Sign.consts_of thy)) c);
   267              of SOME (c, tyco) => thyname_of_instance thy
       
   268                   ((the o AxClass.class_of_param thy) c, tyco)
       
   269               | NONE => thyname_of thy (Consts.the_tags (Sign.consts_of thy)) c));
       
   270   fun namify thy get_basename get_thyname name =
   268   fun namify thy get_basename get_thyname name =
   271     let
   269     let
   272       val prefix = get_thyname thy name;
   270       val prefix = get_thyname thy name;
   273       val base = (Code_Name.purify_base true o get_basename) name;
   271       val base = (Code_Name.purify_base true o get_basename) name;
   274     in NameSpace.append prefix base end;
   272     in NameSpace.append prefix base end;
   451 
   449 
   452 (** translation kernel **)
   450 (** translation kernel **)
   453 
   451 
   454 fun ensure_stmt lookup declare generate thing (dep, (naming, program)) =
   452 fun ensure_stmt lookup declare generate thing (dep, (naming, program)) =
   455   let
   453   let
   456     fun add_dep name = case dep
   454     fun add_dep name = case dep of NONE => I
   457      of NONE => I | SOME dep => Graph.add_edge (dep, name);
   455       | SOME dep => Graph.add_edge (dep, name);
   458   in case lookup naming thing
   456     val (name, naming') = case lookup naming thing
   459    of SOME name => program
   457      of SOME name => (name, naming)
       
   458       | NONE => declare thing naming;
       
   459   in case try (Graph.get_node program) name
       
   460    of SOME stmt => program
   460         |> add_dep name
   461         |> add_dep name
   461         |> pair naming
   462         |> pair naming'
   462         |> pair dep
   463         |> pair dep
   463         |> pair name
   464         |> pair name
   464     | NONE => let
   465     | NONE => program
   465           val (name, naming') = declare thing naming;
   466         |> Graph.default_node (name, NoStmt)
   466         in
   467         |> add_dep name
   467           program
   468         |> pair naming'
   468           |> Graph.default_node (name, NoStmt)
   469         |> curry generate (SOME name)
   469           |> add_dep name
   470         ||> snd
   470           |> pair naming'
   471         |-> (fn stmt => (apsnd o Graph.map_node name) (K stmt))
   471           |> curry generate (SOME name)
   472         |> pair dep
   472           ||> snd
   473         |> pair name
   473           |-> (fn stmt => (apsnd o Graph.map_node name) (K stmt))
       
   474           |> pair dep
       
   475           |> pair name
       
   476         end
       
   477   end;
   474   end;
   478 
   475 
   479 fun not_wellsorted thy thm ty sort e =
   476 fun not_wellsorted thy thm ty sort e =
   480   let
   477   let
   481     val err_class = Sorts.class_error (Syntax.pp_global thy) e;
   478     val err_class = Sorts.class_error (Syntax.pp_global thy) e;
   717 
   714 
   718 structure Program = CodeDataFun
   715 structure Program = CodeDataFun
   719 (
   716 (
   720   type T = naming * program;
   717   type T = naming * program;
   721   val empty = (empty_naming, Graph.empty);
   718   val empty = (empty_naming, Graph.empty);
   722   fun purge thy cs (naming, program) = empty (*FIXME: problem: un-declaration of names
   719   fun purge thy cs (naming, program) =
   723     let
   720     let
   724       val cs_exisiting =
   721       val names_delete = cs
   725         map_filter (Code_Name.const_rev thy) (Graph.keys program);
   722         |> map_filter (lookup_const naming)
   726       val dels = (Graph.all_preds program
   723         |> filter (can (Graph.get_node program))
   727           o map (Code_Name.const thy)
   724         |> Graph.all_preds program;
   728           o filter (member (op =) cs_exisiting)
   725       val program' = Graph.del_nodes names_delete program;
   729         ) cs;
   726     in (naming, program') end;
   730     in Graph.del_nodes dels program end;*)
       
   731 );
   727 );
   732 
   728 
   733 val cached_program = Program.get;
   729 val cached_program = Program.get;
   734 
   730 
   735 fun generate thy funcgr f name =
   731 fun generate thy funcgr f name =