src/HOLCF/cont_consts.ML
author wenzelm
Thu Jul 14 19:28:24 2005 +0200 (2005-07-14)
changeset 16842 5979c46853d1
parent 16621 78b32293a8b1
child 17057 0934ac31985f
permissions -rw-r--r--
tuned;
wenzelm@4129
     1
(*  Title:      HOLCF/cont_consts.ML
wenzelm@4129
     2
    ID:         $Id$
wenzelm@12625
     3
    Author:     Tobias Mayr, David von Oheimb, and Markus Wenzel
wenzelm@4129
     4
wenzelm@14682
     5
HOLCF version of consts: handle continuous function types in mixfix
wenzelm@14682
     6
syntax.
wenzelm@4129
     7
*)
wenzelm@4129
     8
wenzelm@12625
     9
signature CONT_CONSTS =
wenzelm@12625
    10
sig
wenzelm@12625
    11
  val add_consts: (bstring * string * mixfix) list -> theory -> theory
wenzelm@12625
    12
  val add_consts_i: (bstring * typ * mixfix) list -> theory -> theory
wenzelm@12625
    13
end;
wenzelm@12625
    14
wenzelm@12625
    15
structure ContConsts: CONT_CONSTS =
wenzelm@4129
    16
struct
wenzelm@4129
    17
wenzelm@12625
    18
wenzelm@12625
    19
(* misc utils *)
wenzelm@4129
    20
wenzelm@4129
    21
open HOLCFLogic;
wenzelm@4129
    22
wenzelm@12625
    23
fun first  (x,_,_) = x;
wenzelm@12625
    24
fun second (_,x,_) = x;
wenzelm@12625
    25
fun third  (_,_,x) = x;
wenzelm@4129
    26
fun upd_first  f (x,y,z) = (f x,   y,   z);
wenzelm@4129
    27
fun upd_second f (x,y,z) = (  x, f y,   z);
wenzelm@4129
    28
fun upd_third  f (x,y,z) = (  x,   y, f z);
wenzelm@4129
    29
wenzelm@4129
    30
fun change_arrow 0 T               = T
wenzelm@4129
    31
|   change_arrow n (Type(_,[S,T])) = Type ("fun",[S,change_arrow (n-1) T])
wenzelm@12625
    32
|   change_arrow _ _               = sys_error "cont_consts: change_arrow";
wenzelm@4129
    33
wenzelm@4129
    34
fun trans_rules name2 name1 n mx = let
wenzelm@4129
    35
  fun argnames _ 0 = []
wenzelm@4129
    36
  |   argnames c n = chr c::argnames (c+1) (n-1);
wenzelm@4129
    37
  val vnames = argnames (ord "A") n;
wenzelm@4129
    38
  val extra_parse_rule = Syntax.ParseRule (Constant name2, Constant name1);
wenzelm@5700
    39
  in [Syntax.ParsePrintRule (Syntax.mk_appl (Constant name2) (map Variable vnames),
skalberg@15570
    40
                          Library.foldl (fn (t,arg) => (Syntax.mk_appl (Constant "Rep_CFun")
wenzelm@12625
    41
                                                [t,Variable arg]))
wenzelm@12625
    42
                          (Constant name1,vnames))]
wenzelm@11651
    43
     @(case mx of InfixName _ => [extra_parse_rule]
wenzelm@11651
    44
                | InfixlName _ => [extra_parse_rule]
wenzelm@12625
    45
                | InfixrName _ => [extra_parse_rule] | _ => []) end;
wenzelm@4129
    46
wenzelm@4129
    47
wenzelm@4129
    48
(* transforming infix/mixfix declarations of constants with type ...->...
wenzelm@12625
    49
   a declaration of such a constant is transformed to a normal declaration with
wenzelm@4129
    50
   an internal name, the same type, and nofix. Additionally, a purely syntactic
wenzelm@4129
    51
   declaration with the original name, type ...=>..., and the original mixfix
wenzelm@4129
    52
   is generated and connected to the other declaration via some translation.
wenzelm@4129
    53
*)
wenzelm@12625
    54
fun fix_mixfix (syn                     , T, mx as Infix           p ) =
wenzelm@12625
    55
               (Syntax.const_name syn mx, T,       InfixName (syn, p))
wenzelm@12625
    56
  | fix_mixfix (syn                     , T, mx as Infixl           p ) =
wenzelm@12625
    57
               (Syntax.const_name syn mx, T,       InfixlName (syn, p))
wenzelm@12625
    58
  | fix_mixfix (syn                     , T, mx as Infixr           p ) =
wenzelm@12625
    59
               (Syntax.const_name syn mx, T,       InfixrName (syn, p))
wenzelm@4129
    60
  | fix_mixfix decl = decl;
wenzelm@12625
    61
fun transform decl = let
wenzelm@12625
    62
        val (c, T, mx) = fix_mixfix decl;
wenzelm@12625
    63
        val c2 = "_cont_" ^ c;
wenzelm@12625
    64
        val n  = Syntax.mixfix_args mx
wenzelm@12625
    65
    in     ((c ,               T,NoSyn),
wenzelm@12625
    66
            (c2,change_arrow n T,mx   ),
wenzelm@12625
    67
            trans_rules c2 c n mx) end;
wenzelm@4129
    68
wenzelm@4129
    69
fun cfun_arity (Type(n,[_,T])) = if n = cfun_arrow then 1+cfun_arity T else 0
wenzelm@4129
    70
|   cfun_arity _               = 0;
wenzelm@4129
    71
wenzelm@4129
    72
fun is_contconst (_,_,NoSyn   ) = false
wenzelm@4129
    73
|   is_contconst (_,_,Binder _) = false
wenzelm@4129
    74
|   is_contconst (c,T,mx      ) = cfun_arity T >= Syntax.mixfix_args mx
wenzelm@12625
    75
                         handle ERROR => error ("in mixfix annotation for " ^
wenzelm@12625
    76
                                               quote (Syntax.const_name c mx));
wenzelm@12625
    77
wenzelm@4129
    78
wenzelm@12625
    79
(* add_consts(_i) *)
wenzelm@4129
    80
wenzelm@12625
    81
fun gen_add_consts prep_typ raw_decls thy =
wenzelm@12625
    82
  let
wenzelm@16842
    83
    val decls = map (upd_second (Thm.typ_of o prep_typ thy)) raw_decls;
huffman@16621
    84
    val (contconst_decls, normal_decls) = List.partition is_contconst decls;
wenzelm@4129
    85
    val transformed_decls = map transform contconst_decls;
wenzelm@12625
    86
  in
wenzelm@12625
    87
    thy
wenzelm@12625
    88
    |> Theory.add_consts_i normal_decls
wenzelm@12625
    89
    |> Theory.add_consts_i (map first transformed_decls)
wenzelm@12625
    90
    |> Theory.add_syntax_i (map second transformed_decls)
skalberg@15570
    91
    |> Theory.add_trrules_i (List.concat (map third transformed_decls))
wenzelm@12625
    92
  end;
wenzelm@12625
    93
wenzelm@12625
    94
val add_consts = gen_add_consts Thm.read_ctyp;
wenzelm@12625
    95
val add_consts_i = gen_add_consts Thm.ctyp_of;
wenzelm@12625
    96
wenzelm@12625
    97
wenzelm@12625
    98
(* outer syntax *)
wenzelm@12625
    99
wenzelm@12625
   100
local structure P = OuterParse and K = OuterSyntax.Keyword in
wenzelm@12625
   101
wenzelm@12625
   102
val constsP =
wenzelm@12625
   103
  OuterSyntax.command "consts" "declare constants (HOLCF)" K.thy_decl
wenzelm@12876
   104
    (Scan.repeat1 P.const >> (Toplevel.theory o add_consts));
wenzelm@12625
   105
wenzelm@14682
   106
val _ = OuterSyntax.add_parsers [constsP];
wenzelm@12625
   107
wenzelm@4129
   108
end;
wenzelm@4129
   109
wenzelm@4129
   110
wenzelm@12625
   111
(* old-style theory syntax *)
wenzelm@4129
   112
wenzelm@4129
   113
val _ = ThySyn.add_syntax []
wenzelm@12625
   114
  [ThyParse.section "consts" "|> ContConsts.add_consts" ThyParse.const_decls];
wenzelm@4129
   115
wenzelm@12625
   116
end;