Skip to content

Instantly share code, notes, and snippets.

@phadej
Created September 27, 2012 18:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phadej/3795754 to your computer and use it in GitHub Desktop.
Save phadej/3795754 to your computer and use it in GitHub Desktop.
Testit vs Proof

Hehkutettu Haskellin QuickCheck

import Prelude hiding (take)

import Test.QuickCheck

take :: Int -> [a] -> [a]
take _ []     = []
take 0 _      = []
take n (x:xs) = x : take (n-1) xs

take_length_prop :: NonNegative Int -> [a] -> Bool
take_length_prop n l = length (take intn l) <= intn
  where intn = fromIntegral n

main :: IO ()
main = quickCheckWith stdArgs { maxSuccess = 1000 } p
  where p = take_length_prop :: NonNegative Int -> [Int] -> Bool

Vähän joutuu kikkailee testin kirjottamisen kanssa.

Ihan huike Coq

Lemma le_0_n : forall n : nat,
  0 <= n.
Proof.
  induction n.
  apply le_n.
  apply le_S. apply IHn. Qed.

Lemma le_n_S : forall n m : nat,
  n <= m -> S n <= S m.
Proof.
  intros n m H.
  induction H.
    apply le_n.
    apply le_S. apply IHle. Qed.

Fixpoint take {X : Type} (n : nat) (l : list X) : list X :=
  match l, n with
  | nil     , _    => nil
  | _       , O    => nil
  | cons h t, S n' => cons h (take n' t)
  end.

Fact take_length_fact : forall (X : Type) (n : nat) (l : list X),
  length (take n l) <= n.
Proof.
  intros X n.
  induction n as [| n'].
    destruct l.
      simpl. apply le_n.
      simpl. apply le_n.
    destruct l.
      simpl. apply le_0_n.
      simpl. apply le_n_S. apply IHn'. Qed.

Perusteista ku lähtee niin verbosea on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment