minmax.v 15.4 KB
Newer Older
1
Require Import rt.util.tactics rt.util.notation rt.util.sorting rt.util.nat rt.util.list.
2 3 4 5
From mathcomp Require Import ssreflect ssrbool eqtype ssrnat seq fintype bigop.

Section MinMaxSeq.

6
  Section ArgGeneric.
7

8 9 10 11
    Context {T1 T2: eqType}.

    Variable rel: T2 -> T2 -> bool.
    Variable F: T1 -> T2.
12
    
13
    Fixpoint seq_argmin (l: seq T1) :=
14
      if l is x :: l' then
15 16
        if seq_argmin l' is Some y then
          if rel (F x) (F y) then Some x else Some y
17 18 19
        else Some x
      else None.

20
    Fixpoint seq_argmax (l: seq T1) :=
21
      if l is x :: l' then
22 23
        if seq_argmax l' is Some y then
          if rel (F y) (F x) then Some x else Some y
24 25
        else Some x
      else None.
26
    
27 28
    Section Lemmas.

29
      Lemma seq_argmin_exists:
30 31
        forall l x,
          x \in l ->
32
          seq_argmin l != None.
33 34 35 36
      Proof.
        induction l; first by done.
        intros x; rewrite in_cons.
        move => /orP [/eqP EQ | IN] /=;
37 38
          first by subst; destruct (seq_argmin l); first by case: ifP.
        by destruct (seq_argmin l); first by case: ifP.
39 40
      Qed.
        
41
      Lemma seq_argmin_in_seq:
42
        forall l x,
43
          seq_argmin l = Some x ->
44 45 46 47
          x \in l.
      Proof.
        induction l; simpl; first by done.
        intros x ARG.
48
        destruct (seq_argmin l);
49
          last by case: ARG => EQ; subst; rewrite in_cons eq_refl.
50
        destruct (rel (F a) (F s));
51 52 53 54 55
          first by case: ARG => EQ; subst; rewrite in_cons eq_refl.
        case: ARG => EQ; subst.
        by rewrite in_cons; apply/orP; right; apply IHl.
      Qed.

56
      Lemma seq_argmax_exists:
57 58
        forall l x,
          x \in l ->
59
          seq_argmax l != None.
60 61 62 63
      Proof.
        induction l; first by done.
        intros x; rewrite in_cons.
        move => /orP [/eqP EQ | IN] /=;
64 65
          first by subst; destruct (seq_argmax l); first by case: ifP.
        by destruct (seq_argmax l); first by case: ifP.
66 67
      Qed.
        
68
      Lemma seq_argmax_in_seq:
69
        forall l x,
70
          seq_argmax l = Some x ->
71 72 73 74
          x \in l.
      Proof.
        induction l; simpl; first by done.
        intros x ARG.
75
        destruct (seq_argmax l);
76
          last by case: ARG => EQ; subst; rewrite in_cons eq_refl.
77
        destruct (rel (F s) (F a));
78 79 80 81
          first by case: ARG => EQ; subst; rewrite in_cons eq_refl.
        case: ARG => EQ; subst.
        by rewrite in_cons; apply/orP; right; apply IHl.
      Qed.
82 83
      
      Section TotalOrder.
84

85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
        Hypothesis H_transitive: transitive rel.
        
        Variable l: seq T1.
        Hypothesis H_total_over_list:
          forall x y,
            x \in l ->
            y \in l ->
            rel (F x) (F y) || rel (F y) (F x).

        Lemma seq_argmin_computes_min:
          forall x y,
            seq_argmin l = Some x ->
            y \in l ->
            rel (F x) (F y).
        Proof.
          rename H_transitive into TRANS, H_total_over_list into TOT, l into l'.
          induction l'; first by done.
          intros x y EQmin IN; simpl in EQmin.
          rewrite in_cons in IN.
          move: IN => /orP [/eqP EQ | IN].
105
          {
106 107 108 109 110 111 112 113 114 115 116 117 118
            subst; destruct (seq_argmin l') eqn:ARG; last first.
            {
              case: EQmin => EQ; subst.
              by exploit (TOT x x); try (by rewrite in_cons eq_refl); rewrite orbb.
            }
            {
              destruct (rel (F a) (F s)) eqn:REL; case: EQmin => EQ; subst;
                first by exploit (TOT x x); try (by rewrite in_cons eq_refl); rewrite orbb.
              exploit (TOT a x).
              - by rewrite in_cons eq_refl.
              - by rewrite in_cons; apply/orP; right; apply seq_argmin_in_seq.
              - by rewrite REL /=.
            }
119
          }
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
          { 
            destruct (seq_argmin l') eqn:ARG.
            {
              destruct (rel (F a) (F s)) eqn:REL; case: EQmin => EQ; subst; last first.
              {
                apply IHl'; [| by done | by done].
                by intros x0 y0 INx INy; apply TOT; rewrite in_cons; apply/orP; right.
              }
              {                
                apply TRANS with (y := F s); first by done.
                apply IHl'; [| by done | by done].
                by intros x0 y0 INx INy; apply TOT; rewrite in_cons; apply/orP; right.
              }
            }
            {
              case: EQmin => EQ; subst.
              by apply seq_argmin_exists in IN; rewrite ARG in IN.
            }
          }
        Qed.

        Lemma seq_argmax_computes_max:
          forall x y,
            seq_argmax l = Some x ->
            y \in l ->
            rel (F y) (F x).
        Proof.
          rename H_transitive into TRANS, H_total_over_list into TOT, l into l'.
          induction l'; first by done.
          intros x y EQmin IN; simpl in EQmin.
          rewrite in_cons in IN.
          move: IN => /orP [/eqP EQ | IN].
152
          {
153 154 155 156 157 158 159 160 161 162 163 164 165
            subst; destruct (seq_argmax l') eqn:ARG; last first.
            {
              case: EQmin => EQ; subst.
              by exploit (TOT x x); try (by rewrite in_cons eq_refl); rewrite orbb.
            }
            {
              destruct (rel (F s) (F a)) eqn:REL; case: EQmin => EQ; subst;
                first by exploit (TOT x x); try (by rewrite in_cons eq_refl); rewrite orbb.
              exploit (TOT a x).
              - by rewrite in_cons eq_refl.
              - by rewrite in_cons; apply/orP; right; apply seq_argmax_in_seq.
              - by rewrite REL orbF.
            }
166
          }
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 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 232 233
          { 
            destruct (seq_argmax l') eqn:ARG.
            {
              destruct (rel (F s) (F a)) eqn:REL; case: EQmin => EQ; subst; last first.
              {
                apply IHl'; [| by done | by done].
                by intros x0 y0 INx INy; apply TOT; rewrite in_cons; apply/orP; right.
              }
              {                
                apply TRANS with (y := F s); last by done.
                apply IHl'; [| by done | by done].
                by intros x0 y0 INx INy; apply TOT; rewrite in_cons; apply/orP; right.
              }
            }
            {
              case: EQmin => EQ; subst.
              by apply seq_argmax_exists in IN; rewrite ARG in IN.
            }
          }
        Qed.

      End TotalOrder.

    End Lemmas.

  End ArgGeneric.
  
  Section MinGeneric.

    Context {T: eqType}.
    Variable rel: rel T.
    
    Definition seq_min := seq_argmin rel id.
    Definition seq_max := seq_argmax rel id.

    Section Lemmas.

      Lemma seq_min_exists:
        forall l x,
          x \in l ->
          seq_min l != None.
      Proof.
        by apply seq_argmin_exists.
      Qed.
        
      Lemma seq_min_in_seq:
        forall l x,
          seq_min l = Some x ->
          x \in l.
      Proof.
        by apply seq_argmin_in_seq.
      Qed.

      Lemma seq_max_exists:
        forall l x,
          x \in l ->
          seq_max l != None.
      Proof.
        by apply seq_argmax_exists.
      Qed.
        
      Lemma seq_max_in_seq:
        forall l x,
          seq_max l = Some x ->
          x \in l.
      Proof.
        by apply seq_argmax_in_seq.
234 235
      Qed.
      
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 263 264 265 266
      Section TotalOrder.

        Hypothesis H_transitive: transitive rel.
        
        Variable l: seq T.
        Hypothesis H_total_over_list:
          forall x y,
            x \in l ->
            y \in l ->
            rel x y || rel y x.

        Lemma seq_min_computes_min:
          forall x y,
            seq_min l = Some x ->
            y \in l ->
            rel x y.
        Proof.
          by apply seq_argmin_computes_min.
        Qed.

        Lemma seq_max_computes_max:
          forall x y,
            seq_max l = Some x ->
            y \in l ->
            rel y x.
        Proof.
          by apply seq_argmax_computes_max.
        Qed.

      End TotalOrder.

267
    End Lemmas.
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 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
        
  End MinGeneric.

  Section ArgNat.
      
    Context {T: eqType}.

    Variable F: T -> nat.

    Definition seq_argmin_nat := seq_argmin leq F.
    Definition seq_argmax_nat := seq_argmax leq F.

    Section Lemmas.

      Lemma seq_argmin_nat_exists:
        forall l x,
          x \in l ->
          seq_argmin_nat l != None.
      Proof.
        by apply seq_argmin_exists.
      Qed.
        
      Lemma seq_argmin_nat_in_seq:
        forall l x,
          seq_argmin_nat l = Some x ->
          x \in l.
      Proof.
        by apply seq_argmin_in_seq.
      Qed.

      Lemma seq_argmax_nat_exists:
        forall l x,
          x \in l ->
          seq_argmax_nat l != None.
      Proof.
        by apply seq_argmax_exists.
      Qed.
        
      Lemma seq_argmax_nat_in_seq:
        forall l x,
          seq_argmax_nat l = Some x ->
          x \in l.
      Proof.
        by apply seq_argmax_in_seq.
      Qed.
      
      Section TotalOrder.

        Lemma seq_argmin_nat_computes_min:
          forall l x y,
            seq_argmin_nat l = Some x ->
            y \in l ->
            F x <= F y.
        Proof.
          intros l x y SOME IN.
          apply seq_argmin_computes_min with (l0 := l); try (by done).
          - by intros x1 x2 x3; apply leq_trans.
          - by intros x1 x2 IN1 IN2; apply leq_total.
        Qed.

        Lemma seq_argmax_nat_computes_max:
          forall l x y,
            seq_argmax_nat l = Some x ->
            y \in l ->
            F x >= F y.
        Proof.
          intros l x y SOME IN.
          apply seq_argmax_computes_max with (l0 := l); try (by done).
          - by intros x1 x2 x3; apply leq_trans.
          - by intros x1 x2 IN1 IN2; apply leq_total.
        Qed.

      End TotalOrder.

    End Lemmas.
        
  End ArgNat.
345
  
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
  Section MinNat.

    Definition seq_min_nat := seq_argmin leq id.
    Definition seq_max_nat := seq_argmax leq id.

    Section Lemmas.

      Lemma seq_min_nat_exists:
        forall l x,
          x \in l ->
          seq_min_nat l != None.
      Proof.
        by apply seq_argmin_exists.
      Qed.
        
      Lemma seq_min_nat_in_seq:
        forall l x,
          seq_min_nat l = Some x ->
          x \in l.
      Proof.
        by apply seq_argmin_in_seq.
      Qed.

      Lemma seq_max_nat_exists:
        forall l x,
          x \in l ->
          seq_max_nat l != None.
      Proof.
        by apply seq_argmax_exists.
      Qed.
        
      Lemma seq_max_nat_in_seq:
        forall l x,
          seq_max_nat l = Some x ->
          x \in l.
      Proof.
        by apply seq_argmax_in_seq.
      Qed.
      
      Section TotalOrder.

        Lemma seq_min_nat_computes_min:
          forall l x y,
            seq_min_nat l = Some x ->
            y \in l ->
            x <= y.
        Proof.
          intros l x y SOME IN.
          apply seq_min_computes_min with (l0 := l); try (by done).
          - by intros x1 x2 x3; apply leq_trans.
          - by intros x1 x2 IN1 IN2; apply leq_total.
        Qed.

        Lemma seq_max_nat_computes_max:
          forall l x y,
            seq_max_nat l = Some x ->
            y \in l ->
            x >= y.
        Proof.
          intros l x y SOME IN.
          apply seq_max_computes_max with (l0 := l); try (by done).
          - by intros x1 x2 x3; apply leq_trans.
          - by intros x1 x2 IN1 IN2; apply leq_total.
        Qed.

      End TotalOrder.

    End Lemmas.
        
  End MinNat.

  Section NatRange.
    
    Definition values_between (a b: nat) :=
      filter (fun x => x >= a) (map (@nat_of_ord _) (enum 'I_b)).

    Lemma mem_values_between a b:
      forall x, x \in values_between a b = (a <= x < b).
    Proof.
      intros x; rewrite mem_filter.
      apply/idP/idP.
      {
        move => /andP [GE IN].
        move: IN => /mapP [x' IN] EQ; subst.
        rewrite mem_enum in IN.
        by apply/andP; split.
      }
      {
        move => /andP [GE LT].
        rewrite GE andTb.
        apply/mapP; exists (Ordinal LT); last by done.
        by rewrite mem_enum.
      }
    Qed.

    Definition min_nat_cond P (a b: nat) :=
      seq_min_nat (filter P (values_between a b)).

    Definition max_nat_cond P (a b: nat) :=
      seq_max_nat (filter P (values_between a b)).

    Lemma min_nat_cond_in_seq:
      forall P a b x,
        min_nat_cond P a b = Some x ->
        a <= x < b /\ P x.
    Proof.
      intros P a b x SOME.
      apply seq_min_nat_in_seq in SOME.
      rewrite mem_filter in SOME; move: SOME => /andP [Px LE].
      by split; first by rewrite mem_values_between in LE.
    Qed.

    Lemma min_nat_cond_computes_min:
      forall P a b x,
        min_nat_cond P a b = Some x ->
        (forall y, a <= y < b -> P y -> x <= y).
    Proof.
      intros P a b x SOME y LE Py.
      apply seq_min_nat_computes_min with (y := y) in SOME; first by done.
      by rewrite mem_filter Py andTb mem_values_between.
    Qed.

    Lemma max_nat_cond_in_seq:
      forall P a b x,
        max_nat_cond P a b = Some x ->
        a <= x < b /\ P x.
    Proof.
      intros P a b x SOME.
      apply seq_max_nat_in_seq in SOME.
      rewrite mem_filter in SOME; move: SOME => /andP [Px LE].
      by split; first by rewrite mem_values_between in LE.
    Qed.

    Lemma max_nat_cond_computes_max:
      forall P a b x,
        max_nat_cond P a b = Some x ->
        (forall y, a <= y < b -> P y -> y <= x).
    Proof.
      intros P a b x SOME y LE Py.
      apply seq_max_nat_computes_max with (y := y) in SOME; first by done.
      by rewrite mem_filter Py andTb mem_values_between.
    Qed.
    
  End NatRange.

491 492
End MinMaxSeq.

Felipe Cerqueira's avatar
Felipe Cerqueira committed
493 494
(* Additional lemmas about max. *)
Section ExtraLemmas.
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
  
  Lemma leq_big_max I r (P : pred I) (E1 E2 : I -> nat) :
    (forall i, P i -> E1 i <= E2 i) ->
      \max_(i <- r | P i) E1 i <= \max_(i <- r | P i) E2 i.
  Proof.
    move => leE12; elim/big_ind2 : _ => // m1 m2 n1 n2.
    intros LE1 LE2; rewrite leq_max; unfold maxn.
    by destruct (m2 < n2) eqn:LT; [by apply/orP; right | by apply/orP; left].
  Qed.

  Lemma bigmax_ord_ltn_identity n :
    n > 0 ->
    \max_(i < n) i < n.
  Proof.
    intros LT.
    destruct n; first by rewrite ltn0 in LT.
    clear LT.
    induction n; first by rewrite big_ord_recr /= big_ord0 maxn0.
    rewrite big_ord_recr /=.
    unfold maxn at 1; desf.
    by apply leq_trans with (n := n.+1).
  Qed.
    
  Lemma bigmax_ltn_ord n (P : pred nat) (i0: 'I_n) :
    P i0 ->
    \max_(i < n | P i) i < n.
  Proof.
    intros LT.
    destruct n; first by destruct i0 as [i0 P0]; move: (P0) => P0'; rewrite ltn0 in P0'.
    rewrite big_mkcond.
    apply leq_ltn_trans with (n := \max_(i < n.+1) i).
    {
      apply/bigmax_leqP; ins.
      destruct (P i); last by done.
      by apply leq_bigmax_cond.
    }
    by apply bigmax_ord_ltn_identity.
  Qed.

  Lemma bigmax_pred n (P : pred nat) (i0: 'I_n) :
    P (i0) ->
    P (\max_(i < n | P i) i).
  Proof.
    intros PRED.
    induction n.
    {
      destruct i0 as [i0 P0].
      by move: (P0) => P1; rewrite ltn0 in P1. 
    }
    rewrite big_mkcond big_ord_recr /=; desf.
    {
      destruct n; first by rewrite big_ord0 maxn0.
      unfold maxn at 1; desf.
      exfalso.
      apply negbT in Heq0; move: Heq0 => /negP BUG.
      apply BUG. 
      apply leq_ltn_trans with (n := \max_(i < n.+1) i).
      {
        apply/bigmax_leqP; ins.
        destruct (P i); last by done.
        by apply leq_bigmax_cond.
      }
      by apply bigmax_ord_ltn_identity.
    }
    {
      rewrite maxn0.
      rewrite -big_mkcond /=.
      have LT: i0 < n.
      {
        rewrite ltn_neqAle; apply/andP; split;
          last by rewrite -ltnS; apply ltn_ord.
        apply/negP; move => /eqP BUG.
        by rewrite -BUG PRED in Heq.
      }
      by rewrite (IHn (Ordinal LT)).
    }
  Qed.
    
Felipe Cerqueira's avatar
Felipe Cerqueira committed
573
End ExtraLemmas.