src/Tools/Code/code_thingol.ML
changeset 52138 e21426f244aa
parent 51685 385ef6706252
child 52161 51eca565b153
equal deleted inserted replaced
52137:7f7337447b1b 52138:e21426f244aa
    84   val empty_funs: program -> string list
    84   val empty_funs: program -> string list
    85   val map_terms_bottom_up: (iterm -> iterm) -> iterm -> iterm
    85   val map_terms_bottom_up: (iterm -> iterm) -> iterm -> iterm
    86   val map_terms_stmt: (iterm -> iterm) -> stmt -> stmt
    86   val map_terms_stmt: (iterm -> iterm) -> stmt -> stmt
    87   val is_constr: program -> string -> bool
    87   val is_constr: program -> string -> bool
    88   val is_case: stmt -> bool
    88   val is_case: stmt -> bool
    89   val labelled_name: theory -> program -> string -> string
    89   val symbol_of: program -> string -> Code_Symbol.symbol;
    90   val group_stmts: theory -> program
    90   val group_stmts: theory -> program
    91     -> ((string * stmt) list * (string * stmt) list
    91     -> ((string * stmt) list * (string * stmt) list
    92       * ((string * stmt) list * (string * stmt) list)) list
    92       * ((string * stmt) list * (string * stmt) list)) list
    93 
    93 
    94   val read_const_exprs: theory -> string list -> string list * string list
    94   val read_const_exprs: theory -> string list -> string list * string list
   469   | _ => false;
   469   | _ => false;
   470 
   470 
   471 fun is_case (Fun (_, (_, SOME _))) = true
   471 fun is_case (Fun (_, (_, SOME _))) = true
   472   | is_case _ = false;
   472   | is_case _ = false;
   473 
   473 
   474 fun labelled_name thy program name =
   474 fun symbol_of program name = 
   475   let val ctxt = Proof_Context.init_global thy in
   475   case try (Graph.get_node program) name of
   476     case Graph.get_node program name of
   476       NONE => Code_Symbol.Module "(unknown)"
   477       Fun (c, _) => quote (Code.string_of_const thy c)
   477         (*FIXME: need to be liberal due to ad-hoc introduction of value for evaluation*)
   478     | Datatype (tyco, _) => "type " ^ quote (Proof_Context.extern_type ctxt tyco)
   478     | SOME stmt => case stmt of
   479     | Datatypecons (c, _) => quote (Code.string_of_const thy c)
   479           Fun (c, _) => Code_Symbol.Constant c
   480     | Class (class, _) => "class " ^ quote (Proof_Context.extern_class ctxt class)
   480         | Datatype (tyco, _) => Code_Symbol.Type_Constructor tyco
   481     | Classrel (sub, super) =>
   481         | Datatypecons (c, _) => Code_Symbol.Constant c
   482         let
   482         | Class (class, _) => Code_Symbol.Type_Class class
   483           val Class (sub, _) = Graph.get_node program sub;
   483         | Classrel (sub, super) =>
   484           val Class (super, _) = Graph.get_node program super;
   484             let
   485         in
   485               val Class (sub, _) = Graph.get_node program sub;
   486           quote (Proof_Context.extern_class ctxt sub ^ " < " ^ Proof_Context.extern_class ctxt super)
   486               val Class (super, _) = Graph.get_node program super;
   487         end
   487             in
   488     | Classparam (c, _) => quote (Code.string_of_const thy c)
   488               Code_Symbol.Class_Relation (sub, super)
   489     | Classinst { class, tyco, ... } =>
   489             end
   490         let
   490         | Classparam (c, _) => Code_Symbol.Constant c
   491           val Class (class, _) = Graph.get_node program class;
   491         | Classinst { class, tyco, ... } =>
   492           val Datatype (tyco, _) = Graph.get_node program tyco;
   492             let
   493         in
   493               val Class (class, _) = Graph.get_node program class;
   494           quote (Proof_Context.extern_type ctxt tyco ^ " :: " ^ Proof_Context.extern_class ctxt class)
   494               val Datatype (tyco, _) = Graph.get_node program tyco;
   495         end
   495             in
   496   end;
   496               Code_Symbol.Class_Instance (tyco, class)
       
   497             end;
   497 
   498 
   498 fun linear_stmts program =
   499 fun linear_stmts program =
   499   rev (Graph.strong_conn program)
   500   rev (Graph.strong_conn program)
   500   |> map (AList.make (Graph.get_node program));
   501   |> map (AList.make (Graph.get_node program));
   501 
   502 
   513       then (filter is_datatype stmts, [], ([], []))
   514       then (filter is_datatype stmts, [], ([], []))
   514       else if forall (is_class orf is_classrel orf is_classparam) stmts
   515       else if forall (is_class orf is_classrel orf is_classparam) stmts
   515       then ([], filter is_class stmts, ([], []))
   516       then ([], filter is_class stmts, ([], []))
   516       else if forall (is_fun orf is_classinst) stmts
   517       else if forall (is_fun orf is_classinst) stmts
   517       then ([], [], List.partition is_fun stmts)
   518       then ([], [], List.partition is_fun stmts)
   518       else error ("Illegal mutual dependencies: " ^
   519       else error ("Illegal mutual dependencies: " ^ (commas
   519         (commas o map (labelled_name thy program o fst)) stmts)
   520         o map (Code_Symbol.quote_symbol (Proof_Context.init_global thy)
       
   521         o symbol_of program o fst)) stmts);
   520   in
   522   in
   521     linear_stmts program
   523     linear_stmts program
   522     |> map group
   524     |> map group
   523   end;
   525   end;
   524 
   526