Skip to content

Instantly share code, notes, and snippets.

@frodo821
Last active November 28, 2020 05:24
Show Gist options
  • Select an option

  • Save frodo821/c6ce56d29aa50280abd21affc2fbce80 to your computer and use it in GitHub Desktop.

Select an option

Save frodo821/c6ce56d29aa50280abd21affc2fbce80 to your computer and use it in GitHub Desktop.
Proof Driven Development sample
Require Import List.
Import ListNotations.
Fixpoint reversed { A: Type } (xs: list A) :=
match xs with
| nil => nil
| x :: xs' => reversed xs' ++ [x]
end.
Lemma reversed_sub:
forall (A: Type) (xs ys: list A), reversed (xs ++ ys) = reversed(ys) ++ reversed(xs).
Proof.
intros A xs ys. induction xs.
-
simpl.
rewrite app_nil_r. reflexivity.
-
simpl. rewrite IHxs.
rewrite app_assoc_reverse. reflexivity.
Qed.
Theorem matches_double_reversed : forall (A: Type) (xs: list A), reversed (reversed xs) = xs.
Proof.
intros A xs. induction xs.
- simpl. reflexivity.
- simpl.
rewrite reversed_sub. rewrite IHxs.
simpl. reflexivity.
Qed.
Require Extraction.
Extraction Language Haskell.
Extraction "out.hs" reversed.
import Data.List
import Out
toHList :: (List a) -> [a]
toHList lis =
case lis of {
Nil -> [];
Cons x lis' -> [x] ++ (toHList lis')}
toList :: [a] -> List a
toList lis =
case lis of {
[] -> Nil;
(x:xs') -> app (Cons x Nil) (toList xs')}
main :: IO()
main = do
mapM_ putStrLn $ map show $ toHList $ reversed $ toList [1,2,3,4,5]
module Out where
import qualified Prelude
data List a =
Nil
| Cons a (List a)
app :: (List a1) -> (List a1) -> List a1
app l m =
case l of {
Nil -> m;
Cons a l1 -> Cons a (app l1 m)}
reversed :: (List a1) -> List a1
reversed xs =
case xs of {
Nil -> Nil;
Cons x xs' -> app (reversed xs') (Cons x Nil)}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment