list.why 10.1 KB
Newer Older
1

2 3 4 5
(** {1 Polymorphic Lists} *)

(** {2 Basic theory of polymorphic lists} *)

6 7
theory List

8
  type list 'a = Nil | Cons 'a (list 'a)
9 10 11

end

12 13
(** {2 Length of a list} *)

14
theory Length
15

16 17 18
  use import int.Int
  use import List

Andrei Paskevich's avatar
Andrei Paskevich committed
19
  function length (l: list 'a) : int =
20
    match l with
21
    | Nil      -> 0
22
    | Cons _ r -> 1 + length r
23 24
    end

25
  lemma Length_nonnegative: forall l: list 'a. length l >= 0
26

27
  lemma Length_nil: forall l: list 'a. length l = 0 <-> l = Nil
28

29 30
end

31 32
(** {2 Membership in a list} *)

33 34
theory Mem
  use export List
MARCHE Claude's avatar
MARCHE Claude committed
35

Andrei Paskevich's avatar
Andrei Paskevich committed
36
  predicate mem (x: 'a) (l: list 'a) = match l with
37
    | Nil      -> false
Andrei Paskevich's avatar
Andrei Paskevich committed
38
    | Cons y r -> x = y \/ mem x r
39 40 41 42
    end

end

MARCHE Claude's avatar
MARCHE Claude committed
43 44 45 46 47 48 49 50 51 52 53 54
theory Elements

  use import List
  use Mem
  use set.Fset as FSet

  function elements (list 'a) : FSet.set 'a

  axiom elements_mem:
    forall l:list 'a, x:'a.
      Mem.mem x l <-> FSet.mem x (elements l)

55 56 57
  lemma elements_Nil:
    elements (Nil : list 'a) = FSet.empty

MARCHE Claude's avatar
MARCHE Claude committed
58 59
end

60 61
(** {2 Nth element of a list} *)

62
theory Nth
63

64
  use export List
65
  use export option.Option
66
  use import int.Int
MARCHE Claude's avatar
MARCHE Claude committed
67

Andrei Paskevich's avatar
Andrei Paskevich committed
68
  function nth (n: int) (l: list 'a) : option 'a = match l with
69
    | Nil      -> None
70
    | Cons x r -> if n = 0 then Some x else nth (n - 1) r
71 72 73 74
  end

end

MARCHE Claude's avatar
MARCHE Claude committed
75 76 77 78 79
theory NthNoOpt

  use export List
  use import int.Int

80
  function nth (n: int) (l: list 'a) : 'a
MARCHE Claude's avatar
MARCHE Claude committed
81 82 83 84 85 86 87

  axiom nth_cons_0: forall x:'a, r:list 'a. nth 0 (Cons x r) = x
  axiom nth_cons_n: forall x:'a, r:list 'a, n:int.
    n > 0 -> nth n (Cons x r) = nth (n-1) r

end

88 89 90 91 92 93 94
theory NthLength

  use export Nth
  use export Length
  use import int.Int

  lemma nth_none_1:
95
     forall l: list 'a, i: int. i < 0 -> nth i l = None
96 97

  lemma nth_none_2:
98
     forall l: list 'a, i: int. i >= length l -> nth i l = None
99 100 101 102 103 104

  lemma nth_none_3:
     forall l: list 'a, i: int. nth i l = None -> i < 0 \/ i >= length l

end

105 106
(** {2 Head and tail} *)

107
theory HdTl
108 109 110 111

  use export List
  use export option.Option

Andrei Paskevich's avatar
Andrei Paskevich committed
112
  function hd (l: list 'a) : option 'a = match l with
113
    | Nil      -> None
114 115 116
    | Cons h _ -> Some h
  end

Andrei Paskevich's avatar
Andrei Paskevich committed
117
  function tl (l: list 'a) : option (list 'a) = match l with
118
    | Nil      -> None
119 120 121
    | Cons _ t -> Some t
  end

122 123
end

124 125 126 127 128 129 130 131 132 133 134 135 136 137
theory HdTlNoOpt

  use export List

  function hd (l: list 'a) : 'a

  axiom hd_cons: forall x:'a, r:list 'a. hd (Cons x r) = x

  function tl (l: list 'a) : list 'a

  axiom tl_cons: forall x:'a, r:list 'a. tl (Cons x r) = r

end

138
(** {2 Relation between head, tail, and nth} *)
139

140 141
theory NthHdTl

142
  use import int.Int
143 144
  use import Nth
  use import HdTl
145

146 147
  lemma Nth_tl:
    forall l1 l2: list 'a. tl l1 = Some l2 ->
148
    forall i: int. i <> -1 -> nth i l2 = nth (i+1) l1
149 150

  lemma Nth0_head:
151
    forall l: list 'a. nth 0 l = hd l
152 153 154

end

155
(** {2 Appending two lists} *)
156

157 158 159
theory Append
  use export List

Andrei Paskevich's avatar
Andrei Paskevich committed
160
  function (++) (l1 l2: list 'a) : list 'a = match l1 with
161
    | Nil -> l2
162
    | Cons x1 r1 -> Cons x1 (r1 ++ l2)
163 164
  end

165 166
  lemma Append_assoc:
    forall l1 l2 l3: list 'a.
167
    l1 ++ (l2 ++ l3) = (l1 ++ l2) ++ l3
168

169 170
  lemma Append_l_nil:
    forall l: list 'a. l ++ Nil = l
171 172 173 174

  use import Length
  use import int.Int

175 176
  lemma Append_length:
    forall l1 l2: list 'a. length (l1 ++ l2) = length l1 + length l2
177

178 179
  use import Mem

180 181
  lemma mem_append:
    forall x: 'a, l1 l2: list 'a.
Andrei Paskevich's avatar
Andrei Paskevich committed
182
    mem x (l1 ++ l2) <-> mem x l1 \/ mem x l2
183

184 185 186 187
  lemma mem_decomp:
    forall x: 'a, l: list 'a.
    mem x l -> exists l1 l2: list 'a. l = l1 ++ Cons x l2

188 189
end

190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
theory NthLengthAppend

  use export NthLength
  use export Append
  use import int.Int

  lemma nth_append_1:
    forall l1 l2: list 'a, i: int.
    i < length l1 -> nth i (l1 ++ l2) = nth i l1

  lemma nth_append_2:
    forall l1 l2: list 'a, i: int.
    length l1 <= i -> nth i (l1 ++ l2) = nth (i - length l1) l2

end

206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
(** {2 Reversing a list} *)

theory Reverse

  use export List
  use import Append

  function reverse (l: list 'a) : list 'a = match l with
    | Nil      -> Nil
    | Cons x r -> reverse r ++ Cons x Nil
  end

  lemma reverse_append:
    forall l1 l2: list 'a, x: 'a.
    (reverse (Cons x l1)) ++ l2 = (reverse l1) ++ (Cons x l2)

  lemma reverse_reverse:
    forall l: list 'a. reverse (reverse l) = l

  use import Length

  lemma Reverse_length:
    forall l: list 'a. length (reverse l) = length l

end

232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
(** {2 Reverse append} *)

theory RevAppend

  use import List

  function rev_append (s t: list 'a) : list 'a =
    match s with
    | Cons x r -> rev_append r (Cons x t)
    | Nil -> t
    end

  use import Append

  lemma rev_append_append_l:
    forall r s t: list 'a.
      rev_append (r ++ s) t = rev_append s (rev_append r t)

  lemma rev_append_append_r:
    forall r s t: list 'a.
      rev_append r (s ++ t) = rev_append (rev_append s r) t

  use import int.Int
  use import Length

  lemma rev_append_length:
    forall s t: list 'a.
      length (rev_append s t) = length s + length t

end

263 264 265 266 267 268 269 270 271 272 273 274 275 276
(** {2 Zip} *)

theory Combine

  use export List

  function combine (x: list 'a) (y: list 'b) : list ('a, 'b) =
    match x, y with
    | Cons x0 x, Cons y0 y -> Cons (x0, y0) (combine x y)
    | _ -> Nil
    end

end

277 278
(** {2 Sorted lists for some order as parameter} *)

279 280 281
theory Sorted

  use export List
MARCHE Claude's avatar
MARCHE Claude committed
282

283 284 285 286
  type t
  predicate le t t

  inductive sorted (l: list t) =
287
    | Sorted_Nil:
288
        sorted Nil
289
    | Sorted_One:
290
        forall x: t. sorted (Cons x Nil)
291
    | Sorted_Two:
292 293
        forall x y: t, l: list t.
        le x y -> sorted (Cons y l) -> sorted (Cons x (Cons y l))
MARCHE Claude's avatar
MARCHE Claude committed
294

295 296
  use import Mem

297 298 299 300 301 302
  lemma sorted_mem:
    forall x: t, l: list t.
    (forall y: t. mem y l -> le x y) /\ sorted l <-> sorted (Cons x l)

end

303 304
(** {2 Sorted lists of integers} *)

305 306 307 308
theory SortedInt

  use import int.Int
  clone export Sorted with type t = int, predicate le = (<=)
309 310 311

end

312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
theory RevSorted

  type t
  predicate le t t
  predicate ge (x y: t) = le y x

  use import List

  clone Sorted as Incr with type t = t, predicate le = le
  clone Sorted as Decr with type t = t, predicate le = ge

  predicate compat (s t: list t) =
    match s, t with
    | Cons x _, Cons y _ -> le x y
    | _, _ -> true
    end

  use import RevAppend

  lemma rev_append_sorted_incr:
    forall s t: list t.
      Incr.sorted (rev_append s t) <->
        Decr.sorted s /\ Incr.sorted t /\ compat s t

  lemma rev_append_sorted_decr:
    forall s t: list t.
      Decr.sorted (rev_append s t) <->
        Incr.sorted s /\ Decr.sorted t /\ compat t s

  (*
  use import Reverse

  lemma rev_sorted_incr:
    forall s: list t. Incr.sorted (reverse s) <-> Decr.sorted s

  lemma rev_sorted_decr:
    forall s: list t. Decr.sorted (reverse s) <-> Incr.sorted s
  *)
end

352 353
(** {2 Number of occurrences in a list} *)

354
theory NumOcc
MARCHE Claude's avatar
MARCHE Claude committed
355

356 357 358
  use import int.Int
  use import List

359
  (** number of occurrences of [x] in [l] *)
Andrei Paskevich's avatar
Andrei Paskevich committed
360
  function num_occ (x: 'a) (l: list 'a) : int =
361
    match l with
362
    | Nil      -> 0
363 364 365 366 367
    | Cons y r -> (if x = y then 1 else 0) + num_occ x r
   end

  use import Mem

MARCHE Claude's avatar
MARCHE Claude committed
368
  lemma Mem_Num_Occ :
369
    forall x: 'a, l: list 'a. mem x l <-> num_occ x l > 0
370

371 372 373 374 375 376
  use import Append

  lemma Append_Num_Occ :
    forall x: 'a, l1 l2: list 'a.
    num_occ x (l1 ++ l2) = num_occ x l1 + num_occ x l2

377 378
end

379 380
(** {2 Permutation of lists} *)

381
theory Permut
382 383 384 385

  use import NumOcc
  use import List

Andrei Paskevich's avatar
Andrei Paskevich committed
386
  predicate permut (l1: list 'a) (l2: list 'a) =
387
    forall x: 'a. num_occ x l1 = num_occ x l2
388

389
  lemma Permut_refl: forall l: list 'a. permut l l
390

391
  lemma Permut_sym: forall l1 l2: list 'a. permut l1 l2 -> permut l2 l1
392

393 394
  lemma Permut_trans:
    forall l1 l2 l3: list 'a. permut l1 l2 -> permut l2 l3 -> permut l1 l3
395

396 397
  lemma Permut_cons:
    forall x: 'a, l1 l2: list 'a.
398 399
    permut l1 l2 -> permut (Cons x l1) (Cons x l2)

400 401
  lemma Permut_swap:
    forall x y: 'a, l: list 'a. permut (Cons x (Cons y l)) (Cons y (Cons x l))
MARCHE Claude's avatar
MARCHE Claude committed
402

403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
  use import Append

  lemma Permut_cons_append:
    forall x : 'a, l1 l2 : list 'a.
    permut (Cons x l1 ++ l2) (l1 ++ Cons x l2)

  lemma Permut_assoc:
    forall l1 l2 l3: list 'a.
    permut ((l1 ++ l2) ++ l3) (l1 ++ (l2 ++ l3))

  lemma Permut_append:
    forall l1 l2 k1 k2 : list 'a.
    permut l1 k1 -> permut l2 k2 -> permut (l1 ++ l2) (k1 ++ k2)

  lemma Permut_append_swap:
    forall l1 l2 : list 'a.
    permut (l1 ++ l2) (l2 ++ l1)

421 422
  use import Mem

423 424
  lemma Permut_mem:
    forall x: 'a, l1 l2: list 'a. permut l1 l2 -> mem x l1 -> mem x l2
425 426 427

  use import Length

428 429
  lemma Permut_length:
    forall l1 l2: list 'a. permut l1 l2 -> length l1 = length l2
430

431 432
end

433 434
(** {2 List with pairwise distinct elements} *)

Jean-Christophe's avatar
Jean-Christophe committed
435 436 437 438 439
theory Distinct

  use import List
  use import Mem

440 441 442 443 444
  inductive distinct (l: list 'a) =
    | distinct_zero: distinct (Nil: list 'a)
    | distinct_one : forall x:'a. distinct (Cons x Nil)
    | distinct_many:
        forall x:'a, l: list 'a.
Jean-Christophe's avatar
Jean-Christophe committed
445 446 447 448
        not (mem x l) -> distinct l -> distinct (Cons x l)

  use import Append

449 450
  lemma distinct_append:
    forall l1 l2: list 'a.
Jean-Christophe's avatar
Jean-Christophe committed
451 452 453 454 455
    distinct l1 -> distinct l2 -> (forall x:'a. mem x l1 -> not (mem x l2)) ->
    distinct (l1 ++ l2)

end

456 457 458 459 460 461 462 463 464 465 466 467 468 469
theory Prefix

  use export List
  use import int.Int

  function prefix (n: int) (l: list 'a) : list 'a =
    if n <= 0 then Nil else
    match l with
    | Nil -> Nil
    | Cons x r -> Cons x (prefix (n-1) r)
    end

end

470 471
(** {2 Induction on lists} *)

472
theory Induction
473

474 475
  use import List

476 477
  type elt

Andrei Paskevich's avatar
Andrei Paskevich committed
478
  predicate p (list elt)
479

480 481
  axiom Induction:
    p (Nil: list elt) ->
482 483
    (forall x:elt. forall l:list elt. p l -> p (Cons x l)) ->
    forall l:list elt. p l
484 485 486

end

MARCHE Claude's avatar
MARCHE Claude committed
487

488 489
(** {2 Maps as lists of pairs} *)

490
theory Map
491

492
  use import List
MARCHE Claude's avatar
MARCHE Claude committed
493

494
  type a
MARCHE Claude's avatar
MARCHE Claude committed
495
  type b
Andrei Paskevich's avatar
Andrei Paskevich committed
496
  function f a : b
497

498
  function map (l: list a) : list b =
499
    match l with
500
    | Nil      -> Nil
501
    | Cons x r -> Cons (f x) (map r)
502
    end
503 504
end

505 506
(** {2 Generic recursors on lists} *)

507
theory FoldLeft
508

509 510 511 512 513
  use import List

  type a
  type b
  function f b a : b
514

515 516 517 518 519
  function fold_left (acc: b) (l: list a) : b =
    match l with
    | Nil      -> acc
    | Cons x r -> fold_left (f acc x) r
    end
520

521 522
end

523
theory FoldRight
524

525 526 527 528 529 530 531 532 533 534 535
  use import List

  type a
  type b
  function f a b : b

  function fold_right (l: list a) (acc: b) : b =
    match l with
    | Nil      -> acc
    | Cons x r -> f x (fold_right r acc)
    end
536

537
end
538

539 540
(** {2 Importation of all list theories in one} *)

541
theory ListRich
542

543 544 545 546 547 548 549 550 551 552 553
  use export List
  use export Length
  use export Mem
  use export Nth
  use export HdTl
  use export NthHdTl
  use export Append
  use export Reverse
  use export Sorted
  use export NumOcc
  use export Permut
554

555
end