src/HOLCF/Tools/Domain/domain_extender.ML
author haftmann
Wed May 05 18:25:34 2010 +0200 (2010-05-05)
changeset 36692 54b64d4ad524
parent 36119 ff605b8fdfdb
child 36960 01594f816e3a
permissions -rw-r--r--
farewell to old-style mem infixes -- type inference in situations with mem_int and mem_string should provide enough information to resolve the type of (op =)
haftmann@32126
     1
(*  Title:      HOLCF/Tools/Domain/domain_extender.ML
wenzelm@23152
     2
    Author:     David von Oheimb
huffman@35794
     3
    Author:     Brian Huffman
wenzelm@23152
     4
wenzelm@23152
     5
Theory extender for domain command, including theory syntax.
wenzelm@23152
     6
*)
wenzelm@23152
     7
wenzelm@23152
     8
signature DOMAIN_EXTENDER =
wenzelm@23152
     9
sig
huffman@33796
    10
  val add_domain_cmd:
huffman@35774
    11
      binding ->
huffman@33796
    12
      ((string * string option) list * binding * mixfix *
huffman@33796
    13
       (binding * (bool * binding option * string) list * mixfix) list) list
huffman@33796
    14
      -> theory -> theory
huffman@33796
    15
huffman@33796
    16
  val add_domain:
huffman@35774
    17
      binding ->
huffman@33796
    18
      ((string * string option) list * binding * mixfix *
huffman@33796
    19
       (binding * (bool * binding option * typ) list * mixfix) list) list
huffman@33796
    20
      -> theory -> theory
huffman@33798
    21
huffman@33798
    22
  val add_new_domain_cmd:
huffman@35774
    23
      binding ->
huffman@33798
    24
      ((string * string option) list * binding * mixfix *
huffman@33798
    25
       (binding * (bool * binding option * string) list * mixfix) list) list
huffman@33798
    26
      -> theory -> theory
huffman@33798
    27
huffman@33798
    28
  val add_new_domain:
huffman@35774
    29
      binding ->
huffman@33798
    30
      ((string * string option) list * binding * mixfix *
huffman@33798
    31
       (binding * (bool * binding option * typ) list * mixfix) list) list
huffman@33798
    32
      -> theory -> theory
wenzelm@23152
    33
end;
wenzelm@23152
    34
huffman@31023
    35
structure Domain_Extender :> DOMAIN_EXTENDER =
wenzelm@23152
    36
struct
wenzelm@23152
    37
wenzelm@23152
    38
open Domain_Library;
wenzelm@23152
    39
wenzelm@23152
    40
(* ----- general testing and preprocessing of constructor list -------------- *)
huffman@30919
    41
fun check_and_sort_domain
huffman@33796
    42
    (dtnvs : (string * typ list) list)
huffman@33796
    43
    (cons'' : (binding * (bool * binding option * typ) list * mixfix) list list)
huffman@33798
    44
    (thy : theory)
huffman@31288
    45
    : ((string * typ list) *
huffman@31288
    46
       (binding * (bool * binding option * typ) list * mixfix) list) list =
huffman@33796
    47
  let
huffman@33798
    48
    val defaultS = Sign.defaultS thy;
huffman@33796
    49
huffman@33796
    50
    val test_dupl_typs =
huffman@33796
    51
      case duplicates (op =) (map fst dtnvs) of 
huffman@33796
    52
        [] => false | dups => error ("Duplicate types: " ^ commas_quote dups);
huffman@33796
    53
huffman@33796
    54
    val all_cons = map (Binding.name_of o first) (flat cons'');
huffman@33796
    55
    val test_dupl_cons =
huffman@33796
    56
      case duplicates (op =) all_cons of 
huffman@33796
    57
        [] => false | dups => error ("Duplicate constructors: " 
huffman@33796
    58
                                      ^ commas_quote dups);
huffman@33796
    59
    val all_sels =
huffman@33796
    60
      (map Binding.name_of o map_filter second o maps second) (flat cons'');
huffman@33796
    61
    val test_dupl_sels =
huffman@33796
    62
      case duplicates (op =) all_sels of
huffman@33796
    63
        [] => false | dups => error("Duplicate selectors: "^commas_quote dups);
huffman@33796
    64
huffman@33796
    65
    fun test_dupl_tvars s =
huffman@33796
    66
      case duplicates (op =) (map(fst o dest_TFree)s) of
huffman@33796
    67
        [] => false | dups => error("Duplicate type arguments: " 
huffman@33796
    68
                                    ^commas_quote dups);
huffman@33796
    69
    val test_dupl_tvars' = exists test_dupl_tvars (map snd dtnvs);
huffman@33796
    70
huffman@33796
    71
    (* test for free type variables, illegal sort constraints on rhs,
huffman@33796
    72
       non-pcpo-types and invalid use of recursive type;
huffman@33796
    73
       replace sorts in type variables on rhs *)
huffman@33796
    74
    fun analyse_equation ((dname,typevars),cons') = 
huffman@33796
    75
      let
huffman@33796
    76
        val tvars = map dest_TFree typevars;
huffman@33796
    77
        val distinct_typevars = map TFree tvars;
huffman@33796
    78
        fun rm_sorts (TFree(s,_)) = TFree(s,[])
huffman@33796
    79
          | rm_sorts (Type(s,ts)) = Type(s,remove_sorts ts)
huffman@33796
    80
          | rm_sorts (TVar(s,_))  = TVar(s,[])
huffman@33796
    81
        and remove_sorts l = map rm_sorts l;
huffman@33796
    82
        fun analyse indirect (TFree(v,s))  =
huffman@33796
    83
            (case AList.lookup (op =) tvars v of 
huffman@33796
    84
               NONE => error ("Free type variable " ^ quote v ^ " on rhs.")
huffman@33796
    85
             | SOME sort => if eq_set (op =) (s, defaultS) orelse
huffman@33796
    86
                               eq_set (op =) (s, sort)
huffman@33796
    87
                            then TFree(v,sort)
huffman@33796
    88
                            else error ("Inconsistent sort constraint" ^
huffman@33796
    89
                                        " for type variable " ^ quote v))
huffman@33796
    90
          | analyse indirect (t as Type(s,typl)) =
huffman@33796
    91
            (case AList.lookup (op =) dtnvs s of
huffman@36119
    92
               NONE => Type (s, map (analyse false) typl)
huffman@33796
    93
             | SOME typevars =>
huffman@33796
    94
                 if indirect 
huffman@33796
    95
                 then error ("Indirect recursion of type " ^ 
huffman@33798
    96
                             quote (string_of_typ thy t))
huffman@33796
    97
                 else if dname <> s orelse
huffman@33796
    98
                         (** BUG OR FEATURE?:
huffman@33796
    99
                             mutual recursion may use different arguments **)
huffman@33796
   100
                         remove_sorts typevars = remove_sorts typl 
huffman@33796
   101
                 then Type(s,map (analyse true) typl)
huffman@33796
   102
                 else error ("Direct recursion of type " ^ 
huffman@33798
   103
                             quote (string_of_typ thy t) ^ 
huffman@33796
   104
                             " with different arguments"))
huffman@33796
   105
          | analyse indirect (TVar _) = Imposs "extender:analyse";
huffman@33796
   106
        fun check_pcpo lazy T =
huffman@33796
   107
            let val ok = if lazy then cpo_type else pcpo_type
huffman@33798
   108
            in if ok thy T then T
huffman@33796
   109
               else error ("Constructor argument type is not of sort pcpo: " ^
huffman@33798
   110
                           string_of_typ thy T)
huffman@33796
   111
            end;
huffman@33796
   112
        fun analyse_arg (lazy, sel, T) =
huffman@33796
   113
            (lazy, sel, check_pcpo lazy (analyse false T));
huffman@33796
   114
        fun analyse_con (b, args, mx) = (b, map analyse_arg args, mx);
huffman@33796
   115
      in ((dname,distinct_typevars), map analyse_con cons') end; 
huffman@33796
   116
  in ListPair.map analyse_equation (dtnvs,cons'')
huffman@33796
   117
  end; (* let *)
wenzelm@23152
   118
wenzelm@23152
   119
(* ----- calls for building new thy and thms -------------------------------- *)
wenzelm@23152
   120
huffman@36118
   121
type info =
huffman@36118
   122
     Domain_Take_Proofs.iso_info list * Domain_Take_Proofs.take_induct_info;
huffman@36118
   123
huffman@30919
   124
fun gen_add_domain
huffman@33796
   125
    (prep_typ : theory -> 'a -> typ)
huffman@36118
   126
    (add_isos : (binding * mixfix * (typ * typ)) list -> theory -> info * theory)
huffman@35774
   127
    (comp_dbind : binding)
huffman@33798
   128
    (eqs''' : ((string * string option) list * binding * mixfix *
huffman@33798
   129
               (binding * (bool * binding option * 'a) list * mixfix) list) list)
huffman@35516
   130
    (thy : theory) =
huffman@33798
   131
  let
huffman@35520
   132
    val dtnvs : (binding * typ list * mixfix) list =
huffman@35520
   133
      let
huffman@35520
   134
        fun readS (SOME s) = Syntax.read_sort_global thy s
huffman@35520
   135
          | readS NONE = Sign.defaultS thy;
huffman@35520
   136
        fun readTFree (a, s) = TFree (a, readS s);
huffman@35520
   137
      in
huffman@35520
   138
        map (fn (vs,dname:binding,mx,_) =>
huffman@35520
   139
                (dname, map readTFree vs, mx)) eqs'''
huffman@35520
   140
      end;
huffman@33798
   141
huffman@33798
   142
    fun thy_type  (dname,tvars,mx) = (dname, length tvars, mx);
huffman@33798
   143
    fun thy_arity (dname,tvars,mx) =
huffman@35516
   144
      (Sign.full_name thy dname, map (snd o dest_TFree) tvars, @{sort rep});
huffman@33798
   145
huffman@33798
   146
    (* this theory is used just for parsing and error checking *)
huffman@35516
   147
    val tmp_thy = thy
huffman@33798
   148
      |> Theory.copy
huffman@33798
   149
      |> Sign.add_types (map thy_type dtnvs)
huffman@33798
   150
      |> fold (AxClass.axiomatize_arity o thy_arity) dtnvs;
huffman@33798
   151
huffman@35776
   152
    val dbinds : binding list =
huffman@35776
   153
        map (fn (_,dbind,_,_) => dbind) eqs''';
huffman@35520
   154
    val cons''' :
huffman@35520
   155
        (binding * (bool * binding option * 'a) list * mixfix) list list =
huffman@35520
   156
        map (fn (_,_,_,cons) => cons) eqs''';
huffman@35520
   157
    val cons'' :
huffman@35520
   158
        (binding * (bool * binding option * typ) list * mixfix) list list =
huffman@35520
   159
        map (map (upd_second (map (upd_third (prep_typ tmp_thy))))) cons''';
huffman@33798
   160
    val dtnvs' : (string * typ list) list =
huffman@35520
   161
        map (fn (dname,vs,mx) => (Sign.full_name thy dname,vs)) dtnvs;
huffman@33798
   162
    val eqs' : ((string * typ list) *
huffman@33798
   163
        (binding * (bool * binding option * typ) list * mixfix) list) list =
huffman@36119
   164
        check_and_sort_domain dtnvs' cons'' tmp_thy;
huffman@36118
   165
    val dts : typ list = map (Type o fst) eqs';
huffman@33798
   166
huffman@33798
   167
    fun mk_arg_typ (lazy, dest_opt, T) = if lazy then mk_uT T else T;
huffman@33798
   168
    fun mk_con_typ (bind, args, mx) =
huffman@33798
   169
        if null args then oneT else foldr1 mk_sprodT (map mk_arg_typ args);
huffman@33798
   170
    fun mk_eq_typ (_, cons) = foldr1 mk_ssumT (map mk_con_typ cons);
huffman@36118
   171
    val repTs : typ list = map mk_eq_typ eqs';
huffman@33798
   172
huffman@36118
   173
    val iso_spec : (binding * mixfix * (typ * typ)) list =
huffman@36118
   174
        map (fn ((dbind, _, mx), eq) => (dbind, mx, eq))
huffman@36118
   175
          (dtnvs ~~ (dts ~~ repTs));
huffman@36118
   176
huffman@36118
   177
    val ((iso_infos, take_info), thy) = add_isos iso_spec thy;
huffman@36118
   178
huffman@35520
   179
    val new_dts : (string * string list) list =
huffman@35520
   180
        map (fn ((s,Ts),_) => (s, map (fst o dest_TFree) Ts)) eqs';
huffman@35520
   181
    fun one_con (con,args,mx) : cons =
huffman@36118
   182
        (Binding.name_of con,  (* FIXME preverse binding (!?) *)
huffman@33798
   183
         ListPair.map (fn ((lazy,sel,tp),vn) =>
huffman@35519
   184
           mk_arg ((lazy, Datatype_Aux.dtyp_of_typ new_dts tp), vn))
huffman@36118
   185
                      (args, Datatype_Prop.make_tnames (map third args)));
huffman@33798
   186
    val eqs : eq list =
huffman@33798
   187
        map (fn (dtnvs,cons') => (dtnvs, map one_con cons')) eqs';
huffman@36118
   188
huffman@33796
   189
    val ((rewss, take_rews), theorems_thy) =
huffman@33796
   190
        thy
huffman@36118
   191
          |> fold_map (fn (((dbind, eq), (_,cs)), info) =>
huffman@36118
   192
                Domain_Theorems.theorems (eq, eqs) dbind cs info take_info)
huffman@35776
   193
             (dbinds ~~ eqs ~~ eqs' ~~ iso_infos)
huffman@35774
   194
          ||>> Domain_Theorems.comp_theorems (comp_dbind, eqs) take_info;
huffman@33796
   195
  in
huffman@33796
   196
    theorems_thy
huffman@33796
   197
      |> PureThy.add_thmss
huffman@35774
   198
           [((Binding.qualified true "rews" comp_dbind,
huffman@35774
   199
              flat rewss @ take_rews), [])]
huffman@33796
   200
      |> snd
huffman@33796
   201
  end;
wenzelm@23152
   202
huffman@36118
   203
fun define_isos (spec : (binding * mixfix * (typ * typ)) list) =
huffman@36118
   204
  let
huffman@36118
   205
    fun prep (dbind, mx, (lhsT, rhsT)) =
huffman@36118
   206
      let val (dname, vs) = dest_Type lhsT;
huffman@36118
   207
      in (map (fst o dest_TFree) vs, dbind, mx, rhsT, NONE) end;
huffman@36118
   208
  in
huffman@36118
   209
    Domain_Isomorphism.domain_isomorphism (map prep spec)
huffman@36118
   210
  end;
wenzelm@23152
   211
huffman@36118
   212
val add_domain =
huffman@36118
   213
    gen_add_domain Sign.certify_typ Domain_Axioms.add_axioms;
huffman@36118
   214
huffman@36118
   215
val add_new_domain =
huffman@36118
   216
    gen_add_domain Sign.certify_typ define_isos;
huffman@36118
   217
huffman@36118
   218
val add_domain_cmd =
huffman@36118
   219
    gen_add_domain Syntax.read_typ_global Domain_Axioms.add_axioms;
huffman@36118
   220
huffman@36118
   221
val add_new_domain_cmd =
huffman@36118
   222
    gen_add_domain Syntax.read_typ_global define_isos;
huffman@33798
   223
wenzelm@23152
   224
wenzelm@23152
   225
(** outer syntax **)
wenzelm@23152
   226
wenzelm@23152
   227
local structure P = OuterParse and K = OuterKeyword in
wenzelm@23152
   228
wenzelm@27353
   229
val _ = OuterKeyword.keyword "lazy";
wenzelm@24867
   230
huffman@30919
   231
val dest_decl : (bool * binding option * string) parser =
huffman@33796
   232
  P.$$$ "(" |-- Scan.optional (P.$$$ "lazy" >> K true) false --
huffman@33796
   233
    (P.binding >> SOME) -- (P.$$$ "::" |-- P.typ)  --| P.$$$ ")" >> P.triple1
huffman@33796
   234
    || P.$$$ "(" |-- P.$$$ "lazy" |-- P.typ --| P.$$$ ")"
huffman@33796
   235
    >> (fn t => (true,NONE,t))
huffman@33796
   236
    || P.typ >> (fn t => (false,NONE,t));
wenzelm@23152
   237
wenzelm@23152
   238
val cons_decl =
huffman@33796
   239
  P.binding -- Scan.repeat dest_decl -- P.opt_mixfix;
huffman@30916
   240
huffman@30916
   241
val domain_decl =
wenzelm@35839
   242
  (P.type_args_constrained -- P.binding -- P.opt_mixfix) --
huffman@33796
   243
    (P.$$$ "=" |-- P.enum1 "|" cons_decl);
wenzelm@23152
   244
huffman@30916
   245
val domains_decl =
huffman@35774
   246
  Scan.option (P.$$$ "(" |-- P.binding --| P.$$$ ")") --
huffman@33796
   247
    P.and_list1 domain_decl;
wenzelm@23152
   248
huffman@33796
   249
fun mk_domain
huffman@33798
   250
    (definitional : bool)
huffman@35774
   251
    (opt_name : binding option,
huffman@33796
   252
     doms : ((((string * string option) list * binding) * mixfix) *
huffman@33796
   253
             ((binding * (bool * binding option * string) list) * mixfix) list) list ) =
huffman@33796
   254
  let
huffman@33796
   255
    val names = map (fn (((_, t), _), _) => Binding.name_of t) doms;
huffman@33796
   256
    val specs : ((string * string option) list * binding * mixfix *
huffman@33796
   257
                 (binding * (bool * binding option * string) list * mixfix) list) list =
huffman@33796
   258
        map (fn (((vs, t), mx), cons) =>
huffman@33796
   259
                (vs, t, mx, map (fn ((c, ds), mx) => (c, ds, mx)) cons)) doms;
huffman@35774
   260
    val comp_dbind =
huffman@35774
   261
        case opt_name of NONE => Binding.name (space_implode "_" names)
huffman@35774
   262
                       | SOME s => s;
huffman@33798
   263
  in
huffman@33798
   264
    if definitional 
huffman@35774
   265
    then add_new_domain_cmd comp_dbind specs
huffman@35774
   266
    else add_domain_cmd comp_dbind specs
huffman@33798
   267
  end;
wenzelm@23152
   268
wenzelm@24867
   269
val _ =
huffman@33796
   270
  OuterSyntax.command "domain" "define recursive domains (HOLCF)"
huffman@33798
   271
    K.thy_decl (domains_decl >> (Toplevel.theory o mk_domain false));
huffman@33798
   272
huffman@33798
   273
val _ =
huffman@33798
   274
  OuterSyntax.command "new_domain" "define recursive domains (HOLCF)"
huffman@33798
   275
    K.thy_decl (domains_decl >> (Toplevel.theory o mk_domain true));
wenzelm@23152
   276
wenzelm@24867
   277
end;
wenzelm@23152
   278
wenzelm@23152
   279
end;