src/HOL/Induct/SList.thy
author paulson
Wed May 07 12:50:26 1997 +0200 (1997-05-07)
changeset 3120 c58423c20740
child 3320 3a5e4930fb77
permissions -rw-r--r--
New directory to contain examples of (co)inductive definitions
paulson@3120
     1
(*  Title:      HOL/ex/SList.thy
paulson@3120
     2
    ID:         $Id$
paulson@3120
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
paulson@3120
     4
    Copyright   1993  University of Cambridge
paulson@3120
     5
paulson@3120
     6
Definition of type 'a list (strict lists) by a least fixed point
paulson@3120
     7
paulson@3120
     8
We use          list(A) == lfp(%Z. {NUMB(0)} <+> A <*> Z)
paulson@3120
     9
and not         list    == lfp(%Z. {NUMB(0)} <+> range(Leaf) <*> Z)
paulson@3120
    10
so that list can serve as a "functor" for defining other recursive types
paulson@3120
    11
*)
paulson@3120
    12
paulson@3120
    13
SList = Sexp +
paulson@3120
    14
paulson@3120
    15
types
paulson@3120
    16
  'a list
paulson@3120
    17
paulson@3120
    18
arities
paulson@3120
    19
  list :: (term) term
paulson@3120
    20
paulson@3120
    21
paulson@3120
    22
consts
paulson@3120
    23
paulson@3120
    24
  list        :: 'a item set => 'a item set
paulson@3120
    25
  Rep_list    :: 'a list => 'a item
paulson@3120
    26
  Abs_list    :: 'a item => 'a list
paulson@3120
    27
  NIL         :: 'a item
paulson@3120
    28
  CONS        :: ['a item, 'a item] => 'a item
paulson@3120
    29
  Nil         :: 'a list
paulson@3120
    30
  "#"         :: ['a, 'a list] => 'a list                         (infixr 65)
paulson@3120
    31
  List_case   :: ['b, ['a item, 'a item]=>'b, 'a item] => 'b
paulson@3120
    32
  List_rec    :: ['a item, 'b, ['a item, 'a item, 'b]=>'b] => 'b
paulson@3120
    33
  list_case   :: ['b, ['a, 'a list]=>'b, 'a list] => 'b
paulson@3120
    34
  list_rec    :: ['a list, 'b, ['a, 'a list, 'b]=>'b] => 'b
paulson@3120
    35
  Rep_map     :: ('b => 'a item) => ('b list => 'a item)
paulson@3120
    36
  Abs_map     :: ('a item => 'b) => 'a item => 'b list
paulson@3120
    37
  null        :: 'a list => bool
paulson@3120
    38
  hd          :: 'a list => 'a
paulson@3120
    39
  tl,ttl      :: 'a list => 'a list
paulson@3120
    40
  set_of_list :: ('a list => 'a set)
paulson@3120
    41
  mem         :: ['a, 'a list] => bool                            (infixl 55)
paulson@3120
    42
  map         :: ('a=>'b) => ('a list => 'b list)
paulson@3120
    43
  "@"         :: ['a list, 'a list] => 'a list                    (infixr 65)
paulson@3120
    44
  filter      :: ['a => bool, 'a list] => 'a list
paulson@3120
    45
paulson@3120
    46
  (* list Enumeration *)
paulson@3120
    47
paulson@3120
    48
  "[]"        :: 'a list                              ("[]")
paulson@3120
    49
  "@list"     :: args => 'a list                      ("[(_)]")
paulson@3120
    50
paulson@3120
    51
  (* Special syntax for filter *)
paulson@3120
    52
  "@filter"   :: [idt, 'a list, bool] => 'a list      ("(1[_:_ ./ _])")
paulson@3120
    53
paulson@3120
    54
translations
paulson@3120
    55
  "[x, xs]"     == "x#[xs]"
paulson@3120
    56
  "[x]"         == "x#[]"
paulson@3120
    57
  "[]"          == "Nil"
paulson@3120
    58
paulson@3120
    59
  "case xs of Nil => a | y#ys => b" == "list_case a (%y ys.b) xs"
paulson@3120
    60
paulson@3120
    61
  "[x:xs . P]"  == "filter (%x.P) xs"
paulson@3120
    62
paulson@3120
    63
defs
paulson@3120
    64
  (* Defining the Concrete Constructors *)
paulson@3120
    65
  NIL_def       "NIL == In0(Numb(0))"
paulson@3120
    66
  CONS_def      "CONS M N == In1(M $ N)"
paulson@3120
    67
paulson@3120
    68
inductive "list(A)"
paulson@3120
    69
  intrs
paulson@3120
    70
    NIL_I  "NIL: list(A)"
paulson@3120
    71
    CONS_I "[| a: A;  M: list(A) |] ==> CONS a M : list(A)"
paulson@3120
    72
paulson@3120
    73
rules
paulson@3120
    74
  (* Faking a Type Definition ... *)
paulson@3120
    75
  Rep_list          "Rep_list(xs): list(range(Leaf))"
paulson@3120
    76
  Rep_list_inverse  "Abs_list(Rep_list(xs)) = xs"
paulson@3120
    77
  Abs_list_inverse  "M: list(range(Leaf)) ==> Rep_list(Abs_list(M)) = M"
paulson@3120
    78
paulson@3120
    79
paulson@3120
    80
defs
paulson@3120
    81
  (* Defining the Abstract Constructors *)
paulson@3120
    82
  Nil_def       "Nil == Abs_list(NIL)"
paulson@3120
    83
  Cons_def      "x#xs == Abs_list(CONS (Leaf x) (Rep_list xs))"
paulson@3120
    84
paulson@3120
    85
  List_case_def "List_case c d == Case (%x.c) (Split d)"
paulson@3120
    86
paulson@3120
    87
  (* list Recursion -- the trancl is Essential; see list.ML *)
paulson@3120
    88
paulson@3120
    89
  List_rec_def
paulson@3120
    90
   "List_rec M c d == wfrec (trancl pred_sexp)
paulson@3120
    91
                            (%g. List_case c (%x y. d x y (g y))) M"
paulson@3120
    92
paulson@3120
    93
  list_rec_def
paulson@3120
    94
   "list_rec l c d == 
paulson@3120
    95
   List_rec (Rep_list l) c (%x y r. d (inv Leaf x) (Abs_list y) r)"
paulson@3120
    96
paulson@3120
    97
  (* Generalized Map Functionals *)
paulson@3120
    98
paulson@3120
    99
  Rep_map_def "Rep_map f xs == list_rec xs NIL (%x l r. CONS (f x) r)"
paulson@3120
   100
  Abs_map_def "Abs_map g M == List_rec M Nil (%N L r. g(N)#r)"
paulson@3120
   101
paulson@3120
   102
  null_def      "null(xs)            == list_rec xs True (%x xs r.False)"
paulson@3120
   103
  hd_def        "hd(xs)              == list_rec xs (@x.True) (%x xs r.x)"
paulson@3120
   104
  tl_def        "tl(xs)              == list_rec xs (@xs.True) (%x xs r.xs)"
paulson@3120
   105
  (* a total version of tl: *)
paulson@3120
   106
  ttl_def       "ttl(xs)             == list_rec xs [] (%x xs r.xs)"
paulson@3120
   107
paulson@3120
   108
  set_of_list_def "set_of_list xs    == list_rec xs {} (%x l r. insert x r)"
paulson@3120
   109
paulson@3120
   110
  mem_def       "x mem xs            == 
paulson@3120
   111
                   list_rec xs False (%y ys r. if y=x then True else r)"
paulson@3120
   112
  map_def       "map f xs            == list_rec xs [] (%x l r. f(x)#r)"
paulson@3120
   113
  append_def    "xs@ys               == list_rec xs ys (%x l r. x#r)"
paulson@3120
   114
  filter_def    "filter P xs         == 
paulson@3120
   115
                  list_rec xs [] (%x xs r. if P(x) then x#r else r)"
paulson@3120
   116
paulson@3120
   117
  list_case_def  "list_case a f xs == list_rec xs a (%x xs r.f x xs)"
paulson@3120
   118
paulson@3120
   119
end