List.thy
changeset 128 89669c58e506
parent 113 0b9b8eb74101
child 196 61620d959717
equal deleted inserted replaced
127:d9527f97246e 128:89669c58e506
     3     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
     3     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
     4     Copyright   1993  University of Cambridge
     4     Copyright   1993  University of Cambridge
     5 
     5 
     6 Definition of type 'a list by a least fixed point
     6 Definition of type 'a list by a least fixed point
     7 
     7 
     8 We use          List(A) == lfp(%Z. {NUMB(0)} <+> A <*> Z)
     8 We use          list(A) == lfp(%Z. {NUMB(0)} <+> A <*> Z)
     9 and not         List    == lfp(%Z. {NUMB(0)} <+> range(Leaf) <*> Z)
     9 and not         list    == lfp(%Z. {NUMB(0)} <+> range(Leaf) <*> Z)
    10 so that List can serve as a "functor" for defining other recursive types
    10 so that list can serve as a "functor" for defining other recursive types
    11 *)
    11 *)
    12 
    12 
    13 List = Sexp +
    13 List = Sexp +
    14 
    14 
    15 types
    15 types
    19   list :: (term) term
    19   list :: (term) term
    20 
    20 
    21 
    21 
    22 consts
    22 consts
    23 
    23 
    24   List_Fun  :: "['a node set set, 'a node set set] => 'a node set set"
    24   list      :: "'a item set => 'a item set"
    25   List      :: "'a node set set => 'a node set set"
    25   Rep_list  :: "'a list => 'a item"
    26   Rep_List  :: "'a list => 'a node set"
    26   Abs_list  :: "'a item => 'a list"
    27   Abs_List  :: "'a node set => 'a list"
    27   NIL       :: "'a item"
    28   NIL       :: "'a node set"
    28   CONS      :: "['a item, 'a item] => 'a item"
    29   CONS      :: "['a node set, 'a node set] => 'a node set"
       
    30   Nil       :: "'a list"
    29   Nil       :: "'a list"
    31   "#"       :: "['a, 'a list] => 'a list"                   	(infixr 65)
    30   "#"       :: "['a, 'a list] => 'a list"                   	(infixr 65)
    32   List_case :: "['b, ['a node set, 'a node set]=>'b, 'a node set] => 'b"
    31   List_case :: "['b, ['a item, 'a item]=>'b, 'a item] => 'b"
    33   List_rec  :: "['a node set, 'b, ['a node set, 'a node set, 'b]=>'b] => 'b"
    32   List_rec  :: "['a item, 'b, ['a item, 'a item, 'b]=>'b] => 'b"
       
    33   list_case :: "['b, ['a, 'a list]=>'b, 'a list] => 'b"
    34   list_rec  :: "['a list, 'b, ['a, 'a list, 'b]=>'b] => 'b"
    34   list_rec  :: "['a list, 'b, ['a, 'a list, 'b]=>'b] => 'b"
    35   Rep_map   :: "('b => 'a node set) => ('b list => 'a node set)"
    35   Rep_map   :: "('b => 'a item) => ('b list => 'a item)"
    36   Abs_map   :: "('a node set => 'b) => 'a node set => 'b list"
    36   Abs_map   :: "('a item => 'b) => 'a item => 'b list"
    37   null      :: "'a list => bool"
    37   null      :: "'a list => bool"
    38   hd        :: "'a list => 'a"
    38   hd        :: "'a list => 'a"
    39   tl,ttl    :: "'a list => 'a list"
    39   tl,ttl    :: "'a list => 'a list"
    40   mem		:: "['a, 'a list] => bool"			(infixl 55)
    40   mem		:: "['a, 'a list] => bool"			(infixl 55)
    41   list_all  :: "('a => bool) => ('a list => bool)"
    41   list_all  :: "('a => bool) => ('a list => bool)"
    42   map       :: "('a=>'b) => ('a list => 'b list)"
    42   map       :: "('a=>'b) => ('a list => 'b list)"
    43   "@"	    :: "['a list, 'a list] => 'a list"			(infixr 65)
    43   "@"	    :: "['a list, 'a list] => 'a list"			(infixr 65)
    44   list_case :: "['b, ['a, 'a list]=>'b, 'a list] => 'b"
       
    45   filter    :: "['a => bool, 'a list] => 'a list"
    44   filter    :: "['a => bool, 'a list] => 'a list"
    46 
    45 
    47   (* List Enumeration *)
    46   (* list Enumeration *)
    48 
    47 
    49   "[]"      :: "'a list"                            ("[]")
    48   "[]"      :: "'a list"                            ("[]")
    50   "@List"   :: "args => 'a list"                    ("[(_)]")
    49   "@list"   :: "args => 'a list"                    ("[(_)]")
    51 
    50 
    52   (* Special syntax for list_all and filter *)
    51   (* Special syntax for list_all and filter *)
    53   "@Alls"	:: "[idt, 'a list, bool] => bool"	("(2Alls _:_./ _)" 10)
    52   "@Alls"	:: "[idt, 'a list, bool] => bool"	("(2Alls _:_./ _)" 10)
    54   "@filter"	:: "[idt, 'a list, bool] => 'a list"	("(1[_:_ ./ _])")
    53   "@filter"	:: "[idt, 'a list, bool] => 'a list"	("(1[_:_ ./ _])")
    55 
    54 
    61   "case xs of Nil => a | y#ys => b" == "list_case(a, %y ys.b, xs)"
    60   "case xs of Nil => a | y#ys => b" == "list_case(a, %y ys.b, xs)"
    62 
    61 
    63   "[x:xs . P]"	== "filter(%x.P,xs)"
    62   "[x:xs . P]"	== "filter(%x.P,xs)"
    64   "Alls x:xs.P"	== "list_all(%x.P,xs)"
    63   "Alls x:xs.P"	== "list_all(%x.P,xs)"
    65 
    64 
    66 rules
    65 defs
    67 
       
    68   List_Fun_def  "List_Fun(A) == (%Z. {Numb(0)} <+> A <*> Z)"
       
    69   List_def      "List(A) == lfp(List_Fun(A))"
       
    70 
       
    71   (* Faking a Type Definition ... *)
       
    72 
       
    73   Rep_List          "Rep_List(xs): List(range(Leaf))"
       
    74   Rep_List_inverse  "Abs_List(Rep_List(xs)) = xs"
       
    75   Abs_List_inverse  "M: List(range(Leaf)) ==> Rep_List(Abs_List(M)) = M"
       
    76 
       
    77   (* Defining the Concrete Constructors *)
    66   (* Defining the Concrete Constructors *)
    78 
       
    79   NIL_def       "NIL == In0(Numb(0))"
    67   NIL_def       "NIL == In0(Numb(0))"
    80   CONS_def      "CONS(M, N) == In1(M $ N)"
    68   CONS_def      "CONS(M, N) == In1(M $ N)"
    81 
    69 
       
    70 inductive "list(A)"
       
    71   intrs
       
    72     NIL_I  "NIL: list(A)"
       
    73     CONS_I "[| a: A;  M: list(A) |] ==> CONS(a,M) : list(A)"
       
    74 
       
    75 rules
       
    76   (* Faking a Type Definition ... *)
       
    77   Rep_list          "Rep_list(xs): list(range(Leaf))"
       
    78   Rep_list_inverse  "Abs_list(Rep_list(xs)) = xs"
       
    79   Abs_list_inverse  "M: list(range(Leaf)) ==> Rep_list(Abs_list(M)) = M"
       
    80 
       
    81 
       
    82 defs
    82   (* Defining the Abstract Constructors *)
    83   (* Defining the Abstract Constructors *)
    83 
    84   Nil_def       "Nil == Abs_list(NIL)"
    84   Nil_def       "Nil == Abs_List(NIL)"
    85   Cons_def      "x#xs == Abs_list(CONS(Leaf(x), Rep_list(xs)))"
    85   Cons_def      "x#xs == Abs_List(CONS(Leaf(x), Rep_List(xs)))"
       
    86 
    86 
    87   List_case_def "List_case(c, d) == Case(%x.c, Split(d))"
    87   List_case_def "List_case(c, d) == Case(%x.c, Split(d))"
    88 
    88 
    89   (* List Recursion -- the trancl is Essential; see list.ML *)
    89   (* list Recursion -- the trancl is Essential; see list.ML *)
    90 
    90 
    91   List_rec_def
    91   List_rec_def
    92    "List_rec(M, c, d) == wfrec(trancl(pred_Sexp), M, \
    92    "List_rec(M, c, d) == wfrec(trancl(pred_sexp), M, \
    93 \                         List_case(%g.c, %x y g. d(x, y, g(y))))"
    93 \                         List_case(%g.c, %x y g. d(x, y, g(y))))"
    94 
    94 
    95   list_rec_def
    95   list_rec_def
    96    "list_rec(l, c, d) == \
    96    "list_rec(l, c, d) == \
    97 \   List_rec(Rep_List(l), c, %x y r. d(Inv(Leaf, x), Abs_List(y), r))"
    97 \   List_rec(Rep_list(l), c, %x y r. d(Inv(Leaf, x), Abs_list(y), r))"
    98 
    98 
    99   (* Generalized Map Functionals *)
    99   (* Generalized Map Functionals *)
   100 
   100 
   101   Rep_map_def
   101   Rep_map_def "Rep_map(f, xs) == list_rec(xs, NIL, %x l r. CONS(f(x), r))"
   102    "Rep_map(f, xs) == list_rec(xs, NIL, %x l r. CONS(f(x), r))"
   102   Abs_map_def "Abs_map(g, M) == List_rec(M, Nil, %N L r. g(N)#r)"
   103   Abs_map_def
       
   104    "Abs_map(g, M) == List_rec(M, Nil, %N L r. g(N)#r)"
       
   105 
   103 
   106   null_def      "null(xs)            == list_rec(xs, True, %x xs r.False)"
   104   null_def      "null(xs)            == list_rec(xs, True, %x xs r.False)"
   107   hd_def        "hd(xs)              == list_rec(xs, @x.True, %x xs r.x)"
   105   hd_def        "hd(xs)              == list_rec(xs, @x.True, %x xs r.x)"
   108   tl_def        "tl(xs)              == list_rec(xs, @xs.True, %x xs r.xs)"
   106   tl_def        "tl(xs)              == list_rec(xs, @xs.True, %x xs r.xs)"
   109   (* a total version of tl: *)
   107   (* a total version of tl: *)
   110   ttl_def	"ttl(xs)             == list_rec(xs, [], %x xs r.xs)"
   108   ttl_def	"ttl(xs)             == list_rec(xs, [], %x xs r.xs)"
       
   109 
   111   mem_def	"x mem xs            == \
   110   mem_def	"x mem xs            == \
   112 \		   list_rec(xs, False, %y ys r. if(y=x, True, r))"
   111 \		   list_rec(xs, False, %y ys r. if(y=x, True, r))"
   113   list_all_def  "list_all(P, xs)     == list_rec(xs, True, %x l r. P(x) & r)"
   112   list_all_def  "list_all(P, xs)     == list_rec(xs, True, %x l r. P(x) & r)"
   114   map_def       "map(f, xs)          == list_rec(xs, [], %x l r. f(x)#r)"
   113   map_def       "map(f, xs)          == list_rec(xs, [], %x l r. f(x)#r)"
   115   append_def	"xs@ys               == list_rec(xs, ys, %x l r. x#r)"
   114   append_def	"xs@ys               == list_rec(xs, ys, %x l r. x#r)"
   116   filter_def	"filter(P,xs)        == \
   115   filter_def	"filter(P,xs)        == \
   117 \                  list_rec(xs, [], %x xs r. if(P(x), x#r, r))"
   116 \                  list_rec(xs, [], %x xs r. if(P(x), x#r, r))"
       
   117 
   118   list_case_def "list_case(a, f, xs) == list_rec(xs, a, %x xs r.f(x, xs))"
   118   list_case_def "list_case(a, f, xs) == list_rec(xs, a, %x xs r.f(x, xs))"
   119 
   119 
   120 end
   120 end