SlideShare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.
SlideShare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.
Successfully reported this slideshow.
Activate your 14 day free trial to unlock unlimited reading.
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
14.
%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)); }
15.
適当なライブラリを作る
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
16.
構文定義構文
• 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(どういう仕組みなんだろう・・・)
17.
使用例
syntax [object] "[" [member] "]" ":=" [value] =
set object member value
syntax [object] "[" [member] "]" =
get object member
main : IO ()
main = do
document["innerHTML"] := "(´・_・`)<つらぽよ"
24.
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
25.
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!