
paulson@3120: (*  Title:      HOL/ex/SList.thy
paulson@3120:     ID:         $Id$
paulson@3120:     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
paulson@3120:     Copyright   1993  University of Cambridge
paulson@3120: 
paulson@3120: Definition of type 'a list (strict lists) by a least fixed point
paulson@3120: 
paulson@3120: We use          list(A) == lfp(%Z. {NUMB(0)} <+> A <*> Z)
paulson@3120: and not         list    == lfp(%Z. {NUMB(0)} <+> range(Leaf) <*> Z)
paulson@3120: so that list can serve as a "functor" for defining other recursive types
paulson@3120: *)
paulson@3120: 
paulson@3120: SList = Sexp +
paulson@3120: 
paulson@3120: types
paulson@3120:   'a list
paulson@3120: 
paulson@3120: arities
paulson@3120:   list :: (term) term
paulson@3120: 
paulson@3120: 
paulson@3120: consts
paulson@3120: 
paulson@3120:   list        :: 'a item set => 'a item set
paulson@3120:   Rep_list    :: 'a list => 'a item
paulson@3120:   Abs_list    :: 'a item => 'a list
paulson@3120:   NIL         :: 'a item
paulson@3120:   CONS        :: ['a item, 'a item] => 'a item
paulson@3120:   Nil         :: 'a list
paulson@3120:   "#"         :: ['a, 'a list] => 'a list                         (infixr 65)
paulson@3120:   List_case   :: ['b, ['a item, 'a item]=>'b, 'a item] => 'b
paulson@3120:   List_rec    :: ['a item, 'b, ['a item, 'a item, 'b]=>'b] => 'b
paulson@3120:   list_case   :: ['b, ['a, 'a list]=>'b, 'a list] => 'b
paulson@3120:   list_rec    :: ['a list, 'b, ['a, 'a list, 'b]=>'b] => 'b
paulson@3120:   Rep_map     :: ('b => 'a item) => ('b list => 'a item)
paulson@3120:   Abs_map     :: ('a item => 'b) => 'a item => 'b list
paulson@3120:   null        :: 'a list => bool
paulson@3120:   hd          :: 'a list => 'a
paulson@3120:   tl,ttl      :: 'a list => 'a list
paulson@3120:   set_of_list :: ('a list => 'a set)
paulson@3120:   mem         :: ['a, 'a list] => bool                            (infixl 55)
paulson@3120:   map         :: ('a=>'b) => ('a list => 'b list)
paulson@3120:   "@"         :: ['a list, 'a list] => 'a list                    (infixr 65)
paulson@3120:   filter      :: ['a => bool, 'a list] => 'a list
paulson@3120: 
paulson@3120:   (* list Enumeration *)
paulson@3120: 
paulson@3120:   "[]"        :: 'a list                              ("[]")
paulson@3120:   "@list"     :: args => 'a list                      ("[(_)]")
paulson@3120: 
paulson@3120:   (* Special syntax for filter *)
paulson@3120:   "@filter"   :: [idt, 'a list, bool] => 'a list      ("(1[_:_ ./ _])")
paulson@3120: 
paulson@3120: translations
paulson@3120:   "[x, xs]"     == "x#[xs]"
paulson@3120:   "[x]"         == "x#[]"
paulson@3120:   "[]"          == "Nil"
paulson@3120: 
paulson@3120:   "case xs of Nil => a | y#ys => b" == "list_case a (%y ys.b) xs"
paulson@3120: 
paulson@3120:   "[x:xs . P]"  == "filter (%x.P) xs"
paulson@3120: 
paulson@3120: defs
paulson@3120:   (* Defining the Concrete Constructors *)
paulson@3120:   NIL_def       "NIL == In0(Numb(0))"
paulson@3120:   CONS_def      "CONS M N == In1(M $ N)"
paulson@3120: 
paulson@3120: inductive "list(A)"
paulson@3120:   intrs
paulson@3120:     NIL_I  "NIL: list(A)"
paulson@3120:     CONS_I "[| a: A;  M: list(A) |] ==> CONS a M : list(A)"
paulson@3120: 
paulson@3120: rules
paulson@3120:   (* Faking a Type Definition ... *)
paulson@3120:   Rep_list          "Rep_list(xs): list(range(Leaf))"
paulson@3120:   Rep_list_inverse  "Abs_list(Rep_list(xs)) = xs"
paulson@3120:   Abs_list_inverse  "M: list(range(Leaf)) ==> Rep_list(Abs_list(M)) = M"
paulson@3120: 
paulson@3120: 
paulson@3120: defs
paulson@3120:   (* Defining the Abstract Constructors *)
paulson@3120:   Nil_def       "Nil == Abs_list(NIL)"
paulson@3120:   Cons_def      "x#xs == Abs_list(CONS (Leaf x) (Rep_list xs))"
paulson@3120: 
paulson@3120:   List_case_def "List_case c d == Case (%x.c) (Split d)"
paulson@3120: 
paulson@3120:   (* list Recursion -- the trancl is Essential; see list.ML *)
paulson@3120: 
paulson@3120:   List_rec_def
paulson@3120:    "List_rec M c d == wfrec (trancl pred_sexp)
paulson@3120:                             (%g. List_case c (%x y. d x y (g y))) M"
paulson@3120: 
paulson@3120:   list_rec_def
paulson@3120:    "list_rec l c d == 
paulson@3120:    List_rec (Rep_list l) c (%x y r. d (inv Leaf x) (Abs_list y) r)"
paulson@3120: 
paulson@3120:   (* Generalized Map Functionals *)
paulson@3120: 
paulson@3120:   Rep_map_def "Rep_map f xs == list_rec xs NIL (%x l r. CONS (f x) r)"
paulson@3120:   Abs_map_def "Abs_map g M == List_rec M Nil (%N L r. g(N)#r)"
paulson@3120: 
paulson@3120:   null_def      "null(xs)            == list_rec xs True (%x xs r.False)"
paulson@3120:   hd_def        "hd(xs)              == list_rec xs (@x.True) (%x xs r.x)"
paulson@3120:   tl_def        "tl(xs)              == list_rec xs (@xs.True) (%x xs r.xs)"
paulson@3120:   (* a total version of tl: *)
paulson@3120:   ttl_def       "ttl(xs)             == list_rec xs [] (%x xs r.xs)"
paulson@3120: 
paulson@3120:   set_of_list_def "set_of_list xs    == list_rec xs {} (%x l r. insert x r)"
paulson@3120: 
paulson@3120:   mem_def       "x mem xs            == 
paulson@3120:                    list_rec xs False (%y ys r. if y=x then True else r)"
paulson@3120:   map_def       "map f xs            == list_rec xs [] (%x l r. f(x)#r)"
paulson@3120:   append_def    "xs@ys               == list_rec xs ys (%x l r. x#r)"
paulson@3120:   filter_def    "filter P xs         == 
paulson@3120:                   list_rec xs [] (%x xs r. if P(x) then x#r else r)"
paulson@3120: 
paulson@3120:   list_case_def  "list_case a f xs == list_rec xs a (%x xs r.f x xs)"
paulson@3120: 
paulson@3120: end




