comparison src/DataStruct.v @ 568:81d63d9c1cc5

Port to Coq 8.9.0
author Adam Chlipala <adam@chlipala.net>
date Sun, 20 Jan 2019 15:16:29 -0500
parents ed829eaa91b2
children 0ce9829efa3b
comparison
equal deleted inserted replaced
567:ec0ce5129fc4 568:81d63d9c1cc5
115 end (get ls') 115 end (get ls')
116 end. 116 end.
117 (* end thide *) 117 (* end thide *)
118 End ilist. 118 End ilist.
119 119
120 Implicit Arguments Nil [A]. 120 Arguments Nil [A].
121 Implicit Arguments First [n]. 121 Arguments First [n].
122 122
123 (** A few examples show how to make use of these definitions. *) 123 (** A few examples show how to make use of these definitions. *)
124 124
125 Check Cons 0 (Cons 1 (Cons 2 Nil)). 125 Check Cons 0 (Cons 1 (Cons 2 Nil)).
126 (** %\vspace{-.15in}% [[ 126 (** %\vspace{-.15in}% [[
240 end. 240 end.
241 (* end thide *) 241 (* end thide *)
242 End hlist. 242 End hlist.
243 243
244 (* begin thide *) 244 (* begin thide *)
245 Implicit Arguments HNil [A B]. 245 Arguments HNil [A B].
246 Implicit Arguments HCons [A B x ls]. 246 Arguments HCons [A B x ls].
247 247
248 Implicit Arguments HFirst [A elm ls]. 248 Arguments HFirst [A elm ls].
249 Implicit Arguments HNext [A elm x ls]. 249 Arguments HNext [A elm x ls].
250 (* end thide *) 250 (* end thide *)
251 251
252 (** By putting the parameters [A] and [B] in [Type], we enable fancier kinds of polymorphism than in mainstream functional languages. For instance, one use of [hlist] is for the simple heterogeneous lists that we referred to earlier. *) 252 (** By putting the parameters [A] and [B] in [Type], we enable fancier kinds of polymorphism than in mainstream functional languages. For instance, one use of [hlist] is for the simple heterogeneous lists that we referred to earlier. *)
253 253
254 Definition someTypes : list Set := nat :: bool :: nil. 254 Definition someTypes : list Set := nat :: bool :: nil.
300 | Var : forall ts t, member t ts -> exp ts t 300 | Var : forall ts t, member t ts -> exp ts t
301 | App : forall ts dom ran, exp ts (Arrow dom ran) -> exp ts dom -> exp ts ran 301 | App : forall ts dom ran, exp ts (Arrow dom ran) -> exp ts dom -> exp ts ran
302 | Abs : forall ts dom ran, exp (dom :: ts) ran -> exp ts (Arrow dom ran). 302 | Abs : forall ts dom ran, exp (dom :: ts) ran -> exp ts (Arrow dom ran).
303 (* end thide *) 303 (* end thide *)
304 304
305 Implicit Arguments Const [ts]. 305 Arguments Const [ts].
306 306
307 (** We write a simple recursive function to translate [type]s into [Set]s. *) 307 (** We write a simple recursive function to translate [type]s into [Set]s. *)
308 308
309 Fixpoint typeDenote (t : type) : Set := 309 Fixpoint typeDenote (t : type) : Set :=
310 match t with 310 match t with
479 In a proposition [x = y], we see that [x] is a parameter and [y] is a regular argument. The type of the constructor [eq_refl] shows that [y] can only ever be instantiated to [x]. Thus, within a pattern-match with [eq_refl], occurrences of [y] can be replaced with occurrences of [x] for typing purposes. *) 479 In a proposition [x = y], we see that [x] is a parameter and [y] is a regular argument. The type of the constructor [eq_refl] shows that [y] can only ever be instantiated to [x]. Thus, within a pattern-match with [eq_refl], occurrences of [y] can be replaced with occurrences of [x] for typing purposes. *)
480 (* end thide *) 480 (* end thide *)
481 481
482 End fhlist. 482 End fhlist.
483 483
484 Implicit Arguments fhget [A B elm ls]. 484 Arguments fhget [A B elm ls].
485 485
486 (** How does one choose between the two data structure encoding strategies we have presented so far? Before answering that question in this chapter's final section, we introduce one further approach. *) 486 (** How does one choose between the two data structure encoding strategies we have presented so far? Before answering that question in this chapter's final section, we introduce one further approach. *)
487 487
488 488
489 (** * Data Structures as Index Functions *) 489 (** * Data Structures as Index Functions *)
590 590
591 (** A [Node] is indexed by a natural number [n], and the node's [n] children are represented as a function from [ffin n] to trees, which is isomorphic to the [ilist]-based representation that we used above. *) 591 (** A [Node] is indexed by a natural number [n], and the node's [n] children are represented as a function from [ffin n] to trees, which is isomorphic to the [ilist]-based representation that we used above. *)
592 592
593 End tree. 593 End tree.
594 594
595 Implicit Arguments Node [A n]. 595 Arguments Node [A n].
596 596
597 (** We can redefine [sum] and [inc] for our new [tree] type. Again, it is useful to define a generic fold function first. This time, it takes in a function whose domain is some [ffin] type, and it folds another function over the results of calling the first function at every possible [ffin] value. *) 597 (** We can redefine [sum] and [inc] for our new [tree] type. Again, it is useful to define a generic fold function first. This time, it takes in a function whose domain is some [ffin] type, and it folds another function over the results of calling the first function at every possible [ffin] value. *)
598 598
599 Section rifoldr. 599 Section rifoldr.
600 Variables A B : Set. 600 Variables A B : Set.
606 | O => fun _ => i 606 | O => fun _ => i
607 | S n' => fun get => f (get None) (rifoldr n' (fun idx => get (Some idx))) 607 | S n' => fun get => f (get None) (rifoldr n' (fun idx => get (Some idx)))
608 end. 608 end.
609 End rifoldr. 609 End rifoldr.
610 610
611 Implicit Arguments rifoldr [A B n]. 611 Arguments rifoldr [A B] f i [n].
612 612
613 Fixpoint sum (t : tree nat) : nat := 613 Fixpoint sum (t : tree nat) : nat :=
614 match t with 614 match t with
615 | Leaf n => n 615 | Leaf n => n
616 | Node _ f => rifoldr plus O (fun idx => sum (f idx)) 616 | Node _ f => rifoldr plus O (fun idx => sum (f idx))
706 (fun idx => tests (Some idx)) 706 (fun idx => tests (Some idx))
707 (fun idx => bodies (Some idx)) 707 (fun idx => bodies (Some idx))
708 end. 708 end.
709 End cond. 709 End cond.
710 710
711 Implicit Arguments cond [A n]. 711 Arguments cond [A] default [n].
712 (* end thide *) 712 (* end thide *)
713 713
714 (** Now the expression interpreter is straightforward to write. *) 714 (** Now the expression interpreter is straightforward to write. *)
715 715
716 (* begin thide *) 716 (* begin thide *)
795 end (bodies None) 795 end (bodies None)
796 end 796 end
797 end. 797 end.
798 End cfoldCond. 798 End cfoldCond.
799 799
800 Implicit Arguments cfoldCond [t n]. 800 Arguments cfoldCond [t] default [n].
801 (* end thide *) 801 (* end thide *)
802 802
803 (** Like for the interpreters, most of the action was in this helper function, and [cfold] itself is easy to write. *) 803 (** Like for the interpreters, most of the action was in this helper function, and [cfold] itself is easy to write. *)
804 804
805 (* begin thide *) 805 (* begin thide *)