src/HOLCF/Tools/domain/domain_extender.ML
author haftmann
Fri, 10 Jul 2009 07:59:27 +0200
changeset 31986 a68f88d264f7
parent 31288 67dff9c5b2bd
permissions -rw-r--r--
dropped find_index_eq
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
     1
(*  Title:      HOLCF/Tools/domain/domain_extender.ML
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
31161
a27d4254ff4c fix domain package parsing of lhs sort constraints
huffman
parents: 31023
diff changeset
     9
  val add_domain_cmd: string ->
31288
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    10
                      ((string * string option) list * binding * mixfix *
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    11
                       (binding * (bool * binding option * string) list * mixfix) list) list
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    12
                      -> theory -> theory
31161
a27d4254ff4c fix domain package parsing of lhs sort constraints
huffman
parents: 31023
diff changeset
    13
  val add_domain: string ->
31288
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    14
                  ((string * string option) list * binding * mixfix *
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    15
                   (binding * (bool * binding option * typ) list * mixfix) list) list
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    16
                  -> theory -> theory
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    17
end;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    18
31023
d027411c9a38 use opaque ascription for all HOLCF code
huffman
parents: 30919
diff changeset
    19
structure Domain_Extender :> DOMAIN_EXTENDER =
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    20
struct
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    21
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    22
open Domain_Library;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    23
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    24
(* ----- 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
    25
fun check_and_sort_domain
31288
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    26
      (dtnvs : (string * typ list) list)
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    27
      (cons'' : (binding * (bool * binding option * typ) list * mixfix) list list)
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    28
      (sg : theory)
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    29
    : ((string * typ list) *
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    30
       (binding * (bool * binding option * typ) list * mixfix) list) list =
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    31
    let
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    32
      val defaultS = Sign.defaultS sg;
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    33
      val test_dupl_typs = (case duplicates (op =) (map fst dtnvs) of 
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    34
                              [] => false | dups => error ("Duplicate types: " ^ commas_quote dups));
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    35
      val test_dupl_cons =
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    36
          (case duplicates (op =) (map (Binding.name_of o first) (List.concat cons'')) of 
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    37
             [] => false | dups => error ("Duplicate constructors: " 
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    38
                                          ^ commas_quote dups));
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    39
      val test_dupl_sels =
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    40
          (case duplicates (op =) (map Binding.name_of (List.mapPartial second
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    41
                                                                        (List.concat (map second (List.concat cons''))))) of
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    42
             [] => false | dups => error("Duplicate selectors: "^commas_quote dups));
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    43
      val test_dupl_tvars =
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    44
          exists(fn s=>case duplicates (op =) (map(fst o dest_TFree)s)of
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    45
                         [] => false | dups => error("Duplicate type arguments: " 
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    46
                                                     ^commas_quote dups)) (map snd dtnvs);
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    47
      (* test for free type variables, illegal sort constraints on rhs,
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    48
         non-pcpo-types and invalid use of recursive type;
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    49
         replace sorts in type variables on rhs *)
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    50
      fun analyse_equation ((dname,typevars),cons') = 
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    51
          let
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    52
            val tvars = map dest_TFree typevars;
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    53
            val distinct_typevars = map TFree tvars;
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    54
            fun rm_sorts (TFree(s,_)) = TFree(s,[])
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    55
              | rm_sorts (Type(s,ts)) = Type(s,remove_sorts ts)
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    56
              | rm_sorts (TVar(s,_))  = TVar(s,[])
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    57
            and remove_sorts l = map rm_sorts l;
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    58
            val indirect_ok = ["*","Cfun.->","Ssum.++","Sprod.**","Up.u"]
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    59
            fun analyse indirect (TFree(v,s))  =
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    60
                (case AList.lookup (op =) tvars v of 
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    61
                   NONE => error ("Free type variable " ^ quote v ^ " on rhs.")
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    62
                 | SOME sort => if eq_set_string (s,defaultS) orelse
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    63
                                   eq_set_string (s,sort    )
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    64
                                then TFree(v,sort)
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    65
                                else error ("Inconsistent sort constraint" ^
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    66
                                            " for type variable " ^ quote v))
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    67
              | analyse indirect (t as Type(s,typl)) =
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    68
                (case AList.lookup (op =) dtnvs s of
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    69
                   NONE          => if s mem indirect_ok
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    70
                                    then Type(s,map (analyse false) typl)
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    71
                                    else Type(s,map (analyse true) typl)
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    72
                 | SOME typevars => if indirect 
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    73
                                    then error ("Indirect recursion of type " ^ 
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    74
                                                quote (string_of_typ sg t))
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    75
                                    else if dname <> s orelse
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    76
                                            (** BUG OR FEATURE?:
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    77
                                                mutual recursion may use different arguments **)
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    78
                                            remove_sorts typevars = remove_sorts typl 
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    79
                                    then Type(s,map (analyse true) typl)
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    80
                                    else error ("Direct recursion of type " ^ 
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    81
                                                quote (string_of_typ sg t) ^ 
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    82
                                                " with different arguments"))
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    83
              | analyse indirect (TVar _) = Imposs "extender:analyse";
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    84
            fun check_pcpo lazy T =
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    85
                let val ok = if lazy then cpo_type else pcpo_type
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    86
                in if ok sg T then T else error
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    87
                                            ("Constructor argument type is not of sort pcpo: " ^
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    88
                                             string_of_typ sg T)
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    89
                end;
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    90
            fun analyse_arg (lazy, sel, T) =
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    91
                (lazy, sel, check_pcpo lazy (analyse false T));
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    92
            fun analyse_con (b, args, mx) = (b, map analyse_arg args, mx);
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    93
          in ((dname,distinct_typevars), map analyse_con cons') end; 
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    94
    in ListPair.map analyse_equation (dtnvs,cons'')
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
    95
    end; (* let *)
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    96
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    97
(* ----- calls for building new thy and thms -------------------------------- *)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    98
30919
dcf8a7a66bd1 make domain package ML interface more consistent with datatype package; use binding instead of bstring
huffman
parents: 30916
diff changeset
    99
fun gen_add_domain
31288
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   100
      (prep_typ : theory -> 'a -> typ)
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   101
      (comp_dnam : string)
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   102
      (eqs''' : ((string * string option) list * binding * mixfix *
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   103
                 (binding * (bool * binding option * 'a) list * mixfix) list) list)
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   104
      (thy''' : theory) =
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   105
    let
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   106
      fun readS (SOME s) = Syntax.read_sort_global thy''' s
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   107
        | readS NONE = Sign.defaultS thy''';
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   108
      fun readTFree (a, s) = TFree (a, readS s);
31161
a27d4254ff4c fix domain package parsing of lhs sort constraints
huffman
parents: 31023
diff changeset
   109
31288
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   110
      val dtnvs = map (fn (vs,dname:binding,mx,_) => 
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   111
                          (dname, map readTFree vs, mx)) eqs''';
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   112
      val cons''' = map (fn (_,_,_,cons) => cons) eqs''';
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   113
      fun thy_type  (dname,tvars,mx) = (dname, length tvars, mx);
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   114
      fun thy_arity (dname,tvars,mx) = (Sign.full_name thy''' dname, map (snd o dest_TFree) tvars, pcpoS);
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   115
      val thy'' = thy''' |> Sign.add_types (map thy_type dtnvs)
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   116
                         |> fold (AxClass.axiomatize_arity o thy_arity) dtnvs;
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   117
      val cons'' = map (map (upd_second (map (upd_third (prep_typ thy''))))) cons''';
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   118
      val dtnvs' = map (fn (dname,vs,mx) => (Sign.full_name thy''' dname,vs)) dtnvs;
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   119
      val eqs' : ((string * typ list) * (binding * (bool * binding option * typ) list * mixfix) list) list =
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   120
          check_and_sort_domain dtnvs' cons'' thy'';
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   121
      val thy' = thy'' |> Domain_Syntax.add_syntax comp_dnam eqs';
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   122
      val dts  = map (Type o fst) eqs';
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   123
      val new_dts = map (fn ((s,Ts),_) => (s, map (fst o dest_TFree) Ts)) eqs';
31986
a68f88d264f7 dropped find_index_eq
haftmann
parents: 31288
diff changeset
   124
      fun strip ss = Library.drop (find_index (fn s => s = "'") ss + 1, ss);
31288
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   125
      fun typid (Type  (id,_)) =
30364
577edc39b501 moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents: 30345
diff changeset
   126
          let val c = hd (Symbol.explode (Long_Name.base_name id))
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   127
          in if Symbol.is_letter c then c else "t" end
31288
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   128
        | typid (TFree (id,_)   ) = hd (strip (tl (Symbol.explode id)))
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   129
        | typid (TVar ((id,_),_)) = hd (tl (Symbol.explode id));
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   130
      fun one_con (con,args,mx) =
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   131
          ((Syntax.const_name mx (Binding.name_of con)),
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   132
           ListPair.map (fn ((lazy,sel,tp),vn) => mk_arg ((lazy,
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   133
                                                           DatatypeAux.dtyp_of_typ new_dts tp),
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   134
                                                          Option.map Binding.name_of sel,vn))
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   135
                        (args,(mk_var_names(map (typid o third) args)))
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   136
          ) : cons;
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   137
      val eqs = map (fn (dtnvs,cons') => (dtnvs, map one_con cons')) eqs' : eq list;
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   138
      val thy = thy' |> Domain_Axioms.add_axioms comp_dnam eqs;
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   139
      val ((rewss, take_rews), theorems_thy) =
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   140
          thy |> fold_map (fn eq => Domain_Theorems.theorems (eq, eqs)) eqs
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   141
              ||>> Domain_Theorems.comp_theorems (comp_dnam, eqs);
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   142
    in
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   143
      theorems_thy
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   144
        |> Sign.add_path (Long_Name.base_name comp_dnam)
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   145
        |> (snd o (PureThy.add_thmss [((Binding.name "rews", List.concat rewss @ take_rews), [])]))
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   146
        |> Sign.parent_path
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   147
    end;
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   148
30919
dcf8a7a66bd1 make domain package ML interface more consistent with datatype package; use binding instead of bstring
huffman
parents: 30916
diff changeset
   149
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
   150
val add_domain_cmd = gen_add_domain Syntax.read_typ_global;
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   151
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   153
(** outer syntax **)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   154
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   155
local structure P = OuterParse and K = OuterKeyword in
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   156
27353
71c4dd53d4cb moved global keywords from OuterSyntax to OuterKeyword, tuned interfaces;
wenzelm
parents: 24926
diff changeset
   157
val _ = OuterKeyword.keyword "lazy";
24867
e5b55d7be9bb simplified interfaces for outer syntax;
wenzelm
parents: 24712
diff changeset
   158
30919
dcf8a7a66bd1 make domain package ML interface more consistent with datatype package; use binding instead of bstring
huffman
parents: 30916
diff changeset
   159
val dest_decl : (bool * binding option * string) parser =
31288
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   160
    P.$$$ "(" |-- Scan.optional (P.$$$ "lazy" >> K true) false --
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   161
      (P.binding >> SOME) -- (P.$$$ "::" |-- P.typ)  --| P.$$$ ")" >> P.triple1
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   162
      || P.$$$ "(" |-- P.$$$ "lazy" |-- P.typ --| P.$$$ ")"
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   163
      >> (fn t => (true,NONE,t))
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   164
      || P.typ >> (fn t => (false,NONE,t));
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   165
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   166
val cons_decl =
31288
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   167
    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
   168
31161
a27d4254ff4c fix domain package parsing of lhs sort constraints
huffman
parents: 31023
diff changeset
   169
val type_var' : (string * string option) parser =
31288
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   170
    (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
   171
31161
a27d4254ff4c fix domain package parsing of lhs sort constraints
huffman
parents: 31023
diff changeset
   172
val type_args' : (string * string option) list parser =
31288
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   173
    type_var' >> single ||
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   174
              P.$$$ "(" |-- P.!!! (P.list1 type_var' --| P.$$$ ")") ||
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   175
              Scan.succeed [];
30916
a3d2128cac92 allow infix declarations for type constructors defined with domain package
huffman
parents: 30915
diff changeset
   176
a3d2128cac92 allow infix declarations for type constructors defined with domain package
huffman
parents: 30915
diff changeset
   177
val domain_decl =
31288
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   178
    (type_args' -- P.binding -- P.opt_infix) --
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   179
                                             (P.$$$ "=" |-- P.enum1 "|" cons_decl);
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   180
30916
a3d2128cac92 allow infix declarations for type constructors defined with domain package
huffman
parents: 30915
diff changeset
   181
val domains_decl =
31288
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   182
    Scan.option (P.$$$ "(" |-- P.name --| P.$$$ ")") --
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   183
                P.and_list1 domain_decl;
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   184
31161
a27d4254ff4c fix domain package parsing of lhs sort constraints
huffman
parents: 31023
diff changeset
   185
fun mk_domain (opt_name : string option,
31288
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   186
               doms : ((((string * string option) list * binding) * mixfix) *
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   187
                       ((binding * (bool * binding option * string) list) * mixfix) list) list ) =
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   188
    let
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   189
      val names = map (fn (((_, t), _), _) => Binding.name_of t) doms;
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   190
      val specs : ((string * string option) list * binding * mixfix *
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   191
                   (binding * (bool * binding option * string) list * mixfix) list) list =
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   192
          map (fn (((vs, t), mx), cons) =>
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   193
                  (vs, t, mx, map (fn ((c, ds), mx) => (c, ds, mx)) cons)) doms;
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   194
      val comp_dnam =
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   195
          case opt_name of NONE => space_implode "_" names | SOME s => s;
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   196
    in add_domain_cmd comp_dnam specs end;
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   197
24867
e5b55d7be9bb simplified interfaces for outer syntax;
wenzelm
parents: 24712
diff changeset
   198
val _ =
31288
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   199
    OuterSyntax.command "domain" "define recursive domains (HOLCF)" K.thy_decl
67dff9c5b2bd remove hard tabs, fix indentation
huffman
parents: 31229
diff changeset
   200
                        (domains_decl >> (Toplevel.theory o mk_domain));
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   201
24867
e5b55d7be9bb simplified interfaces for outer syntax;
wenzelm
parents: 24712
diff changeset
   202
end;
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   203
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   204
end;