Improve docs

This commit is contained in:
rnhmjoj 2016-04-24 13:53:45 +02:00
parent 5d7ce06597
commit c7508bc57e
No known key found for this signature in database
GPG Key ID: 362BB82B7E496B7C
7 changed files with 73 additions and 10 deletions

View File

@ -10,6 +10,7 @@
{-# LANGUAGE PolyKinds #-} {-# LANGUAGE PolyKinds #-}
{-# LANGUAGE UnicodeSyntax #-} {-# LANGUAGE UnicodeSyntax #-}
-- | Defines the boolean type and logical operators
module Data.Bool where module Data.Bool where
import Data.TypeClass (Eq, Enum, Show) import Data.TypeClass (Eq, Enum, Show)
@ -25,7 +26,8 @@ singletons [d|
F ∷ 𝔹 -- ^ False F ∷ 𝔹 -- ^ False
deriving (Eq, Show) deriving (Eq, Show)
-- Logical operatos
-- * Logical operatos
-- | Conjuction -- | Conjuction
(∧) ∷ 𝔹 β†’ 𝔹 β†’ 𝔹 (∧) ∷ 𝔹 β†’ 𝔹 β†’ 𝔹

View File

@ -11,7 +11,7 @@
{-# LANGUAGE PolyKinds #-} {-# LANGUAGE PolyKinds #-}
{-# LANGUAGE UnicodeSyntax #-} {-# LANGUAGE UnicodeSyntax #-}
-- | Defines commonly used functions and combinators
module Data.Function where module Data.Function where
-- | Identity Function -- | Identity Function

View File

@ -1,3 +1,4 @@
-- | An arbitrary-precision integers datatype
module Data.Integer module Data.Integer
( module GHC.Integer ( module GHC.Integer
) where ) where

View File

@ -11,6 +11,7 @@
{-# LANGUAGE PolyKinds #-} {-# LANGUAGE PolyKinds #-}
{-# LANGUAGE UnicodeSyntax #-} {-# LANGUAGE UnicodeSyntax #-}
-- | Defines natural numbers on both term and type level
module Data.Nat where module Data.Nat where
import Relation.Equality import Relation.Equality
@ -86,13 +87,13 @@ instance Bounded β„• where
maxBound = (∞) maxBound = (∞)
-- | Convert a natural into an "Integer" -- | Convert a natural into an 'Integer'
toInteger ∷ β„• β†’ Integer toInteger ∷ β„• β†’ Integer
toInteger Z = 0 toInteger Z = 0
toInteger (S n) = 1 T.+ toInteger n toInteger (S n) = 1 T.+ toInteger n
-- | Convert na "Integer" into a natural -- | Convert an 'Integer' into a natural
fromInteger ∷ Integer β†’ β„• fromInteger ∷ Integer β†’ β„•
fromInteger 0 = Z fromInteger 0 = Z
fromInteger n fromInteger n

View File

@ -1,3 +1,4 @@
-- | Exports haskell built-in typeclasses
module Data.TypeClass module Data.TypeClass
( Eq(..) ( Eq(..)
, Ord(..) , Ord(..)

View File

@ -12,6 +12,9 @@
{-# LANGUAGE PolyKinds #-} {-# LANGUAGE PolyKinds #-}
{-# LANGUAGE UnicodeSyntax #-} {-# LANGUAGE UnicodeSyntax #-}
-- | Defines a vector type, analogous to a list, that achieves
-- type-safe operation like resizing and concatenation by using
-- new dependantly typed language features in haskell
module Data.Vec where module Data.Vec where
import Relation.Equality (gcastWith) import Relation.Equality (gcastWith)
@ -27,11 +30,13 @@ import qualified Data.TypeClass as T
infixr 5 :- infixr 5 :-
-- | The 'Vec' datatype
data Vec (a ∷ β˜…) ∷ β„• β†’ β˜… where data Vec (a ∷ β˜…) ∷ β„• β†’ β˜… where
Nil ∷ Vec a Z Nil ∷ Vec a Z -- ^ empty 'Vec', length 0
(:-) ∷ a β†’ Vec a n β†’ Vec a (S n) (:-) ∷ a β†’ Vec a n β†’ Vec a (S n) -- ^ "cons" operator
-- | 'String' type alias for vector of characters
type String n = Vec Char n type String n = Vec Char n
@ -49,11 +54,20 @@ instance SingI n β‡’ IsString (String n) where
fromString = fromList fromString = fromList
-- * Conversions
-- | Convert a 'Vec' into a List
toList ∷ Vec a n β†’ [a] toList ∷ Vec a n β†’ [a]
toList Nil = [] toList Nil = []
toList (x :- xs) = x : toList xs toList (x :- xs) = x : toList xs
-- | Convert a List into a 'Vec'
--
-- It's not possible to convert an infinite list to a vector
-- as haskell does not permit constructing infinite type,
-- which the resulting vector length would be.
fromList ∷ SingI n β‡’ [a] β†’ Vec a n fromList ∷ SingI n β‡’ [a] β†’ Vec a n
fromList = f sing fromList = f sing
where f ∷ Sβ„• n β†’ [a] β†’ Vec a n where f ∷ Sβ„• n β†’ [a] β†’ Vec a n
@ -61,84 +75,127 @@ fromList = f sing
f (SS n) (x:xs) = x :- f n xs f (SS n) (x:xs) = x :- f n xs
-- * Basic functions
-- | Vector concatenation
(β§Ί) ∷ Vec a n β†’ Vec a m β†’ Vec a (n :+ m) (β§Ί) ∷ Vec a n β†’ Vec a m β†’ Vec a (n :+ m)
(β§Ί) (x :- xs) ys = x :- xs β§Ί ys (β§Ί) (x :- xs) ys = x :- xs β§Ί ys
(β§Ί) Nil ys = ys (β§Ί) Nil ys = ys
-- | Given a nonempty 'Vec' returns the first element
-- and the rest of the vector in a tuple.
-- > uncons x ≑ (head x, tail x)
uncons ∷ Vec a (S n) β†’ (a, Vec a n)
uncons (x :- xs) = (x, xs)
-- | Returns the 'head' (ie the first element) of a nonempty 'Vec'.
head ∷ Vec a (S n) β†’ a head ∷ Vec a (S n) β†’ a
head (x :- _) = x head (x :- _) = x
-- | Returns the last element of a nonempty 'Vec'.
last ∷ Vec a (S n) β†’ a last ∷ Vec a (S n) β†’ a
last (x :- Nil) = x last (x :- Nil) = x
last (x :- y :- ys) = last (y :- ys) last (x :- y :- ys) = last (y :- ys)
-- | Applied to a nonempty 'Vec' returns everything but its first element.
-- > x ≑ head x β§Ί tail x
tail ∷ Vec a (S n) β†’ Vec a n tail ∷ Vec a (S n) β†’ Vec a n
tail (_ :- xs) = xs tail (_ :- xs) = xs
-- | Applied to a nonempty 'Vec' returns everything but its last element.
-- > x ≑ init x β§Ί last x
init ∷ Vec a (S n) β†’ Vec a n init ∷ Vec a (S n) β†’ Vec a n
init (x :- Nil) = Nil init (x :- Nil) = Nil
init (x :- y :- ys) = x :- init (y :- ys) init (x :- y :- ys) = x :- init (y :- ys)
uncons ∷ Vec a (S n) β†’ (a, Vec a n)
uncons (x :- xs) = (x, xs)
-- * Length information
-- | Test whether a 'Vec' has zero length.
null ∷ Vec a n β†’ 𝔹 null ∷ Vec a n β†’ 𝔹
null Nil = T null Nil = T
null _ = F null _ = F
-- | Returns the length (numbers of elements) of a 'Vec'.
length ∷ Vec a n β†’ β„• length ∷ Vec a n β†’ β„•
length Nil = Z length Nil = Z
length (_ :- xs) = S (length xs) length (_ :- xs) = S (length xs)
-- | Same as 'length' but produces a singleton type 'Sβ„•'.
sLength ∷ Vec a n β†’ Sβ„• n sLength ∷ Vec a n β†’ Sβ„• n
sLength Nil = SZ sLength Nil = SZ
sLength (x :- xs) = SS (sLength xs) sLength (x :- xs) = SS (sLength xs)
-- * Transforming and reducing a 'Vec'
-- | Applies a function on every element of a 'Vec'.
map ∷ (a β†’ b) β†’ Vec a n β†’ Vec b n map ∷ (a β†’ b) β†’ Vec a n β†’ Vec b n
map _ Nil = Nil map _ Nil = Nil
map f (x :- xs) = f x :- map f xs map f (x :- xs) = f x :- map f xs
-- | 'foldl' applied to a binary operator, a starting value
-- and a 'Vec' reduces the vector to a single value obtained
-- by sequentially applying the operation from the left to the right.
foldl ∷ (a β†’ b β†’ a) β†’ a β†’ Vec b n β†’ a foldl ∷ (a β†’ b β†’ a) β†’ a β†’ Vec b n β†’ a
foldl _ x Nil = x foldl _ x Nil = x
foldl f x (y :- ys) = foldl f (f x y) ys foldl f x (y :- ys) = foldl f (f x y) ys
-- | Same as "fold" but reduces the vector in the opposite
-- direction: from the right to the left.
foldr ∷ (a β†’ b β†’ b) β†’ b β†’ Vec a n β†’ b foldr ∷ (a β†’ b β†’ b) β†’ b β†’ Vec a n β†’ b
foldr f x Nil = x foldr f x Nil = x
foldr f x (y:-ys) = f y (foldr f x ys) foldr f x (y:-ys) = f y (foldr f x ys)
-- | Variant of 'foldl' which requires no starting value but
-- applies only on nonempty 'Vec'
foldl₁ ∷ (a β†’ a β†’ a) β†’ Vec a (S n) β†’ a foldl₁ ∷ (a β†’ a β†’ a) β†’ Vec a (S n) β†’ a
foldl₁ f (x :- Nil) = x foldl₁ f (x :- Nil) = x
foldl₁ f (x :- y :- ys) = foldl₁ f (f x y :- ys) foldl₁ f (x :- y :- ys) = foldl₁ f (f x y :- ys)
-- | As for 'foldl', a variant of 'foldr' with no starting point
foldr₁ ∷ (a β†’ a β†’ a) β†’ Vec a (S n) β†’ a foldr₁ ∷ (a β†’ a β†’ a) β†’ Vec a (S n) β†’ a
foldr₁ f (x :- Nil) = x foldr₁ f (x :- Nil) = x
foldr₁ f (x :- y :- ys) = f x (foldr₁ f (y :- ys)) foldr₁ f (x :- y :- ys) = f x (foldr₁ f (y :- ys))
-- | Reverse the order of the element of a 'Vec'
-- > reverse ∘ reverse = id
reverse ∷ Vec a n β†’ Vec a n reverse ∷ Vec a n β†’ Vec a n
reverse Nil = Nil reverse Nil = Nil
reverse (y :- ys) = gcastWith proof (reverse ys β§Ί (y :- Nil)) reverse (y :- ys) = gcastWith proof (reverse ys β§Ί (y :- Nil))
where proof = succ_plus (sLength ys) where proof = succ_plus (sLength ys)
-- | Applied to a singleton natural @n@ and a value produces
-- a vector obtained by duplicating the value @n@ times
replicate ∷ Sβ„• n β†’ a β†’ Vec a n replicate ∷ Sβ„• n β†’ a β†’ Vec a n
replicate SZ _ = Nil replicate SZ _ = Nil
replicate (SS n) a = a :- replicate n a replicate (SS n) a = a :- replicate n a
-- | 'take' @n@ applied to a 'Vec', returns the first @n@ element
-- of the vector
take ∷ Sβ„• n β†’ Vec a m β†’ Vec a n take ∷ Sβ„• n β†’ Vec a m β†’ Vec a n
take SZ _ = Nil take SZ _ = Nil
take (SS n) (x :- xs) = x :- take n xs take (SS n) (x :- xs) = x :- take n xs
-- | 'drop' @n@ applied to a 'Vec', returns every element of
-- the vector but the first @n@
drop ∷ Sβ„• n β†’ Vec a m β†’ Vec a (m :- n)
drop SZ x = x
drop (SS n) (x :- xs) = drop n xs

View File

@ -11,6 +11,8 @@
{-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE UnicodeSyntax #-} {-# LANGUAGE UnicodeSyntax #-}
-- | Defines equality relation on types and provides
-- a simple interface to write proof on haskell types
module Relation.Equality where module Relation.Equality where
import Data.Singletons (Sing) import Data.Singletons (Sing)
@ -44,4 +46,3 @@ castWith Refl x = x
-- | Generalized form of cast -- | Generalized form of cast
gcastWith ∷ βˆ€ a b r. (a :≑ b) β†’ ((a ~ b) β‡’ r) β†’ r gcastWith ∷ βˆ€ a b r. (a :≑ b) β†’ ((a ~ b) β‡’ r) β†’ r
gcastWith Refl x = x gcastWith Refl x = x