Stefan Wehr (wehr@cp-med.com)
28.2.2020
boolOrChar : Bool -> Type
boolOrChar False = Char
boolOrChar True = Bool
toString : (isChar : Bool) -> boolOrChar isChar -> String
toString False c = singleton c -- c is a Char
toString True False = "F" -- c is a Bool
toString True True = "T" -- c a Bool
toFormat : String -> Format
toFormat s = loop (unpack s)
where
prepend : Char -> Format -> Format
prepend c (FLit s fmt) = FLit (strCons c s) fmt
prepend c fmt = FLit (singleton c) fmt
loop : List Char -> Format
loop [] = FEnd
loop ('%' :: 's' :: rest) = FString (loop rest)
loop ('%' :: '%' :: rest) = prepend '%' (loop rest)
loop (c :: rest) = prepend c (loop rest)
PrintfType : Format -> Type
PrintfType (FString f) = String -> PrintfType f
PrintfType (FLit s f) = PrintfType f
PrintfType FEnd = String
Vim | Emacs | Atom | |
---|---|---|---|
Open response window | \i | ||
Reload file | \r | C-c C-l | Cntl-Alt-R |
Show type | \t | C-c C-t | Cntl-Alt-T |
Create definition | \d | C-c C-s | Cntl-Alt-A |
Case split | \c | C-c C-c | Cntl-Alt-C |
Proof search | \p | Cntl-Alt-S |
Format
data type with an alternative for %d
specifier for printing ints."name = %s, age = %d"
printf "name = %s, age = %d" "Stefan" 41
yields "name = Stefan, age = 41"
Vect n String
is a vector of Strings with length n
.infixr 7 ::
data Vect : Nat -> Type -> Type where
Nil : Vect Z a
(::) : a -> Vect n a -> Vect (S n) a
%name Vect xs,ys,zs,ws
head
function can now be defined as a total function.zip
functionThe predicates LT
for <
and LTE
for <=
are defined as follows:
a
has type IO a
.do
notation (as in Haskell).sayHello
sayHello : IO ()
sayHello =
do putStrLn "What's your name?"
name <- getLine
putStrLn ("Hello " ++ name)
:exec sayHello
in the Idris interpreter to run the effectful computation sayHello
.name <- getLine
executes the computation getLine
and binds the result to name
.readVect
that reads from stdin until an empty line is encountered. The function then returns a vector of all strings read (without the empty line).(n : Nat ** Vect n String)
readVect
pure : a -> IO a
to inject a value into an effectful computation(n ** elems) <- readVect
runs the computation readVect and names the first component of the result as n
, the second as elems
.zip
themImplement the function readAndZipVectors : IO ()
. The function should behave as follows:
"First vector"
to the user and reads the first vector."Second vector"
to the user and reads the second vector.exactLength : (len : Nat) -> Vect m a -> Maybe (Vect len a)
for checking whether a vector has the expected length.show
for converting a vector into a string.