8. 例:長さを型に持つリスト
data Vect : Nat -> Type -> Type where
Nil : Vect Z a
(::) : a -> Vect k a -> Vect (S k) a
(++) : Vect n a -> Vect m a -> Vect (n + m) a
(++) Nil ys = ys
(++) (x :: xs) ys = x :: xs ++ ys
index : Fin n -> Vect n a -> a
index fZ (x :: xs) = x
index (fS k) (x :: xs) = index k xs
15. %include
• JavaScriptのコードをincludeできる
(生成されるJSの先頭にくっつくだけ)
%include JavaScript "lib.js"
twice : (Int -> Int) -> Int -> IO Int
twice f x =
mkForeign (FFun "twice(%0,%1)"
[FFunction FInt FInt, FInt] FInt) f x
main : IO ()
main = alert (show !(twice (+1) 1)) -- 3
がでる
// lib.js
function twice(f, x) { return f(f(x)); }
16. 適当なライブラリを作る
Document : Type
Document = Ptr
document : Document
document =
unsafePerformIO $ mkForeign (FFun "document" []
set : Ptr -> String -> String -> IO ()
set e mem val =
mkForeign (FFun "%0[%1]=%2" [FPtr, FString,
FString] FUnit)
e mem val
get : Ptr -> String -> IO String
get e mem =
mkForeign (FFun "%0[%1]" [FPtr, FString] FString) e
17. 構文定義構文
• Idrisには構文を定義する構文がある!
• 例:ifの定義
boolCase : (x:Bool) -> Lazy a -> Lazy a -> a
boolCase True t e = t
boolCase False t e = e
syntax "if" [test] "then" [t] "else" [e] =
boolCase test t e
(´・_・`).oO(どういう仕組みなんだろう・・・)
18. 使用例
syntax [object] "[" [member] "]" ":=" [value] =
set object member value
syntax [object] "[" [member] "]" =
get object member
main : IO ()
main = do
document["innerHTML"] := "(´・_・`)<つらぽよ"
25. Idrisのいけてないところ
• (僕は)証明が書けない
• 例えば、リストの要素アクセス
• Listは型に長さを含まない
• アクセス時は、添字が長さよりも小さいという証明を与
える必要がある・・・
Idris> :doc List.index
index : (n : Nat) -> (l : List a) -> (ok : lt n (length l) =
-> a
Find a particular element of a list.
Arguments:
ok : lt n (length l) = True -- a proof that the
within
bounds
26. beleave_me!!
Idris> :doc believe_me
believe_me : a -> b
Subvert the type checker. This function is abstract, so it
not reduce in the type checker. Use it with care - it can
in segfaults or worse!
Idris> :doc really_believe_me
really_believe_me : a -> b
Subvert the type checker. This function will reduce in th
checker. Use it with extreme care - it can result in
or worse!