src/Tools/Code/code_target.ML
changeset 55147 bce3dbc11f95
parent 55146 525309c2e4ee
child 55149 626d8f08d479
     1.1 --- a/src/Tools/Code/code_target.ML	Sat Jan 25 23:50:49 2014 +0100
     1.2 +++ b/src/Tools/Code/code_target.ML	Sat Jan 25 23:50:49 2014 +0100
     1.3 @@ -11,26 +11,26 @@
     1.4    val read_const_exprs: theory -> string list -> string list
     1.5  
     1.6    val export_code_for: theory -> Path.T option -> string -> int option -> string -> Token.T list
     1.7 -    -> Code_Thingol.naming -> Code_Thingol.program -> string list -> unit
     1.8 +    -> Code_Thingol.program -> Code_Symbol.symbol list -> unit
     1.9    val produce_code_for: theory -> string -> int option -> string -> Token.T list
    1.10 -    -> Code_Thingol.naming -> Code_Thingol.program -> string list -> (string * string) list * string option list
    1.11 +    -> Code_Thingol.program -> Code_Symbol.symbol list -> (string * string) list * string option list
    1.12    val present_code_for: theory -> string -> int option -> string -> Token.T list
    1.13 -    -> Code_Thingol.naming -> Code_Thingol.program -> string list * string list -> string
    1.14 +    -> Code_Thingol.program -> Code_Symbol.symbol list * Code_Symbol.symbol list -> string
    1.15    val check_code_for: theory -> string -> bool -> Token.T list
    1.16 -    -> Code_Thingol.naming -> Code_Thingol.program -> string list -> unit
    1.17 +    -> Code_Thingol.program -> Code_Symbol.symbol list -> unit
    1.18  
    1.19    val export_code: theory -> string list
    1.20      -> (((string * string) * Path.T option) * Token.T list) list -> unit
    1.21    val produce_code: theory -> string list
    1.22      -> string -> int option -> string -> Token.T list -> (string * string) list * string option list
    1.23 -  val present_code: theory -> string list -> (Code_Thingol.naming -> string list)
    1.24 +  val present_code: theory -> string list -> Code_Symbol.symbol list
    1.25      -> string -> int option -> string -> Token.T list -> string
    1.26    val check_code: theory -> string list
    1.27      -> ((string * bool) * Token.T list) list -> unit
    1.28  
    1.29    val generatedN: string
    1.30 -  val evaluator: theory -> string -> Code_Thingol.naming -> Code_Thingol.program
    1.31 -    -> string list -> ((string * class list) list * Code_Thingol.itype) * Code_Thingol.iterm
    1.32 +  val evaluator: theory -> string -> Code_Thingol.program
    1.33 +    -> Code_Symbol.symbol list -> ((string * class list) list * Code_Thingol.itype) * Code_Thingol.iterm
    1.34      -> (string * string) list * string
    1.35  
    1.36    type serializer
    1.37 @@ -39,14 +39,14 @@
    1.38      check: { env_var: string, make_destination: Path.T -> Path.T, make_command: string -> string } }
    1.39      -> theory -> theory
    1.40    val extend_target: string *
    1.41 -      (string * (Code_Thingol.naming -> Code_Thingol.program -> Code_Thingol.program))
    1.42 +      (string * (Code_Thingol.program -> Code_Thingol.program))
    1.43      -> theory -> theory
    1.44    val assert_target: theory -> string -> string
    1.45    val the_literals: theory -> string -> literals
    1.46    type serialization
    1.47    val parse_args: 'a parser -> Token.T list -> 'a
    1.48    val serialization: (int -> Path.T option -> 'a -> unit)
    1.49 -    -> (string list -> int -> 'a -> (string * string) list * (string -> string option))
    1.50 +    -> (Code_Symbol.symbol list -> int -> 'a -> (string * string) list * (Code_Symbol.symbol -> string option))
    1.51      -> 'a -> serialization
    1.52    val set_default_code_width: int -> theory -> theory
    1.53  
    1.54 @@ -117,7 +117,7 @@
    1.55    (cert_class thy class, cert_tyco thy tyco);
    1.56  
    1.57  fun read_inst thy (raw_tyco, raw_class) =
    1.58 -  (read_class thy raw_class, read_tyco thy raw_tyco);
    1.59 +  (read_tyco thy raw_tyco, read_class thy raw_class);
    1.60  
    1.61  val parse_inst_ident = Parse.xname --| @{keyword "::"} -- Parse.class;
    1.62  
    1.63 @@ -154,21 +154,21 @@
    1.64  
    1.65  (* serialization: abstract nonsense to cover different destinies for generated code *)
    1.66  
    1.67 -datatype destination = Export of Path.T option | Produce | Present of string list;
    1.68 -type serialization = int -> destination -> ((string * string) list * (string -> string option)) option;
    1.69 +datatype destination = Export of Path.T option | Produce | Present of Code_Symbol.symbol list;
    1.70 +type serialization = int -> destination -> ((string * string) list * (Code_Symbol.symbol -> string option)) option;
    1.71  
    1.72  fun serialization output _ content width (Export some_path) =
    1.73        (output width some_path content; NONE)
    1.74    | serialization _ string content width Produce =
    1.75        string [] width content |> SOME
    1.76 -  | serialization _ string content width (Present stmt_names) =
    1.77 -     string stmt_names width content
    1.78 +  | serialization _ string content width (Present syms) =
    1.79 +     string syms width content
    1.80       |> (apfst o map o apsnd) (Pretty.output (SOME width) o Pretty.str)
    1.81       |> SOME;
    1.82  
    1.83  fun export some_path f = (f (Export some_path); ());
    1.84  fun produce f = the (f Produce);
    1.85 -fun present stmt_names f = space_implode "\n\n" (map snd (fst (the (f (Present stmt_names)))));
    1.86 +fun present syms f = space_implode "\n\n" (map snd (fst (the (f (Present syms)))));
    1.87  
    1.88  
    1.89  (* serializers: functions producing serializations *)
    1.90 @@ -176,7 +176,6 @@
    1.91  type serializer = Token.T list
    1.92    -> Proof.context
    1.93    -> {
    1.94 -    symbol_of: string -> Code_Symbol.symbol,
    1.95      module_name: string,
    1.96      reserved_syms: string list,
    1.97      identifiers: identifier_data,
    1.98 @@ -193,7 +192,7 @@
    1.99        check: { env_var: string, make_destination: Path.T -> Path.T,
   1.100          make_command: string -> string } }
   1.101    | Extension of string *
   1.102 -      (Code_Thingol.naming -> Code_Thingol.program -> Code_Thingol.program);
   1.103 +      (Code_Thingol.program -> Code_Thingol.program);
   1.104  
   1.105  
   1.106  (** theory data **)
   1.107 @@ -311,10 +310,10 @@
   1.108           of SOME data => data
   1.109            | NONE => error ("Unknown code target language: " ^ quote target);
   1.110        in case the_description data
   1.111 -       of Fundamental _ => (K I, data)
   1.112 +       of Fundamental _ => (I, data)
   1.113          | Extension (super, modify) => let
   1.114              val (modify', data') = collapse super
   1.115 -          in (fn naming => modify' naming #> modify naming, merge_target false target (data', data)) end
   1.116 +          in (modify' #> modify, merge_target false target (data', data)) end
   1.117        end;
   1.118    in collapse end;
   1.119  
   1.120 @@ -326,68 +325,37 @@
   1.121      val (modify, data) = collapse_hierarchy thy target;
   1.122    in (default_width, data, modify) end;
   1.123  
   1.124 -fun activate_const_syntax thy literals cs_data naming =
   1.125 -  (Symtab.empty, naming)
   1.126 -  |> fold_map (fn (c, data) => fn (tab, naming) =>
   1.127 -      case Code_Thingol.lookup_const naming c
   1.128 -       of SOME name => let
   1.129 -              val (syn, naming') =
   1.130 -                Code_Printer.activate_const_syntax thy literals c data naming
   1.131 -            in (SOME name, (Symtab.update_new (name, syn) tab, naming')) end
   1.132 -        | NONE => (NONE, (tab, naming))) cs_data
   1.133 -  |>> map_filter I;
   1.134 +fun activate_symbol_syntax thy literals printings =
   1.135 +  (Code_Symbol.symbols_of printings,
   1.136 +    (Symtab.lookup (Code_Symbol.mapped_const_data (Code_Printer.activate_const_syntax thy literals) printings),
   1.137 +      Code_Symbol.lookup_type_constructor_data printings, Code_Symbol.lookup_type_class_data printings))
   1.138  
   1.139 -fun activate_syntax lookup_name things =
   1.140 -  Symtab.empty
   1.141 -  |> fold_map (fn (thing_identifier, data) => fn tab => case lookup_name thing_identifier
   1.142 -       of SOME name => (SOME name, Symtab.update_new (name, data) tab)
   1.143 -        | NONE => (NONE, tab)) things
   1.144 -  |>> map_filter I;
   1.145 -
   1.146 -fun activate_symbol_syntax thy literals naming printings =
   1.147 -  let
   1.148 -    val (names_const, (const_syntax, _)) =
   1.149 -      activate_const_syntax thy literals (Code_Symbol.dest_constant_data printings) naming;
   1.150 -    val (names_tyco, tyco_syntax) =
   1.151 -      activate_syntax (Code_Thingol.lookup_tyco naming) (Code_Symbol.dest_type_constructor_data printings);
   1.152 -    val (names_class, class_syntax) =
   1.153 -      activate_syntax (Code_Thingol.lookup_class naming) (Code_Symbol.dest_type_class_data printings);
   1.154 -    val names_inst = map_filter (Code_Thingol.lookup_instance naming o fst)
   1.155 -      (Code_Symbol.dest_class_instance_data printings);
   1.156 -  in
   1.157 -    (names_const @ names_tyco @ names_class @ names_inst,
   1.158 -      (Symtab.lookup const_syntax, Symtab.lookup tyco_syntax, Symtab.lookup class_syntax))
   1.159 -  end;
   1.160 -
   1.161 -fun project_program thy names_hidden names1 program2 =
   1.162 +fun project_program thy syms_hidden syms1 program2 =
   1.163    let
   1.164      val ctxt = Proof_Context.init_global thy;
   1.165 -    val names2 = subtract (op =) names_hidden names1;
   1.166 -    val program3 = Graph.restrict (not o member (op =) names_hidden) program2;
   1.167 -    val names4 = Graph.all_succs program3 names2;
   1.168 +    val syms2 = subtract (op =) syms_hidden syms1;
   1.169 +    val program3 = Code_Symbol.Graph.restrict (not o member (op =) syms_hidden) program2;
   1.170 +    val syms4 = Code_Symbol.Graph.all_succs program3 syms2;
   1.171      val unimplemented = Code_Thingol.unimplemented program3;
   1.172      val _ =
   1.173        if null unimplemented then ()
   1.174        else error ("No code equations for " ^
   1.175          commas (map (Proof_Context.extern_const ctxt) unimplemented));
   1.176 -    val program4 = Graph.restrict (member (op =) names4) program3;
   1.177 -  in (names4, program4) end;
   1.178 +    val program4 = Code_Symbol.Graph.restrict (member (op =) syms4) program3;
   1.179 +  in (syms4, program4) end;
   1.180  
   1.181  fun prepare_serializer thy (serializer : serializer) literals reserved identifiers
   1.182 -    printings module_name args naming proto_program names =
   1.183 +    printings module_name args proto_program syms =
   1.184    let
   1.185 -    val (names_hidden, (const_syntax, tyco_syntax, class_syntax)) =
   1.186 -      activate_symbol_syntax thy literals naming printings;
   1.187 -    val (names_all, program) = project_program thy names_hidden names proto_program;
   1.188 +    val (syms_hidden, (const_syntax, tyco_syntax, class_syntax)) =
   1.189 +      activate_symbol_syntax thy literals printings;
   1.190 +    val (syms_all, program) = project_program thy syms_hidden syms proto_program;
   1.191      fun select_include (name, (content, cs)) =
   1.192 -      if null cs orelse exists (fn c => case Code_Thingol.lookup_const naming c
   1.193 -       of SOME name => member (op =) names_all name
   1.194 -        | NONE => false) cs
   1.195 +      if null cs orelse exists (fn c => member (op =) syms_all (Code_Symbol.Constant c)) cs
   1.196        then SOME (name, content) else NONE;
   1.197      val includes = map_filter select_include (Code_Symbol.dest_module_data printings);
   1.198    in
   1.199      (serializer args (Proof_Context.init_global thy) {
   1.200 -      symbol_of = Code_Thingol.symbol_of proto_program,
   1.201        module_name = module_name,
   1.202        reserved_syms = reserved,
   1.203        identifiers = identifiers,
   1.204 @@ -398,7 +366,7 @@
   1.205        program)
   1.206    end;
   1.207  
   1.208 -fun mount_serializer thy target some_width module_name args naming program names =
   1.209 +fun mount_serializer thy target some_width module_name args program syms =
   1.210    let
   1.211      val (default_width, data, modify) = activate_target thy target;
   1.212      val serializer = case the_description data
   1.213 @@ -406,15 +374,15 @@
   1.214      val (prepared_serializer, prepared_program) =
   1.215        prepare_serializer thy serializer (the_literals thy target)
   1.216          (the_reserved data) (the_identifiers data) (the_printings data)
   1.217 -        module_name args naming (modify naming program) names
   1.218 +        module_name args (modify program) syms
   1.219      val width = the_default default_width some_width;
   1.220    in (fn program => prepared_serializer program width, prepared_program) end;
   1.221  
   1.222 -fun invoke_serializer thy target some_width module_name args naming program names =
   1.223 +fun invoke_serializer thy target some_width module_name args program syms =
   1.224    let
   1.225      val check = if module_name = "" then I else check_name true;
   1.226      val (mounted_serializer, prepared_program) = mount_serializer thy
   1.227 -      target some_width (check module_name) args naming program names;
   1.228 +      target some_width (check module_name) args program syms;
   1.229    in mounted_serializer prepared_program end;
   1.230  
   1.231  fun assert_module_name "" = error "Empty module name not allowed here"
   1.232 @@ -429,23 +397,23 @@
   1.233  
   1.234  fun export_code_for thy some_path target some_width module_name args =
   1.235    export (using_master_directory thy some_path)
   1.236 -  ooo invoke_serializer thy target some_width module_name args;
   1.237 +  oo invoke_serializer thy target some_width module_name args;
   1.238  
   1.239  fun produce_code_for thy target some_width module_name args =
   1.240    let
   1.241      val serializer = invoke_serializer thy target some_width (assert_module_name module_name) args;
   1.242 -  in fn naming => fn program => fn names =>
   1.243 -    produce (serializer naming program names) |> apsnd (fn deresolve => map deresolve names)
   1.244 +  in fn program => fn syms =>
   1.245 +    produce (serializer program syms) |> apsnd (fn deresolve => map deresolve syms)
   1.246    end;
   1.247  
   1.248  fun present_code_for thy target some_width module_name args =
   1.249    let
   1.250      val serializer = invoke_serializer thy target some_width (assert_module_name module_name) args;
   1.251 -  in fn naming => fn program => fn (names, selects) =>
   1.252 -    present selects (serializer naming program names)
   1.253 +  in fn program => fn (syms, selects) =>
   1.254 +    present selects (serializer program syms)
   1.255    end;
   1.256  
   1.257 -fun check_code_for thy target strict args naming program names_cs =
   1.258 +fun check_code_for thy target strict args program syms =
   1.259    let
   1.260      val { env_var, make_destination, make_command } =
   1.261        (#check o the_fundamental thy) target;
   1.262 @@ -453,7 +421,7 @@
   1.263        let
   1.264          val destination = make_destination p;
   1.265          val _ = export (SOME destination) (invoke_serializer thy target (SOME 80)
   1.266 -          generatedN args naming program names_cs);
   1.267 +          generatedN args program syms);
   1.268          val cmd = make_command generatedN;
   1.269        in
   1.270          if Isabelle_System.bash ("cd " ^ File.shell_path p ^ " && " ^ cmd ^ " 2>&1") <> 0
   1.271 @@ -468,46 +436,38 @@
   1.272      else Isabelle_System.with_tmp_dir "Code_Test" ext_check
   1.273    end;
   1.274  
   1.275 -fun evaluation mounted_serializer prepared_program consts ((vs, ty), t) =
   1.276 +fun evaluation mounted_serializer prepared_program syms ((vs, ty), t) =
   1.277    let
   1.278      val _ = if Code_Thingol.contains_dict_var t then
   1.279        error "Term to be evaluated contains free dictionaries" else ();
   1.280      val v' = singleton (Name.variant_list (map fst vs)) "a";
   1.281      val vs' = (v', []) :: vs;
   1.282 -    val ty' = Code_Thingol.fun_tyco `%% [ITyVar v', ty];
   1.283 -    val value_name = "Value.value.value"
   1.284 +    val ty' = ITyVar v' `-> ty;
   1.285      val program = prepared_program
   1.286 -      |> Graph.new_node (value_name,
   1.287 -          Code_Thingol.Fun (@{const_name dummy_pattern}, (((vs', ty'), [(([IVar NONE], t), (NONE, true))]), NONE)))
   1.288 -      |> fold (curry (perhaps o try o Graph.add_edge) value_name) consts;
   1.289 +      |> Code_Symbol.Graph.new_node (Code_Symbol.value,
   1.290 +          Code_Thingol.Fun (((vs', ty'), [(([IVar NONE], t), (NONE, true))]), NONE))
   1.291 +      |> fold (curry (perhaps o try o
   1.292 +          Code_Symbol.Graph.add_edge) Code_Symbol.value) syms;
   1.293      val (program_code, deresolve) = produce (mounted_serializer program);
   1.294 -    val value_name' = the (deresolve value_name);
   1.295 -  in (program_code, value_name') end;
   1.296 +    val value_name = the (deresolve Code_Symbol.value);
   1.297 +  in (program_code, value_name) end;
   1.298  
   1.299 -fun evaluator thy target naming program consts =
   1.300 +fun evaluator thy target program syms =
   1.301    let
   1.302 -    val (mounted_serializer, prepared_program) = mount_serializer thy
   1.303 -      target NONE generatedN [] naming program consts;
   1.304 -  in evaluation mounted_serializer prepared_program consts end;
   1.305 +    val (mounted_serializer, prepared_program) =
   1.306 +      mount_serializer thy target NONE generatedN [] program syms;
   1.307 +  in evaluation mounted_serializer prepared_program syms end;
   1.308  
   1.309  end; (* local *)
   1.310  
   1.311  
   1.312  (* code generation *)
   1.313  
   1.314 -fun implemented_functions thy naming program =
   1.315 +fun read_const_exprs thy const_exprs =
   1.316    let
   1.317 -    val cs = Code_Thingol.unimplemented program;
   1.318 -    val names = map_filter (Code_Thingol.lookup_const naming) cs;
   1.319 -  in subtract (op =) (Graph.all_preds program names) (Graph.keys program) end;
   1.320 -
   1.321 -fun read_const_exprs thy cs =
   1.322 -  let
   1.323 -    val (cs1, cs2) = Code_Thingol.read_const_exprs thy cs;
   1.324 -    val (names2, (naming, program)) = Code_Thingol.consts_program thy true cs2;
   1.325 -    val names3 = implemented_functions thy naming program;
   1.326 -    val cs3 = map_filter (fn (c, name) =>
   1.327 -      if member (op =) names3 name then SOME c else NONE) (cs2 ~~ names2);
   1.328 +    val (cs1, cs2) = Code_Thingol.read_const_exprs thy const_exprs;
   1.329 +    val program = Code_Thingol.consts_program thy true cs2;
   1.330 +    val cs3 = Code_Thingol.implemented_deps program;
   1.331    in union (op =) cs3 cs1 end;
   1.332  
   1.333  fun prep_destination "" = NONE
   1.334 @@ -515,9 +475,9 @@
   1.335  
   1.336  fun export_code thy cs seris =
   1.337    let
   1.338 -    val (names_cs, (naming, program)) = Code_Thingol.consts_program thy false cs;
   1.339 +    val program = Code_Thingol.consts_program thy false cs;
   1.340      val _ = map (fn (((target, module_name), some_path), args) =>
   1.341 -      export_code_for thy some_path target NONE module_name args naming program names_cs) seris;
   1.342 +      export_code_for thy some_path target NONE module_name args program (map Code_Symbol.Constant cs)) seris;
   1.343    in () end;
   1.344  
   1.345  fun export_code_cmd raw_cs seris thy = export_code thy (read_const_exprs thy raw_cs)
   1.346 @@ -525,19 +485,19 @@
   1.347  
   1.348  fun produce_code thy cs target some_width some_module_name args =
   1.349    let
   1.350 -    val (names_cs, (naming, program)) = Code_Thingol.consts_program thy false cs;
   1.351 -  in produce_code_for thy target some_width some_module_name args naming program names_cs end;
   1.352 +    val program = Code_Thingol.consts_program thy false cs;
   1.353 +  in produce_code_for thy target some_width some_module_name args program (map Code_Symbol.Constant cs) end;
   1.354  
   1.355 -fun present_code thy cs names_stmt target some_width some_module_name args =
   1.356 +fun present_code thy cs syms target some_width some_module_name args =
   1.357    let
   1.358 -    val (names_cs, (naming, program)) = Code_Thingol.consts_program thy false cs;
   1.359 -  in present_code_for thy target some_width some_module_name args naming program (names_cs, names_stmt naming) end;
   1.360 +    val program = Code_Thingol.consts_program thy false cs;
   1.361 +  in present_code_for thy target some_width some_module_name args program (map Code_Symbol.Constant cs, syms) end;
   1.362  
   1.363  fun check_code thy cs seris =
   1.364    let
   1.365 -    val (names_cs, (naming, program)) = Code_Thingol.consts_program thy false cs;
   1.366 +    val program = Code_Thingol.consts_program thy false cs;
   1.367      val _ = map (fn ((target, strict), args) =>
   1.368 -      check_code_for thy target strict args naming program names_cs) seris;
   1.369 +      check_code_for thy target strict args program (map Code_Symbol.Constant cs)) seris;
   1.370    in () end;
   1.371  
   1.372  fun check_code_cmd raw_cs seris thy = check_code thy (read_const_exprs thy raw_cs) seris;
   1.373 @@ -547,22 +507,22 @@
   1.374  val parse_const_terms = Scan.repeat1 Args.term
   1.375    >> (fn ts => fn thy => map (Code.check_const thy) ts);
   1.376  
   1.377 -fun parse_names category parse internalize lookup =
   1.378 +fun parse_names category parse internalize mark_symbol =
   1.379    Scan.lift (Args.parens (Args.$$$ category)) |-- Scan.repeat1 parse
   1.380 -  >> (fn xs => fn thy => fn naming => map_filter (lookup naming o internalize thy) xs);
   1.381 +  >> (fn xs => fn thy => map (mark_symbol o internalize thy) xs);
   1.382  
   1.383  val parse_consts = parse_names "consts" Args.term
   1.384 -  Code.check_const Code_Thingol.lookup_const;
   1.385 +  Code.check_const Code_Symbol.Constant;
   1.386  
   1.387  val parse_types = parse_names "types" (Scan.lift Args.name)
   1.388 -  Sign.intern_type Code_Thingol.lookup_tyco;
   1.389 +  Sign.intern_type Code_Symbol.Type_Constructor;
   1.390  
   1.391  val parse_classes = parse_names "classes" (Scan.lift Args.name)
   1.392 -  Sign.intern_class Code_Thingol.lookup_class;
   1.393 +  Sign.intern_class Code_Symbol.Type_Class;
   1.394  
   1.395  val parse_instances = parse_names "instances" (Scan.lift (Args.name --| Args.$$$ "::" -- Args.name))
   1.396 -  (fn thy => fn (raw_tyco, raw_class) => (Sign.intern_class thy raw_class, Sign.intern_type thy raw_tyco))
   1.397 -    Code_Thingol.lookup_instance;
   1.398 +  (fn thy => fn (raw_tyco, raw_class) => (Sign.intern_class thy raw_tyco, Sign.intern_type thy raw_class))
   1.399 +    Code_Symbol.Class_Instance;
   1.400  
   1.401  in
   1.402  
   1.403 @@ -574,7 +534,7 @@
   1.404      (fn {context = ctxt, ...} => fn ((mk_cs, mk_stmtss), (target, some_width)) =>
   1.405        let val thy = Proof_Context.theory_of ctxt in
   1.406          present_code thy (mk_cs thy)
   1.407 -          (fn naming => maps (fn f => f thy naming) mk_stmtss)
   1.408 +          (maps (fn f => f thy) mk_stmtss)
   1.409            target some_width "Example" []
   1.410        end);
   1.411