section 11.1 -11.2
・11.1 Base Types
特に構造を持たない型
Bool, Nat, Floatなど
・11.2 The Unit Type
値unitを唯一の要素として持つ型
Base Typeの一種
意味のある値を受け渡ししない場合に
利用する(副作用を用いる場合など)
3
4.
section 11.3
・11.3 DerivedForms
一種の省略形。構文糖衣(*)の事。
Sequence
t1; t2 (λx: Unit . t2) t1
where x FV(t2) [def FV on P.69]
Wildcard
(λ_: S . t) (λx: S . t)
where x FV(t)
* 構文糖衣: 読み書きのしやすさの為に導入される、既に定義されている構文の
書き換えとして定義されたもの.
4
5.
section 11.4 -11.5
・11.4 Ascription
型に関する情報・記法の提供
・特定の型の明示(as T)
・型の省略記法(UU = Unit -> Unit)
・11.5 Let Binding
Derived Formの一種
let x = t1 in t2 (λx: T1 . t2) t1
Evaluation Ruleから、t2中のxはt1に置き換わる
5
6.
section 11.6 -11.8
・11.6 Pairs
2つの要素を持つ値
{v1, v2} : T1 T2 と表現
・11.7 Tuples
Pairの拡張。n個の要素を持つ値
{vi i ∈ 1..n} : {Ti i ∈ 1..n} と表現
・11.8 Records
Tupleの拡張。要素にラベルをつけたもの
{li = vi i ∈ 1..n} : {li : Ti i ∈ 1..n} と表現
ここでは、等価性に順序を考慮する
({x=5, y=1} {y=1, x=5} )
6
General Recursion
・11.11 GeneralRecursion
fix (λx: T1 . t2)
[E-FixBeta]
→ [x ⇨ (fix (λx:T1 . t2))] t2
fix ff = [P.143 評価を進めた]
λx: Nat .
if iszero x then true
else if iszero (pred x) then false
else fix ff (pred (pred x));
このfixの場合、正格評価戦略を取ると、展開が終わらなくなる。
Wikipedia: 不動点コンビネータ(Fix Combinator)なども参照。
12
13.
General Recursion
・Exercise 11.11.1
equal
equal : Nat -> Nat -> Bool
equal = fix ffeq
ffeq : (Nat -> Nat -> Bool) -> Nat -> Nat -> Bool
ffeq = λeq: Nat -> Nat -> Bool .
λx: Nat . λy: Nat .
if and (iszero x) (iszero y) then true
else if or (iszero x) (iszero y) then false
else eq (pred x) (pred x);
13
14.
General Recursion
・Exercise 11.11.1
plus
plus : Nat -> Nat -> Nat
plus = fix ffpl
ffpl : (Nat -> Nat -> Nat) -> Nat -> Nat -> Nat
ffpl = λpl: Nat -> Nat -> Nat .
λx: Nat . λy: Nat .
if iszero y then x
else pl (succ x) (pred y);
14
15.
General Recursion
・Exercise 11.11.1
times
times : Nat -> Nat -> Nat
times = fix fftm
fftm : (Nat -> Nat -> Nat) -> Nat -> Nat -> Nat
fftm = λtm: Nat -> Nat -> Nat .
λx: Nat . λy: Nat .
if iszero y then 0
else plus x (tm x (pred y));
15
16.
General Recursion
・Exercise 11.11.1
factorial
factorial : Nat -> Nat
factorial = fix fffc
fffc : (Nat -> Nat) -> Nat -> Nat
fffc = λfc: Nat -> Nat .
λn: Nat .
if iszero n then 1
else times n (fc (pred n));
16
17.
General Recursion
・11.11 GeneralRecursion
letrec letrecは以下の定義の構文糖衣
letrec x: T1 = t1 in t2 let x = fix (λx: T1 . t1) in t2
letrec iseven : Nat -> Bool =
λx: Nat .
if iszero x then true
else if iszero (pred x) then false
else iseven (pred (pred x))
in
iseven 7;
17
18.
Lists
・11.12 Lists
型Tの要素を持つList型を定義する
List a ::= nil ¦ Cons a List [Defenition of List]
リストの定義からいくつか
v ::= ...
nil[T]
cons[T] v v
Γ-¦ t1:T1 Γ-¦ t2:List T1
[T-Cons]
Γ-¦ cons[T1] t1 t2 : List T1
18