src/Pure/codegen_axclass.ML
author obua
Mon, 13 Jun 2005 21:28:57 +0200
changeset 16384 90c51c932154
parent 16381 f53ccd0cc70e
permissions -rw-r--r--
internalize axiom names in Defs.define; compress types via Term.compress_type
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16381
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
     1
(*  Title:      Pure/codegen.ML
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
     2
    ID:         $Id$
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
     3
    Author:     Florian Haftmann, TU Muenchen
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
     4
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
     5
Stub code generator for (axiomatic) type classes
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
     6
*)
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
     7
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
     8
(*!!! for now, only experimental scratch code !!!*)
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
     9
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    10
type theory = unit;
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    11
type constname = string;
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    12
type typerepr = string;
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    13
type clsname = string;
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    14
type typname = string;
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    15
type defname = string;
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    16
type constdef = constname * typerepr;
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    17
type instdecl = typname * clsname list * defname list;
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    18
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    19
signature CODEGEN_AXCLASS =
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    20
sig
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    21
  val gen_clsdict_typ: theory -> clsname -> clsname list -> constdef list -> string; 
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    22
  val gen_clsdict_insts: theory -> clsname -> clsname list -> instdecl list -> constdef list -> string; 
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    23
  val gen_clsdicts: theory -> clsname -> clsname list -> instdecl list -> constdef list -> string -> string; 
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    24
end;
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    25
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    26
structure CodegenAxclass : CODEGEN_AXCLASS =
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    27
struct
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    28
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    29
fun enumerate ls = "  " ^ (space_implode ",\n  " ls) ^ "\n"
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    30
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    31
fun get_dictidf thy cls = cls ^ "_dict";
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    32
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    33
fun get_dictidf_inst thy cls ty = cls ^ "_" ^ ty ^ "_dict";
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    34
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    35
fun get_clsidf thy cls = cls ^ "_class";
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    36
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    37
fun get_memidf thy const = const;
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    38
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    39
fun gen_clsdict_typ thy cls supclss consts =
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    40
  "type 'a " ^ (get_dictidf thy cls) ^ " = {\n"
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    41
  ^ (((supclss |> map (fn supcls => (get_clsidf thy supcls) ^ ": 'a " ^ (get_dictidf thy supcls)))
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    42
      @ (consts |> map (fn (cname, ctyp) => (get_memidf thy cname) ^ ": " ^ ctyp)))
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    43
      |> enumerate)
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    44
  ^ "};\n";
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    45
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    46
fun gen_clsdict_mem thy cls consts =
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    47
  consts |> map (fn (cname, _) =>
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    48
    "fun " ^ (get_memidf thy cname) ^ " (cdict:'a " ^ (get_dictidf thy cls) ^ ") = #" ^ (get_memidf thy cname) ^ " cdict;\n")
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    49
  |> space_implode "";
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    50
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    51
fun gen_clsdict_inst thy cls supclss (ty, clsargs, constdefs) consts =
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    52
  "val " ^ (get_dictidf_inst thy cls ty) ^ " = {\n"
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    53
  ^ (((supclss |> map (fn supcls => (get_clsidf thy cls) ^ " = " ^ (get_dictidf_inst thy supcls ty)))
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    54
      @ ((fst (split_list consts) ~~ constdefs) |>
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    55
        map (fn (cname, dname) => (get_memidf thy cname) ^ " = " ^ dname)))
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    56
      |> enumerate)
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    57
  ^ "};\n";
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    58
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    59
fun gen_clsdict_insts thy cls supclss insts consts =
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    60
  insts |> map (fn inst => gen_clsdict_inst thy cls supclss inst consts) |> cat_lines
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    61
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    62
fun gen_clsdicts thy cls supclss insts consts constdefs =
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    63
  gen_clsdict_typ thy cls supclss consts ^ "\n"
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    64
  ^ gen_clsdict_mem thy cls consts ^ "\n"
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    65
  ^ constdefs ^ "\n"
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    66
  ^ (insts |> map (fn inst => gen_clsdict_inst thy cls supclss inst consts) |> cat_lines);
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    67
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    68
end;
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    69
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    70
open CodegenAxclass;
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    71
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    72
val ex_list =
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    73
"fun list_le _ _ [] _  = true\n" ^ 
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    74
"  | list_le _ _ _  [] = false\n" ^ 
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    75
"  | list_le (cdict:'a list ord_dict) (cdict1:'a ord_dict) (x::xs) (y::ys) = #lt cdict1 x y orelse #eq (#eq_class cdict1) x y andalso list_le cdict cdict1 xs ys;\n\n" ^
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    76
"fun list_lt _ _ [] [] = false\n" ^ 
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    77
"  | list_lt _ _ [] _  = true\n" ^ 
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    78
"  | list_lt _ _ _  [] = false\n" ^ 
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    79
"  | list_lt (cdict:'a list ord_dict) (cdict1:'a ord_dict) (x::xs) (y::ys) = #lt cdict1 x y orelse #eq (#eq_class cdict1) x y andalso list_lt cdict cdict1 xs ys;\n\n"
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    80
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    81
val ex_eq = gen_clsdicts ()
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    82
    "eq" []
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    83
    [("int", [], ["(op =)"]),
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    84
     ("char", [], ["(op =)"]),
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    85
     ("list", [], ["(op =)"])]
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    86
    [("eq", "'a -> 'a -> bool")]
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    87
    "";
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    88
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    89
val ex_ord = gen_clsdicts ()
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    90
    "ord" ["eq"]
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    91
    [("int", [], ["(op <=)", "(op <)"]),
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    92
     ("char", [], ["(op <=)", "(op <)"]),
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    93
     ("list", ["ord"], ["list_le", "list_lt"])]
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    94
    [("le", "'a -> 'a -> bool"), ("lt", "'a -> 'a -> bool")]
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    95
    ex_list;
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    96
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    97
fun ex _ = TextIO.output (TextIO.stdOut, cat_lines [ex_eq, ex_ord]);
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    98
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
    99
(* TODO:
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
   100
- Dependency: dicttypdecl, members, (instancedef, instance)+
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
   101
- Nahtstelle ausfindig machen
f53ccd0cc70e just non_functional experimantal code, by now
haftmann
parents:
diff changeset
   102
*)