Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
Mistral Contrastin committed Nov 6, 2019
0 parents commit 4f2abf1
Show file tree
Hide file tree
Showing 2 changed files with 334 additions and 0 deletions.
334 changes: 334 additions & 0 deletions Demo.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,334 @@
module Demo where

import Prelude hiding (head, take, gcd, pred)

infixr 6 :::
data List a = Nil | (:::) a (List a)

instance Show a => Show (List a) where
show Nil = "Nil"
show (x ::: xs) = show x ++ " ::: " ++ show xs

{-@ measure len' @-}
len' :: List a -> Int
len' Nil = 0
len' (_ ::: xs) = 1 + len' xs














--------------------------------------------------------------------------------
-- Milner lied
--------------------------------------------------------------------------------

-- "Well-typed programs don't go wrong" - Robin Milner

{-
head :: List a -> a
head (x ::: _) = x
head Nil = error "I wasn't supposed to go wrong!"
headEx1 :: Int
headEx1 = head (1 ::: 2 ::: 3 ::: Nil)
headEx2 :: Int
headEx2 = head Nil
-}





































--------------------------------------------------------------------------------
-- Constraints on natural numbers
--------------------------------------------------------------------------------

{-
take :: Int -> List a -> List a
take 0 _ = Nil
take _ Nil = Nil
take n (x ::: xs) =
if n > 0
then x ::: take (n - 1) xs
else error "Negative number of elements to take"
takeEx1 = take 2 (1 ::: 2 ::: 3 ::: Nil)
takeEx2 = take (-2) (1 ::: 2 ::: 3 ::: Nil)
-}


































--------------------------------------------------------------------------------
-- Parameters may depend on previous parameters
--------------------------------------------------------------------------------

{-
take' :: Int -> List a -> List a
take' 0 _ = Nil
take' _ Nil = error "Not enough items to take"
take' n (x ::: xs) =
if n > 0
then x ::: take' (n - 1) xs
else error "Negative number of elements to take"
take'Ex :: List Int
take'Ex = take' 5 (1 ::: 2 ::: 3 ::: Nil)
-}
































--------------------------------------------------------------------------------
-- Outputs can reveal information
--------------------------------------------------------------------------------

{-
{-@ type ListGTE n = {xs:List a | len' xs >= n} @-}
{-@ take'' :: n:Nat -> ListGTE n -> List a @-}
take'' :: Int -> List a -> List a
take'' 0 _ = Nil
take'' _ Nil = error "Not enough items to take"
take'' n (x ::: xs) =
if n > 0
then x ::: take'' (n - 1) xs
else error "Negative number of elements to take"
take''Ex = take'' 1 (take'' 2 (1 ::: 2 ::: 3 ::: 4 ::: Nil))
-}
































--------------------------------------------------------------------------------
-- Proofs require remination
--------------------------------------------------------------------------------

{-
countSteps :: Int -> Int -> Int
countSteps stepSize n =
if n == 0
then 0
else 1 + countSteps stepSize (n - stepSize)
-}







































--------------------------------------------------------------------------------
-- Termination may need some help
--------------------------------------------------------------------------------

{-
range :: Int -> Int -> List Int
range lo hi =
if lo == hi
then Nil
else if lo < hi
then lo ::: range (lo + 1) hi
else error "The upper bound is lower than the lower bound."
rangeEx = range 1 4
-}




































Binary file added Refinement types.pdf
Binary file not shown.

0 comments on commit 4f2abf1

Please sign in to comment.