src/Pure/Tools/codegen_names.ML
changeset 21461 51239d45247b
parent 21338 56d55dd30311
child 21858 05f57309170c
equal deleted inserted replaced
21460:cda5cd8bfd16 21461:51239d45247b
     1 (*  Title:      Pure/Tools/codegen_names.ML
     1 (*  Title:      Pure/Tools/codegen_names.ML
     2     ID:         $Id$
     2     ID:         $Id$
     3     Author:     Florian Haftmann, TU Muenchen
     3     Author:     Florian Haftmann, TU Muenchen
     4 
     4 
     5 Name policies for code generation: prefixing any name by corresponding theory name,
     5 Naming policies for code generation: prefixing any name by corresponding theory name,
     6 conversion to alphanumeric representation, shallow name spaces.
     6 conversion to alphanumeric representation, shallow name spaces.
     7 Mappings are incrementally cached.
     7 Mappings are incrementally cached.
     8 *)
     8 *)
     9 
     9 
    10 signature CODEGEN_NAMES =
    10 signature CODEGEN_NAMES =
   217   in
   217   in
   218     if mns' = mns then mn else error ("Invalid module name: " ^ quote mn ^ "\n"
   218     if mns' = mns then mn else error ("Invalid module name: " ^ quote mn ^ "\n"
   219       ^ "perhaps try " ^ quote (NameSpace.pack mns'))
   219       ^ "perhaps try " ^ quote (NameSpace.pack mns'))
   220   end
   220   end
   221 
   221 
   222 
       
   223 fun purify_var "" = "x"
   222 fun purify_var "" = "x"
   224   | purify_var v =
   223   | purify_var v =
   225       if nth (explode v) 0 = "'"
   224       if nth (explode v) 0 = "'"
   226       then (unprefix "'" #> purify_name #> purify_lower #> prefix "'") v
   225       then (unprefix "'" #> purify_name #> purify_lower #> prefix "'") v
   227       else (purify_name #> purify_lower) v;
   226       else (purify_name #> purify_lower) v;
   242 fun class_policy thy = policy thy NameSpace.base thyname_of_class;
   241 fun class_policy thy = policy thy NameSpace.base thyname_of_class;
   243 fun tyco_policy thy = policy thy NameSpace.base thyname_of_tyco;
   242 fun tyco_policy thy = policy thy NameSpace.base thyname_of_tyco;
   244 fun instance_policy thy = policy thy (fn (class, tyco) => 
   243 fun instance_policy thy = policy thy (fn (class, tyco) => 
   245   NameSpace.base class ^ "_" ^ NameSpace.base tyco) thyname_of_instance;
   244   NameSpace.base class ^ "_" ^ NameSpace.base tyco) thyname_of_instance;
   246 
   245 
   247 fun force_thyname thy (c, tys) =
   246 fun force_thyname thy (const as (c, tys)) =
   248   case AxClass.class_of_param thy c
   247   case AxClass.class_of_param thy c
   249    of SOME class => (case tys
   248    of SOME class => (case tys
   250        of [Type (tyco, _)] => SOME (thyname_of_instance thy (class, tyco))
   249        of [Type (tyco, _)] => SOME (thyname_of_instance thy (class, tyco))
   251         | _ => NONE)
   250         | _ => SOME (thyname_of_class thy class))
   252     | NONE => (case CodegenConsts.find_def thy (c, tys)
   251     | NONE => (case CodegenData.get_datatype_of_constr thy const
       
   252    of SOME dtco => SOME (thyname_of_tyco thy dtco)
       
   253     | NONE => (case CodegenConsts.find_def thy const
   253    of SOME ((thyname, _), _) => SOME thyname
   254    of SOME ((thyname, _), _) => SOME thyname
   254     | NONE => NONE);
   255     | NONE => NONE));
   255 
   256 
   256 fun const_policy thy (c, tys) =
   257 fun const_policy thy (c, tys) =
   257   case force_thyname thy (c, tys)
   258   case force_thyname thy (c, tys)
   258    of NONE => policy thy NameSpace.base thyname_of_const c
   259    of NONE => policy thy NameSpace.base thyname_of_const c
   259     | SOME thyname => let
   260     | SOME thyname => let