code generator: case expressions, improved name resolving
authorhaftmann
Fri Nov 25 17:41:52 2005 +0100 (2005-11-25)
changeset 18247b17724cae935
parent 18246 676d2e625d98
child 18248 929659a46ecf
code generator: case expressions, improved name resolving
src/HOL/Tools/datatype_codegen.ML
src/Pure/Tools/codegen_package.ML
src/Pure/Tools/codegen_serializer.ML
src/Pure/Tools/codegen_thingol.ML
     1.1 --- a/src/HOL/Tools/datatype_codegen.ML	Fri Nov 25 14:51:39 2005 +0100
     1.2 +++ b/src/HOL/Tools/datatype_codegen.ML	Fri Nov 25 17:41:52 2005 +0100
     1.3 @@ -10,6 +10,7 @@
     1.4    val is_datatype: theory -> string -> bool
     1.5    val get_datatype: theory -> string -> (string list * string list) option
     1.6    val get_datacons: theory -> string * string -> typ list option
     1.7 +  val get_case_const_data: theory -> string -> (string * int) list option;
     1.8    val setup: (theory -> theory) list
     1.9  end;
    1.10  
    1.11 @@ -264,17 +265,17 @@
    1.12  
    1.13  fun datatype_codegen thy defs gr dep module brack t = (case strip_comb t of
    1.14     (c as Const (s, T), ts) =>
    1.15 -       (case find_first (fn (_, {index, descr, case_name, ...}) =>
    1.16 +       (case Library.find_first (fn (_, {index, descr, case_name, ...}) =>
    1.17           s = case_name orelse
    1.18             AList.defined (op =) ((#3 o the o AList.lookup (op =) descr) index) s)
    1.19               (Symtab.dest (DatatypePackage.get_datatypes thy)) of
    1.20            NONE => NONE
    1.21          | SOME (tname, {index, descr, ...}) =>
    1.22 -           if isSome (get_assoc_code thy s T) then NONE else
    1.23 +           if is_some (get_assoc_code thy s T) then NONE else
    1.24             let val SOME (_, _, constrs) = AList.lookup (op =) descr index
    1.25             in (case (AList.lookup (op =) constrs s, strip_type T) of
    1.26                 (NONE, _) => SOME (pretty_case thy defs gr dep module brack
    1.27 -                 (#3 (valOf (AList.lookup (op =) descr index))) c ts)
    1.28 +                 ((#3 o the o AList.lookup (op =) descr) index) c ts)
    1.29               | (SOME args, (_, Type _)) => SOME (pretty_constr thy defs
    1.30                   (fst (invoke_tycodegen thy defs dep module false
    1.31                      (gr, snd (strip_type T))))
    1.32 @@ -342,15 +343,26 @@
    1.33      else NONE
    1.34    end;
    1.35  
    1.36 +fun get_case_const_data thy f =
    1.37 +  case Library.find_first (fn (_, {index, descr, case_name, ...}) =>
    1.38 +      case_name = f
    1.39 +    ) ((Symtab.dest o DatatypePackage.get_datatypes) thy)
    1.40 +   of NONE => NONE
    1.41 +    | SOME (_, {index, descr, ...}) =>
    1.42 +        (SOME o map (apsnd length) o #3 o the o AList.lookup (op =) descr) index;
    1.43 +
    1.44 +
    1.45  val setup = [
    1.46    add_codegen "datatype" datatype_codegen,
    1.47    add_tycodegen "datatype" datatype_tycodegen,
    1.48    CodegenPackage.set_is_datatype
    1.49      is_datatype,
    1.50 -  CodegenPackage.add_defgen 
    1.51 +  CodegenPackage.add_defgen
    1.52      ("datatype", CodegenPackage.defgen_datatype get_datatype),
    1.53    CodegenPackage.add_defgen
    1.54 -    ("datacons", CodegenPackage.defgen_datacons get_datacons)
    1.55 +    ("datacons", CodegenPackage.defgen_datacons get_datacons),
    1.56 +  CodegenPackage.add_codegen_expr
    1.57 +    ("case", CodegenPackage.codegen_case get_case_const_data)
    1.58  ];
    1.59  
    1.60  end;
     2.1 --- a/src/Pure/Tools/codegen_package.ML	Fri Nov 25 14:51:39 2005 +0100
     2.2 +++ b/src/Pure/Tools/codegen_package.ML	Fri Nov 25 17:41:52 2005 +0100
     2.3 @@ -59,6 +59,8 @@
     2.4      -> codegen_expr;
     2.5    val codegen_number_of: (term -> IntInf.int) -> (term -> term)
     2.6      -> codegen_expr;
     2.7 +  val codegen_case: (theory -> string -> (string * int) list option)
     2.8 +    -> codegen_expr;
     2.9    val defgen_datatype: (theory -> string -> (string list * string list) option)
    2.10      -> defgen;
    2.11    val defgen_datacons: (theory -> string * string -> typ list option)
    2.12 @@ -66,7 +68,7 @@
    2.13    val defgen_recfun: (theory -> string * typ -> (term list * term) list * typ)
    2.14      -> defgen;
    2.15  
    2.16 -  val print_codegen_modl: theory -> unit;
    2.17 +  val print_codegen_generated: theory -> unit;
    2.18    val mk_deftab: theory -> deftab;
    2.19    structure CodegenData: THEORY_DATA;
    2.20    structure Insttab: TABLE;
    2.21 @@ -117,7 +119,7 @@
    2.22  
    2.23  val serializer_ml =
    2.24    let
    2.25 -    val name_root = "module";
    2.26 +    val name_root = "Generated";
    2.27      val nsp_conn_ml = [
    2.28        [nsp_class, nsp_type, nsp_eq_class], [nsp_const, nsp_inst, nsp_mem, nsp_eq]
    2.29      ];
    2.30 @@ -283,7 +285,7 @@
    2.31        in CodegenData.put { modl = modl, gens = gens, lookups = lookups,
    2.32             serialize_data = serialize_data, logic_data = logic_data } thy end;
    2.33  
    2.34 -val print_codegen_modl = writeln o Pretty.output o pretty_module o #modl o CodegenData.get;
    2.35 +val print_codegen_generated = writeln o Pretty.output o pretty_module o #modl o CodegenData.get;
    2.36  
    2.37  fun add_codegen_sort (name, cg) =
    2.38    map_codegen_data
    2.39 @@ -527,39 +529,18 @@
    2.40      |-> (fn ((eqs, ty), sortctxt) => pair (Fun (eqs, (sortctxt, ty))))
    2.41    end;
    2.42  
    2.43 -fun mk_app thy defs f ty_use args trns =
    2.44 -  let
    2.45 -    val _ = debug 5 (fn _ => "making application of " ^ quote f) ();
    2.46 -    val ty_def = Sign.the_const_constraint thy f;
    2.47 -    val _ = debug 10 (fn _ => "making application (2)") ();
    2.48 -    fun mk_lookup (ClassPackage.Instance (i, ls)) trns =
    2.49 -          trns
    2.50 -          |> ensure_def_class thy defs ((idf_of_name thy nsp_class o fst) i)
    2.51 -          ||>> ensure_def_class thy defs (idf_of_inst thy defs i)
    2.52 -          ||>> (fold_map o fold_map) mk_lookup ls
    2.53 -          |-> (fn ((cls, i), ls) => pair (ClassPackage.Instance ((cls, i), ls)))
    2.54 -      | mk_lookup (ClassPackage.Lookup (clss, (v, i))) trns =
    2.55 -          trns
    2.56 -          |> fold_map (ensure_def_class thy defs) (map (idf_of_name thy nsp_class) clss)
    2.57 -          |-> (fn clss => pair (ClassPackage.Lookup (clss, (name_of_tvar (TFree (v, [])), i))));
    2.58 -    val _ = debug 10 (fn _ => "making application (3)") ();
    2.59 -    fun mk_itapp e [] = e
    2.60 -      | mk_itapp e lookup = IInst (e, lookup);
    2.61 -  in 
    2.62 +fun fix_nargs thy defs gen i (t, ts) trns =
    2.63 +  if length ts < i
    2.64 +  then
    2.65      trns
    2.66 -    |> debug 10 (fn _ => "making application (4): " ^ f ^ "::" ^ Sign.string_of_typ thy ty_use)
    2.67 -    |> ensure_def_const thy defs (idf_of_cname thy defs (f, ty_use))
    2.68 -    |> debug 10 (fn _ => "making application (5)")
    2.69 -    ||>> fold_map (invoke_cg_expr thy defs) args
    2.70 -    |> debug 10 (fn _ => "making application (6)")
    2.71 -    ||>> (fold_map o fold_map) mk_lookup (ClassPackage.extract_sortlookup thy (ty_def, ty_use))
    2.72 -    |> debug 10 (fn _ => "making application (7)")
    2.73 -    ||>> invoke_cg_type thy defs ty_use
    2.74 -    |> debug 10 (fn _ => "making application (8)")
    2.75 -    |-> (fn (((f, args), lookup), ty_use) =>
    2.76 -           pair (mk_apps (mk_itapp (IConst (f, ty_use)) lookup, args)))
    2.77 -  end;
    2.78 -
    2.79 +    |> debug 10 (fn _ => "eta-expanding")
    2.80 +    |> gen (strip_comb (Codegen.eta_expand t ts i))
    2.81 +  else
    2.82 +    trns
    2.83 +    |> debug 10 (fn _ => "splitting arguments (" ^ string_of_int i ^ ", " ^ string_of_int (length ts) ^ ")")
    2.84 +    |> gen (t, Library.take (i, ts))
    2.85 +    ||>> fold_map (invoke_cg_expr thy defs) (Library.drop (i, ts))
    2.86 +    |-> pair o mk_apps;
    2.87  
    2.88  local
    2.89    open CodegenThingolOp;
    2.90 @@ -568,6 +549,8 @@
    2.91    infixr 6 `-->;
    2.92    infix 4 `$;
    2.93    infix 4 `$$;
    2.94 +  infixr 5 `|->;
    2.95 +  infixr 5 `|-->;
    2.96  in
    2.97  
    2.98  (* code generators *)
    2.99 @@ -597,38 +580,54 @@
   2.100        ||>> fold_map (invoke_cg_type thy defs) tys
   2.101        |-> (fn (tyco, tys) => succeed (tyco `%% tys))
   2.102  
   2.103 -fun codegen_expr_default thy defs t trns =
   2.104 -  let
   2.105 -    val (u, ts) = strip_comb t;
   2.106 -    fun name_of_var (Free (v, _)) = v
   2.107 -      | name_of_var (Var ((v, i), _)) = if i=0 then v else v ^ string_of_int i
   2.108 -    fun cg_default (Var (_, ty)) =
   2.109 -          trns
   2.110 -          |> fold_map (invoke_cg_expr thy defs) ts
   2.111 -          ||>> invoke_cg_type thy defs ty
   2.112 -          |-> (fn (args, ty) => succeed (IVarE (name_of_var u, ty) `$$ args))
   2.113 -      | cg_default (Free (_, ty)) =
   2.114 -          trns
   2.115 -          |> fold_map (invoke_cg_expr thy defs) ts
   2.116 -          ||>> invoke_cg_type thy defs ty
   2.117 -          |-> (fn (args, ty) => succeed (IVarE (name_of_var u, ty) `$$ args))
   2.118 -      | cg_default (Const (f, ty)) =
   2.119 -          trns
   2.120 -          |> mk_app thy defs f ty ts
   2.121 -          |-> (fn e => succeed e)
   2.122 -      | cg_default (Abs _) =
   2.123 -          let
   2.124 -            val (bs, tys) = ListPair.unzip (strip_abs_vars u);
   2.125 -            val t = strip_abs_body u;
   2.126 -            val bs' = Codegen.new_names t bs;
   2.127 -            val t' = subst_bounds (map Free (rev (bs' ~~ tys)), t)
   2.128 -          in
   2.129 -            trns
   2.130 -            |> fold_map (invoke_cg_expr thy defs) ts
   2.131 -            ||>> invoke_cg_expr thy defs t'
   2.132 -            |-> (fn (es, e) => succeed (e `$$ es))
   2.133 -          end;
   2.134 -  in cg_default u end;
   2.135 +fun codegen_expr_default thy defs (Const (f, ty)) trns =
   2.136 +      let
   2.137 +        val _ = debug 5 (fn _ => "making application of " ^ quote f) ();
   2.138 +        val ty_def = Sign.the_const_constraint thy f;
   2.139 +        val _ = debug 10 (fn _ => "making application (2)") ();
   2.140 +        fun mk_lookup (ClassPackage.Instance (i, ls)) trns =
   2.141 +              trns
   2.142 +              |> ensure_def_class thy defs ((idf_of_name thy nsp_class o fst) i)
   2.143 +              ||>> ensure_def_class thy defs (idf_of_inst thy defs i)
   2.144 +              ||>> (fold_map o fold_map) mk_lookup ls
   2.145 +              |-> (fn ((cls, i), ls) => pair (ClassPackage.Instance ((cls, i), ls)))
   2.146 +          | mk_lookup (ClassPackage.Lookup (clss, (v, i))) trns =
   2.147 +              trns
   2.148 +              |> fold_map (ensure_def_class thy defs) (map (idf_of_name thy nsp_class) clss)
   2.149 +              |-> (fn clss => pair (ClassPackage.Lookup (clss, (name_of_tvar (TFree (v, [])), i))));
   2.150 +        val _ = debug 10 (fn _ => "making application (3)") ();
   2.151 +        fun mk_itapp e [] = e
   2.152 +          | mk_itapp e lookup = IInst (e, lookup);
   2.153 +      in 
   2.154 +        trns
   2.155 +        |> debug 10 (fn _ => "making application (4): " ^ f ^ "::" ^ Sign.string_of_typ thy ty ^ " <~> " ^ Sign.string_of_typ thy ty_def)
   2.156 +        |> ensure_def_const thy defs (idf_of_cname thy defs (f, ty))
   2.157 +        |> debug 10 (fn _ => "making application (5)")
   2.158 +        ||>> (fold_map o fold_map) mk_lookup (ClassPackage.extract_sortlookup thy (ty_def, ty))
   2.159 +        |> debug 10 (fn _ => "making application (6)")
   2.160 +        ||>> invoke_cg_type thy defs ty
   2.161 +        |> debug 10 (fn _ => "making application (7)")
   2.162 +        |-> (fn ((f, lookup), ty) =>
   2.163 +               succeed (mk_itapp (IConst (f, ty)) lookup))
   2.164 +      end
   2.165 +  | codegen_expr_default thy defs (Free (v, ty)) trns =
   2.166 +      trns
   2.167 +      |> invoke_cg_type thy defs ty
   2.168 +      |-> (fn ty => succeed (IVarE (v, ty)))
   2.169 +  | codegen_expr_default thy defs (Var ((v, i), ty)) trns =
   2.170 +      trns
   2.171 +      |> invoke_cg_type thy defs ty
   2.172 +      |-> (fn ty => succeed (IVarE (if i=0 then v else v ^ string_of_int i, ty)))
   2.173 +  | codegen_expr_default thy defs (Abs (v, ty, t)) trns =
   2.174 +      trns
   2.175 +      |> invoke_cg_type thy defs ty
   2.176 +      ||>> invoke_cg_expr thy defs (subst_bound (Free (v, ty), t))
   2.177 +      |-> (fn (ty, e) => succeed ((v, ty) `|-> e))
   2.178 +  | codegen_expr_default thy defs (t1 $ t2) trns =
   2.179 +      trns
   2.180 +      |> invoke_cg_expr thy defs t1
   2.181 +      ||>> invoke_cg_expr thy defs t2
   2.182 +      |-> (fn (e1, e2) => succeed (e1 `$ e2));
   2.183  
   2.184  (*fun codegen_eq thy defs t trns =
   2.185   let
   2.186 @@ -810,6 +809,59 @@
   2.187        trns
   2.188        |> fail ("not a number: " ^ Sign.string_of_term thy t);
   2.189  
   2.190 +fun codegen_case get_case_const_data thy defs t trns =
   2.191 +  let
   2.192 +    fun cg_case_d gen_names dty (((cname, i), ty), t) trns =
   2.193 +      let
   2.194 +        val vs = gen_names i;
   2.195 +        val tys = Library.take (i, (fst o strip_type) ty);
   2.196 +        val frees = map2 Free (vs, tys);
   2.197 +        val t' = Envir.beta_norm (list_comb (t, frees));
   2.198 +      in
   2.199 +        trns
   2.200 +        |> invoke_cg_expr thy defs (list_comb (Const (cname, tys ---> dty), frees))
   2.201 +        ||>> invoke_cg_expr thy defs t'
   2.202 +        |-> (fn (ep, e) => pair (ipat_of_iexpr ep, e))
   2.203 +      end;
   2.204 +    fun cg_case dty cs (_, ts) trns =
   2.205 +      let
   2.206 +        val (ts', t) = split_last ts
   2.207 +        val _ = debug 10 (fn _ => "  in " ^ Sign.string_of_typ thy dty ^ ", pairing "
   2.208 +          ^ (commas o map (fst o fst)) cs ^ " with " ^ (commas o map (Sign.string_of_term thy)) ts') ();
   2.209 +        fun gen_names i =
   2.210 +          variantlist (replicate i "x", foldr add_term_names
   2.211 +           (map (fst o fst o dest_Var) (foldr add_term_vars [] ts)) ts)
   2.212 +      in
   2.213 +        trns
   2.214 +        |> invoke_cg_expr thy defs t
   2.215 +        ||>> fold_map (cg_case_d gen_names dty) (cs ~~ ts')
   2.216 +        |-> (fn (t, ds) => pair (ICase (t, ds)))
   2.217 +      end;
   2.218 +  in case strip_comb t
   2.219 +   of (t as Const (f, ty), ts) =>
   2.220 +        (case get_case_const_data thy f
   2.221 +         of NONE =>
   2.222 +              trns
   2.223 +              |> fail ("not a case constant: " ^ quote f)
   2.224 +          | SOME cs =>
   2.225 +              let
   2.226 +                val (tys, dty) = (split_last o fst o strip_type) ty;
   2.227 +              in
   2.228 +                trns
   2.229 +                |> debug 9 (fn _ => "for case const " ^ f ^ "::"
   2.230 +                     ^ Sign.string_of_typ thy ty ^ ",\n  with " ^ AList.string_of_alist I string_of_int cs
   2.231 +                     ^ ",\n  given as args " ^ (commas o map (Sign.string_of_term thy)) ts
   2.232 +                     ^ ",\n  with significant length " ^ string_of_int (length cs + 1))
   2.233 +                |> fix_nargs thy defs (cg_case dty (cs ~~ tys))
   2.234 +                     (length cs + 1) (t, ts)
   2.235 +                |-> succeed
   2.236 +              end
   2.237 +        )
   2.238 +    | _ =>
   2.239 +        trns
   2.240 +        |> fail ("not a case constant expression: " ^ Sign.string_of_term thy t)
   2.241 +  end;
   2.242 +
   2.243  fun defgen_datatype get_datatype thy defs tyco trns =
   2.244    case tname_of_idf thy tyco
   2.245     of SOME dtname =>
   2.246 @@ -912,9 +964,8 @@
   2.247                | mk_idf (nm, ty) =
   2.248                    if is_number nm
   2.249                    then nm
   2.250 -                  else idf_of_name thy nsp_const
   2.251 -                    (((perhaps o Symtab.lookup) ((fst o #alias o #logic_data o CodegenData.get) thy) nm)
   2.252 -                     ^ "_" ^ mangle_tyname (ty_decl, ty))
   2.253 +                  else idf_of_name thy nsp_const nm
   2.254 +                     ^ "_" ^ mangle_tyname (ty_decl, ty)
   2.255              val overl_lookups = map
   2.256                (fn (ty, (_, (args, rhs))) => (ty, mk_idf (name, ty), args, rhs)) ds;
   2.257            in
   2.258 @@ -964,8 +1015,8 @@
   2.259               classtab))
   2.260    in 
   2.261      ((Symtab.empty, Symtab.empty), Symtab.empty, (Insttab.empty, Symtab.empty, Symtab.empty))
   2.262 +    |> add_clsmems (ClassPackage.get_classtab thy)
   2.263      |> fold add_def (Codegen.mk_deftab thy |> Symtab.dest)
   2.264 -    |> add_clsmems (ClassPackage.get_classtab thy)
   2.265    end;
   2.266  
   2.267  fun expand_module defs gen thy =
   2.268 @@ -1102,15 +1153,18 @@
   2.269    (#serializer o (fn data => (the oo Symtab.lookup) data serial_name)
   2.270      o #serialize_data o CodegenData.get) thy;
   2.271  
   2.272 -fun mk_const thy f =
   2.273 +fun mk_const thy (f, s_ty) =
   2.274    let
   2.275      val f' = Sign.intern_const thy f;
   2.276 -  in (f', Sign.the_const_constraint thy f') end;
   2.277 +    val ty = case s_ty
   2.278 +     of NONE => Sign.the_const_constraint thy f'
   2.279 +      | SOME s => Sign.read_typ (thy, K NONE) s;
   2.280 +  in (f', ty) end;
   2.281  
   2.282 -fun gen_generate_code consts thy =
   2.283 +fun generate_code consts thy =
   2.284    let
   2.285      val defs = mk_deftab thy;
   2.286 -    val consts' = map (idf_of_cname thy defs) consts;
   2.287 +    val consts' = map (idf_of_cname thy defs o mk_const thy) consts;
   2.288      fun generate thy defs = fold_map (ensure_def_const thy defs) consts'
   2.289    in
   2.290      thy
   2.291 @@ -1118,9 +1172,6 @@
   2.292      |-> (fn _ => pair consts')
   2.293    end;
   2.294  
   2.295 -fun generate_code consts thy =
   2.296 -  gen_generate_code (map (mk_const thy) consts) thy;
   2.297 -
   2.298  fun serialize_code serial_name filename consts thy =
   2.299    let
   2.300      fun mk_sfun tab name args f =
   2.301 @@ -1141,10 +1192,9 @@
   2.302        if compile_it
   2.303        then use_text Context.ml_output false code
   2.304        else File.write (Path.unpack filename) (code ^ "\n");
   2.305 -    val consts' = Option.map (map (mk_const thy)) consts;
   2.306    in
   2.307      thy
   2.308 -    |> (if is_some consts then gen_generate_code (the consts') else pair [])
   2.309 +    |> (if is_some consts then generate_code (the consts) else pair [])
   2.310      |-> (fn [] => `(serializer' NONE o #modl o CodegenData.get)
   2.311            | consts => `(serializer' (SOME consts) o #modl o CodegenData.get))
   2.312      |-> (fn code => ((use_code o Pretty.output) code; I))
   2.313 @@ -1165,7 +1215,7 @@
   2.314  
   2.315  val generateP =
   2.316    OuterSyntax.command generateK "generate executable code for constants" K.thy_decl ( 
   2.317 -    Scan.repeat1 P.name
   2.318 +    Scan.repeat1 (P.name -- Scan.option (P.$$$ "::" |-- P.typ))
   2.319      >> (fn consts =>
   2.320            Toplevel.theory (generate_code consts #> snd))
   2.321    );
   2.322 @@ -1176,7 +1226,7 @@
   2.323      -- P.name
   2.324      -- Scan.option (
   2.325           P.$$$ extractingK
   2.326 -         |-- Scan.repeat1 P.name
   2.327 +         |-- Scan.repeat1 (P.name -- Scan.option (P.$$$ "::" |-- P.typ))
   2.328         )
   2.329      >> (fn ((serial_name, filename), consts) =>
   2.330            Toplevel.theory (serialize_code serial_name filename consts))
   2.331 @@ -1253,7 +1303,11 @@
   2.332      add_alias ("op <>", "neq"),
   2.333      add_alias ("op >=", "ge"),
   2.334      add_alias ("op >", "gt"),
   2.335 +    add_alias ("op <=", "le"),
   2.336 +    add_alias ("op <", "lt"),
   2.337 +    add_alias ("op +", "add"),
   2.338      add_alias ("op -", "minus"),
   2.339 +    add_alias ("op *", "times"),
   2.340      add_alias ("op @", "append"),
   2.341      add_lookup_tyco ("bool", type_bool),
   2.342      add_lookup_tyco ("IntDef.int", type_integer),
     3.1 --- a/src/Pure/Tools/codegen_serializer.ML	Fri Nov 25 14:51:39 2005 +0100
     3.2 +++ b/src/Pure/Tools/codegen_serializer.ML	Fri Nov 25 17:41:52 2005 +0100
     3.3 @@ -282,10 +282,10 @@
     3.4                  ml_from_iexpr NOBR e
     3.5                ]
     3.6            in brackify (eval_br br BR) (
     3.7 -            Pretty.str "case "
     3.8 +            Pretty.str "case"
     3.9              :: ml_from_iexpr NOBR e
    3.10              :: mk_clause "of " c
    3.11 -            :: map (mk_clause "|") cs
    3.12 +            :: map (mk_clause "| ") cs
    3.13            ) end
    3.14        | ml_from_iexpr br (IInst _) =
    3.15            error "cannot serialize poly instant to ML"
    3.16 @@ -391,13 +391,13 @@
    3.17            | check_args (_, Fun ((pats, _)::_, _)) (SOME definer) =
    3.18                if mk_definer pats = definer
    3.19                then SOME definer
    3.20 -              else error ("Mixing simultaneous vals and funs not implemented")
    3.21 +              else error ("mixing simultaneous vals and funs not implemented")
    3.22            | check_args _ _ =
    3.23 -              error ("Function definition block containing other definitions than functions")
    3.24 +              error ("function definition block containing other definitions than functions")
    3.25          val definer = the (fold check_args ds NONE);
    3.26 -        fun mk_eq definer f' ty (pats, expr) =
    3.27 +        fun mk_eq definer f ty (pats, expr) =
    3.28            let
    3.29 -            val lhs = [Pretty.str (definer ^ " " ^ f')]
    3.30 +            val lhs = [Pretty.str (definer ^ " " ^ f)]
    3.31                         @ (if null pats
    3.32                            then [Pretty.str ":", ml_from_typ NOBR ty]
    3.33                            else map (ml_from_pat BR) pats)
    3.34 @@ -407,15 +407,11 @@
    3.35            end
    3.36          fun mk_fun definer (f, Fun (eqs as eq::eq_tl, (_, ty))) =
    3.37            let
    3.38 -            val (pats_hd::pats_tl) = (fst o split_list) eqs
    3.39 -            val _ = fold (fn x => fn y => (x ~~ y; y)) pats_tl pats_hd
    3.40 +            val (pats_hd::pats_tl) = (fst o split_list) eqs;
    3.41 +            val _ = fold (fn x => fn y => (x ~~ y; y)) pats_tl pats_hd;
    3.42                (*check for equal length of argument lists*)
    3.43 -          in (Pretty.block o Pretty.fbreaks) (
    3.44 -               (*Pretty.block [
    3.45 -                 Pretty.brk 1,
    3.46 -                 Pretty.str ": ",
    3.47 -                 ml_from_typ NOBR ty
    3.48 -               ]*)
    3.49 +            val shift = if null eq_tl then I else map (Pretty.block o single);
    3.50 +          in (Pretty.block o Pretty.fbreaks o shift) (
    3.51                 mk_eq definer f ty eq
    3.52                 :: map (mk_eq "|" f ty) eq_tl
    3.53               )
    3.54 @@ -516,7 +512,7 @@
    3.55          Pretty.str ("structure " ^ name ^ " = "),
    3.56          Pretty.str "struct",
    3.57          Pretty.str ""
    3.58 -      ] @ ps @ [
    3.59 +      ] @ separate (Pretty.str "") ps @ [
    3.60          Pretty.str "",
    3.61          Pretty.str ("end; (* struct " ^ name ^ " *)")
    3.62        ]);
    3.63 @@ -542,7 +538,7 @@
    3.64            else 0
    3.65    in 
    3.66      module
    3.67 -    |> debug 5 (Pretty.output o pretty_module)
    3.68 +    |> debug 12 (Pretty.output o pretty_module)
    3.69      |> debug 3 (fn _ => "connecting datatypes and classdecls...")
    3.70      |> connect_datatypes_clsdecls
    3.71      |> debug 3 (fn _ => "selecting submodule...")
    3.72 @@ -551,7 +547,7 @@
    3.73      |> eta_expand eta_expander
    3.74      |> debug 3 (fn _ => "eta-expanding polydefs...")
    3.75      |> eta_expand_poly
    3.76 -    |> debug 5 (Pretty.output o pretty_module)
    3.77 +    |> debug 12 (Pretty.output o pretty_module)
    3.78      |> debug 3 (fn _ => "tupelizing datatypes...")
    3.79      |> tupelize_cons
    3.80      |> debug 3 (fn _ => "eliminating classes...")
     4.1 --- a/src/Pure/Tools/codegen_thingol.ML	Fri Nov 25 14:51:39 2005 +0100
     4.2 +++ b/src/Pure/Tools/codegen_thingol.ML	Fri Nov 25 17:41:52 2005 +0100
     4.3 @@ -30,6 +30,7 @@
     4.4    val eq_iexpr: iexpr * iexpr -> bool
     4.5    val mk_funs: itype list * itype -> itype;
     4.6    val mk_apps: iexpr * iexpr list -> iexpr;
     4.7 +  val mk_abss: (vname * itype) list * iexpr -> iexpr;
     4.8    val pretty_itype: itype -> Pretty.T;
     4.9    val pretty_ipat: ipat -> Pretty.T;
    4.10    val pretty_iexpr: iexpr -> Pretty.T;
    4.11 @@ -129,6 +130,8 @@
    4.12    val `--> : itype list * itype -> itype;
    4.13    val `$ : iexpr * iexpr -> iexpr;
    4.14    val `$$ : iexpr * iexpr list -> iexpr;
    4.15 +  val `|-> : (vname * itype) * iexpr -> iexpr;
    4.16 +  val `|--> : (vname * itype) list * iexpr -> iexpr;
    4.17  end;
    4.18  
    4.19  
    4.20 @@ -192,6 +195,8 @@
    4.21  infixr 6 `-->;
    4.22  infix 4 `$;
    4.23  infix 4 `$$;
    4.24 +infixr 5 `|->;
    4.25 +infixr 5 `|-->;
    4.26  
    4.27  type vname = string;
    4.28  
    4.29 @@ -223,12 +228,15 @@
    4.30  
    4.31  val mk_funs = Library.foldr IFun;
    4.32  val mk_apps = Library.foldl IApp;
    4.33 +val mk_abss = Library.foldr IAbs;
    4.34  
    4.35 -fun tyco `%% tys = IType (tyco, tys);
    4.36 +val op `%% = IType;
    4.37  val op `-> = IFun;
    4.38 -fun f `$ x = IApp (f, x);
    4.39 +val op `$ = IApp;
    4.40 +val op `|-> = IAbs;
    4.41  val op `--> = mk_funs;
    4.42  val op `$$ = mk_apps;
    4.43 +val op `|--> = mk_abss;
    4.44  
    4.45  val unfold_fun = unfoldr
    4.46    (fn IFun t => SOME t
    4.47 @@ -1171,10 +1179,12 @@
    4.48      fun resolver modl name =
    4.49        if NameSpace.is_qualified name then
    4.50          let
    4.51 +          val _ = debug 12 (fn name' => "resolving " ^ quote name ^ " in " ^ (quote o NameSpace.pack) modl) ();
    4.52            val modl' = if null modl then [] else (NameSpace.unpack o the o Symtab.lookup tab o NameSpace.pack) modl;
    4.53            val name' = (NameSpace.unpack o the o Symtab.lookup tab) name
    4.54          in
    4.55            (NameSpace.pack o #3 o get_prefix (op =)) (modl', name')
    4.56 +          |> debug 12 (fn name' => "resolving " ^ quote name ^ " to " ^ quote name' ^ " in " ^ (quote o NameSpace.pack) modl)
    4.57          end
    4.58        else name
    4.59    in resolver end;
    4.60 @@ -1187,13 +1197,14 @@
    4.61      val resolvtab = mk_resolvtab nspgrp validate module;
    4.62      val resolver = mk_resolv resolvtab;
    4.63      fun seri prfx ([(name, Module module)]) =
    4.64 -          s_module (name,
    4.65 +          s_module (resolver prfx (prfx @ [name] |> NameSpace.pack),
    4.66              (map (seri (prfx @ [name]))
    4.67                 ((map (AList.make (Graph.get_node module)) o rev o Graph.strong_conn) module)))
    4.68        | seri prfx ds =
    4.69 -          s_def (resolver prfx) (map (fn (name, Def def) => (name, def)) ds)
    4.70 +          s_def (resolver prfx) (map (fn (name, Def def) => (resolver prfx (prfx @ [name] |> NameSpace.pack), def)) ds)
    4.71    in
    4.72 -    seri [] [(name_root, Module module)]
    4.73 +    s_module (name_root, (map (seri [])
    4.74 +      ((map (AList.make (Graph.get_node module)) o rev o Graph.strong_conn) module)))
    4.75    end;
    4.76  
    4.77  end; (* struct *)