Require Import prosa.classic.util.all.
Require Import prosa.classic.model.arrival.basic.job.
Require Import prosa.classic.model.schedule.uni.service
               prosa.classic.model.schedule.uni.schedule.

(* Let's import definition of nonpreemptive schedule. *)
Require Import prosa.classic.model.schedule.uni.nonpreemptive.schedule.

From mathcomp Require Import ssreflect ssrbool eqtype ssrnat seq fintype bigop.

(* In this file, we provide additional definitions and
   lemmas about lock-in-serivce-compliant schedules. *)
  Import Job Service UniprocessorSchedule.

  Section Definitions.

    Context {Task: eqType}.
    Variable task_cost: Task -> time.

    Context {Job: eqType}.
    Variable job_arrival: Job -> time.
    Variable job_cost: Job -> time.
    Variable job_task: Job -> Task.

    (* Consider any arrival sequence... *)
    Variable arr_seq: arrival_sequence Job.

    (* ...and any uniprocessor schedule. *)
    Variable sched: schedule Job.

    (* We define the notion of lock-in service: lock-in service is the amount of service
       after which a job cannot be preempted until its completion. *)
    Variable job_lock_in_service: Job -> time.

    (* We require the lock-in service to be positive for any job, i.e., in order to
       become non-preemptive a job must receive at least one unit of service. *)
    Definition job_lock_in_service_positive :=
      forall j,
        arrives_in arr_seq j ->
        job_cost_positive job_cost j ->
        0 < job_lock_in_service j.

    (* We also require a job's lock-in service to be at most the cost of the job. *)
    Definition job_lock_in_service_le_job_cost :=
      forall j,
        arrives_in arr_seq j ->
        job_cost_positive job_cost j ->
        job_lock_in_service j <= job_cost j.

    (* In order to get a consistent schedule, the scheduler should respect the notion of
       lock-in service. We assume that, after a job reaches its lock-in service, it
       cannot be preempted until its completion. *)
    Definition job_nonpreemptive_after_lock_in_service :=
      forall j t t',
        arrives_in arr_seq j ->
        t <= t' ->
        job_lock_in_service j <= service sched j t ->
        ~~ completed_by job_cost sched j t' ->
        scheduled_at sched j t'.

    (* We say that job_lock_in_service is a proper job lock-in service iff for all jobs in the
       arrival sequence the lock-in service is (1) positive, (2) no bigger than the costs of
       the corresponding jobs, and (3) a job becomes nonpreemptive after it reaches the
       lock-in service. *)
    Definition proper_job_lock_in_service :=
      job_lock_in_service_positive /\
      job_lock_in_service_le_job_cost /\
      job_nonpreemptive_after_lock_in_service.

    (* Similarly, we define the notion of task lock-in service: task lock-in service is the
       amount of service after which any job from a task reaches its lock-in service. *)
    Variable task_lock_in_service: Task -> time.

    (* A task's lock-in service should be at most the cost of the task. *)
    Definition task_lock_in_service_le_task_cost tsk :=
      task_lock_in_service tsk <= task_cost tsk.

    (* We say that the lock-in service of a task tsk bounds the job lock-in service iff for any
       job j of task tsk the job lock-in service is less-than-or-equal to the task lock-in
       service. *)
    Definition task_lock_in_service_bounds_job_lock_in_service tsk :=
      forall j,
        arrives_in arr_seq j ->
        job_task j = tsk ->
        job_lock_in_service j <= task_lock_in_service tsk.

    (* We say that task_lock_in_service is a proper task lock-in service for some task tsk
       iff [task_lock_in_service tsk] is (1) no bigger than tsk's cost, (2) for any job of
       task tsk job_lock_in_service is bounded by task_lock_in_service. *)
    Definition proper_task_lock_in_service tsk :=
      task_lock_in_service_le_task_cost tsk /\
      task_lock_in_service_bounds_job_lock_in_service tsk.

  End Definitions.

  Section Examples.

    Context {Job: eqType}.
    Variable job_arrival: Job -> time.
    Variable job_cost: Job -> time.

    (* Consider any arrival sequence... *)
    Variable arr_seq: arrival_sequence Job.

    (* ...and any uniprocessor schedule. *)
    Variable sched: schedule Job.

    (* Assume that completed jobs do not execute. *)
    Hypothesis H_completed_jobs_dont_execute:
      completed_jobs_dont_execute job_cost sched.

    (* In this section we prove that in case of the fully preemptive scheduling model
       the job_nonpreemptive_after_lock_in_service hypothesis becomes trivial. *)
    Section FullyPreemptiveModel.

      (* In the fully preemptive model any job can be preempted at any moment. *)
      Let job_lock_in_service (j: Job) := job_cost j.

      (* Then, we prove that the job_nonpreemptive_after_lock_in_service hypothesis is trivial. *)
      Lemma job_nonpreemptive_after_lock_in_service_trivial:
         job_nonpreemptive_after_lock_in_service job_cost arr_seq sched job_lock_in_service .
      Proof.
        intros j ? ? ARR LE SERV NCOMP.
        move: NCOMP => /negP NCOMP; exfalso; apply: NCOMP.
        move: (H_completed_jobs_dont_execute j t) => SERV2.
          by apply completion_monotonic with t.
      Qed.

    End FullyPreemptiveModel.

    (* In this section we prove that in case of the fully nonpreemptive scheduling model
       the job_nonpreemptive_after_lock_in_service hypothesis holds. *)
    Section FullyNonPreemptiveModel.

      (* In fully nonpreemptive model any job becomes nonpreemptive as soon as it receives one unit of service. *)
      Let job_lock_in_service (j: Job) := ε.

      (* Next, we assume that the schedule is fully nonpreemptive. *)
      Hypothesis H_is_nonpreemptive_schedule:
        NonpreemptiveSchedule.is_nonpreemptive_schedule job_cost sched.

      (* Then, we prove that the job_nonpreemptive_after_lock_in_service hypothesis holds. *)
      Lemma property_last_segment_is_nonpreemptive_holds:
        job_nonpreemptive_after_lock_in_service job_cost arr_seq sched job_lock_in_service .
      Proof.
        unfold NonpreemptiveSchedule.is_nonpreemptive_schedule in *.
        intros j ? ? ARR LE NEQ NCOMPL; unfold job_lock_in_service in *.
        have POS: 0 < job_cost j.
        { rewrite -[0 < _]Bool.negb_involutive -eqn0Ngt; apply/negP; intros ZERO.
          move: ZERO => /eqP ZERO.
          rewrite /completed_by in NCOMPL.
          rewrite ZERO -lt0n in NCOMPL.
          move: (H_completed_jobs_dont_execute j t') => NN.
            by rewrite ZERO leqNgt in NN; move: NN => /negP NN; apply: NN.
        }
        move: NEQ => /sum_seq_gt0P [ts [IN SCHED]].
        rewrite lt0b in SCHED.
        apply H_is_nonpreemptive_schedule with ts; try done.
        apply ltnW, leq_trans with t; last by done.
          by rewrite mem_iota add0n subn0 in IN; move: IN => /andP [_ IN].
      Qed.

    End FullyNonPreemptiveModel.

  End Examples.


Set Implicit Arguments.
From TLC Require Import LibTactics LibLogic LibFun LibProd LibContainer LibSet
     LibRelation LibPer LibMonoid.
Local Notation path := rtclosure.
From iris_time.union_find.math Require Import LibNatExtra LibIter
     InverseNatNat Ackermann InverseAckermann MiscArith TLCBuffer
     UnionFind01Data UnionFind03Link UnionFind04Compress
     UnionFind05IteratedCompression UnionFind11Rank UnionFind13RankLink
     UnionFind14RankCompress UnionFind21Parent UnionFind22ParentEvolution.

(* -------------------------------------------------------------------------- *)

(* We define a notion of evolution over time, which includes links and
   compressions, and study how parent and rank evolve over time. *)

Section Evolution.

Variable V : Type.

(* -------------------------------------------------------------------------- *)

(* The state evolves through links and compressions. This is encoded by the
   predicate [evolution D F K F' K']. *)

Inductive evolution (D : set V) : binary V -> (V -> nat) -> binary V -> (V -> nat) -> Prop :=
| EvLink:
    forall F K x y,
    x \in D ->
    y \in D ->
    is_root F x ->
    is_root F y ->
    x <> y ->
    evolution D F K
      (link_by_rank_F F K x y)
      (link_by_rank_K K x y)
| EvCompress:
    forall (F : binary V) K x y z,
    F x y ->
    path F y z ->
    evolution D F K
      (compress F x z)
      K.

End Evolution.

(* A path compression step that creates a direct edge from a non-root vertex
   [x] to its representative [z] is a valid evolution. *)

Lemma compress_evolution:
  forall V (D : set V) F K,
  is_rdsf D F K ->
  forall x,
  ~ is_root F x ->
  forall z,
  is_repr F x z ->
  evolution D F K (compress F x z) K.
Proof using.
  intros.
  assert (pxz: is_repr F (p F x) z).
  { eauto using is_repr_is_equiv_is_repr, is_equiv_parent with is_dsf. }
  destruct pxz.
  econstructor.
    { eapply (@parent_spec _ F). eauto. }
    { eauto. }
Qed.

(* -------------------------------------------------------------------------- *)

Section StudyOfEvolution.

Variable  V    : Type.
Variable  D    : set V.
Variables F F' : binary V.
Variables K K' : V -> nat.

Hypothesis is_rdsf_F : is_rdsf D F K.
Hypothesis ev  : evolution D F K F' K'.

(* -------------------------------------------------------------------------- *)

(* Evolution preserves [is_rdsf]. *)

Lemma is_rdsf_evolution:
  is_rdsf D F' K'.
Proof using is_rdsf_F ev.
  inversion ev; subst; eauto using is_rdsf_link, is_rdsf_compress.
Qed.

(* -------------------------------------------------------------------------- *)

(* The rank of every vertex grows with time. *)

Lemma rank_grows:
  forall v,
  K v <= K' v.
Proof using ev.
  clear is_rdsf_F.
  intros. inversion ev; subst; eauto using link_cannot_decrease_rank.
Qed.

(* -------------------------------------------------------------------------- *)

Section StudyOfV.

(* Now, let us study a vertex [v], which we assume already has a parent. *)

Variable v : V.
Hypothesis v_has_a_parent: ~ is_root F v.

(* -------------------------------------------------------------------------- *)

(* If [v] has a parent, [K v] remains constant. *)

Lemma non_root_has_constant_rank:
  K v = K' v.
Proof using ev v_has_a_parent.
  clear is_rdsf_F.
  inversion ev; subst; eauto using link_preserves_rank_of_non_roots.
Qed.

(* -------------------------------------------------------------------------- *)

(* If [v] has a parent, then this remains true forever. In other words,
   a non-root remains a non-root. *)

Lemma non_root_forever:
  ~ is_root F' v.
Proof using ev v_has_a_parent.
  clear is_rdsf_F.
  inversion ev; subst.
  (* Link. *)
  { unfold link_by_rank_F. cases_if; eauto using is_root_link_converse. }
  (* Compression. *)
  { eauto using compress_preserves_roots_converse. }
Qed.

(* -------------------------------------------------------------------------- *)

(* The quantity [K (p v)] grows with time, either because the rank of
   the parent of [v] changes (during a link), or because the parent
   of [v] changes (during a compression). *)

Lemma Kpv_grows:
  K (p F v) <= K' (p F' v).
Proof using is_rdsf_F ev v_has_a_parent.
  (* Let us distinguish two cases: either the parent of [v] has remained
     the same, or it has changed. *)
  tests : (p F v = p F' v).
  { (* If it has remained the same, then the result follows from the fact
       that the rank grows with time. *)
    match goal with h: _ = _ |- _ => rewrite h end.
    eauto using rank_grows. }
  { (* If it has changed: *)
    inversion ev; subst; clear ev.
    (* During a link, this means that [v] must be the source of the new
       edge. However, we have assume that [v] is not a root, so this
       case is impossible. *)
    { false.
      unfold link_by_rank_F, not in *.
      tests : (K x < K y); eauto using link_preserves_parent. }
    (* During a compression, this means that [v] must be [x], and we
       have replaced an edge of [x] to [y] with an edge of [x] to [z].
       Because there is a path of [y] to [z], the rank of [z] is greater
       than the rank of [y]. *)
    { forwards [ ? hz ]: compress_changes_parent_of_x_only; eauto.
      subst v. rewrite hz in *; clear hz.
      forwards hpx: parent_unique; eauto.
      rewrite hpx in *; clear hpx.
      eauto using rank_increases_along_a_path. }
  }
Qed.

End StudyOfV.

End StudyOfEvolution.

Hint Constructors evolution : evolution.


(** * Poly: Polymorphism and Higher-Order Functions *)

(** In this chapter we continue our development of basic
    concepts of functional programming.  The critical new ideas are
    _polymorphism_ (abstracting functions over the types of the data
    they manipulate) and _higher-order functions_ (treating functions
    as data).
*)

Require Export Lists.
Check nandb.
Check NatList.swap_pair.

(* ###################################################### *)
(** * Polymorphism *)
(* ###################################################### *)
(** ** Polymorphic Lists *)

(** For the last couple of chapters, we've been working just
    with lists of numbers.  Obviously, interesting programs also need
    to be able to manipulate lists with elements from other types --
    lists of strings, lists of booleans, lists of lists, etc.  We
    _could_ just define a new inductive datatype for each of these,
    for example... *)

Inductive boollist : Type :=
  | bool_nil : boollist
  | bool_cons : bool -> boollist -> boollist.

(** ... but this would quickly become tedious, partly because we
    have to make up different constructor names for each datatype, but
    mostly because we would also need to define new versions of all
    our list manipulating functions ([length], [rev], etc.)  for each
    new datatype definition. *)

(** *** *)

(** To avoid all this repetition, Coq supports _polymorphic_
    inductive type definitions.  For example, here is a _polymorphic
    list_ datatype. *)

Inductive list (X:Type) : Type :=
  | nil : list X
  | cons : X -> list X -> list X.

(** This is exactly like the definition of [natlist] from the
    previous chapter, except that the [nat] argument to the [cons]
    constructor has been replaced by an arbitrary type [X], a binding
    for [X] has been added to the header, and the occurrences of
    [natlist] in the types of the constructors have been replaced by
    [list X].  (We can re-use the constructor names [nil] and [cons]
    because the earlier definition of [natlist] was inside of a
    [Module] definition that is now out of scope.) *)

(** What sort of thing is [list] itself?  One good way to think
    about it is that [list] is a _function_ from [Type]s to
    [Inductive] definitions; or, to put it another way, [list] is a
    function from [Type]s to [Type]s.  For any particular type [X],
    the type [list X] is an [Inductive]ly defined set of lists whose
    elements are things of type [X]. *)

(** With this definition, when we use the constructors [nil] and
    [cons] to build lists, we need to tell Coq the type of the
    elements in the lists we are building -- that is, [nil] and [cons]
    are now _polymorphic constructors_.  Observe the types of these
    constructors: *)

Check nil.
(* ===> nil : forall X : Type, list X *)
Check cons.
(* ===> cons : forall X : Type, X -> list X -> list X *)

(** The "[forall X]" in these types can be read as an additional
    argument to the constructors that determines the expected types of
    the arguments that follow.  When [nil] and [cons] are used, these
    arguments are supplied in the same way as the others.  For
    example, the list containing [2] and [1] is written like this: *)

Check nil bool.
Check nil nat.
Check (cons nat 2 (cons nat 1 (nil nat))).

(** (We've gone back to writing [nil] and [cons] explicitly here
    because we haven't yet defined the [ [] ] and [::] notations for
    the new version of lists.  We'll do that in a bit.) *)

(** We can now go back and make polymorphic (or "generic")
    versions of all the list-processing functions that we wrote
    before.  Here is [length], for example: *)

(** *** *)

Fixpoint length (X:Type) (l:list X) : nat :=
  match l with
  | nil      => 0
  | cons h t => S (length X t)
  end.
Check (cons nat 1 (nil nat)).
Check length nat (cons nat 2 (nil nat)).
(** Note that the uses of [nil] and [cons] in [match] patterns
    do not require any type annotations: Coq already knows that the list
    [l] contains elements of type [X], so there's no reason to include
    [X] in the pattern.  (More precisely, the type [X] is a parameter
    of the whole definition of [list], not of the individual
    constructors.  We'll come back to this point later.)

    As with [nil] and [cons], we can use [length] by applying it first
    to a type and then to its list argument: *)

Eval compute in length nat (cons nat 1 (cons nat 2 (nil nat))).
Eval compute in length nat (cons nat 1 (nil nat)).
Example test_length1 :
    length nat (cons nat 1 (cons nat 2 (nil nat))) = 2.
Proof. reflexivity.  Qed.

(** To use our length with other kinds of lists, we simply
    instantiate it with an appropriate type parameter: *)

Example test_length2 :
    length bool (cons bool true (nil bool)) = 1.
Proof. reflexivity.  Qed.

(** *** *)
(** Let's close this subsection by re-implementing a few other
    standard list functions on our new polymorphic lists: *)

Fixpoint app (X : Type) (l1 l2 : list X)
                : (list X) :=
  match l1 with
  | nil      => l2
  | cons h t => cons X h (app X t l2)
  end.
Check app nat (cons nat 2 (nil nat)) (nil nat).
Eval compute in app nat (cons nat 1 (cons nat 2 (nil nat))) (cons nat 1 (nil nat)).
Fixpoint snoc (X:Type) (l:list X) (v:X) : (list X) :=
  match l with
  | nil      => cons X v (nil X)
  | cons h t => cons X h (snoc X t v)
  end.

Fixpoint rev (X:Type) (l:list X) : list X :=
  match l with
  | nil      => nil X
  | cons h t => snoc X (rev X t) h
  end.

Example test_rev1 :
    rev nat (cons nat 1 (cons nat 2 (nil nat)))
  = (cons nat 2 (cons nat 1 (nil nat))).
Proof. reflexivity.  Qed.

Example test_rev2:
  rev bool (nil bool) = nil bool.
Proof. reflexivity.  Qed.

Module MumbleBaz.
(** **** Exercise: 2 stars (mumble_grumble)  *)
(** Consider the following two inductively defined types. *)

Inductive mumble : Type :=
  | a : mumble
  | b : mumble -> nat -> mumble
  | c : mumble.
Inductive grumble (X:Type) : Type :=
  | d : mumble -> grumble X
  | e : X -> grumble X.

(** Which of the following are well-typed elements of [grumble X] for
    some type [X]?
      - [d (b a 5)]
      - [d mumble (b a 5)]
      - [d bool (b a 5)]
      - [e bool true]
      - [e mumble (b c 0)]
      - [e bool (b c 0)]
      - [c]
(* FILL IN HERE *)
*)
Check (b (a) 5).
Check d mumble a.
Check (d mumble (b a 5)).
Check (d bool (b a 5)).
Check (e bool true).
Check (e mumble (b c 0)).
Check c.
(** [] *)

(** **** Exercise: 2 stars (baz_num_elts)  *)
(** Consider the following inductive definition: *)

Inductive baz : Type :=
   | x : baz -> baz
   | y : baz -> bool -> baz.

(** How _many_ elements does the type [baz] have?
(* FILL IN HERE *)
*)

(** [] *)

End MumbleBaz.

(* ###################################################### *)
(** *** Type Annotation Inference *)

(** Let's write the definition of [app] again, but this time we won't
    specify the types of any of the arguments. Will Coq still accept
    it? *)

Fixpoint app' X l1 l2 : list X :=
  match l1 with
  | nil      => l2
  | cons h t => cons X h (app' X t l2)
  end.

(** Indeed it will.  Let's see what type Coq has assigned to [app']: *)

Check app'.
(* ===> forall X : Type, list X -> list X -> list X *)
Check app.
(* ===> forall X : Type, list X -> list X -> list X *)

(** It has exactly the same type type as [app].  Coq was able to
    use a process called _type inference_ to deduce what the types of
    [X], [l1], and [l2] must be, based on how they are used.  For
    example, since [X] is used as an argument to [cons], it must be a
    [Type], since [cons] expects a [Type] as its first argument;
    matching [l1] with [nil] and [cons] means it must be a [list]; and
    so on.

    This powerful facility means we don't always have to write
    explicit type annotations everywhere, although explicit type
    annotations are still quite useful as documentation and sanity
    checks.  You should try to find a balance in your own code between
    too many type annotations (so many that they clutter and distract)
    and too few (which forces readers to perform type inference in
    their heads in order to understand your code). *)

(* ###################################################### *)
(** *** Type Argument Synthesis *)

(** Whenever we use a polymorphic function, we need to pass it
    one or more types in addition to its other arguments.  For
    example, the recursive call in the body of the [length] function
    above must pass along the type [X].  But just like providing
    explicit type annotations everywhere, this is heavy and verbose.
    Since the second argument to [length] is a list of [X]s, it seems
    entirely obvious that the first argument can only be [X] -- why
    should we have to write it explicitly?

    Fortunately, Coq permits us to avoid this kind of redundancy.  In
    place of any type argument we can write the "implicit argument"
    [_], which can be read as "Please figure out for yourself what
    type belongs here."  More precisely, when Coq encounters a [_], it
    will attempt to _unify_ all locally available information -- the
    type of the function being applied, the types of the other
    arguments, and the type expected by the context in which the
    application appears -- to determine what concrete type should
    replace the [_].

    This may sound similar to type annotation inference -- and,
    indeed, the two procedures rely on the same underlying mechanisms.
    Instead of simply omitting the types of some arguments to a
    function, like
      app' X l1 l2 : list X :=
    we can also replace the types with [_], like
      app' (X : _) (l1 l2 : _) : list X :=
    which tells Coq to attempt to infer the missing information, just
    as with argument synthesis.

    Using implicit arguments, the [length] function can be written
    like this: *)

Fixpoint length' (X:Type) (l:list X) : nat :=
  match l with
  | nil      => 0
  | cons h t => S (length' _ t)
  end.

(** In this instance, we don't save much by writing [_] instead of
    [X].  But in many cases the difference can be significant.  For
    example, suppose we want to write down a list containing the
    numbers [1], [2], and [3].  Instead of writing this... *)

Definition list123 :=
  cons nat 1 (cons nat 2 (cons nat 3 (nil nat))).

(** ...we can use argument synthesis to write this: *)

Definition list123' := cons _ 1 (cons _ 2 (cons _ 3 (nil _))).

(* ###################################################### *)
(** *** Implicit Arguments *)

(** In fact, we can go further.  To avoid having to sprinkle [_]'s
    throughout our programs, we can tell Coq _always_ to infer the
    type argument(s) of a given function. The [Arguments] directive
    specifies the name of the function or constructor, and then lists
    its argument names, with curly braces around any arguments to be
    treated as implicit.
    *)

Arguments nil {X}.
Arguments cons {X} _ _.  (* use underscore for argument position that has no name *)
Arguments length {X} l.
Arguments app {X} l1 l2.
Arguments rev {X} l.
Arguments snoc {X} l v.

(* note: no _ arguments required... *)
Definition list123'' := cons 1 (cons 2 (cons 3 nil)).
Check (length list123'').

(** *** *)

(** Alternatively, we can declare an argument to be implicit while
    defining the function itself, by surrounding the argument in curly
    braces.  For example: *)

Fixpoint length'' {X:Type} (l:list X) : nat :=
  match l with
  | nil      => 0
  | cons h t => S (length'' t)
  end.
Check length'' (cons 2 (nil)).
Eval compute in length'' (cons 2 (cons 1 (nil))).
(** (Note that we didn't even have to provide a type argument to
    the recursive call to [length'']; indeed, it is invalid to provide
    one.)  We will use this style whenever possible, although we will
    continue to use use explicit [Argument] declarations for
    [Inductive] constructors. *)

(** *** *)

(** One small problem with declaring arguments [Implicit] is
    that, occasionally, Coq does not have enough local information to
    determine a type argument; in such cases, we need to tell Coq that
    we want to give the argument explicitly this time, even though
    we've globally declared it to be [Implicit].  For example, suppose we
    write this: *)

(* Definition mynil := nil.  *)

(** If we uncomment this definition, Coq will give us an error,
    because it doesn't know what type argument to supply to [nil].  We
    can help it by providing an explicit type declaration (so that Coq
    has more information available when it gets to the "application"
    of [nil]): *)

Definition mynil : list nat := nil.

(** Alternatively, we can force the implicit arguments to be explicit by
   prefixing the function name with [@]. *)
Check nil.
Check @nil.

Definition mynil' := @nil nat.

(** *** *)
(** Using argument synthesis and implicit arguments, we can
    define convenient notation for lists, as before.  Since we have
    made the constructor type arguments implicit, Coq will know to
    automatically infer these when we use the notations. *)

Notation "x :: y" := (cons x y)
                     (at level 60, right associativity).
Notation "[ ]" := nil.
Notation "[ x ; .. ; y ]" := (cons x .. (cons y []) ..).
Notation "x ++ y" := (app x y)
                     (at level 60, right associativity).

(** Now lists can be written just the way we'd hope: *)

Definition list123''' := [1; 2; 3].

(* ###################################################### *)
(** *** Exercises: Polymorphic Lists *)

(** **** Exercise: 2 stars, optional (poly_exercises)  *)
(** Here are a few simple exercises, just like ones in the [Lists]
    chapter, for practice with polymorphism.  Fill in the definitions
    and complete the proofs below. *)

Fixpoint repeat {X : Type} (n : X) (count : nat) : list X :=
match count with
|0 => []
|S count' => n :: (repeat n count')
end.
Eval compute in (repeat true 2).
Eval compute in (repeat 1 2).
Example test_repeat1:
  repeat true 2 = cons true (cons true nil).
Proof. reflexivity. Qed.

Theorem nil_app : forall X:Type, forall l:list X,
  app [] l = l.
Proof.
  intros.
  simpl.
  reflexivity.
Qed.

Theorem rev_snoc : forall X : Type,
                     forall v : X,
                     forall s : list X,
  rev (snoc s v) = v :: (rev s).
Proof.
  intros.
  induction s.
  simpl. reflexivity.
  simpl.
  rewrite -> IHs.
  simpl. reflexivity.
Qed.

Theorem rev_involutive : forall X : Type, forall l : list X,
  rev (rev l) = l.
Proof.
  intros.
  induction l.
  simpl. reflexivity.
  simpl.
  rewrite -> rev_snoc.
  rewrite -> IHl.
  reflexivity.
Qed.

Theorem snoc_with_append : forall X : Type,
                         forall l1 l2 : list X,
                         forall v : X,
  snoc (l1 ++ l2) v = l1 ++ (snoc l2 v).
Proof.
  intros.
  induction l1.
  simpl. reflexivity.
  simpl.
  rewrite -> IHl1.
  reflexivity.
Qed.
(** [] *)

(* ###################################################### *)
(** ** Polymorphic Pairs *)

(** Following the same pattern, the type definition we gave in
    the last chapter for pairs of numbers can be generalized to
    _polymorphic pairs_ (or _products_): *)

Inductive prod (X Y : Type) : Type :=
  pair : X -> Y -> prod X Y.

Arguments pair {X} {Y} _ _.

(** As with lists, we make the type arguments implicit and define the
    familiar concrete notation. *)

Notation "( x , y )" := (pair x y).

(** We can also use the [Notation] mechanism to define the standard
    notation for pair _types_: *)

Notation "X * Y" := (prod X Y) : type_scope.

(** (The annotation [: type_scope] tells Coq that this abbreviation
    should be used when parsing types.  This avoids a clash with the
    multiplication symbol.) *)

(** *** *)
(** A note of caution: it is easy at first to get [(x,y)] and
    [X*Y] confused.  Remember that [(x,y)] is a _value_ built from two
    other values; [X*Y] is a _type_ built from two other types.  If
    [x] has type [X] and [y] has type [Y], then [(x,y)] has type
    [X*Y]. *)

(** The first and second projection functions now look pretty
    much as they would in any functional programming language. *)

Definition fst {X Y : Type} (p : X * Y) : X :=
  match p with (x,y) => x end.
Eval compute in (fst (1,true)).

Definition snd {X Y : Type} (p : X * Y) : Y :=
  match p with (x,y) => y end.

(** The following function takes two lists and combines them
    into a list of pairs.  In many functional programming languages,
    it is called [zip].  We call it [combine] for consistency with
    Coq's standard library. *)
(** Note that the pair notation can be used both in expressions and in
    patterns... *)

Fixpoint combine {X Y : Type} (lx : list X) (ly : list Y)
           : list (X*Y) :=
  match (lx,ly) with
  | ([],_) => []
  | (_,[]) => []
  | (x::tx, y::ty) => (x,y) :: (combine tx ty)
  end.
Check combine [1;2] [true;false].
Eval compute in (combine [1] [true]).

(** **** Exercise: 1 star, optional (combine_checks)  *)
(** Try answering the following questions on paper and
    checking your answers in coq:
    - What is the type of [combine] (i.e., what does [Check
      @combine] print?)
    - What does
        Eval compute in (combine [1;2] [false;false;true;true]).
      print?   []
*)

(** **** Exercise: 2 stars (split)  *)
(** The function [split] is the right inverse of combine: it takes a
    list of pairs and returns a pair of lists.  In many functional
    programing languages, this function is called [unzip].

    Uncomment the material below and fill in the definition of
    [split].  Make sure it passes the given unit tests. *)

Fixpoint split
           {X Y : Type} (l : list (X*Y))
           : (list X) * (list Y) :=
match l with
| [] => ([],[])
| (h1,h2) :: t => (h1::fst (split t),h2::snd (split t))
end.
Eval compute in split ([(1,false);(2,true)]).
Example test_split:
  split [(1,false);(2,false)] = ([1;2],[false;false]).
Proof.
  intros. reflexivity. Qed.
(** [] *)

(* ###################################################### *)
(** ** Polymorphic Options *)

(** One last polymorphic type for now: _polymorphic options_.
    The type declaration generalizes the one for [natoption] in the
    previous chapter: *)

Inductive option (X:Type) : Type :=
  | Some : X -> option X
  | None : option X.

Arguments Some {X} _.
Arguments None {X}.

(** *** *)
(** We can now rewrite the [index] function so that it works
    with any type of lists. *)

Fixpoint index {X : Type} (n : nat)
               (l : list X) : option X :=
  match l with
  | [] => None
  | a :: l' => if beq_nat n O then Some a else index (pred n) l'
  end.
Eval compute in index 2 [1;2;3;4].

Example test_index1 :    index 0 [4;5;6;7]  = Some 4.
Proof. reflexivity.  Qed.
Example test_index2 :    index  1 [[1];[2]]  = Some [2].
Proof. reflexivity.  Qed.
Example test_index3 :    index  2 [true]  = None.
Proof. reflexivity.  Qed.

(** **** Exercise: 1 star, optional (hd_opt_poly)  *)
(** Complete the definition of a polymorphic version of the
    [hd_opt] function from the last chapter. Be sure that it
    passes the unit tests below. *)

Definition hd_opt {X : Type} (l : list X)  : option X :=
match l with
|[]=>None
|h::t=>Some h
end.
(** Once again, to force the implicit arguments to be explicit,
    we can use [@] before the name of the function. *)
Check @hd_opt.

Example test_hd_opt1 :  hd_opt [1;2] = Some 1.
Proof. reflexivity. Qed.
Example test_hd_opt2 :   hd_opt  [[1];[2]]  = Some [1].
Proof. reflexivity. Qed.
(** [] *)

(* ###################################################### *)
(** * Functions as Data *)
(* ###################################################### *)
(** ** Higher-Order Functions *)

(** Like many other modern programming languages -- including
    all _functional languages_ (ML, Haskell, Scheme, etc.) -- Coq
    treats functions as first-class citizens, allowing functions to be
    passed as arguments to other functions, returned as results,
    stored in data structures, etc.

    Functions that manipulate other functions are often called
    _higher-order_ functions.  Here's a simple one: *)

Definition doit3times {X:Type} (f:X->X) (n:X) : X :=
  f (f (f n)).
Eval compute in doit3times (fun n => n*3) 3.
(** The argument [f] here is itself a function (from [X] to
    [X]); the body of [doit3times] applies [f] three times to some
    value [n]. *)

Check @doit3times.
(* ===> doit3times : forall X : Type, (X -> X) -> X -> X *)

Eval compute in (doit3times negb false).
Example test_doit3times: doit3times minustwo 9 = 3.
Proof. reflexivity.  Qed.

Example test_doit3times': doit3times negb true = false.
Proof. reflexivity.  Qed.

(* ###################################################### *)
(** ** Partial Application *)

(** In fact, the multiple-argument functions we have already
    seen are also examples of passing functions as data.  To see why,
    recall the type of [plus]. *)

Check plus.
(* ==> nat -> nat -> nat *)

(** Each [->] in this expression is actually a _binary_ operator
    on types.  (This is the same as saying that Coq primitively
    supports only one-argument functions -- do you see why?)  This
    operator is _right-associative_, so the type of [plus] is really a
    shorthand for [nat -> (nat -> nat)] -- i.e., it can be read as
    saying that "[plus] is a one-argument function that takes a [nat]
    and returns a one-argument function that takes another [nat] and
    returns a [nat]."  In the examples above, we have always applied
    [plus] to both of its arguments at once, but if we like we can
    supply just the first.  This is called _partial application_. *)

Definition plus3 := plus 3.
Check plus3.

Example test_plus3 :    plus3 4 = 7.
Proof. reflexivity.  Qed.
Example test_plus3' :   doit3times plus3 0 = 9.
Proof. reflexivity.  Qed.
Example test_plus3'' :  doit3times (plus 3) 0 = 9.
Proof. reflexivity.  Qed.

(* ###################################################### *)
(** ** Digression: Currying *)

(** **** Exercise: 2 stars, advanced (currying)  *)
(** In Coq, a function [f : A -> B -> C] really has the type [A
    -> (B -> C)].  That is, if you give [f] a value of type [A], it
    will give you function [f' : B -> C].  If you then give [f'] a
    value of type [B], it will return a value of type [C].  This
    allows for partial application, as in [plus3].  Processing a list
    of arguments with functions that return functions is called
    _currying_, in honor of the logician Haskell Curry.

    Conversely, we can reinterpret the type [A -> B -> C] as [(A *
    B) -> C].  This is called _uncurrying_.  With an uncurried binary
    function, both arguments must be given at once as a pair; there is
    no partial application. *)

(** We can define currying as follows: *)

Definition prod_curry {X Y Z : Type}
  (f : X * Y -> Z) (x : X) (y : Y) : Z := f (x, y).

(** As an exercise, define its inverse, [prod_uncurry].  Then prove
    the theorems below to show that the two are inverses. *)

Definition prod_uncurry {X Y Z : Type}
  (f : X -> Y -> Z) (p : X * Y) : Z := f (fst p) (snd p).

(** (Thought exercise: before running these commands, can you
    calculate the types of [prod_curry] and [prod_uncurry]?) *)

Check @prod_curry.
Check @prod_uncurry.

Theorem uncurry_curry : forall (X Y Z : Type) (f : X -> Y -> Z) x y,
  prod_curry (prod_uncurry f) x y = f x y.
Proof.
  intros.
  reflexivity. Qed.

Theorem curry_uncurry : forall (X Y Z : Type)
                               (f : (X * Y) -> Z) (p : X * Y),
  prod_uncurry (prod_curry f) p = f p.
Proof.
  intros.
  destruct p.
  reflexivity.
Qed.
(** [] *)

(* ###################################################### *)
(** ** Filter *)

(** Here is a useful higher-order function, which takes a list
    of [X]s and a _predicate_ on [X] (a function from [X] to [bool])
    and "filters" the list, returning a new list containing just those
    elements for which the predicate returns [true]. *)

Fixpoint filter {X:Type} (test: X->bool) (l:list X)
                : (list X) :=
  match l with
  | []     => []
  | h :: t => if test h then h :: (filter test t)
                        else       filter test t
  end.
Eval compute in (filter oddb [1;2;3]).
(** For example, if we apply [filter] to the predicate [evenb]
    and a list of numbers [l], it returns a list containing just the
    even members of [l]. *)

Example test_filter1: filter evenb [1;2;3;4] = [2;4].
Proof. reflexivity.  Qed.

(** *** *)
Definition length_is_1 {X : Type} (l : list X) : bool :=
  beq_nat (length l) 1.

Example test_filter2:
    filter length_is_1
           [ [1; 2]; [3]; [4]; [5;6;7]; []; [8] ]
  = [ [3]; [4]; [8] ].
Proof. reflexivity.  Qed.

(** *** *)

(** We can use [filter] to give a concise version of the
    [countoddmembers] function from the [Lists] chapter. *)

Definition countoddmembers' (l:list nat) : nat :=
  length (filter oddb l).

Example test_countoddmembers'1:   countoddmembers' [1;0;3;1;4;5] = 4.
Proof. reflexivity.  Qed.
Example test_countoddmembers'2:   countoddmembers' [0;2;4] = 0.
Proof. reflexivity.  Qed.
Example test_countoddmembers'3:   countoddmembers' nil = 0.
Proof. reflexivity.  Qed.

(* ###################################################### *)
(** ** Anonymous Functions *)

(** It is a little annoying to be forced to define the function
    [length_is_1] and give it a name just to be able to pass it as an
    argument to [filter], since we will probably never use it again.
    Moreover, this is not an isolated example.  When using
    higher-order functions, we often want to pass as arguments
    "one-off" functions that we will never use again; having to give
    each of these functions a name would be tedious.

    Fortunately, there is a better way. It is also possible to
    construct a function "on the fly" without declaring it at the top
    level or giving it a name; this is analogous to the notation we've
    been using for writing down constant lists, natural numbers, and
    so on. *)

Eval compute in doit3times (fun n => n*2) 2.

Example test_anon_fun':
  doit3times (fun n => n * n) 2 = 256.
Proof. reflexivity.  Qed.

(** Here is the motivating example from before, rewritten to use
    an anonymous function. *)

Example test_filter2':
    filter (fun l => beq_nat (length l) 1)
           [ [1; 2]; [3]; [4]; [5;6;7]; []; [8] ]
  = [ [3]; [4]; [8] ].
Proof. reflexivity.  Qed.

(** **** Exercise: 2 stars (filter_even_gt7)  *)

(** Use [filter] (instead of [Fixpoint]) to write a Coq function
    [filter_even_gt7] that takes a list of natural numbers as input
    and returns a list of just those that are even and greater than
    7. *)

Definition filter_even_gt7 (l : list nat) : list nat :=
filter (fun n => andb (evenb n) (ble_nat 8 n)) l.

Example test_filter_even_gt7_1 :
  filter_even_gt7 [1;2;6;9;10;3;12;8] = [10;12;8].
Proof. reflexivity. Qed.
Example test_filter_even_gt7_2 :
  filter_even_gt7 [5;2;6;19;129] = [].
Proof. reflexivity. Qed.
(** [] *)

(** **** Exercise: 3 stars (partition)  *)
(** Use [filter] to write a Coq function [partition]:
  partition : forall X : Type,
              (X -> bool) -> list X -> list X * list X
   Given a set [X], a test function of type [X -> bool] and a [list
   X], [partition] should return a pair of lists.  The first member of
   the pair is the sublist of the original list containing the
   elements that satisfy the test, and the second is the sublist
   containing those that fail the test.  The order of elements in the
   two sublists should be the same as their order in the original
   list.
*)

Definition partition {X : Type} (test : X -> bool) (l : list X)
                     : list X * list X :=
(filter test l,filter (fun n => negb (test n)) l).

Eval compute in partition (fun x => evenb x) ([1;2;3;4;5;6]).

Example test_partition1: partition oddb [1;2;3;4;5] = ([1;3;5], [2;4]).
Proof. reflexivity. Qed.
Example test_partition2: partition (fun x => false) [5;9;0] = ([], [5;9;0]).
Proof. reflexivity. Qed.
(** [] *)

(* ###################################################### *)
(** ** Map *)

(** Another handy higher-order function is called [map]. *)

Fixpoint map {X Y:Type} (f:X->Y) (l:list X)
             : (list Y) :=
  match l with
  | []     => []
  | h :: t => (f h) :: (map f t)
  end.

(** *** *)
(** It takes a function [f] and a list [ l = [n1, n2, n3, ...] ]
    and returns the list [ [f n1, f n2, f n3,...] ], where [f] has
    been applied to each element of [l] in turn.  For example: *)

Example test_map1: map (plus 3) [2;0;2] = [5;3;5].
Proof. reflexivity.  Qed.

(** The element types of the input and output lists need not be
    the same ([map] takes _two_ type arguments, [X] and [Y]).  This
    version of [map] can thus be applied to a list of numbers and a
    function from numbers to booleans to yield a list of booleans: *)

Example test_map2: map oddb [2;1;2;5] = [false;true;false;true].
Proof. reflexivity.  Qed.

(** It can even be applied to a list of numbers and
    a function from numbers to _lists_ of booleans to
    yield a list of lists of booleans: *)

Example test_map3:
    map (fun n => [evenb n;oddb n]) [2;1;2;5]
  = [[true;false];[false;true];[true;false];[false;true]].
Proof. reflexivity.  Qed.

(** ** Map for options *)
(** **** Exercise: 3 stars (map_rev)  *)
(** Show that [map] and [rev] commute.  You may need to define an
    auxiliary lemma. *)
Lemma map_snoc: forall (X Y:Type) (f:X->Y) (l:list X) (x: X),
map f (snoc l x) = snoc (map f l) (f x).
Proof.
  intros.
  induction l.
  simpl. reflexivity.
  simpl.
  rewrite -> IHl.
  reflexivity.
Qed.
Theorem map_rev : forall (X Y : Type) (f : X -> Y) (l : list X),
  map f (rev l) = rev (map f l).
Proof.
  intros.
  induction l.
  simpl. reflexivity.
  simpl.
  rewrite -> map_snoc.
  rewrite -> IHl.
  reflexivity.
Qed.
(** [] *)

(** **** Exercise: 2 stars (flat_map)  *)
(** The function [map] maps a [list X] to a [list Y] using a function
    of type [X -> Y].  We can define a similar function, [flat_map],
    which maps a [list X] to a [list Y] using a function [f] of type
    [X -> list Y].  Your definition should work by 'flattening' the
    results of [f], like so:
        flat_map (fun n => [n;n+1;n+2]) [1;5;10]
      = [1; 2; 3; 5; 6; 7; 10; 11; 12].
*)

Fixpoint flat_map {X Y:Type} (f:X -> list Y) (l:list X)
                   : (list Y) :=
match l with
|[] => []
|h::t=>(f h) ++ (flat_map f t)
end.

Example test_flat_map1:
  flat_map (fun n => [n;n;n]) [1;5;4]
  = [1; 1; 1; 5; 5; 5; 4; 4; 4].
Proof. reflexivity. Qed.
(** [] *)

(** Lists are not the only inductive type that we can write a
    [map] function for.  Here is the definition of [map] for the
    [option] type: *)

Definition option_map {X Y : Type} (f : X -> Y) (xo : option X)
                      : option Y :=
  match xo with
    | None => None
    | Some x => Some (f x)
  end.

(** **** Exercise: 2 stars, optional (implicit_args)  *)
(** The definitions and uses of [filter] and [map] use implicit
    arguments in many places.  Replace the curly braces around the
    implicit arguments with parentheses, and then fill in explicit
    type parameters where necessary and use Coq to check that you've
    done so correctly.  (This exercise is not to be turned in; it is
    probably easiest to do it on a _copy_ of this file that you can
    throw away afterwards.)  [] *)

(* ###################################################### *)
(** ** Fold *)

(** An even more powerful higher-order function is called
    [fold].  This function is the inspiration for the "[reduce]"
    operation that lies at the heart of Google's map/reduce
    distributed programming framework. *)

Fixpoint fold {X Y:Type} (f: X->Y->Y) (l:list X) (b:Y) : Y :=
  match l with
  | nil => b
  | h :: t => f h (fold f t b)
  end.

(** *** *)

(** Intuitively, the behavior of the [fold] operation is to
    insert a given binary operator [f] between every pair of elements
    in a given list.  For example, [ fold plus [1;2;3;4] ] intuitively
    means [1+2+3+4].  To make this precise, we also need a "starting
    element" that serves as the initial second input to [f].  So, for
    example,
   fold plus [1;2;3;4] 0
    yields
   1 + (2 + (3 + (4 + 0))).
    Here are some more examples:
*)

Check (fold andb).
(* ===> fold andb : list bool -> bool -> bool *)
Eval compute in (fold mult [2;3;4] 2).
Example fold_example1 : fold mult [1;2;3;4] 1 = 24.
Proof. reflexivity. Qed.

Example fold_example2 : fold andb [true;true;false;true] true = false.
Proof. reflexivity. Qed.

Eval compute in (fold app [[1];[];[2;3];[4]] [2]).
Example fold_example3 : fold app  [[1];[];[2;3];[4]] [] = [1;2;3;4].
Proof. reflexivity. Qed.

(** **** Exercise: 1 star, advanced (fold_types_different)  *)
(** Observe that the type of [fold] is parameterized by _two_ type
    variables, [X] and [Y], and the parameter [f] is a binary operator
    that takes an [X] and a [Y] and returns a [Y].  Can you think of a
    situation where it would be useful for [X] and [Y] to be
    different? *)

(* ###################################################### *)
(** ** Functions For Constructing Functions *)

(** Most of the higher-order functions we have talked about so
    far take functions as _arguments_.  Now let's look at some
    examples involving _returning_ functions as the results of other
    functions.

    To begin, here is a function that takes a value [x] (drawn from
    some type [X]) and returns a function from [nat] to [X] that
    yields [x] whenever it is called, ignoring its [nat] argument. *)

Definition constfun {X: Type} (x: X) : nat->X :=
  fun (k:nat) => x.

Definition ftrue := constfun true.

Example constfun_example1 : ftrue 0 = true.
Proof. reflexivity. Qed.

Example constfun_example2 : (constfun 5) 99 = 5.
Proof. reflexivity. Qed.
Example constfun_example3 : (constfun 5) 98 = 5.
Proof. reflexivity. Qed.

(** *** *)
(** Similarly, but a bit more interestingly, here is a function
    that takes a function [f] from numbers to some type [X], a number
    [k], and a value [x], and constructs a function that behaves
    exactly like [f] except that, when called with the argument [k],
    it returns [x]. *)

Definition override {X: Type} (f: nat->X) (k:nat) (x:X) : nat->X:=
  fun (k':nat) => if beq_nat k k' then x else f k'.

(** For example, we can apply [override] twice to obtain a
    function from numbers to booleans that returns [false] on [1] and
    [3] and returns [true] on all other arguments. *)
Eval compute in (ftrue).
Eval compute in (override ftrue 1 false).

Definition fmostlytrue := override (override ftrue 1 false) 3 false.

(** *** *)

Example override_example1 : fmostlytrue 0 = true.
Proof. reflexivity. Qed.

Example override_example2 : fmostlytrue 1 = false.
Proof. reflexivity. Qed.

Example override_example3 : fmostlytrue 2 = true.
Proof. reflexivity. Qed.

Example override_example4 : fmostlytrue 3 = false.
Proof. reflexivity. Qed.

(** *** *)

(** **** Exercise: 1 star (override_example)  *)
(** Before starting to work on the following proof, make sure you
    understand exactly what the theorem is saying and can paraphrase
    it in your own words.  The proof itself is straightforward. *)

Theorem override_example : forall (b:bool),
  (override (constfun b) 3 true) 2 = b.
Proof.
  intros.
  induction b.
  simpl. reflexivity.
  simpl. reflexivity.
Qed.
(** [] *)

(** We'll use function overriding heavily in parts of the rest of the
    course, and we will end up needing to know quite a bit about its
    properties.  To prove these properties, though, we need to know
    about a few more of Coq's tactics; developing these is the main
    topic of the next chapter.  For now, though, let's introduce just
    one very useful tactic that will also help us with proving
    properties of some of the other functions we have introduced in
    this chapter. *)

(* ###################################################### *)

(* ###################################################### *)
(** * The [unfold] Tactic *)

(** Sometimes, a proof will get stuck because Coq doesn't
    automatically expand a function call into its definition.  (This
    is a feature, not a bug: if Coq automatically expanded everything
    possible, our proof goals would quickly become enormous -- hard to
    read and slow for Coq to manipulate!) *)

Theorem unfold_example_bad : forall m n,
  3 + n = m ->
  plus3 n + 1 = m + 1.
Proof.
  intros m n H.
(* At this point, we'd like to do [rewrite -> H], since
     [plus3 n] is definitionally equal to [3 + n].  However,
     Coq doesn't automatically expand [plus3 n] to its
     definition. *)
  Abort.

(** The [unfold] tactic can be used to explicitly replace a
    defined name by the right-hand side of its definition.  *)

Theorem unfold_example : forall m n,
  3 + n = m ->
  plus3 n + 1 = m + 1.
Proof.
  intros m n H.
  unfold plus3.
  rewrite -> H.
  reflexivity.  Qed.

(** Now we can prove a first property of [override]: If we
    override a function at some argument [k] and then look up [k], we
    get back the overridden value. *)

Theorem override_eq : forall {X:Type} x k (f:nat->X),
  (override f k x) k = x.
Proof.
  intros X x k f.
  unfold override.
  rewrite <- beq_nat_refl.
  reflexivity.  Qed.

(** This proof was straightforward, but note that it requires
    [unfold] to expand the definition of [override]. *)

(** **** Exercise: 2 stars (override_neq)  *)
Theorem override_neq : forall (X:Type) x1 x2 k1 k2 (f : nat->X),
  f k1 = x1 ->
  beq_nat k2 k1 = false ->
  (override f k2 x2) k1 = x1.
Proof.
  intros.
  unfold override.
  rewrite H0.
  rewrite H.
  reflexivity.
Qed.
(** [] *)

(** As the inverse of [unfold], Coq also provides a tactic
    [fold], which can be used to "unexpand" a definition.  It is used
    much less often. *)

(* ##################################################### *)
(** * Additional Exercises *)

(** **** Exercise: 2 stars (fold_length)  *)
(** Many common functions on lists can be implemented in terms of
   [fold].  For example, here is an alternative definition of [length]: *)

Definition fold_length {X : Type} (l : list X) : nat :=
  fold (fun _ n => S n) l 0.

Example test_fold_length1 : fold_length [4;7;0] = 3.
Proof. reflexivity. Qed.

(** Prove the correctness of [fold_length]. *)

Theorem fold_length_correct : forall X (l : list X),
  fold_length l = length l.
Proof.
  intros.
  induction l.
  unfold fold_length.
  simpl. reflexivity.
  simpl.
  rewrite <- IHl.
  unfold fold_length.
  simpl. reflexivity.
Qed.
(** [] *)

(** **** Exercise: 3 stars (fold_map)  *)
(** We can also define [map] in terms of [fold].  Finish [fold_map]
    below. *)

Definition fold_map {X Y:Type} (f : X -> Y) (l : list X) : list Y :=
fold (fun x l' => (f x) :: l') l [].

(** Write down a theorem [fold_map_correct] in Coq stating that
   [fold_map] is correct, and prove it. *)
Theorem fold_map_correct:forall X Y (l:list X) (f:X->Y),
fold_map f l = map f l.
Proof.
  intros.
  induction l.
  unfold fold_map.
  simpl. reflexivity.
  simpl.
  rewrite <- IHl.
  unfold fold_map.
  simpl.
  reflexivity.
Qed.
(* FILL IN HERE *)
(** [] *)

(** **** Exercise: 2 stars, advanced (index_informal)  *)
(** Recall the definition of the [index] function:
   Fixpoint index {X : Type} (n : nat) (l : list X) : option X :=
     match l with
     | [] => None
     | a :: l' => if beq_nat n O then Some a else index (pred n) l'
     end.
   Write an informal proof of the following theorem:
   forall X n l, length l = n -> @index X n l = None.
(* FILL IN HERE *)
*)
(** [] *)

(** **** Exercise: 4 stars, advanced (church_numerals)  *)

Module Church.

(** In this exercise, we will explore an alternative way of defining
    natural numbers, using the so-called _Church numerals_, named
    after mathematician Alonzo Church. We can represent a natural
    number [n] as a function that takes a function [f] as a parameter
    and returns [f] iterated [n] times. More formally, *)

Definition nat := forall X : Type, (X -> X) -> X -> X.

(** Let's see how to write some numbers with this notation. Any
    function [f] iterated once shouldn't change. Thus, *)

Definition one : nat :=
  fun (X : Type) (f : X -> X) (x : X) => f x.

Eval compute in one.
(** [two] should apply [f] twice to its argument: *)

Definition two : nat :=
  fun (X : Type) (f : X -> X) (x : X) => f (f x).

(** [zero] is somewhat trickier: how can we apply a function zero
    times? The answer is simple: just leave the argument untouched. *)

Definition zero : nat :=
  fun (X : Type) (f : X -> X) (x : X) => x.

(** More generally, a number [n] will be written as [fun X f x => f (f
    ... (f x) ...)], with [n] occurrences of [f]. Notice in particular
    how the [doit3times] function we've defined previously is actually
    just the representation of [3]. *)

Definition three : nat := @doit3times.

(** Complete the definitions of the following functions. Make sure
    that the corresponding unit tests pass by proving them with
    [reflexivity]. *)

(** Successor of a natural number *)

Definition succ (n : nat) : nat :=
  fun X f z => (f (n X f z)).

Example succ_1 : succ zero = one.
Proof. reflexivity. Qed.

Example succ_2 : succ one = two.
Proof. reflexivity. Qed.

Example succ_3 : succ two = three.
Proof. reflexivity. Qed.

(** Addition of two natural numbers *)

Definition plus (n m : nat) : nat :=
fun (X:Type) (f:X->X) (x:X) => m X f (n X f x).

Example plus_1 : plus zero one = one.
Proof. reflexivity. Qed.

Example plus_2 : plus two three = plus three two.
Proof. reflexivity. Qed.

Example plus_3 :
  plus (plus two two) three = plus one (plus three three).
Proof. reflexivity. Qed.

(** Multiplication *)

Definition mult (n m : nat) : nat :=
fun (X:Type) (f:X->X) => m X (n X f).

Example mult_1 : mult one one = one.
Proof. reflexivity. Qed.

Example mult_2 : mult zero (plus three three) = zero.
Proof. reflexivity. Qed.

Example mult_3 : mult two three = plus three three.
Proof. reflexivity. Qed.
(** Exponentiation *)

(** Hint: Polymorphism plays a crucial role here. However, choosing
    the right type to iterate over can be tricky. If you hit a
    "Universe inconsistency" error, try iterating over a different
    type: [nat] itself is usually problematic. *)

Definition exp (n m : nat) : nat :=
fun (X:Type) => m (X->X) (n X).

Example exp_1 : exp two two = plus two two.
Proof. reflexivity. Qed.

Example exp_2 : exp three two = plus (mult two (mult two two)) one.
Proof. reflexivity. Qed.

Example exp_3 : exp three zero = one.
Proof. reflexivity. Qed.

End Church.

(** [] *)

(** $Date: 2014-12-31 11:17:56 -0500 (Wed, 31 Dec 2014) $ *)


(* Copyright (C) 2005-2008 Sebastien Briais *)
(* http://lamp.epfl.ch/~sbriais/ *)

(* This library is free software; you can redistribute it and/or modify *)
(* it under the terms of the GNU Lesser General Public License as *)
(* published by the Free Software Foundation; either version 2.1 of the *)
(* License, or (at your option) any later version. *)

(* This library is distributed in the hope that it will be useful, but *)
(* WITHOUT ANY WARRANTY; without even the implied warranty of *)
(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU *)
(* Lesser General Public License for more details. *)

(* You should have received a copy of the GNU Lesser General Public *)
(* License along with this library; if not, write to the Free Software *)
(* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA *)
(* 02110-1301 USA *)

Require Export Arith.
Require Export ArithRing.
Require Export Omega.

Unset Standard Proposition Elimination Names.

(** We first begin with some lemmas that relates *)
(** +, * and - that are not in the standard library *)
Lemma mult_lemma1 : forall (n m:nat),(n <> O)->(m <> 0)->(n <= n*m).
  intros.
  rewrite mult_comm.
  induction m;simpl;auto with arith.
  elim H0;trivial.
Qed.

Lemma mult_lemma2 : forall (n m:nat),(n*m = O)->(n=O)\/(m=O).
  intros.
  induction n.
  tauto.
  simpl in H.
  right.
  assert (m <= O);try omega.
  rewrite <- H.
  auto with arith.
Qed.

Lemma mult_lemma3 : forall (n m:nat),(n <> O)->(m > 1)->(n < n*m).
  intros.
  rewrite mult_comm.
  induction m.
  inversion H0.
  simpl.
  assert (O < m*n);try omega.
  inversion H0;try omega.
  assert (1 <= n);try omega.
  assert (m > 1);try omega.
  generalize (IHm H4);omega.
Qed.

Lemma mult_lemma4 : forall (n m:nat),n=n*m -> n=O \/ m=1.
  intros n m.
  case n.
  left;trivial.
  intros.
  right.
  destruct m.
  rewrite mult_comm in H.
  discriminate.
  destruct m;trivial.
  assert ((S n0)<(S n0)*(S (S m))).
  apply mult_lemma3;intros;auto with arith.
  rewrite <- H in H0.
  elim (lt_irrefl (S n0) H0).
Qed.

Lemma mult_lemma5 : forall (n m:nat),((n * m) =1)->(n=1)/\(m=1).
  induction n;simpl;intros;try discriminate.
  induction m.
  rewrite mult_comm in H.
  simpl in H;discriminate.
  assert ((S n)<=((S n)*(S m))).
  apply mult_lemma1;discriminate.
  assert (((S n)*(S m))=((S m)+n*(S m))).
  reflexivity.
  rewrite H1 in H0.
  rewrite H in H0.
  assert ((S n)=1).
  omega.
  split;trivial.
  inversion H2.
  rewrite H4 in H.
  simpl in H.
  omega.
Qed.

Lemma plus_minus_lemma1 : forall (y x:nat),(x+y-y=x).
  induction y;intros;rewrite plus_comm;simpl.
  auto with arith.
  rewrite plus_comm.
  apply IHy.
Qed.

Lemma mult_minus_lemma1 : forall (a n:nat),a*n-n = (a-1)*n.
  intros.
  induction a.
  simpl.
  trivial.
  replace (S a*n) with (n+a*n);try (auto with arith).
  rewrite plus_comm.
  rewrite plus_minus_lemma1.
  simpl.
  rewrite <- minus_n_O;trivial.
Qed.

Lemma mult_lemma6 : forall (a b n:nat),(n <> O)->(n*a=n*b)->(a=b).
  induction a.
  intros;rewrite <- mult_n_O in H0; generalize (mult_lemma2 n b); intros Hl2; elim Hl2; intros; (auto || elim H ; auto).
  intros b n H.
  rewrite mult_comm;simpl;rewrite mult_comm;intro.
  assert (n*a = n*b-n).
  apply plus_minus;auto.
  assert (a*n=(b-1)*n).
  rewrite <- mult_minus_lemma1;rewrite mult_comm;rewrite (mult_comm b n);trivial.
  assert (a=(b-1)).
  apply (IHa (b-1) n);trivial.
  rewrite mult_comm;rewrite (mult_comm n (b-1));trivial.
  destruct b;simpl in H3.
  rewrite H3 in H0;rewrite (mult_comm n 0) in H0;rewrite plus_comm in H0;simpl in H0;elim H;trivial.
  rewrite <- minus_n_O in H3;auto.
Qed.

Lemma mult_lemma7 : forall (x y z t:nat),x*y*(z*t)=z*(x*y*t).
  intros.
  ring.
Qed.

Lemma minus_lemma1 : forall (a b:nat),(S a-S b)<S a.
  intros.
  omega.
Qed.

Lemma minus_lemma2 : forall (n m:nat),(n<=m)->(n-m=O).
  intros.
  omega.
Qed.

Lemma mult_minus_lemma2 : forall (x y z:nat),(x*(y-z))=(x*y-x*z).
  intros.
  case (le_lt_dec y z);intro.
  rewrite (minus_lemma2 y z l);rewrite mult_comm;simpl;rewrite minus_lemma2;trivial;auto with arith.
  assert (y=z+(y-z)).
  rewrite <- (le_plus_minus z y);try (auto with arith).
  replace (x*y) with (x*(z+(y-z))).
  rewrite mult_plus_distr_l;rewrite minus_plus;trivial.
  rewrite <- H;trivial.
Qed.

Lemma plus_minus_lemma2 : forall (x y z:nat),(y<=x)->(x-y+z)=(x+z-y).
  intros.
  rewrite (le_plus_minus y x);try (auto with arith).
  rewrite minus_plus;rewrite <- plus_assoc;rewrite minus_plus;trivial.
Qed.

Lemma minus_minus_lemma1 : forall (x y z:nat),(z<=y)->(x-(y-z))=(x+z-y).
  intros.
  rewrite (le_plus_minus z y);trivial.
  rewrite minus_plus;rewrite plus_comm;rewrite <- minus_plus_simpl_l_reverse;trivial.
Qed.

Lemma minus_minus_lemma2 : forall (x y z:nat),(x-y-z)=(x-(y+z)).
  induction x;simpl;trivial.
  intros.
  case y;simpl;trivial.
Qed.

Lemma minus_lt_lemma1 : forall (b a:nat),(a<b)->(0<b-a).
  intros.
  omega.
Qed.


Require Import List.
Require Import Compare_dec.

Require Import Le Lt.
Require Import Plus Minus.

Definition ap A B (f:A->B) a1 a2 (p:a1 = a2) : f a1 = f a2 := match p with eq_refl => eq_refl _ end.
Implicit Arguments ap [A B a1 a2].

Definition cast (T1 T2:Type) (p:T1 = T2) : T1->T2 := match p with eq_refl => fun x=>x end.
Implicit Arguments cast [T1 T2].

Definition tr A (B:A->Type) a1 a2 (p:a1 = a2) : B a1->B a2 := match p with eq_refl => fun b=>b end.
Implicit Arguments tr [A a1 a2].

Definition Sinj n m : forall e:S n = S m,n = m := @ap _ _ pred _ _.
Implicit Arguments Sinj [n m].

Definition IsTrue b := match b with
	true => True |
	false => False
end.

Definition true_false : true <> false := fun e=>tr IsTrue e I.

Definition IsO n := match n with O => True | _ => False end.
Definition IsS n := match n with O => False | _ => True end.

Definition OUnfold n := match n return IsO n->(n = O) with
	O => fun _=>eq_refl _ |
	_ => fun z=>match z with end
end.
Implicit Arguments OUnfold [n].

Definition SUnfold n := match n return IsS n->(n = S (pred n)) with
	O => fun nz=>match nz with end |
	_ => fun _=>eq_refl _
end.
Implicit Arguments SUnfold [n].

Definition IsLt c := match c with Lt => True | _ => False end.
Definition IsEq c := match c with Eq => True | _ => False end.
Definition IsGt c := match c with Gt => True | _ => False end.

Definition ltd a b := IsLt (nat_compare a b).
Definition eqd a b := IsEq (nat_compare a b).
Definition gtd a b := IsGt (nat_compare a b).

Definition ltd_n_O n : ~ltd n O.
	destruct n;exact (fun x=>x).
Defined.
Implicit Arguments ltd_n_O [n].

Lemma lt_ltd l i : (i < l)->ltd i l.
	intro.
	unfold ltd.
	rewrite (proj1 (nat_compare_lt _ _) H).
	exact I.
Qed.
Implicit Arguments lt_ltd [l i].

Lemma not_lt_ltd l i : ~(i < l)->ltd l (S i).
	intro.
	apply lt_ltd.
	apply le_n_S.
	exact (not_lt _ _ H).
Qed.
Implicit Arguments not_lt_ltd [l i].

Fixpoint gtd_ltd a b : gtd a b = ltd b a := match a,b with
	O,O => eq_refl False |
	O,S _ => eq_refl False |
	S _,O => eq_refl True |
	S a,S b => gtd_ltd a b
end.

Fixpoint eqd_eq a b : eqd a b->(a = b) := match a,b with
	O,O => fun x:True=>eq_refl O |
	O,S _ => fun x:False=>match x with end |
	S _,O => fun x:False=>match x with end |
	S a,S b => fun p:eqd a b=>ap S (eqd_eq a b p)
end.
Implicit Arguments eqd_eq [a b].

(* Like rev_append, but moves at most s elements *)
Fixpoint unzip T s (l l':list T) := match s with
	O => l' |
	S s => match l with
		nil => l' |
		a :: l => unzip T s l (a :: l')
	end
end.
Implicit Arguments unzip [T].

Lemma minus_n_Sm n : forall m,pred (n - m) = n - S m.
	induction n;intro.

	reflexivity.

	destruct m.
		apply minus_n_O.

		apply IHn.
Qed.

Lemma le_plus_minus_assoc a b c : (c <= b)->(a + (b - c) = a + b - c).
	intro.
	apply plus_reg_l with c.
	rewrite <- le_plus_minus;[| apply le_trans with (1 := H);apply le_plus_r].
	rewrite plus_assoc.
	rewrite <- (plus_comm a).
	rewrite <- plus_assoc.
	rewrite <- le_plus_minus with (1 := H).
	reflexivity.
Qed.

Lemma length_tl T (l:list T) : length (tl l) = pred (length l).
	destruct l;reflexivity.
Qed.

Lemma map_ext_In A B (f g:A->B) l : (forall a,In a l->(f a = g a))->(map f l = map g l).
	induction l;intro.

	reflexivity.

	simpl in H |- *.
	rewrite <- (H _ (or_introl _ (eq_refl _))).
	rewrite <- IHl.
		reflexivity.
	intros.
	exact (H _ (or_intror _ H0)).
Qed.

Lemma skipnSkipAll T (l:list T) : skipn (length l) l = nil.
	induction l.

	reflexivity.

	exact IHl.
Qed.

Lemma skipn_consNth T n (d:T) : forall l,(n < length l)->(nth n l d :: skipn (S n) l = skipn n l).
	induction n;intro;(destruct l;intro;[destruct lt_n_O with (1 := H) |]).

	reflexivity.

	apply IHn.
	apply lt_S_n with (1 := H).
Qed.

Lemma seq_In n l : forall s,In n (seq s l)->(s <= n < s + l).
	induction l;intros;simpl in H.

	destruct H.

	rewrite <- plus_n_Sm.
	destruct H.
		rewrite <- H.
		apply conj with (1 := le_n _).
		apply (le_plus_l (S s)).

		destruct (IHl _ H).
		apply conj with (1 := lt_le_weak _ _ H0).
		exact H1.
Qed.

Lemma unzip_length T n : forall l l':list T,(n <= length l)->(length (unzip n l l') = n + length l').
	induction n;intros ? ?.

	intro.
	reflexivity.

	destruct l;intro.
		destruct le_Sn_O with (1 := H).
	simpl in H |- *.
	rewrite plus_n_Sm.
	apply IHn.
	apply le_S_n with (1 := H).
Qed.

Lemma unzip_consNth T n (d:T) : forall l l',(n < length l)->(nth n l d :: unzip n l l' = unzip (S n) l l').
	induction n;intros ? ?;(destruct l;intro;[destruct lt_n_O with (1 := H) |]).

	reflexivity.

	apply IHn.
	apply lt_S_n with (1 := H).
Qed.

Lemma unzip_nthHi T n l l' (d:T) : forall m,(n <= m)->(n <= length l)->(nth m (unzip n l l') d = nth (m - n) l' d).
	induction n;intro.

	intros.
	rewrite <- minus_n_O.
	reflexivity.

	destruct m;intro.
		destruct le_Sn_O with (1 := H).
	intro.
	rewrite <- unzip_consNth with (d := d) (1 := H0).
	simpl.
	apply IHn with (1 := le_S_n _ _ H) (2 := lt_le_weak _ _ H0).
Qed.

Lemma unzip_nthLo T n m (d:T) : (m < n)->forall l l',(n <= length l)->(nth m (unzip n l l') d = nth (n - S m) l d).
	intro.
	induction H as [| n];intros ? ?;(destruct l;intro Hl;simpl in Hl;[destruct le_Sn_O with (1 := Hl) |]).

	simpl unzip.
	simpl minus.
	rewrite <- minus_n_n.
	change (nth O (t :: l) d) with (nth O (t :: l') d).
	rewrite (minus_n_n m).
	apply unzip_nthHi with (1 := le_n _) (2 := le_S_n _ _ Hl).

	rewrite <- minus_Sn_m with (1 := H).
	simpl.
	apply IHle with (1 := le_S_n _ _ Hl).
Qed.