#ifdef LANGUAGE_PTS_HAS_EQUALITY
#ifdef LANGUAGE_PTS_HAS_BOOL
#ifdef LANGUAGE_PTS_HAS_NAT
#ifdef LANGUAGE_PTS_HAS_SIGMA
#ifdef LANGUAGE_PTS_HAS_PROP
#define EQUALITY_EXAMPLES
{-# LANGUAGE OverloadedStrings, ScopedTypeVariables #-}
#endif
#endif
#endif
#endif
#endif
module Language.PTS.Examples.EvenOrOdd (
#ifdef EQUALITY_EXAMPLES
evenOrOddScript,
#endif
) where
#ifdef EQUALITY_EXAMPLES
import Language.PTS
import Language.PTS.Bound
evenOrOddScript :: forall s m. Script s m => m ()
evenOrOddScript = do
section_ "double"
define_ "succ"
$$ TermNat ~> TermNat
$$ lam_ "n" (Inf $ TermNatS "n")
define_ "double"
$$ TermNat ~> TermNat
$$ lam_ "n" (Inf $ TermNatElim "_" (liftH TermNat)
(Inf TermNatZ)
(lams_ ["_","m"] ("succ" @@ ("succ" @@ "m")))
"n")
example_ $ "double" @@ 0
example_ $ "double" @@ 1
example_ $ "double" @@ 5
section_ "Even"
define_ "Even"
$$ TermNat ~> sort_ typeSort
$$ lam_ "n" (Inf $ Sigma "m" TermNat $ abstract1HSym "m" $
Equality TermNat "n" ("double" @@ "m"))
define_ "zero-is-even"
$$ "Even" @@ 0
$$ Pair 0 Refl
example_ $$ Pair 1 Refl -:- "Even" @@ 2
section_ "Odd"
define_ "Odd"
$$ TermNat ~> sort_ typeSort
$$ lam_ "n" (Inf $ Sigma "m" TermNat $ abstract1HSym "m" $
Equality TermNat "n" (Inf $ TermNatS $ "double" @@ "m"))
example_ $$ Pair 3 Refl -:- "Odd" @@ 7
let j_ u v w a p = J (V3 (IrrSym u) (IrrSym v) (IrrSym w)) a (abstract3HSym u v w p)
section_ "Congruence"
define_ "congruence"
$$ foralls_ ["A","B"] (pi_ "f" ("A"~> "B") $ pi_ "x" "A" $ pi_ "y" "A"
$ Equality "A" "x" "y" ~> Equality "B" ("f" @@ "x") ("f" @@ "y"))
$$ lams_ ["A","B","f","x","y","p"]
(Inf $ j_ "u" "v" "_" "A" (Equality "B" ("f" @@ "u") ("f" @@ "v")) (lam_ "_" Refl) "x" "y" "p")
section_ "Succ Even is Odd"
define_ "succ-even-is-odd"
$$ pi_ "n" TermNat ("Even" @@ "n" ~> "Odd" @@ (Inf $ TermNatS $ "n"))
$$ lams_ ["n","even-n"] (Match "even-n" "m" "proof"
$ abstract2HSym "m" "proof"
$ Pair "m" $ "congruence" @@@ TermNat @@@ TermNat @@ "succ" @@ "n" @@ ("double" @@ "m") @@ "proof")
define_ "succ-odd-is-even"
$$ pi_ "n" TermNat ("Odd" @@ "n" ~> "Even" @@ (Inf $ TermNatS $ "n"))
$$ lams_ ["n","odd-n"] (Match "odd-n" "m" "proof"
$ abstract2HSym "m" "proof"
$ Pair ("succ" @@ "m") $ "congruence" @@@ TermNat @@@ TermNat @@ "succ" @@ "n" @@ ("succ" @@ ("double" @@ "m")) @@ "proof")
section_ "Boolean"
define_ "if1"
$$ pi_ "r" (sort_ typeSortSort) (TermBool ~> "r" ~> "r" ~> "r")
$$ lams_ ["r", "b", "t", "f"]
(Inf $ TermBoolElim "_" (liftH "r") "t" "f" "b")
section_ "Either"
subsection_ "Type"
define_ "Either"
$$ sort_ typeSort ~> sort_ typeSort ~> sort_ typeSort
$$ lams_ ["A", "B"] (Inf $ Sigma "t" TermBool $ abstract1HSym "x" $ "if1" @@ sort_ typeSort @@ "x" @@ "A" @@ "B")
subsection_ "Constructors"
define_ "left"
$$ foralls_ ["A", "B"] ("A" ~> "Either" @@ "A" @@ "B")
$$ lams_ ["A","B","x"] (Pair (Inf TermTrue) "x")
define_ "right"
$$ foralls_ ["A", "B"] ("B" ~> "Either" @@ "A" @@ "B")
$$ lams_ ["A","B","y"] (Pair (Inf TermFalse) "y")
subsection_ "Non-dependent elimination"
let eitherBody :: TermInf s Sym
eitherBody = TermBoolElim
"t" (abstract1HSym "t" $ "if1" @@ sort_ typeSort @@ "t" @@ "A" @@ "B" ~> "C")
(lam_ "x" $ "f" @@ "x")
(lam_ "y" $ "g" @@ "y")
"t"
define_ "either"
$$ foralls_ ["A","B","C"] (("A" ~> "C") ~> ("B" ~> "C") ~> "Either" @@ "A" @@ "B" ~> "C")
$$ lams_ ["A","B","C","f","g","e"]
(Match "e" "t" "v" $ abstract2HSym "t" "v" $ eitherBody @@ "v")
section_ "All number are either even or odd"
let motive :: TermChk s Sym -> TermInf s Sym
motive n = "Either" @@ ("Even" @@ n) @@ ("Odd" @@ n)
define_ "theorem"
$$ pi_ "n" TermNat (motive "n")
$$ lam_ "n" (Inf $ TermNatElim "m" (abstract1HSym "m" $ motive "m")
(Pair (Inf TermTrue) $ "zero-is-even")
(lams_ ["l","H"] (Inf $ apps_ "either"
[ "Even" @@ "l"
, "Odd" @@ "l"
, Inf (motive (Inf $ "succ" @@ "l"))
, lam_ "p" $ Pair (Inf TermFalse) $ "succ-even-is-odd" @@ "l" @@ "p"
, lam_ "p" $ Pair (Inf TermTrue) $ "succ-odd-is-even" @@ "l" @@ "p"
, "H"
]))
"n")
example_ $ "theorem" @@ 0
example_ $ "theorem" @@ 100
example_ $ "theorem" @@ 113
#endif