src/Tools/code/code_thingol.ML
author wenzelm
Tue, 24 Jun 2008 23:38:44 +0200
changeset 27350 c8adf88960ad
parent 27103 d8549f4d900b
child 27422 73d25d422124
permissions -rw-r--r--
back to 1.36 (Poly/ML crash!?);
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     1
(*  Title:      Tools/code/code_thingol.ML
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     2
    ID:         $Id$
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     3
    Author:     Florian Haftmann, TU Muenchen
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     4
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     5
Intermediate language ("Thin-gol") representing executable code.
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
     6
Representation and translation.
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     7
*)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     8
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     9
infix 8 `%%;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    10
infixr 6 `->;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    11
infixr 6 `-->;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    12
infix 4 `$;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    13
infix 4 `$$;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    14
infixr 3 `|->;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    15
infixr 3 `|-->;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    16
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    17
signature BASIC_CODE_THINGOL =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    18
sig
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    19
  type vname = string;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    20
  datatype dict =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    21
      DictConst of string * dict list list
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    22
    | DictVar of string list * (vname * (int * int));
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    23
  datatype itype =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    24
      `%% of string * itype list
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    25
    | ITyVar of vname;
24591
6509626eb2c9 added explicit theorems
haftmann
parents: 24381
diff changeset
    26
  type const = string * (dict list list * itype list (*types of arguments*))
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    27
  datatype iterm =
24591
6509626eb2c9 added explicit theorems
haftmann
parents: 24381
diff changeset
    28
      IConst of const
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    29
    | IVar of vname
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    30
    | `$ of iterm * iterm
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    31
    | `|-> of (vname * itype) * iterm
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    32
    | ICase of ((iterm * itype) * (iterm * iterm) list) * iterm;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    33
        (*((term, type), [(selector pattern, body term )]), primitive term)*)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    34
  val `-> : itype * itype -> itype;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    35
  val `--> : itype list * itype -> itype;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    36
  val `$$ : iterm * iterm list -> iterm;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    37
  val `|--> : (vname * itype) list * iterm -> iterm;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    38
  type typscheme = (vname * sort) list * itype;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    39
end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    40
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    41
signature CODE_THINGOL =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    42
sig
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    43
  include BASIC_CODE_THINGOL;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    44
  val unfoldl: ('a -> ('a * 'b) option) -> 'a -> 'a * 'b list;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    45
  val unfoldr: ('a -> ('b * 'a) option) -> 'a -> 'b list * 'a;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    46
  val unfold_fun: itype -> itype list * itype;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    47
  val unfold_app: iterm -> iterm * iterm list;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    48
  val split_abs: iterm -> (((vname * iterm option) * itype) * iterm) option;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    49
  val unfold_abs: iterm -> ((vname * iterm option) * itype) list * iterm;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    50
  val split_let: iterm -> (((iterm * itype) * iterm) * iterm) option;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    51
  val unfold_let: iterm -> ((iterm * itype) * iterm) list * iterm;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    52
  val unfold_const_app: iterm ->
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    53
    ((string * (dict list list * itype list)) * iterm list) option;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    54
  val collapse_let: ((vname * itype) * iterm) * iterm
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    55
    -> (iterm * itype) * (iterm * iterm) list;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    56
  val eta_expand: (string * (dict list list * itype list)) * iterm list -> int -> iterm;
24662
f79f6061525c more precise treatment of free dictionary parameters for evaluation
haftmann
parents: 24591
diff changeset
    57
  val contains_dictvar: iterm -> bool;
25621
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25597
diff changeset
    58
  val locally_monomorphic: iterm -> bool;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    59
  val fold_constnames: (string -> 'a -> 'a) -> iterm -> 'a -> 'a;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    60
  val fold_varnames: (string -> 'a -> 'a) -> iterm -> 'a -> 'a;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    61
  val fold_unbound_varnames: (string -> 'a -> 'a) -> iterm -> 'a -> 'a;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    62
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
    63
  datatype stmt =
27024
fcab2dd46872 various code streamlining
haftmann
parents: 26972
diff changeset
    64
      NoStmt
24591
6509626eb2c9 added explicit theorems
haftmann
parents: 24381
diff changeset
    65
    | Fun of typscheme * ((iterm list * iterm) * thm) list
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    66
    | Datatype of (vname * sort) list * (string * itype list) list
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    67
    | Datatypecons of string
24811
3bf788a0c49a clarified role of class relations
haftmann
parents: 24662
diff changeset
    68
    | Class of vname * ((class * string) list * (string * itype) list)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    69
    | Classrel of class * class
24837
cacc5744be75 clarified terminology
haftmann
parents: 24811
diff changeset
    70
    | Classparam of class
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    71
    | Classinst of (class * (string * (vname * sort) list))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    72
          * ((class * (string * (string * dict list list))) list
24591
6509626eb2c9 added explicit theorems
haftmann
parents: 24381
diff changeset
    73
        * ((string * const) * thm) list);
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
    74
  type program = stmt Graph.T;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
    75
  val empty_funs: program -> string list;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
    76
  val transitivly_non_empty_funs: program -> string list -> string list;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
    77
  val is_cons: program -> string -> bool;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
    78
  val contr_classparam_typs: program -> string -> itype option list;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    79
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
    80
  val consts_program: theory -> string list -> string list * program;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
    81
  val cached_program: theory -> program;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
    82
  val eval_conv: theory
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
    83
    -> (term -> term * (program -> typscheme * iterm -> string list -> thm))
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
    84
    -> cterm -> thm;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
    85
  val eval_term: theory
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
    86
    -> (term -> term * (program -> typscheme * iterm -> string list -> 'a))
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
    87
    -> term -> 'a;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    88
end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    89
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    90
structure CodeThingol: CODE_THINGOL =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    91
struct
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    92
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    93
(** auxiliary **)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    94
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    95
fun unfoldl dest x =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    96
  case dest x
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    97
   of NONE => (x, [])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    98
    | SOME (x1, x2) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    99
        let val (x', xs') = unfoldl dest x1 in (x', xs' @ [x2]) end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   100
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   101
fun unfoldr dest x =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   102
  case dest x
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   103
   of NONE => ([], x)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   104
    | SOME (x1, x2) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   105
        let val (xs', x') = unfoldr dest x2 in (x1::xs', x') end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   106
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   107
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   108
(** language core - types, pattern, expressions **)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   109
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   110
(* language representation *)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   111
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   112
type vname = string;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   113
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   114
datatype dict =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   115
    DictConst of string * dict list list
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   116
  | DictVar of string list * (vname * (int * int));
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   117
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   118
datatype itype =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   119
    `%% of string * itype list
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   120
  | ITyVar of vname;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   121
24591
6509626eb2c9 added explicit theorems
haftmann
parents: 24381
diff changeset
   122
type const = string * (dict list list * itype list (*types of arguments*))
6509626eb2c9 added explicit theorems
haftmann
parents: 24381
diff changeset
   123
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   124
datatype iterm =
24591
6509626eb2c9 added explicit theorems
haftmann
parents: 24381
diff changeset
   125
    IConst of const
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   126
  | IVar of vname
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   127
  | `$ of iterm * iterm
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   128
  | `|-> of (vname * itype) * iterm
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   129
  | ICase of ((iterm * itype) * (iterm * iterm) list) * iterm;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   130
    (*see also signature*)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   131
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   132
(*
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   133
  variable naming conventions
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   134
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   135
  bare names:
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   136
    variable names          v
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   137
    class names             class
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   138
    type constructor names  tyco
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   139
    datatype names          dtco
24837
cacc5744be75 clarified terminology
haftmann
parents: 24811
diff changeset
   140
    const names (general)   c (const)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   141
    constructor names       co
24837
cacc5744be75 clarified terminology
haftmann
parents: 24811
diff changeset
   142
    class parameter names   classparam
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   143
    arbitrary name          s
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   144
24837
cacc5744be75 clarified terminology
haftmann
parents: 24811
diff changeset
   145
    v, c, co, classparam also annotated with types etc.
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   146
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   147
  constructs:
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   148
    sort                    sort
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   149
    type parameters         vs
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   150
    type                    ty
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   151
    type schemes            tysm
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   152
    term                    t
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   153
    (term as pattern)       p
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   154
    instance (class, tyco)  inst
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   155
 *)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   156
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   157
fun ty1 `-> ty2 = "fun" `%% [ty1, ty2];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   158
val op `--> = Library.foldr (op `->);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   159
val op `$$ = Library.foldl (op `$);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   160
val op `|--> = Library.foldr (op `|->);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   161
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   162
val unfold_fun = unfoldr
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   163
  (fn "fun" `%% [ty1, ty2] => SOME (ty1, ty2)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   164
    | _ => NONE);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   165
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   166
val unfold_app = unfoldl
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   167
  (fn op `$ t => SOME t
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   168
    | _ => NONE);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   169
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   170
val split_abs =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   171
  (fn (v, ty) `|-> (t as ICase (((IVar w, _), [(p, t')]), _)) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   172
        if v = w then SOME (((v, SOME p), ty), t') else SOME (((v, NONE), ty), t)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   173
    | (v, ty) `|-> t => SOME (((v, NONE), ty), t)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   174
    | _ => NONE);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   175
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   176
val unfold_abs = unfoldr split_abs;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   177
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   178
val split_let = 
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   179
  (fn ICase (((td, ty), [(p, t)]), _) => SOME (((p, ty), td), t)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   180
    | _ => NONE);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   181
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   182
val unfold_let = unfoldr split_let;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   183
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   184
fun unfold_const_app t =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   185
 case unfold_app t
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   186
  of (IConst c, ts) => SOME (c, ts)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   187
   | _ => NONE;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   188
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   189
fun fold_aiterms f (t as IConst _) = f t
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   190
  | fold_aiterms f (t as IVar _) = f t
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   191
  | fold_aiterms f (t1 `$ t2) = fold_aiterms f t1 #> fold_aiterms f t2
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   192
  | fold_aiterms f (t as _ `|-> t') = f t #> fold_aiterms f t'
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   193
  | fold_aiterms f (ICase (_, t)) = fold_aiterms f t;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   194
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   195
fun fold_constnames f =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   196
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   197
    fun add (IConst (c, _)) = f c
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   198
      | add _ = I;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   199
  in fold_aiterms add end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   200
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   201
fun fold_varnames f =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   202
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   203
    fun add (IVar v) = f v
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   204
      | add ((v, _) `|-> _) = f v
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   205
      | add _ = I;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   206
  in fold_aiterms add end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   207
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   208
fun fold_unbound_varnames f =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   209
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   210
    fun add _ (IConst _) = I
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   211
      | add vs (IVar v) = if not (member (op =) vs v) then f v else I
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   212
      | add vs (t1 `$ t2) = add vs t1 #> add vs t2
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   213
      | add vs ((v, _) `|-> t) = add (insert (op =) v vs) t
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   214
      | add vs (ICase (_, t)) = add vs t;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   215
  in add [] end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   216
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   217
fun collapse_let (((v, ty), se), be as ICase (((IVar w, _), ds), _)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   218
      let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   219
        fun exists_v t = fold_unbound_varnames (fn w => fn b =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   220
          b orelse v = w) t false;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   221
      in if v = w andalso forall (fn (t1, t2) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   222
        exists_v t1 orelse not (exists_v t2)) ds
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   223
        then ((se, ty), ds)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   224
        else ((se, ty), [(IVar v, be)])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   225
      end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   226
  | collapse_let (((v, ty), se), be) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   227
      ((se, ty), [(IVar v, be)])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   228
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   229
fun eta_expand (c as (_, (_, tys)), ts) k =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   230
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   231
    val j = length ts;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   232
    val l = k - j;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   233
    val ctxt = (fold o fold_varnames) Name.declare ts Name.context;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   234
    val vs_tys = Name.names ctxt "a" ((curry Library.take l o curry Library.drop j) tys);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   235
  in vs_tys `|--> IConst c `$$ ts @ map (fn (v, _) => IVar v) vs_tys end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   236
24662
f79f6061525c more precise treatment of free dictionary parameters for evaluation
haftmann
parents: 24591
diff changeset
   237
fun contains_dictvar t =
f79f6061525c more precise treatment of free dictionary parameters for evaluation
haftmann
parents: 24591
diff changeset
   238
  let
f79f6061525c more precise treatment of free dictionary parameters for evaluation
haftmann
parents: 24591
diff changeset
   239
    fun contains (DictConst (_, dss)) = (fold o fold) contains dss
f79f6061525c more precise treatment of free dictionary parameters for evaluation
haftmann
parents: 24591
diff changeset
   240
      | contains (DictVar _) = K true;
f79f6061525c more precise treatment of free dictionary parameters for evaluation
haftmann
parents: 24591
diff changeset
   241
  in
f79f6061525c more precise treatment of free dictionary parameters for evaluation
haftmann
parents: 24591
diff changeset
   242
    fold_aiterms
f79f6061525c more precise treatment of free dictionary parameters for evaluation
haftmann
parents: 24591
diff changeset
   243
      (fn IConst (_, (dss, _)) => (fold o fold) contains dss | _ => I) t false
f79f6061525c more precise treatment of free dictionary parameters for evaluation
haftmann
parents: 24591
diff changeset
   244
  end;
f79f6061525c more precise treatment of free dictionary parameters for evaluation
haftmann
parents: 24591
diff changeset
   245
  
25621
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25597
diff changeset
   246
fun locally_monomorphic (IConst _) = false
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25597
diff changeset
   247
  | locally_monomorphic (IVar _) = true
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25597
diff changeset
   248
  | locally_monomorphic (t `$ _) = locally_monomorphic t
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25597
diff changeset
   249
  | locally_monomorphic (_ `|-> t) = locally_monomorphic t
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25597
diff changeset
   250
  | locally_monomorphic (ICase ((_, ds), _)) = exists (locally_monomorphic o snd) ds;
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25597
diff changeset
   251
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25597
diff changeset
   252
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   253
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   254
(** statements, abstract programs **)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   255
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   256
type typscheme = (vname * sort) list * itype;
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   257
datatype stmt =
27024
fcab2dd46872 various code streamlining
haftmann
parents: 26972
diff changeset
   258
    NoStmt
24591
6509626eb2c9 added explicit theorems
haftmann
parents: 24381
diff changeset
   259
  | Fun of typscheme * ((iterm list * iterm) * thm) list
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   260
  | Datatype of (vname * sort) list * (string * itype list) list
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   261
  | Datatypecons of string
24811
3bf788a0c49a clarified role of class relations
haftmann
parents: 24662
diff changeset
   262
  | Class of vname * ((class * string) list * (string * itype) list)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   263
  | Classrel of class * class
24837
cacc5744be75 clarified terminology
haftmann
parents: 24811
diff changeset
   264
  | Classparam of class
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   265
  | Classinst of (class * (string * (vname * sort) list))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   266
        * ((class * (string * (string * dict list list))) list
24591
6509626eb2c9 added explicit theorems
haftmann
parents: 24381
diff changeset
   267
      * ((string * const) * thm) list);
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   268
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   269
type program = stmt Graph.T;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   270
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   271
fun empty_funs program =
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   272
  Graph.fold (fn (name, (Fun (_, []), _)) => cons name
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   273
               | _ => I) program [];
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   274
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   275
fun transitivly_non_empty_funs program names_ignored =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   276
  let
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   277
    val names_empty = empty_funs program;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   278
    val names_delete = Graph.all_preds program (subtract (op =) names_ignored names_empty)
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   279
  in subtract (op =) names_delete (Graph.keys program) end;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   280
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   281
fun is_cons program name = case Graph.get_node program name
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   282
 of Datatypecons _ => true
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   283
  | _ => false;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   284
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   285
fun contr_classparam_typs program name = case Graph.get_node program name
25621
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25597
diff changeset
   286
 of Classparam class => let
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   287
        val Class (_, (_, params)) = Graph.get_node program class;
25621
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25597
diff changeset
   288
        val SOME ty = AList.lookup (op =) params name;
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25597
diff changeset
   289
        val (tys, res_ty) = unfold_fun ty;
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25597
diff changeset
   290
        fun no_tyvar (_ `%% tys) = forall no_tyvar tys
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25597
diff changeset
   291
          | no_tyvar (ITyVar _) = false;
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25597
diff changeset
   292
      in if no_tyvar res_ty
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25597
diff changeset
   293
        then map (fn ty => if no_tyvar ty then NONE else SOME ty) tys
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25597
diff changeset
   294
        else []
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25597
diff changeset
   295
      end
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25597
diff changeset
   296
  | _ => [];
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25597
diff changeset
   297
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   298
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   299
(** translation kernel **)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   300
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   301
type transaction = Graph.key option * program;
24283
haftmann
parents: 24252
diff changeset
   302
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   303
fun ensure_stmt stmtgen name (dep, program) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   304
  let
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   305
    val add_dep = case dep of NONE => I | SOME dep => Graph.add_edge (dep, name);
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   306
    fun add_stmt false =
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   307
          Graph.default_node (name, NoStmt)
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   308
          #> add_dep
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   309
          #> curry stmtgen (SOME name)
24252
4eb5bc6af008 simplified
haftmann
parents: 24219
diff changeset
   310
          ##> snd
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   311
          #-> (fn stmt => Graph.map_node name (K stmt))
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   312
      | add_stmt true =
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   313
          add_dep;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   314
  in
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   315
    program
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   316
    |> add_stmt (can (Graph.get_node program) name)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   317
    |> pair dep
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   318
    |> pair name
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   319
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   320
26972
haftmann
parents: 26939
diff changeset
   321
fun not_wellsorted thy thm ty sort e =
haftmann
parents: 26939
diff changeset
   322
  let
haftmann
parents: 26939
diff changeset
   323
    val err_class = Sorts.class_error (Syntax.pp_global thy) e;
haftmann
parents: 26939
diff changeset
   324
    val err_thm = case thm
haftmann
parents: 26939
diff changeset
   325
     of SOME thm => "\n(in defining equation " ^ Display.string_of_thm thm ^ ")" | NONE => "";
haftmann
parents: 26939
diff changeset
   326
    val err_typ = "Type " ^ Syntax.string_of_typ_global thy ty ^ " not of sort "
haftmann
parents: 26939
diff changeset
   327
      ^ Syntax.string_of_sort_global thy sort;
haftmann
parents: 26939
diff changeset
   328
  in error ("Wellsortedness error" ^ err_thm ^ ":\n" ^ err_typ ^ "\n" ^ err_class) end;
haftmann
parents: 26939
diff changeset
   329
haftmann
parents: 26939
diff changeset
   330
fun ensure_class thy (algbr as (_, algebra)) funcgr class =
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   331
  let
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   332
    val superclasses = (Sorts.certify_sort algebra o Sorts.super_classes algebra) class;
24969
b38527eefb3b removed obsolete AxClass.params_of_class;
wenzelm
parents: 24918
diff changeset
   333
    val cs = #params (AxClass.get_info thy class);
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   334
    val class' = CodeName.class thy class;
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   335
    val stmt_class =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   336
      fold_map (fn superclass => ensure_class thy algbr funcgr superclass
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   337
        ##>> ensure_classrel thy algbr funcgr (class, superclass)) superclasses
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   338
      ##>> fold_map (fn (c, ty) => ensure_const thy algbr funcgr c
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   339
        ##>> exprgen_typ thy algbr funcgr ty) cs
24969
b38527eefb3b removed obsolete AxClass.params_of_class;
wenzelm
parents: 24918
diff changeset
   340
      #>> (fn info => Class (unprefix "'" Name.aT, info))
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   341
  in
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   342
    ensure_stmt stmt_class class'
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   343
  end
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   344
and ensure_classrel thy algbr funcgr (subclass, superclass) =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   345
  let
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   346
    val classrel' = CodeName.classrel thy (subclass, superclass);
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   347
    val stmt_classrel =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   348
      ensure_class thy algbr funcgr subclass
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   349
      ##>> ensure_class thy algbr funcgr superclass
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   350
      #>> Classrel;
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   351
  in
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   352
    ensure_stmt stmt_classrel classrel'
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   353
  end
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   354
and ensure_tyco thy algbr funcgr "fun" =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   355
      pair "fun"
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   356
  | ensure_tyco thy algbr funcgr tyco =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   357
      let
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   358
        val stmt_datatype =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   359
          let
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   360
            val (vs, cos) = Code.get_datatype thy tyco;
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   361
          in
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   362
            fold_map (exprgen_tyvar_sort thy algbr funcgr) vs
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   363
            ##>> fold_map (fn (c, tys) =>
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   364
              ensure_const thy algbr funcgr c
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   365
              ##>> fold_map (exprgen_typ thy algbr funcgr) tys) cos
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   366
            #>> Datatype
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   367
          end;
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   368
        val tyco' = CodeName.tyco thy tyco;
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   369
      in
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   370
        ensure_stmt stmt_datatype tyco'
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   371
      end
26972
haftmann
parents: 26939
diff changeset
   372
and exprgen_tyvar_sort thy (algbr as (proj_sort, _)) funcgr (v, sort) =
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   373
  fold_map (ensure_class thy algbr funcgr) (proj_sort sort)
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   374
  #>> (fn sort => (unprefix "'" v, sort))
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   375
and exprgen_typ thy algbr funcgr (TFree vs) =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   376
      exprgen_tyvar_sort thy algbr funcgr vs
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   377
      #>> (fn (v, sort) => ITyVar v)
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   378
  | exprgen_typ thy algbr funcgr (Type (tyco, tys)) =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   379
      ensure_tyco thy algbr funcgr tyco
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   380
      ##>> fold_map (exprgen_typ thy algbr funcgr) tys
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   381
      #>> (fn (tyco, tys) => tyco `%% tys)
26972
haftmann
parents: 26939
diff changeset
   382
and exprgen_dicts thy (algbr as (proj_sort, algebra)) funcgr thm (ty, sort) =
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   383
  let
26939
1035c89b4c02 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26011
diff changeset
   384
    val pp = Syntax.pp_global thy;
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   385
    datatype typarg =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   386
        Global of (class * string) * typarg list list
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   387
      | Local of (class * class) list * (string * (int * sort));
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   388
    fun class_relation (Global ((_, tyco), yss), _) class =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   389
          Global ((class, tyco), yss)
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   390
      | class_relation (Local (classrels, v), subclass) superclass =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   391
          Local ((subclass, superclass) :: classrels, v);
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   392
    fun type_constructor tyco yss class =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   393
      Global ((class, tyco), (map o map) fst yss);
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   394
    fun type_variable (TFree (v, sort)) =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   395
      let
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   396
        val sort' = proj_sort sort;
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   397
      in map_index (fn (n, class) => (Local ([], (v, (n, sort'))), class)) sort' end;
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   398
    val typargs = Sorts.of_sort_derivation pp algebra
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   399
      {class_relation = class_relation, type_constructor = type_constructor,
26972
haftmann
parents: 26939
diff changeset
   400
       type_variable = type_variable} (ty, proj_sort sort)
haftmann
parents: 26939
diff changeset
   401
      handle Sorts.CLASS_ERROR e => not_wellsorted thy thm ty sort e;
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   402
    fun mk_dict (Global (inst, yss)) =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   403
          ensure_inst thy algbr funcgr inst
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   404
          ##>> (fold_map o fold_map) mk_dict yss
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   405
          #>> (fn (inst, dss) => DictConst (inst, dss))
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   406
      | mk_dict (Local (classrels, (v, (k, sort)))) =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   407
          fold_map (ensure_classrel thy algbr funcgr) classrels
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   408
          #>> (fn classrels => DictVar (classrels, (unprefix "'" v, (k, length sort))))
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   409
  in
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   410
    fold_map mk_dict typargs
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   411
  end
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   412
and exprgen_eq thy algbr funcgr thm =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   413
  let
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   414
    val (args, rhs) = (apfst (snd o strip_comb) o Logic.dest_equals
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   415
      o Logic.unvarify o prop_of) thm;
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   416
  in
26972
haftmann
parents: 26939
diff changeset
   417
    fold_map (exprgen_term thy algbr funcgr (SOME thm)) args
haftmann
parents: 26939
diff changeset
   418
    ##>> exprgen_term thy algbr funcgr (SOME thm) rhs
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   419
    #>> rpair thm
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   420
  end
26972
haftmann
parents: 26939
diff changeset
   421
and ensure_inst thy (algbr as (_, algebra)) funcgr (class, tyco) =
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   422
  let
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   423
    val superclasses = (Sorts.certify_sort algebra o Sorts.super_classes algebra) class;
24969
b38527eefb3b removed obsolete AxClass.params_of_class;
wenzelm
parents: 24918
diff changeset
   424
    val classparams = these (try (#params o AxClass.get_info thy) class);
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   425
    val vs = Name.names Name.context "'a" (Sorts.mg_domain algebra tyco [class]);
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   426
    val sorts' = Sorts.mg_domain (Sign.classes_of thy) tyco [class];
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   427
    val vs' = map2 (fn (v, sort1) => fn sort2 => (v,
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   428
      Sorts.inter_sort (Sign.classes_of thy) (sort1, sort2))) vs sorts';
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   429
    val arity_typ = Type (tyco, map TFree vs);
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   430
    val arity_typ' = Type (tyco, map (fn (v, sort) => TVar ((v, 0), sort)) vs');
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   431
    fun exprgen_superarity superclass =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   432
      ensure_class thy algbr funcgr superclass
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   433
      ##>> ensure_classrel thy algbr funcgr (class, superclass)
26972
haftmann
parents: 26939
diff changeset
   434
      ##>> exprgen_dicts thy algbr funcgr NONE (arity_typ, [superclass])
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   435
      #>> (fn ((superclass, classrel), [DictConst (inst, dss)]) =>
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   436
            (superclass, (classrel, (inst, dss))));
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   437
    fun exprgen_classparam_inst (c, ty) =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   438
      let
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   439
        val c_inst = Const (c, map_type_tfree (K arity_typ') ty);
25597
34860182b250 moved instance parameter management from class.ML to axclass.ML
haftmann
parents: 25485
diff changeset
   440
        val thm = AxClass.unoverload_conv thy (Thm.cterm_of thy c_inst);
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   441
        val c_ty = (apsnd Logic.unvarifyT o dest_Const o snd
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   442
          o Logic.dest_equals o Thm.prop_of) thm;
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   443
      in
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   444
        ensure_const thy algbr funcgr c
26972
haftmann
parents: 26939
diff changeset
   445
        ##>> exprgen_const thy algbr funcgr (SOME thm) c_ty
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   446
        #>> (fn (c, IConst c_inst) => ((c, c_inst), thm))
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   447
      end;
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   448
    val stmt_inst =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   449
      ensure_class thy algbr funcgr class
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   450
      ##>> ensure_tyco thy algbr funcgr tyco
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   451
      ##>> fold_map (exprgen_tyvar_sort thy algbr funcgr) vs
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   452
      ##>> fold_map exprgen_superarity superclasses
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   453
      ##>> fold_map exprgen_classparam_inst classparams
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   454
      #>> (fn ((((class, tyco), arity), superarities), classparams) =>
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   455
             Classinst ((class, (tyco, arity)), (superarities, classparams)));
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   456
    val inst = CodeName.instance thy (class, tyco);
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   457
  in
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   458
    ensure_stmt stmt_inst inst
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   459
  end
26972
haftmann
parents: 26939
diff changeset
   460
and ensure_const thy algbr funcgr c =
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   461
  let
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   462
    val c' = CodeName.const thy c;
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   463
    fun stmt_datatypecons tyco =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   464
      ensure_tyco thy algbr funcgr tyco
25621
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25597
diff changeset
   465
      #>> Datatypecons;
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   466
    fun stmt_classparam class =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   467
      ensure_class thy algbr funcgr class
25621
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25597
diff changeset
   468
      #>> Classparam;
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   469
    fun stmt_fun trns =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   470
      let
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   471
        val raw_thms = CodeFuncgr.funcs funcgr c;
26972
haftmann
parents: 26939
diff changeset
   472
        val (vs, raw_ty) = CodeFuncgr.typ funcgr c;
haftmann
parents: 26939
diff changeset
   473
        val ty = Logic.unvarifyT raw_ty;
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   474
        val thms = if (null o Term.typ_tfrees) ty orelse (null o fst o strip_type) ty
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   475
          then raw_thms
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   476
          else map (CodeUnit.expand_eta 1) raw_thms;
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   477
      in
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   478
        trns
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   479
        |> fold_map (exprgen_tyvar_sort thy algbr funcgr) vs
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   480
        ||>> exprgen_typ thy algbr funcgr ty
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   481
        ||>> fold_map (exprgen_eq thy algbr funcgr) thms
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   482
        |>> (fn ((vs, ty), eqs) => Fun ((vs, ty), eqs))
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   483
      end;
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   484
    val stmtgen = case Code.get_datatype_of_constr thy c
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   485
     of SOME tyco => stmt_datatypecons tyco
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   486
      | NONE => (case AxClass.class_of_param thy c
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   487
         of SOME class => stmt_classparam class
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   488
          | NONE => stmt_fun)
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   489
  in
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   490
    ensure_stmt stmtgen c'
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   491
  end
26972
haftmann
parents: 26939
diff changeset
   492
and exprgen_term thy algbr funcgr thm (Const (c, ty)) =
haftmann
parents: 26939
diff changeset
   493
      exprgen_app thy algbr funcgr thm ((c, ty), [])
haftmann
parents: 26939
diff changeset
   494
  | exprgen_term thy algbr funcgr thm (Free (v, _)) =
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   495
      pair (IVar v)
26972
haftmann
parents: 26939
diff changeset
   496
  | exprgen_term thy algbr funcgr thm (Abs (abs as (_, ty, _))) =
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   497
      let
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   498
        val (v, t) = Syntax.variant_abs abs;
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   499
      in
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   500
        exprgen_typ thy algbr funcgr ty
26972
haftmann
parents: 26939
diff changeset
   501
        ##>> exprgen_term thy algbr funcgr thm t
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   502
        #>> (fn (ty, t) => (v, ty) `|-> t)
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   503
      end
26972
haftmann
parents: 26939
diff changeset
   504
  | exprgen_term thy algbr funcgr thm (t as _ $ _) =
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   505
      case strip_comb t
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   506
       of (Const (c, ty), ts) =>
26972
haftmann
parents: 26939
diff changeset
   507
            exprgen_app thy algbr funcgr thm ((c, ty), ts)
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   508
        | (t', ts) =>
26972
haftmann
parents: 26939
diff changeset
   509
            exprgen_term thy algbr funcgr thm t'
haftmann
parents: 26939
diff changeset
   510
            ##>> fold_map (exprgen_term thy algbr funcgr thm) ts
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   511
            #>> (fn (t, ts) => t `$$ ts)
26972
haftmann
parents: 26939
diff changeset
   512
and exprgen_const thy algbr funcgr thm (c, ty) =
haftmann
parents: 26939
diff changeset
   513
  let
haftmann
parents: 26939
diff changeset
   514
    val tys = Sign.const_typargs thy (c, ty);
haftmann
parents: 26939
diff changeset
   515
    val sorts = (map snd o fst o CodeFuncgr.typ funcgr) c;
haftmann
parents: 26939
diff changeset
   516
    val tys_args = (fst o Term.strip_type) ty;
haftmann
parents: 26939
diff changeset
   517
  in
haftmann
parents: 26939
diff changeset
   518
    ensure_const thy algbr funcgr c
haftmann
parents: 26939
diff changeset
   519
    ##>> fold_map (exprgen_dicts thy algbr funcgr thm) (tys ~~ sorts)
haftmann
parents: 26939
diff changeset
   520
    ##>> fold_map (exprgen_typ thy algbr funcgr) tys_args
haftmann
parents: 26939
diff changeset
   521
    #>> (fn ((c, iss), tys) => IConst (c, (iss, tys)))
haftmann
parents: 26939
diff changeset
   522
  end
haftmann
parents: 26939
diff changeset
   523
and exprgen_app_default thy algbr funcgr thm (c_ty, ts) =
haftmann
parents: 26939
diff changeset
   524
  exprgen_const thy algbr funcgr thm c_ty
haftmann
parents: 26939
diff changeset
   525
  ##>> fold_map (exprgen_term thy algbr funcgr thm) ts
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   526
  #>> (fn (t, ts) => t `$$ ts)
26972
haftmann
parents: 26939
diff changeset
   527
and exprgen_case thy algbr funcgr thm n cases (app as ((c, ty), ts)) =
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   528
  let
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   529
    val (tys, _) =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   530
      (chop (1 + (if null cases then 1 else length cases)) o fst o strip_type) ty;
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   531
    val dt = nth ts n;
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   532
    val dty = nth tys n;
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   533
    fun is_undefined (Const (c, _)) = Code.is_undefined thy c
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   534
      | is_undefined _ = false;
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   535
    fun mk_case (co, n) t =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   536
      let
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   537
        val (vs, body) = Term.strip_abs_eta n t;
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   538
        val selector = list_comb (Const (co, map snd vs ---> dty), map Free vs);
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   539
      in if is_undefined body then NONE else SOME (selector, body) end;
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   540
    fun mk_ds [] =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   541
          let
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   542
            val ([v_ty], body) = Term.strip_abs_eta 1 (the_single (nth_drop n ts))
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   543
          in [(Free v_ty, body)] end
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   544
      | mk_ds cases = map_filter (uncurry mk_case)
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   545
          (AList.make (CodeUnit.no_args thy) cases ~~ nth_drop n ts);
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   546
  in
26972
haftmann
parents: 26939
diff changeset
   547
    exprgen_term thy algbr funcgr thm dt
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   548
    ##>> exprgen_typ thy algbr funcgr dty
26972
haftmann
parents: 26939
diff changeset
   549
    ##>> fold_map (fn (pat, body) => exprgen_term thy algbr funcgr thm pat
haftmann
parents: 26939
diff changeset
   550
          ##>> exprgen_term thy algbr funcgr thm body) (mk_ds cases)
haftmann
parents: 26939
diff changeset
   551
    ##>> exprgen_app_default thy algbr funcgr thm app
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   552
    #>> (fn (((dt, dty), ds), t0) => ICase (((dt, dty), ds), t0))
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   553
  end
26972
haftmann
parents: 26939
diff changeset
   554
and exprgen_app thy algbr funcgr thm ((c, ty), ts) = case Code.get_case_data thy c
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   555
 of SOME (n, cases) => let val i = 1 + (if null cases then 1 else length cases) in
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   556
      if length ts < i then
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   557
        let
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   558
          val k = length ts;
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   559
          val tys = (curry Library.take (i - k) o curry Library.drop k o fst o strip_type) ty;
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   560
          val ctxt = (fold o fold_aterms)
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   561
            (fn Free (v, _) => Name.declare v | _ => I) ts Name.context;
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   562
          val vs = Name.names ctxt "a" tys;
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   563
        in
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   564
          fold_map (exprgen_typ thy algbr funcgr) tys
26972
haftmann
parents: 26939
diff changeset
   565
          ##>> exprgen_case thy algbr funcgr thm n cases ((c, ty), ts @ map Free vs)
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   566
          #>> (fn (tys, t) => map2 (fn (v, _) => pair v) vs tys `|--> t)
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   567
        end
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   568
      else if length ts > i then
26972
haftmann
parents: 26939
diff changeset
   569
        exprgen_case thy algbr funcgr thm n cases ((c, ty), Library.take (i, ts))
haftmann
parents: 26939
diff changeset
   570
        ##>> fold_map (exprgen_term thy algbr funcgr thm) (Library.drop (i, ts))
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   571
        #>> (fn (t, ts) => t `$$ ts)
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   572
      else
26972
haftmann
parents: 26939
diff changeset
   573
        exprgen_case thy algbr funcgr thm n cases ((c, ty), ts)
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   574
      end
26972
haftmann
parents: 26939
diff changeset
   575
  | NONE => exprgen_app_default thy algbr funcgr thm ((c, ty), ts);
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   576
25969
haftmann
parents: 25621
diff changeset
   577
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   578
(** generated programs **)
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   579
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   580
(* store *)
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   581
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   582
structure Program = CodeDataFun
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   583
(
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   584
  type T = program;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   585
  val empty = Graph.empty;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   586
  fun merge _ = Graph.merge (K true);
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   587
  fun purge _ NONE _ = Graph.empty
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   588
    | purge NONE _ _ = Graph.empty
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   589
    | purge (SOME thy) (SOME cs) program =
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   590
        let
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   591
          val cs_exisiting =
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   592
            map_filter (CodeName.const_rev thy) (Graph.keys program);
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   593
          val dels = (Graph.all_preds program
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   594
              o map (CodeName.const thy)
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   595
              o filter (member (op =) cs_exisiting)
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   596
            ) cs;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   597
        in Graph.del_nodes dels program end;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   598
);
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   599
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   600
val cached_program = Program.get;
25969
haftmann
parents: 25621
diff changeset
   601
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   602
fun transact f program =
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   603
  (NONE, program)
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   604
  |> f
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   605
  |-> (fn x => fn (_, program) => (x, program));
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   606
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   607
fun generate thy funcgr f x =
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   608
  Program.change_yield thy (transact (f thy (Code.operational_algebra thy) funcgr x));
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   609
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   610
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   611
(* program generation *)
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   612
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   613
fun consts_program thy cs =
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   614
  let
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   615
    fun project_consts cs program =
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   616
      let
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   617
        val cs_all = Graph.all_succs program cs;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   618
      in (cs, Graph.subgraph (member (op =) cs_all) program) end;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   619
    fun generate_consts thy algebra funcgr =
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   620
      fold_map (ensure_const thy algebra funcgr);
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   621
  in
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   622
    generate thy (CodeFuncgr.make thy cs) generate_consts cs
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   623
    |-> project_consts
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   624
  end;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   625
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   626
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   627
(* value evaluation *)
25969
haftmann
parents: 25621
diff changeset
   628
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   629
fun ensure_value thy algbr funcgr t = 
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   630
  let
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   631
    val ty = fastype_of t;
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   632
    val vs = fold_term_types (K (fold_atyps (insert (eq_fst op =)
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   633
      o dest_TFree))) t [];
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   634
    val stmt_value =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   635
      fold_map (exprgen_tyvar_sort thy algbr funcgr) vs
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   636
      ##>> exprgen_typ thy algbr funcgr ty
26972
haftmann
parents: 26939
diff changeset
   637
      ##>> exprgen_term thy algbr funcgr NONE t
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24837
diff changeset
   638
      #>> (fn ((vs, ty), t) => Fun ((vs, ty), [(([], t), Drule.dummy_thm)]));
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   639
    fun term_value (dep, program1) =
25969
haftmann
parents: 25621
diff changeset
   640
      let
haftmann
parents: 25621
diff changeset
   641
        val Fun ((vs, ty), [(([], t), _)]) =
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   642
          Graph.get_node program1 CodeName.value_name;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   643
        val deps = Graph.imm_succs program1 CodeName.value_name;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   644
        val program2 = Graph.del_nodes [CodeName.value_name] program1;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   645
        val deps_all = Graph.all_succs program2 deps;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   646
        val program3 = Graph.subgraph (member (op =) deps_all) program2;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   647
      in ((program3, (((vs, ty), t), deps)), (dep, program2)) end;
26011
d55224947082 cleaned up evaluation interfaces
haftmann
parents: 25969
diff changeset
   648
  in
d55224947082 cleaned up evaluation interfaces
haftmann
parents: 25969
diff changeset
   649
    ensure_stmt stmt_value CodeName.value_name
d55224947082 cleaned up evaluation interfaces
haftmann
parents: 25969
diff changeset
   650
    #> snd
d55224947082 cleaned up evaluation interfaces
haftmann
parents: 25969
diff changeset
   651
    #> term_value
d55224947082 cleaned up evaluation interfaces
haftmann
parents: 25969
diff changeset
   652
  end;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   653
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   654
fun eval eval_kind thy evaluator =
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   655
  let
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   656
    fun evaluator'' evaluator''' funcgr t =
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   657
      let
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   658
        val ((program, (vs_ty_t, deps)), _) = generate thy funcgr ensure_value t;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   659
      in evaluator''' program vs_ty_t deps end;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   660
    fun evaluator' t =
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   661
      let
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   662
        val (t', evaluator''') = evaluator t;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   663
      in (t', evaluator'' evaluator''') end;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   664
  in eval_kind thy evaluator' end
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   665
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   666
fun eval_conv thy = eval CodeFuncgr.eval_conv thy;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   667
fun eval_term thy = eval CodeFuncgr.eval_term thy;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   668
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   669
end; (*struct*)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   670
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   671
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   672
structure BasicCodeThingol: BASIC_CODE_THINGOL = CodeThingol;