src/HOLCF/Tools/Domain/domain_extender.ML
author wenzelm
Wed, 02 Dec 2009 12:04:07 +0100
changeset 33930 6a973bd43949
parent 33798 46cbbcbd4e68
child 33955 fff6f11b1f09
permissions -rw-r--r--
slightly less ambitious settings, to avoid potential out-of-memory problem;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
32126
a5042f260440 obey captialized directory names convention
haftmann
parents: 31986
diff changeset
     1
(*  Title:      HOLCF/Tools/Domain/domain_extender.ML
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
     2
    Author:     David von Oheimb
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
     3
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
     4
Theory extender for domain command, including theory syntax.
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
     5
*)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
     6
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
     7
signature DOMAIN_EXTENDER =
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
     8
sig
33796
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
     9
  val add_domain_cmd:
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
    10
      string ->
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
    11
      ((string * string option) list * binding * mixfix *
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
    12
       (binding * (bool * binding option * string) list * mixfix) list) list
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
    13
      -> theory -> theory
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
    14
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
    15
  val add_domain:
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
    16
      string ->
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
    17
      ((string * string option) list * binding * mixfix *
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
    18
       (binding * (bool * binding option * typ) list * mixfix) list) list
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
    19
      -> theory -> theory
33798
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
    20
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
    21
  val add_new_domain_cmd:
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
    22
      string ->
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
    23
      ((string * string option) list * binding * mixfix *
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
    24
       (binding * (bool * binding option * string) list * mixfix) list) list
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
    25
      -> theory -> theory
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
    26
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
    27
  val add_new_domain:
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
    28
      string ->
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
    29
      ((string * string option) list * binding * mixfix *
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
    30
       (binding * (bool * binding option * typ) list * mixfix) list) list
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
    31
      -> theory -> theory
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    32
end;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    33
31023
d027411c9a38 use opaque ascription for all HOLCF code
huffman
parents: 30919
diff changeset
    34
structure Domain_Extender :> DOMAIN_EXTENDER =
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    35
struct
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    36
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    37
open Domain_Library;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    38
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    39
(* ----- general testing and preprocessing of constructor list -------------- *)
30919
dcf8a7a66bd1 make domain package ML interface more consistent with datatype package; use binding instead of bstring
huffman
parents: 30916
diff changeset
    40
fun check_and_sort_domain
33798
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
    41
    (definitional : bool)
33796
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
    42
    (dtnvs : (string * typ list) list)
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
    43
    (cons'' : (binding * (bool * binding option * typ) list * mixfix) list list)
33798
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
    44
    (thy : theory)
31288
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    45
    : ((string * typ list) *
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    46
       (binding * (bool * binding option * typ) list * mixfix) list) list =
33796
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
    47
  let
33798
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
    48
    val defaultS = Sign.defaultS thy;
33796
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
    49
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
    50
    val test_dupl_typs =
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
    51
      case duplicates (op =) (map fst dtnvs) of 
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
    52
        [] => false | dups => error ("Duplicate types: " ^ commas_quote dups);
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
    53
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
    54
    val all_cons = map (Binding.name_of o first) (flat cons'');
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
    55
    val test_dupl_cons =
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
    56
      case duplicates (op =) all_cons of 
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
    57
        [] => false | dups => error ("Duplicate constructors: " 
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
    58
                                      ^ commas_quote dups);
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
    59
    val all_sels =
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
    60
      (map Binding.name_of o map_filter second o maps second) (flat cons'');
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
    61
    val test_dupl_sels =
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
    62
      case duplicates (op =) all_sels of
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
    63
        [] => false | dups => error("Duplicate selectors: "^commas_quote dups);
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
    64
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
    65
    fun test_dupl_tvars s =
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
    66
      case duplicates (op =) (map(fst o dest_TFree)s) of
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
    67
        [] => false | dups => error("Duplicate type arguments: " 
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
    68
                                    ^commas_quote dups);
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
    69
    val test_dupl_tvars' = exists test_dupl_tvars (map snd dtnvs);
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
    70
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
    71
    (* test for free type variables, illegal sort constraints on rhs,
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
    72
       non-pcpo-types and invalid use of recursive type;
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
    73
       replace sorts in type variables on rhs *)
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
    74
    fun analyse_equation ((dname,typevars),cons') = 
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
    75
      let
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
    76
        val tvars = map dest_TFree typevars;
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
    77
        val distinct_typevars = map TFree tvars;
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
    78
        fun rm_sorts (TFree(s,_)) = TFree(s,[])
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
    79
          | rm_sorts (Type(s,ts)) = Type(s,remove_sorts ts)
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
    80
          | rm_sorts (TVar(s,_))  = TVar(s,[])
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
    81
        and remove_sorts l = map rm_sorts l;
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
    82
        val indirect_ok = ["*","Cfun.->","Ssum.++","Sprod.**","Up.u"]
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
    83
        fun analyse indirect (TFree(v,s))  =
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
    84
            (case AList.lookup (op =) tvars v of 
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
    85
               NONE => error ("Free type variable " ^ quote v ^ " on rhs.")
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
    86
             | SOME sort => if eq_set (op =) (s, defaultS) orelse
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
    87
                               eq_set (op =) (s, sort)
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
    88
                            then TFree(v,sort)
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
    89
                            else error ("Inconsistent sort constraint" ^
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
    90
                                        " for type variable " ^ quote v))
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
    91
          | analyse indirect (t as Type(s,typl)) =
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
    92
            (case AList.lookup (op =) dtnvs s of
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
    93
               NONE =>
33798
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
    94
                 if definitional orelse s mem indirect_ok
33796
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
    95
                 then Type(s,map (analyse false) typl)
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
    96
                 else Type(s,map (analyse true) typl)
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
    97
             | SOME typevars =>
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
    98
                 if indirect 
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
    99
                 then error ("Indirect recursion of type " ^ 
33798
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   100
                             quote (string_of_typ thy t))
33796
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   101
                 else if dname <> s orelse
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   102
                         (** BUG OR FEATURE?:
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   103
                             mutual recursion may use different arguments **)
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   104
                         remove_sorts typevars = remove_sorts typl 
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   105
                 then Type(s,map (analyse true) typl)
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   106
                 else error ("Direct recursion of type " ^ 
33798
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   107
                             quote (string_of_typ thy t) ^ 
33796
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   108
                             " with different arguments"))
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   109
          | analyse indirect (TVar _) = Imposs "extender:analyse";
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   110
        fun check_pcpo lazy T =
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   111
            let val ok = if lazy then cpo_type else pcpo_type
33798
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   112
            in if ok thy T then T
33796
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   113
               else error ("Constructor argument type is not of sort pcpo: " ^
33798
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   114
                           string_of_typ thy T)
33796
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   115
            end;
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   116
        fun analyse_arg (lazy, sel, T) =
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   117
            (lazy, sel, check_pcpo lazy (analyse false T));
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   118
        fun analyse_con (b, args, mx) = (b, map analyse_arg args, mx);
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   119
      in ((dname,distinct_typevars), map analyse_con cons') end; 
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   120
  in ListPair.map analyse_equation (dtnvs,cons'')
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   121
  end; (* let *)
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   122
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   123
(* ----- calls for building new thy and thms -------------------------------- *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   124
30919
dcf8a7a66bd1 make domain package ML interface more consistent with datatype package; use binding instead of bstring
huffman
parents: 30916
diff changeset
   125
fun gen_add_domain
33796
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   126
    (prep_typ : theory -> 'a -> typ)
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   127
    (comp_dnam : string)
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   128
    (eqs''' : ((string * string option) list * binding * mixfix *
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   129
               (binding * (bool * binding option * 'a) list * mixfix) list) list)
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   130
    (thy''' : theory) =
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   131
  let
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   132
    fun readS (SOME s) = Syntax.read_sort_global thy''' s
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   133
      | readS NONE = Sign.defaultS thy''';
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   134
    fun readTFree (a, s) = TFree (a, readS s);
31161
a27d4254ff4c fix domain package parsing of lhs sort constraints
huffman
parents: 31023
diff changeset
   135
33796
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   136
    val dtnvs = map (fn (vs,dname:binding,mx,_) => 
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   137
                        (dname, map readTFree vs, mx)) eqs''';
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   138
    val cons''' = map (fn (_,_,_,cons) => cons) eqs''';
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   139
    fun thy_type  (dname,tvars,mx) = (dname, length tvars, mx);
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   140
    fun thy_arity (dname,tvars,mx) =
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   141
        (Sign.full_name thy''' dname, map (snd o dest_TFree) tvars, pcpoS);
33798
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   142
    val thy'' =
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   143
      thy'''
33796
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   144
      |> Sign.add_types (map thy_type dtnvs)
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   145
      |> fold (AxClass.axiomatize_arity o thy_arity) dtnvs;
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   146
    val cons'' =
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   147
      map (map (upd_second (map (upd_third (prep_typ thy''))))) cons''';
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   148
    val dtnvs' =
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   149
      map (fn (dname,vs,mx) => (Sign.full_name thy''' dname,vs)) dtnvs;
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   150
    val eqs' : ((string * typ list) *
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   151
        (binding * (bool * binding option * typ) list * mixfix) list) list =
33798
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   152
      check_and_sort_domain false dtnvs' cons'' thy'';
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   153
    val thy' = thy'' |> Domain_Syntax.add_syntax false comp_dnam eqs';
33796
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   154
    val dts  = map (Type o fst) eqs';
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   155
    val new_dts = map (fn ((s,Ts),_) => (s, map (fst o dest_TFree) Ts)) eqs';
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   156
    fun strip ss = Library.drop (find_index (fn s => s = "'") ss + 1, ss);
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   157
    fun typid (Type  (id,_)) =
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   158
        let val c = hd (Symbol.explode (Long_Name.base_name id))
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   159
        in if Symbol.is_letter c then c else "t" end
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   160
      | typid (TFree (id,_)   ) = hd (strip (tl (Symbol.explode id)))
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   161
      | typid (TVar ((id,_),_)) = hd (tl (Symbol.explode id));
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   162
    fun one_con (con,args,mx) =
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   163
        ((Syntax.const_name mx (Binding.name_of con)),
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   164
         ListPair.map (fn ((lazy,sel,tp),vn) =>
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   165
           mk_arg ((lazy, DatatypeAux.dtyp_of_typ new_dts tp),
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   166
                   Option.map Binding.name_of sel,vn))
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   167
                      (args,(mk_var_names(map (typid o third) args)))
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   168
        ) : cons;
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   169
    val eqs : eq list =
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   170
        map (fn (dtnvs,cons') => (dtnvs, map one_con cons')) eqs';
33798
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   171
    val thy = thy' |> Domain_Axioms.add_axioms false comp_dnam eqs;
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   172
    val ((rewss, take_rews), theorems_thy) =
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   173
        thy
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   174
          |> fold_map (fn eq => Domain_Theorems.theorems (eq, eqs)) eqs
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   175
          ||>> Domain_Theorems.comp_theorems (comp_dnam, eqs);
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   176
  in
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   177
    theorems_thy
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   178
      |> Sign.add_path (Long_Name.base_name comp_dnam)
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   179
      |> PureThy.add_thmss
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   180
           [((Binding.name "rews", flat rewss @ take_rews), [])]
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   181
      |> snd
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   182
      |> Sign.parent_path
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   183
  end;
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   184
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   185
fun gen_add_new_domain
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   186
    (prep_typ : theory -> 'a -> typ)
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   187
    (comp_dnam : string)
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   188
    (eqs''' : ((string * string option) list * binding * mixfix *
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   189
               (binding * (bool * binding option * 'a) list * mixfix) list) list)
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   190
    (thy''' : theory) =
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   191
  let
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   192
    fun readS (SOME s) = Syntax.read_sort_global thy''' s
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   193
      | readS NONE = Sign.defaultS thy''';
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   194
    fun readTFree (a, s) = TFree (a, readS s);
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   195
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   196
    val dtnvs = map (fn (vs,dname:binding,mx,_) => 
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   197
                        (dname, map readTFree vs, mx)) eqs''';
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   198
    val cons''' = map (fn (_,_,_,cons) => cons) eqs''';
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   199
    fun thy_type  (dname,tvars,mx) = (dname, length tvars, mx);
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   200
    fun thy_arity (dname,tvars,mx) =
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   201
      (Sign.full_name thy''' dname, map (snd o dest_TFree) tvars, @{sort rep});
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   202
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   203
    (* this theory is used just for parsing and error checking *)
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   204
    val tmp_thy = thy'''
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   205
      |> Theory.copy
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   206
      |> Sign.add_types (map thy_type dtnvs)
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   207
      |> fold (AxClass.axiomatize_arity o thy_arity) dtnvs;
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   208
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   209
    val cons'' : (binding * (bool * binding option * typ) list * mixfix) list list =
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   210
      map (map (upd_second (map (upd_third (prep_typ tmp_thy))))) cons''';
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   211
    val dtnvs' : (string * typ list) list =
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   212
      map (fn (dname,vs,mx) => (Sign.full_name thy''' dname,vs)) dtnvs;
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   213
    val eqs' : ((string * typ list) *
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   214
        (binding * (bool * binding option * typ) list * mixfix) list) list =
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   215
      check_and_sort_domain true dtnvs' cons'' tmp_thy;
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   216
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   217
    fun mk_arg_typ (lazy, dest_opt, T) = if lazy then mk_uT T else T;
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   218
    fun mk_con_typ (bind, args, mx) =
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   219
        if null args then oneT else foldr1 mk_sprodT (map mk_arg_typ args);
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   220
    fun mk_eq_typ (_, cons) = foldr1 mk_ssumT (map mk_con_typ cons);
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   221
    
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   222
    val thy'' = thy''' |>
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   223
      Domain_Isomorphism.domain_isomorphism
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   224
        (map (fn ((vs, dname, mx, _), eq) =>
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   225
                 (map fst vs, dname, mx, mk_eq_typ eq))
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   226
             (eqs''' ~~ eqs'))
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   227
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   228
    val thy' = thy'' |> Domain_Syntax.add_syntax true comp_dnam eqs';
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   229
    val dts  = map (Type o fst) eqs';
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   230
    val new_dts = map (fn ((s,Ts),_) => (s, map (fst o dest_TFree) Ts)) eqs';
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   231
    fun strip ss = Library.drop (find_index (fn s => s = "'") ss + 1, ss);
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   232
    fun typid (Type  (id,_)) =
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   233
        let val c = hd (Symbol.explode (Long_Name.base_name id))
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   234
        in if Symbol.is_letter c then c else "t" end
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   235
      | typid (TFree (id,_)   ) = hd (strip (tl (Symbol.explode id)))
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   236
      | typid (TVar ((id,_),_)) = hd (tl (Symbol.explode id));
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   237
    fun one_con (con,args,mx) =
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   238
        ((Syntax.const_name mx (Binding.name_of con)),
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   239
         ListPair.map (fn ((lazy,sel,tp),vn) =>
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   240
           mk_arg ((lazy, DatatypeAux.dtyp_of_typ new_dts tp),
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   241
                   Option.map Binding.name_of sel,vn))
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   242
                      (args,(mk_var_names(map (typid o third) args)))
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   243
        ) : cons;
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   244
    val eqs : eq list =
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   245
        map (fn (dtnvs,cons') => (dtnvs, map one_con cons')) eqs';
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   246
    val thy = thy' |> Domain_Axioms.add_axioms true comp_dnam eqs;
33796
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   247
    val ((rewss, take_rews), theorems_thy) =
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   248
        thy
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   249
          |> fold_map (fn eq => Domain_Theorems.theorems (eq, eqs)) eqs
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   250
          ||>> Domain_Theorems.comp_theorems (comp_dnam, eqs);
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   251
  in
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   252
    theorems_thy
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   253
      |> Sign.add_path (Long_Name.base_name comp_dnam)
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   254
      |> PureThy.add_thmss
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   255
           [((Binding.name "rews", flat rewss @ take_rews), [])]
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   256
      |> snd
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   257
      |> Sign.parent_path
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   258
  end;
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   259
30919
dcf8a7a66bd1 make domain package ML interface more consistent with datatype package; use binding instead of bstring
huffman
parents: 30916
diff changeset
   260
val add_domain = gen_add_domain Sign.certify_typ;
dcf8a7a66bd1 make domain package ML interface more consistent with datatype package; use binding instead of bstring
huffman
parents: 30916
diff changeset
   261
val add_domain_cmd = gen_add_domain Syntax.read_typ_global;
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   262
33798
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   263
val add_new_domain = gen_add_new_domain Sign.certify_typ;
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   264
val add_new_domain_cmd = gen_add_new_domain Syntax.read_typ_global;
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   265
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   266
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   267
(** outer syntax **)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   268
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   269
local structure P = OuterParse and K = OuterKeyword in
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   270
27353
71c4dd53d4cb moved global keywords from OuterSyntax to OuterKeyword, tuned interfaces;
wenzelm
parents: 24926
diff changeset
   271
val _ = OuterKeyword.keyword "lazy";
24867
e5b55d7be9bb simplified interfaces for outer syntax;
wenzelm
parents: 24712
diff changeset
   272
30919
dcf8a7a66bd1 make domain package ML interface more consistent with datatype package; use binding instead of bstring
huffman
parents: 30916
diff changeset
   273
val dest_decl : (bool * binding option * string) parser =
33796
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   274
  P.$$$ "(" |-- Scan.optional (P.$$$ "lazy" >> K true) false --
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   275
    (P.binding >> SOME) -- (P.$$$ "::" |-- P.typ)  --| P.$$$ ")" >> P.triple1
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   276
    || P.$$$ "(" |-- P.$$$ "lazy" |-- P.typ --| P.$$$ ")"
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   277
    >> (fn t => (true,NONE,t))
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   278
    || P.typ >> (fn t => (false,NONE,t));
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   279
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   280
val cons_decl =
33796
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   281
  P.binding -- Scan.repeat dest_decl -- P.opt_mixfix;
30916
a3d2128cac92 allow infix declarations for type constructors defined with domain package
huffman
parents: 30915
diff changeset
   282
31161
a27d4254ff4c fix domain package parsing of lhs sort constraints
huffman
parents: 31023
diff changeset
   283
val type_var' : (string * string option) parser =
33796
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   284
  (P.type_ident -- Scan.option (P.$$$ "::" |-- P.!!! P.sort));
30916
a3d2128cac92 allow infix declarations for type constructors defined with domain package
huffman
parents: 30915
diff changeset
   285
31161
a27d4254ff4c fix domain package parsing of lhs sort constraints
huffman
parents: 31023
diff changeset
   286
val type_args' : (string * string option) list parser =
33796
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   287
  type_var' >> single
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   288
  || P.$$$ "(" |-- P.!!! (P.list1 type_var' --| P.$$$ ")")
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   289
  || Scan.succeed [];
30916
a3d2128cac92 allow infix declarations for type constructors defined with domain package
huffman
parents: 30915
diff changeset
   290
a3d2128cac92 allow infix declarations for type constructors defined with domain package
huffman
parents: 30915
diff changeset
   291
val domain_decl =
33796
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   292
  (type_args' -- P.binding -- P.opt_infix) --
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   293
    (P.$$$ "=" |-- P.enum1 "|" cons_decl);
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   294
30916
a3d2128cac92 allow infix declarations for type constructors defined with domain package
huffman
parents: 30915
diff changeset
   295
val domains_decl =
33796
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   296
  Scan.option (P.$$$ "(" |-- P.name --| P.$$$ ")") --
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   297
    P.and_list1 domain_decl;
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   298
33796
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   299
fun mk_domain
33798
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   300
    (definitional : bool)
33796
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   301
    (opt_name : string option,
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   302
     doms : ((((string * string option) list * binding) * mixfix) *
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   303
             ((binding * (bool * binding option * string) list) * mixfix) list) list ) =
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   304
  let
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   305
    val names = map (fn (((_, t), _), _) => Binding.name_of t) doms;
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   306
    val specs : ((string * string option) list * binding * mixfix *
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   307
                 (binding * (bool * binding option * string) list * mixfix) list) list =
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   308
        map (fn (((vs, t), mx), cons) =>
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   309
                (vs, t, mx, map (fn ((c, ds), mx) => (c, ds, mx)) cons)) doms;
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   310
    val comp_dnam =
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   311
        case opt_name of NONE => space_implode "_" names | SOME s => s;
33798
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   312
  in
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   313
    if definitional 
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   314
    then add_new_domain_cmd comp_dnam specs
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   315
    else add_domain_cmd comp_dnam specs
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   316
  end;
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   317
24867
e5b55d7be9bb simplified interfaces for outer syntax;
wenzelm
parents: 24712
diff changeset
   318
val _ =
33796
6442aa3773a2 clean up indentation
huffman
parents: 33038
diff changeset
   319
  OuterSyntax.command "domain" "define recursive domains (HOLCF)"
33798
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   320
    K.thy_decl (domains_decl >> (Toplevel.theory o mk_domain false));
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   321
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   322
val _ =
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   323
  OuterSyntax.command "new_domain" "define recursive domains (HOLCF)"
46cbbcbd4e68 clean up indentation; add 'definitional' option flag
huffman
parents: 33796
diff changeset
   324
    K.thy_decl (domains_decl >> (Toplevel.theory o mk_domain true));
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   325
24867
e5b55d7be9bb simplified interfaces for outer syntax;
wenzelm
parents: 24712
diff changeset
   326
end;
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   327
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   328
end;