Merge pull request #8 from imaqtkatt/refactor-book

Refactor book
This commit is contained in:
Victor Taelin 2024-03-22 15:38:06 -03:00 committed by GitHub
commit 2764c4c17d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
57 changed files with 1072 additions and 612 deletions

View File

@ -1,15 +1,19 @@
BBT //BBT
: ∀(K: *) ∀(V: *) * //: ∀(K: *) ∀(V: *) *
= λK λV //= λK λV
$(self: (BBT K V)) //$(self: (BBT K V))
∀(P: ∀(bbt: (BBT K V)) *) //∀(P: ∀(bbt: (BBT K V)) *)
∀(bin: //∀(bin:
∀(size: U60) //∀(size: U60)
∀(key: K) //∀(key: K)
∀(val: V) //∀(val: V)
∀(lft: (BBT K V)) //∀(lft: (BBT K V))
∀(rgt: (BBT K V)) //∀(rgt: (BBT K V))
(P (BBT.bin K V size key val lft rgt)) //(P (BBT.bin K V size key val lft rgt))
) //)
∀(tip: (P (BBT.tip K V))) //∀(tip: (P (BBT.tip K V)))
(P self) //(P self)
data BBT K V
| bin (size: U60) (key: K) (val: V) (lft: (BBT K V)) (rgt: (BBT K V))
| tip

View File

@ -1,66 +1,97 @@
BBT.balance use BBT/{bin,tip}
: ∀(K: *) use Bool/{true,false}
∀(V: *)
∀(cmp: ∀(a: K) ∀(b: K) Cmp) balance <K> <V>
∀(set_key: K) (cmp: ∀(a: K) ∀(b: K) Cmp)
∀(node_key: K) (set_key: K)
∀(val: V) (node_key: K)
∀(lft: (BBT K V)) (val: V)
∀(rgt: (BBT K V)) (lft: (BBT K V))
(BBT K V) (rgt: (BBT K V))
= λK λV λcmp λset_key λnode_key λval λlft λrgt : (BBT K V) =
use P = λx (BBT K V) match got_lft = (BBT/got_size lft) {
use new = λlft.size λlft Pair/new:
use P = λx (BBT K V) match got_rgt = (BBT/got_size rgt) {
use new = λrgt.size λrgt Pair/new:
use new_size = (+ 1 (U60.max lft.size rgt.size)) use new_size = (+ got_lft.fst got_rgt.fst)
use balance = (U60.abs_diff lft.size rgt.size) use balance = (U60/abs_diff got_lft.fst got_rgt.fst)
use P = λx
∀(new_size: U60) match _ = (U60/to_bool (> balance 1)) {
∀(node_key: K) true:
∀(val: V) match _ = (U60/to_bool (< got_lft.fst got_rgt.fst)) {
∀(lft: (BBT K V)) true: (BBT/balance/lft_heavier cmp new_size node_key set_key val lft rgt)
∀(rgt: (BBT K V)) false: (BBT/balance/rgt_heavier cmp new_size node_key set_key val lft rgt)
(BBT K V) }
use true = λnew_size λnode_key λval λlft λrgt false: (bin K V new_size node_key val lft rgt)
use P = λx }
∀(K: *) }
∀(V: *) }
∀(cmp: ∀(a: K) ∀(b: K) Cmp)
∀(new_size: U60)
∀(node_key: K)
∀(set_key: K) //BBT.balance
∀(val: V) //: ∀(K: *)
∀(lft: (BBT K V)) //∀(V: *)
∀(rgt: (BBT K V)) //∀(cmp: ∀(a: K) ∀(b: K) Cmp)
(BBT K V) //∀(set_key: K)
use true = BBT.balance.lft_heavier //∀(node_key: K)
use false = BBT.balance.rgt_heavier //∀(val: V)
(~(U60.to_bool (< rgt.size lft.size)) //∀(lft: (BBT K V))
P //∀(rgt: (BBT K V))
true //(BBT K V)
false //= λK λV λcmp λset_key λnode_key λval λlft λrgt
K //use P = λx (BBT K V)
V //use new = λlft.size λlft
cmp //use P = λx (BBT K V)
new_size //use new = λrgt.size λrgt
node_key //use new_size = (+ 1 (U60.max lft.size rgt.size))
set_key //use balance = (U60.abs_diff lft.size rgt.size)
val //use P = λx
lft //∀(new_size: U60)
rgt //∀(node_key: K)
) //∀(val: V)
use false = λnew_size λnode_key λval λlft λrgt //∀(lft: (BBT K V))
(BBT.bin K V new_size node_key val lft rgt) //∀(rgt: (BBT K V))
(~(U60.to_bool (> balance 1)) //(BBT K V)
P //use true = λnew_size λnode_key λval λlft λrgt
true //use P = λx
false //∀(K: *)
new_size //∀(V: *)
node_key //∀(cmp: ∀(a: K) ∀(b: K) Cmp)
val //∀(new_size: U60)
lft //∀(node_key: K)
rgt //∀(set_key: K)
) //∀(val: V)
(~(BBT.got_size K V rgt) P new) //∀(lft: (BBT K V))
(~(BBT.got_size K V lft) P new) //∀(rgt: (BBT K V))
//(BBT K V)
//use true = BBT.balance.lft_heavier
//use false = BBT.balance.rgt_heavier
//(~(U60.to_bool (< rgt.size lft.size))
//P
//true
//false
//K
//V
//cmp
//new_size
//node_key
//set_key
//val
//lft
//rgt
//)
//use false = λnew_size λnode_key λval λlft λrgt
//(BBT.bin K V new_size node_key val lft rgt)
//(~(U60.to_bool (> balance 1))
//P
//true
//false
//new_size
//node_key
//val
//lft
//rgt
//)
//(~(BBT.got_size K V rgt) P new)
//(~(BBT.got_size K V lft) P new)

View File

@ -1,53 +1,80 @@
BBT.balance.lft_heavier use BBT/{bin,tip,lft_rotate,rgt_rotate}
: ∀(K: *) use Bool/{true,false}
∀(V: *)
∀(cmp: ∀(a: K) ∀(b: K) Cmp) lft_heavier <K> <V>
∀(new_size: U60) (cmp: ∀(a: K) ∀(b: K) Cmp)
∀(node_key: K) (new_size: U60)
∀(set_key: K) (node_key: K)
∀(val: V) (set_key: K)
∀(lft: (BBT K V)) (val: V)
∀(rgt: (BBT K V)) (lft: (BBT K V))
(BBT K V) (rgt: (BBT K V))
= λK λV λcmp λnew_size λnode_key λset_key λval λlft λrgt : (BBT K V) =
use P = λx (BBT K V) match lft {
use bin = λlft.size λlft.key λlft.val λlft.lft λlft.rgt bin:
use P = λx match _ = (Cmp/is_gtn (cmp set_key lft.key)) {
∀(new_size: U60) true:
∀(key: K) use lft = (lft_rotate lft.size lft.key lft.val lft.lft lft.rgt)
∀(val: V) (rgt_rotate new_size lft.key val lft rgt)
∀(lft.key: K) false:
∀(lft.val: V) use lft = (bin K V lft.size lft.key lft.val lft.lft lft.rgt)
∀(lft.lft: (BBT K V)) (rgt_rotate new_size lft.key val lft rgt)
∀(lft.rgt: (BBT K V)) }
∀(rgt: (BBT K V)) tip: (tip K V)
(BBT K V) }
use true = λnew_size λkey λval λlft.key λlft.val λlft.lft λlft.rgt λrgt
use lft = (BBT.lft_rotate
K
V //BBT.balance.lft_heavier
lft.size //: ∀(K: *)
lft.key //∀(V: *)
lft.val //∀(cmp: ∀(a: K) ∀(b: K) Cmp)
lft.lft //∀(new_size: U60)
lft.rgt //∀(node_key: K)
) //∀(set_key: K)
(BBT.rgt_rotate K V new_size key val lft rgt) //∀(val: V)
use false = λnew_size λkey λval λlft.key λlft.val λlft.lft λlft.rgt λrgt //∀(lft: (BBT K V))
use lft = (BBT.bin K V lft.size lft.key lft.val lft.lft lft.rgt) //∀(rgt: (BBT K V))
(BBT.rgt_rotate K V new_size key val lft rgt) //(BBT K V)
(~(Cmp.is_gtn (cmp set_key lft.key)) //= λK λV λcmp λnew_size λnode_key λset_key λval λlft λrgt
P //use P = λx (BBT K V)
true //use bin = λlft.size λlft.key λlft.val λlft.lft λlft.rgt
false //use P = λx
new_size //∀(new_size: U60)
node_key //∀(key: K)
val //∀(val: V)
lft.key //∀(lft.key: K)
lft.val //∀(lft.val: V)
lft.lft //∀(lft.lft: (BBT K V))
lft.rgt //∀(lft.rgt: (BBT K V))
rgt //∀(rgt: (BBT K V))
) //(BBT K V)
use tip = (BBT.tip K V) //use true = λnew_size λkey λval λlft.key λlft.val λlft.lft λlft.rgt λrgt
(~lft P bin tip) //use lft = (BBT.lft_rotate
//K
//V
//lft.size
//lft.key
//lft.val
//lft.lft
//lft.rgt
//)
//(BBT.rgt_rotate K V new_size key val lft rgt)
//use false = λnew_size λkey λval λlft.key λlft.val λlft.lft λlft.rgt λrgt
//use lft = (BBT.bin K V lft.size lft.key lft.val lft.lft lft.rgt)
//(BBT.rgt_rotate K V new_size key val lft rgt)
//(~(Cmp.is_gtn (cmp set_key lft.key))
//P
//true
//false
//new_size
//node_key
//val
//lft.key
//lft.val
//lft.lft
//lft.rgt
//rgt
//)
//use tip = (BBT.tip K V)
//(~lft P bin tip)

View File

@ -1,53 +1,80 @@
BBT.balance.rgt_heavier use BBT/{bin,tip,lft_rotate,rgt_rotate}
: ∀(K: *) use Bool/{true,false}
∀(V: *)
∀(cmp: ∀(a: K) ∀(b: K) Cmp) rgt_heavier <K> <V>
∀(new_size: U60) (cmp: ∀(a: K) ∀(b: K) Cmp)
∀(node_key: K) (new_size: U60)
∀(set_key: K) (node_key: K)
∀(val: V) (set_key: K)
∀(lft: (BBT K V)) (val: V)
∀(rgt: (BBT K V)) (lft: (BBT K V))
(BBT K V) (rgt: (BBT K V))
= λK λV λcmp λnew_size λnode_key λset_key λval λlft λrgt : (BBT K V) =
use P = λx (BBT K V) match rgt {
use bin = λrgt.size λrgt.key λrgt.val λrgt.lft λrgt.rgt bin:
use P = λx match _ = (Cmp/is_gtn (cmp set_key rgt.key)) {
∀(new_size: U60) true:
∀(key: K) use rgt = (bin K V rgt.size rgt.key rgt.val rgt.lft rgt.rgt)
∀(val: V) (lft_rotate new_size rgt.key val lft rgt)
∀(lft: (BBT K V)) false:
∀(rgt.key: K) use rgt = (rgt_rotate rgt.size rgt.key rgt.val rgt.lft rgt.rgt)
∀(rgt.val: V) (lft_rotate new_size rgt.key val lft rgt)
∀(rgt.lft: (BBT K V)) }
∀(rgt.rgt: (BBT K V)) tip: (tip K V)
(BBT K V) }
use true = λnew_size λkey λval λlft λrgt.key λrgt.val λrgt.lft λrgt.rgt
use rgt = (BBT.bin K V rgt.size rgt.key rgt.val rgt.lft rgt.rgt)
(BBT.lft_rotate K V new_size key val lft rgt)
use false = λnew_size λkey λval λlft λrgt.key λrgt.val λrgt.lft λrgt.rgt //BBT.balance.rgt_heavier
use rgt = (BBT.rgt_rotate //: ∀(K: *)
K //∀(V: *)
V //∀(cmp: ∀(a: K) ∀(b: K) Cmp)
rgt.size //∀(new_size: U60)
rgt.key //∀(node_key: K)
rgt.val //∀(set_key: K)
rgt.lft //∀(val: V)
rgt.rgt //∀(lft: (BBT K V))
) //∀(rgt: (BBT K V))
(BBT.lft_rotate K V new_size key val lft rgt) //(BBT K V)
(~(Cmp.is_gtn (cmp set_key rgt.key)) //= λK λV λcmp λnew_size λnode_key λset_key λval λlft λrgt
P //use P = λx (BBT K V)
true //use bin = λrgt.size λrgt.key λrgt.val λrgt.lft λrgt.rgt
false //use P = λx
new_size //∀(new_size: U60)
node_key //∀(key: K)
val //∀(val: V)
lft //∀(lft: (BBT K V))
rgt.key //∀(rgt.key: K)
rgt.val //∀(rgt.val: V)
rgt.lft //∀(rgt.lft: (BBT K V))
rgt.rgt //∀(rgt.rgt: (BBT K V))
) //(BBT K V)
use tip = (BBT.tip K V) //use true = λnew_size λkey λval λlft λrgt.key λrgt.val λrgt.lft λrgt.rgt
(~rgt P bin tip) //use rgt = (BBT.bin K V rgt.size rgt.key rgt.val rgt.lft rgt.rgt)
//(BBT.lft_rotate K V new_size key val lft rgt)
//use false = λnew_size λkey λval λlft λrgt.key λrgt.val λrgt.lft λrgt.rgt
//use rgt = (BBT.rgt_rotate
//K
//V
//rgt.size
//rgt.key
//rgt.val
//rgt.lft
//rgt.rgt
//)
//(BBT.lft_rotate K V new_size key val lft rgt)
//(~(Cmp.is_gtn (cmp set_key rgt.key))
//P
//true
//false
//new_size
//node_key
//val
//lft
//rgt.key
//rgt.val
//rgt.lft
//rgt.rgt
//)
//use tip = (BBT.tip K V)
//(~rgt P bin tip)

View File

@ -8,4 +8,4 @@ BBT.bin
∀(rgt: (BBT K V)) ∀(rgt: (BBT K V))
(BBT K V) (BBT K V)
= λK λV λsize λkey λval λlft λrgt = λK λV λsize λkey λval λlft λrgt
~λP λbin λtip (bin size key val lft rgt) ~λP λbin λtip (bin size key val lft rgt)

View File

@ -1,15 +1,28 @@
BBT.from_list use BBT/{bin,tip,set}
: ∀(K: *)
∀(V: *) from_list <K> <V> (cmp: ∀(a: K) ∀(b: K) Cmp) (list: (List (Pair K V))) : (BBT K V) =
∀(cmp: ∀(a: K) ∀(b: K) Cmp) match list {
∀(list: (List (Pair K V))) List/cons:
(BBT K V) match pair = list.head {
= λK λV λcmp λlist Pair/new: (set cmp pair.fst pair.snd (from_list cmp list.tail))
use P = λx (BBT K V) }
use cons = λhead λtail List/nil: (tip K V)
use P = λx (BBT K V) }
use new = λkey λval
(BBT.set K V cmp key val (BBT.from_list K V cmp tail))
(~head P new)
use nil = (BBT.tip K V) //BBT.from_list
(~list P cons nil) //: ∀(K: *)
//∀(V: *)
//∀(cmp: ∀(a: K) ∀(b: K) Cmp)
//∀(list: (List (Pair K V)))
//(BBT K V)
//= λK λV λcmp λlist
//use P = λx (BBT K V)
//use cons = λhead λtail
//use P = λx (BBT K V)
//use new = λkey λval
//(BBT.set K V cmp key val (BBT.from_list K V cmp tail))
//(~head P new)
//use nil = (BBT.tip K V)
//(~list P cons nil)

View File

@ -1,18 +1,34 @@
BBT.get use BBT/{bin,tip}
: ∀(K: *) use Cmp/{ltn,eql,gtn}
∀(V: *)
∀(cmp: ∀(a: K) ∀(b: K) Cmp) get <K> <V> (cmp: ∀(a: K) ∀(b: K) Cmp) (key: K) (map: (BBT K V)) : (Maybe V) =
∀(key: K) match map {
∀(map: (BBT K V)) bin:
(Maybe V) match _ = (cmp key map.key) {
= λK λV λcmp λkey λmap ltn: (get cmp key map.lft)
use P = λx (Maybe V) eql: (Maybe/some map.val)
use bin = λ_size λnext.key λnext.val λnext.lft λnext.rgt gtn: (get cmp key map.rgt)
use P = λx }
∀(cmp: ∀(a: K) ∀(b: K) Cmp) ∀(key: K) (Maybe V) tip: Maybe/none
use ltn = λcmp λkey (BBT.get K V cmp key next.lft) }
use eql = λcmp λkey (Maybe.some V next.val)
use gtn = λcmp λkey (BBT.get K V cmp key next.rgt)
(~(cmp key next.key) P ltn eql gtn cmp key)
use tip = (Maybe.none V) //BBT.get
(~map P bin tip) //: ∀(K: *)
//∀(V: *)
//∀(cmp: ∀(a: K) ∀(b: K) Cmp)
//∀(key: K)
//∀(map: (BBT K V))
//(Maybe V)
//= λK λV λcmp λkey λmap
//use P = λx (Maybe V)
//use bin = λ_size λnext.key λnext.val λnext.lft λnext.rgt
//use P = λx
//∀(cmp: ∀(a: K) ∀(b: K) Cmp) ∀(key: K) (Maybe V)
//use ltn = λcmp λkey (BBT.get K V cmp key next.lft)
//use eql = λcmp λkey (Maybe.some V next.val)
//use gtn = λcmp λkey (BBT.get K V cmp key next.rgt)
//(~(cmp key next.key) P ltn eql gtn cmp key)
//use tip = (Maybe.none V)
//(~map P bin tip)

View File

@ -1,43 +1,70 @@
BBT.got use BBT/{bin,tip}
: ∀(K: *) use Maybe/{some,none}
∀(V: *) use Cmp/{ltn,eql,gtn}
∀(cmp: ∀(a: K) ∀(b: K) Cmp)
∀(key: K) got <K> <V> (cmp: ∀(a: K) ∀(b: K) Cmp) (key: K) (map: (BBT K V)) : (Pair (Maybe V) (BBT K V)) =
∀(map: (BBT K V)) match map {
(Pair (Maybe V) (BBT K V)) bin:
= λK λV λcmp λkey λmap match _ = (cmp key map.key) {
use P = λx (Pair (Maybe V) (BBT K V)) ltn:
use bin = λsize λnext.key λnext.val λnext.lft λnext.rgt match new_pair = (got cmp key map.lft) {
use P = λx Pair/new:
∀(cmp: ∀(a: K) ∀(b: K) Cmp) ∀(key: K) let map = (bin K V map.size map.key map.val new_pair.snd map.rgt)
(Pair (Maybe V) (BBT K V)) (Pair/new new_pair.fst map)
use ltn = λcmp λkey }
use new_pair = (BBT.got K V cmp key next.lft) eql: (Pair/new (some map.val) map)
use P = λx (Pair (Maybe V) (BBT K V)) gtn:
use new = λval λlft match new_pair = (got cmp key map.rgt) {
use map = (BBT.bin K V size next.key next.val lft next.rgt) Pair/new:
(Pair.new (Maybe V) (BBT K V) val map) let map = (bin K V map.size map.key map.val map.lft new_pair.snd)
(~new_pair P new) (Pair/new new_pair.fst map)
use eql = λcmp λkey }
use map = (BBT.bin K V size next.key next.val next.lft next.rgt) }
(Pair.new tip: (Pair/new none (tip K V))
(Maybe V) }
(BBT K V)
(Maybe.some V next.val)
map
) //BBT.got
use gtn = λcmp λkey //: ∀(K: *)
use new_pair = (BBT.got K V cmp key next.rgt) //∀(V: *)
use P = λx (Pair (Maybe V) (BBT K V)) //∀(cmp: ∀(a: K) ∀(b: K) Cmp)
use new = λval λrgt //∀(key: K)
use map = (BBT.bin K V size next.key next.val next.lft rgt) //∀(map: (BBT K V))
(Pair.new (Maybe V) (BBT K V) val map) //(Pair (Maybe V) (BBT K V))
(~new_pair P new) //= λK λV λcmp λkey λmap
(~(cmp key next.key) P ltn eql gtn cmp key) //use P = λx (Pair (Maybe V) (BBT K V))
use tip = (Pair.new //use bin = λsize λnext.key λnext.val λnext.lft λnext.rgt
(Maybe V) //use P = λx
(BBT K V) //∀(cmp: ∀(a: K) ∀(b: K) Cmp) ∀(key: K)
(Maybe.none V) //(Pair (Maybe V) (BBT K V))
(BBT.tip K V) //use ltn = λcmp λkey
) //use new_pair = (BBT.got K V cmp key next.lft)
(~map P bin tip) //use P = λx (Pair (Maybe V) (BBT K V))
//use new = λval λlft
//use map = (BBT.bin K V size next.key next.val lft next.rgt)
//(Pair.new (Maybe V) (BBT K V) val map)
//(~new_pair P new)
//use eql = λcmp λkey
//use map = (BBT.bin K V size next.key next.val next.lft next.rgt)
//(Pair.new
//(Maybe V)
//(BBT K V)
//(Maybe.some V next.val)
//map
//)
//use gtn = λcmp λkey
//use new_pair = (BBT.got K V cmp key next.rgt)
//use P = λx (Pair (Maybe V) (BBT K V))
//use new = λval λrgt
//use map = (BBT.bin K V size next.key next.val next.lft rgt)
//(Pair.new (Maybe V) (BBT K V) val map)
//(~new_pair P new)
//(~(cmp key next.key) P ltn eql gtn cmp key)
//use tip = (Pair.new
//(Maybe V)
//(BBT K V)
//(Maybe.none V)
//(BBT.tip K V)
//)
//(~map P bin tip)

View File

@ -1,10 +1,20 @@
BBT.got_size use BBT/{bin,tip}
: ∀(K: *) ∀(V: *) ∀(map: (BBT K V))
(Pair U60 (BBT K V)) got_size <K> <V> (map: (BBT K V)) : (Pair U60 (BBT K V)) =
= λK λV λmap match map {
use P = λx (Pair U60 (BBT K V)) bin: (Pair/new map.size map)
use bin = λsize λnext.key λnext.val λnext.lft λnext.rgt tip: (Pair/new 0 (tip K V))
use map = (BBT.bin K V size next.key next.val next.lft next.rgt) }
(Pair.new U60 (BBT K V) size map)
use tip = (Pair.new U60 (BBT K V) 0 (BBT.tip K V))
(~map P bin tip)
//BBT.got_size
//: ∀(K: *) ∀(V: *) ∀(map: (BBT K V))
//(Pair U60 (BBT K V))
//= λK λV λmap
//use P = λx (Pair U60 (BBT K V))
//use bin = λsize λnext.key λnext.val λnext.lft λnext.rgt
//use map = (BBT.bin K V size next.key next.val next.lft next.rgt)
//(Pair.new U60 (BBT K V) size map)
//use tip = (Pair.new U60 (BBT K V) 0 (BBT.tip K V))
//(~map P bin tip)

View File

@ -1,17 +1,33 @@
BBT.has use BBT/{bin,tip}
: ∀(K: *) use Cmp/{ltn,eql,gtn}
∀(V: *)
∀(cmp: ∀(a: K) ∀(b: K) Cmp) has <K> <V> (cmp: ∀(a: K) ∀(b: K) Cmp) (key: K) (map: (BBT K V)) : Bool =
∀(key: K) match map {
∀(map: (BBT K V)) bin:
Bool match _ = (cmp key map.key) {
= λK λV λcmp λkey λmap ltn: (has cmp key map.lft)
use P = λx Bool eql: Bool/true
use bin = λnext.size λnext.key λnext.val λnext.lft λnext.rgt gtn: (has cmp key map.rgt)
use P = λx ∀(cmp: ∀(a: K) ∀(b: K) Cmp) ∀(key: K) Bool }
use ltn = λcmp λkey (BBT.has K V cmp key next.lft) tip: Bool/false
use eql = λcmp λkey Bool.true }
use gtn = λcmp λkey (BBT.has K V cmp key next.rgt)
(~(cmp key next.key) P ltn eql gtn cmp key)
use tip = Bool.false
(~map P bin tip) //BBT.has
//: ∀(K: *)
//∀(V: *)
//∀(cmp: ∀(a: K) ∀(b: K) Cmp)
//∀(key: K)
//∀(map: (BBT K V))
//Bool
//= λK λV λcmp λkey λmap
//use P = λx Bool
//use bin = λnext.size λnext.key λnext.val λnext.lft λnext.rgt
//use P = λx ∀(cmp: ∀(a: K) ∀(b: K) Cmp) ∀(key: K) Bool
//use ltn = λcmp λkey (BBT.has K V cmp key next.lft)
//use eql = λcmp λkey Bool.true
//use gtn = λcmp λkey (BBT.has K V cmp key next.rgt)
//(~(cmp key next.key) P ltn eql gtn cmp key)
//use tip = Bool.false
//(~map P bin tip)

View File

@ -1,51 +1,74 @@
BBT.has.linear use BBT/{bin,tip}
: ∀(K: *) use Bool/{false,true}
∀(V: *) use Cmp/{ltn,eql,gtn}
∀(cmp: ∀(a: K) ∀(b: K) Cmp)
∀(key: K)
∀(map: (BBT K V))
(Pair Bool (BBT K V))
= λK λV λcmp λhas_key λmap
use P = λx (Pair Bool (BBT K V))
use bin = λsize λnode_key λval λlft λrgt
use P = λx
∀(cmp: ∀(a: K) ∀(b: K) Cmp)
∀(size: U60)
∀(node_key: K)
∀(val: V)
∀(lft: (BBT K V))
∀(rgt: (BBT K V))
∀(key: K)
(Pair Bool (BBT K V))
use ltn = λcmp λsize λnode_key λval λlft λrgt λhas_key
use P = λx (Pair Bool (BBT K V))
use new = λbool λlft
use map = (BBT.bin K V size node_key val lft rgt)
(Pair.new Bool (BBT K V) bool map)
(~(BBT.has.linear K V cmp has_key lft) P new)
use eql = λcmp λsize λnode_key λval λlft λrgt λhas_key
(Pair.new
Bool
(BBT K V)
Bool.true
(BBT.bin K V size node_key val lft rgt)
)
use gtn = λcmp λsize λnode_key λval λlft λrgt λhas_key
use P = λx (Pair Bool (BBT K V))
use new = λbool λrgt
use map = (BBT.bin K V size node_key val lft rgt)
(Pair.new Bool (BBT K V) bool rgt)
(~(BBT.has.linear K V cmp has_key rgt) P new)
((~(cmp has_key node_key) P ltn eql gtn)
cmp
size
node_key
val
lft
rgt
has_key
)
use tip = (Pair.new Bool (BBT K V) Bool.false (BBT.tip K V))
(~map P bin tip)
linear <K> <V> (cmp: ∀(a: K) ∀(b: K) Cmp) (key: K) (map: (BBT K V)) : (Pair Bool (BBT K V)) =
match map {
bin:
match _ = (cmp key map.key) {
ltn:
match result = (linear cmp key map.lft) {
Pair/new:
(Pair/new result.fst (bin K V map.size map.key map.val result.snd map.rgt))
}
eql: (Pair/new true (bin K V map.size map.key map.val map.lft map.rgt))
gtn:
match result = (linear cmp key map.rgt) {
Pair/new:
(Pair/new result.fst (bin K V map.size map.key map.val map.lft result.snd))
}
}
tip: (Pair/new false (tip K V))
}
//BBT.has.linear
//: ∀(K: *)
//∀(V: *)
//∀(cmp: ∀(a: K) ∀(b: K) Cmp)
//∀(key: K)
//∀(map: (BBT K V))
//(Pair Bool (BBT K V))
//= λK λV λcmp λhas_key λmap
//use P = λx (Pair Bool (BBT K V))
//use bin = λsize λnode_key λval λlft λrgt
//use P = λx
//∀(cmp: ∀(a: K) ∀(b: K) Cmp)
//∀(size: U60)
//∀(node_key: K)
//∀(val: V)
//∀(lft: (BBT K V))
//∀(rgt: (BBT K V))
//∀(key: K)
//(Pair Bool (BBT K V))
//use ltn = λcmp λsize λnode_key λval λlft λrgt λhas_key
//use P = λx (Pair Bool (BBT K V))
//use new = λbool λlft
//use map = (BBT.bin K V size node_key val lft rgt)
//(Pair.new Bool (BBT K V) bool map)
//(~(BBT.has.linear K V cmp has_key lft) P new)
//use eql = λcmp λsize λnode_key λval λlft λrgt λhas_key
//(Pair.new
//Bool
//(BBT K V)
//Bool.true
//(BBT.bin K V size node_key val lft rgt)
//)
//use gtn = λcmp λsize λnode_key λval λlft λrgt λhas_key
//use P = λx (Pair Bool (BBT K V))
//use new = λbool λrgt
//use map = (BBT.bin K V size node_key val lft rgt)
//(Pair.new Bool (BBT K V) bool rgt)
//(~(BBT.has.linear K V cmp has_key rgt) P new)
//((~(cmp has_key node_key) P ltn eql gtn)
//cmp
//size
//node_key
//val
//lft
//rgt
//has_key
//)
//use tip = (Pair.new Bool (BBT K V) Bool.false (BBT.tip K V))
//(~map P bin tip)

View File

@ -1,18 +1,31 @@
BBT.lft_rotate use BBT/{bin,tip}
: ∀(K: *)
∀(V: *) lft_rotate <K> <V> (size: U60) (key: K) (val: V) (lft: (BBT K V)) (rgt: (BBT K V)) : (BBT K V) =
∀(size: U60) match rgt {
∀(key: K) bin:
∀(val: V) use b = (BBT/new_node key val lft rgt.lft)
∀(lft: (BBT K V)) use a = (BBT/new_node rgt.key rgt.val b rgt.rgt)
∀(rgt: (BBT K V)) a
(BBT K V) tip: (bin K V size key val lft (tip K V))
= λK λV λsize λkey λval λlft λrgt }
use P = λx ∀(key: K) ∀(val: V) ∀(lft: (BBT K V)) (BBT K V)
use bin = λ_size λrgt.key λrgt.val λrgt.lft λrgt.rgt λkey λval λlft
use b = (BBT.new_node K V key val lft rgt.lft)
use a = (BBT.new_node K V rgt.key rgt.val b rgt.rgt) //BBT.lft_rotate
a //: ∀(K: *)
use tip = λkey λval λlft //∀(V: *)
(BBT.bin K V size key val lft (BBT.tip K V)) //∀(size: U60)
(~rgt P bin tip key val lft) //∀(key: K)
//∀(val: V)
//∀(lft: (BBT K V))
//∀(rgt: (BBT K V))
//(BBT K V)
//= λK λV λsize λkey λval λlft λrgt
//use P = λx ∀(key: K) ∀(val: V) ∀(lft: (BBT K V)) (BBT K V)
//use bin = λ_size λrgt.key λrgt.val λrgt.lft λrgt.rgt λkey λval λlft
//use b = (BBT.new_node K V key val lft rgt.lft)
//use a = (BBT.new_node K V rgt.key rgt.val b rgt.rgt)
//a
//use tip = λkey λval λlft
//(BBT.bin K V size key val lft (BBT.tip K V))
//(~rgt P bin tip key val lft)

View File

@ -1,17 +1,30 @@
BBT.new_node use BBT/{bin}
: ∀(K: *)
∀(V: *) new_node <K> <V> (key: K) (val: V) (lft: (BBT K V)) (rgt: (BBT K V)) : (BBT K V) =
∀(key: K) match lft_size = (BBT/got_size lft) {
∀(val: V) Pair/new: match rgt_size = (BBT/got_size rgt) {
∀(lft: (BBT K V)) Pair/new:
∀(rgt: (BBT K V)) use new_size = (+ 1 (U60/max lft_size.fst rgt_size.fst))
(BBT K V) (bin K V new_size key val lft rgt)
= λK λV λkey λval λlft λrgt }
use P = λx (BBT K V) }
use new = λlft.size λlft
use P = λx (BBT K V)
use new = λrgt.size λrgt
use new_size = (+ 1 (U60.max rgt.size lft.size)) //BBT.new_node
(BBT.bin K V new_size key val lft rgt) //: ∀(K: *)
(~(BBT.got_size K V rgt) P new) //∀(V: *)
(~(BBT.got_size K V lft) P new) //∀(key: K)
//∀(val: V)
//∀(lft: (BBT K V))
//∀(rgt: (BBT K V))
//(BBT K V)
//= λK λV λkey λval λlft λrgt
//use P = λx (BBT K V)
//use new = λlft.size λlft
//use P = λx (BBT K V)
//use new = λrgt.size λrgt
//use new_size = (+ 1 (U60.max rgt.size lft.size))
//(BBT.bin K V new_size key val lft rgt)
//(~(BBT.got_size K V rgt) P new)
//(~(BBT.got_size K V lft) P new)

View File

@ -1,18 +1,31 @@
BBT.rgt_rotate use BBT/{bin,tip}
: ∀(K: *)
∀(V: *) rgt_rotate <K> <V> (size: U60) (key: K) (val: V) (lft: (BBT K V)) (rgt: (BBT K V)) : (BBT K V) =
∀(size: U60) match lft {
∀(key: K) bin:
∀(val: V) use b = (BBT/new_node key val lft.rgt rgt)
∀(lft: (BBT K V)) use a = (BBT/new_node lft.key lft.val lft.lft b)
∀(rgt: (BBT K V)) a
(BBT K V) tip: (bin K V size key val (tip K V) rgt)
= λK λV λsize λkey λval λlft λrgt }
use P = λx ∀(key: K) ∀(val: V) ∀(rgt: (BBT K V)) (BBT K V)
use bin = λ_size λlft.key λlft.val λlft.lft λlft.rgt λkey λval λrgt
use b = (BBT.new_node K V key val lft.rgt rgt)
use a = (BBT.new_node K V lft.key lft.val lft.lft b) //BBT.rgt_rotate
a //: ∀(K: *)
use tip = λkey λval λrgt //∀(V: *)
(BBT.bin K V size key val (BBT.tip K V) rgt) //∀(size: U60)
(~lft P bin tip key val rgt) //∀(key: K)
//∀(val: V)
//∀(lft: (BBT K V))
//∀(rgt: (BBT K V))
//(BBT K V)
//= λK λV λsize λkey λval λlft λrgt
//use P = λx ∀(key: K) ∀(val: V) ∀(rgt: (BBT K V)) (BBT K V)
//use bin = λ_size λlft.key λlft.val λlft.lft λlft.rgt λkey λval λrgt
//use b = (BBT.new_node K V key val lft.rgt rgt)
//use a = (BBT.new_node K V lft.key lft.val lft.lft b)
//a
//use tip = λkey λval λrgt
//(BBT.bin K V size key val (BBT.tip K V) rgt)
//(~lft P bin tip key val rgt)

View File

@ -1,61 +1,81 @@
BBT.set use BBT/{bin,tip}
: ∀(K: *) use Cmp/{ltn,eql,gtn}
∀(V: *)
∀(cmp: ∀(a: K) ∀(b: K) Cmp) set <K> <V> (cmp: ∀(a: K) ∀(b: K) Cmp) (key: K) (val: V) (map: (BBT K V)) : (BBT K V) =
∀(key: K) match map {
∀(val: V) bin:
∀(map: (BBT K V)) match _ = (cmp key map.key) {
(BBT K V) ltn:
= λK λV λcmp λkey λval λmap use new_lft = (set cmp key val map.lft)
use P = λx ∀(key: K) ∀(val: V) (BBT K V) (BBT/balance cmp key map.key map.val new_lft map.rgt)
use bin = λsize λnext.key λnext.val λnext.lft λnext.rgt λkey λval eql: (bin K V map.size map.key val map.lft map.rgt)
use P = λx gtn:
∀(key: K) use new_rgt = (set cmp key val map.rgt)
∀(next.key: K) (BBT/balance cmp key map.key map.val map.lft new_rgt)
∀(next.val: V) }
∀(next.lft: (BBT K V)) tip: (BBT/singleton key val)
∀(next.rgt: (BBT K V)) }
∀(key: K)
∀(val: V)
(BBT K V)
use ltn = λkey λnext.key λnext.val λnext.lft λnext.rgt λkey λval //BBT.set
use new_lft = (BBT.set K V cmp key val next.lft) //: ∀(K: *)
(BBT.balance //∀(V: *)
K //∀(cmp: ∀(a: K) ∀(b: K) Cmp)
V //∀(key: K)
cmp //∀(val: V)
key //∀(map: (BBT K V))
next.key //(BBT K V)
next.val //= λK λV λcmp λkey λval λmap
new_lft //use P = λx ∀(key: K) ∀(val: V) (BBT K V)
next.rgt //use bin = λsize λnext.key λnext.val λnext.lft λnext.rgt λkey λval
) //use P = λx
use eql = λkey λnext.key λnext.val λnext.lft λnext.rgt λkey λval //∀(key: K)
(BBT.bin K V size next.key val next.lft next.rgt) //∀(next.key: K)
use gtn = λkey λnext.key λnext.val λnext.lft λnext.rgt λkey λval //∀(next.val: V)
use new_rgt = (BBT.set K V cmp key val next.rgt) //∀(next.lft: (BBT K V))
(BBT.balance //∀(next.rgt: (BBT K V))
K //∀(key: K)
V //∀(val: V)
cmp //(BBT K V)
key //use ltn = λkey λnext.key λnext.val λnext.lft λnext.rgt λkey λval
next.key //use new_lft = (BBT.set K V cmp key val next.lft)
next.val //(BBT.balance
next.lft //K
new_rgt //V
) //cmp
(~(cmp key next.key) //key
P //next.key
ltn //next.val
eql //new_lft
gtn //next.rgt
key //)
next.key //use eql = λkey λnext.key λnext.val λnext.lft λnext.rgt λkey λval
next.val //(BBT.bin K V size next.key val next.lft next.rgt)
next.lft //use gtn = λkey λnext.key λnext.val λnext.lft λnext.rgt λkey λval
next.rgt //use new_rgt = (BBT.set K V cmp key val next.rgt)
key //(BBT.balance
val //K
) //V
use tip = λkey λval (BBT.singleton K V key val) //cmp
(~map P bin tip key val) //key
//next.key
//next.val
//next.lft
//new_rgt
//)
//(~(cmp key next.key)
//P
//ltn
//eql
//gtn
//key
//next.key
//next.val
//next.lft
//next.rgt
//key
//val
//)
//use tip = λkey λval (BBT.singleton K V key val)
//(~map P bin tip key val)

View File

@ -1,4 +1,10 @@
BBT.singleton singleton <K> <V> (key: K) (val: V) : (BBT K V) =
: ∀(K: *) ∀(V: *) ∀(key: K) ∀(val: V) (BBT K V) use tip = (BBT/tip K V)
= λK λV λkey λval (BBT/bin K V 1 key val tip tip)
(BBT.bin K V 1 key val (BBT.tip K V) (BBT.tip K V))
//BBT.singleton
//: ∀(K: *) ∀(V: *) ∀(key: K) ∀(val: V) (BBT K V)
//= λK λV λkey λval
//(BBT.bin K V 1 key val (BBT.tip K V) (BBT.tip K V))

View File

@ -1,3 +1,3 @@
BBT.tip BBT.tip
: ∀(K: *) ∀(V: *) (BBT K V) : ∀(K: *) ∀(V: *) (BBT K V)
= λK λV ~λP λbin λtip tip = λK λV ~λP λbin λtip tip

View File

@ -1,17 +1,32 @@
BBT.to_list use BBT/{bin,tip}
: ∀(K: *) ∀(V: *) ∀(map: (BBT K V))
(List (Pair K V)) to_list <K> <V> (map: (BBT K V)) : (List (Pair K V)) =
= λK λV λmap match map {
use P = λx (List (Pair K V)) bin:
use bin = λsize λkey λval λlft λrgt use lft = (to_list map.lft)
use lft = (BBT.to_list K V lft) use rgt = (to_list map.rgt)
use rgt = (BBT.to_list K V rgt) use pair = (Pair/new map.key map.val)
use pair = (Pair.new K V key val) use list = (List/cons pair (List/concat lft rgt))
use list = (List.cons list
(Pair K V) tip: List/nil
pair }
(List.concat (Pair K V) lft rgt)
)
list
use tip = (List.nil (Pair K V)) //BBT.to_list
(~map P bin tip) //: ∀(K: *) ∀(V: *) ∀(map: (BBT K V))
//(List (Pair K V))
//= λK λV λmap
//use P = λx (List (Pair K V))
//use bin = λsize λkey λval λlft λrgt
//use lft = (BBT.to_list K V lft)
//use rgt = (BBT.to_list K V rgt)
//use pair = (Pair.new K V key val)
//use list = (List.cons
//(Pair K V)
//pair
//(List.concat (Pair K V) lft rgt)
//)
//list
//use tip = (List.nil (Pair K V))
//(~map P bin tip)

3
book/BMap.kind2 Normal file
View File

@ -0,0 +1,3 @@
data BMap A
| node (lft: (BMap A)) (val: A) (rgt: (BMap A))
| leaf

3
book/BMap/leaf.kind2 Normal file
View File

@ -0,0 +1,3 @@
BMap.leaf
: ∀(A: *) (BMap A)
= λA ~λP λnode λleaf leaf

After

Width:  |  Height:  |  Size: 61 B

8
book/BMap/node.kind2 Normal file
View File

@ -0,0 +1,8 @@
BMap.node
: ∀(A: *)
∀(lft: (BMap A))
∀(val: A)
∀(rgt: (BMap A))
(BMap A)
= λA λlft λval λrgt
~λP λnode λleaf (node lft val rgt)

After

Width:  |  Height:  |  Size: 153 B

View File

@ -1,7 +1,7 @@
use Bool/{true,false,and} use Bool/{true,false,and}
and (a: Bool) (b: Bool) : Bool = and (a: Bool) (b: Bool) : Bool =
match b { match a {
true: b true: b
false: false false: false
} }

View File

@ -1,3 +1,8 @@
HVM.load load <A> (file: String) (cont: String -> A) : A =
: ∀(A: *) ∀(file: String) ∀(cont: ∀(x: String) A) A (cont String/nil)
= λA λfile λcont (cont String.nil)
//HVM.load
//: ∀(A: *) ∀(file: String) ∀(cont: ∀(x: String) A) A
//= λA λfile λcont (cont String.nil)

View File

@ -1,3 +1,6 @@
HVM.log log <A> <B> (msg: A) (ret: B) : B =
: ∀(A: *) ∀(B: *) ∀(msg: A) ∀(ret: B) B ret
= λA λB λmsg λret ret
//HVM.log
//: ∀(A: *) ∀(B: *) ∀(msg: A) ∀(ret: B) B
//= λA λB λmsg λret ret

View File

@ -1,3 +1,8 @@
HVM.print print <A> (msg: String) (ret: A) : A =
: ∀(A: *) ∀(msg: String) ∀(ret: A) A ret
= λA λmsg λret ret
//HVM.print
//: ∀(A: *) ∀(msg: String) ∀(ret: A) A
//= λA λmsg λret ret

View File

@ -1,8 +1,18 @@
HVM.print.many use List/{cons,nil}
: ∀(A: *) ∀(msgs: (List String)) ∀(ret: A) A
= λA λmsgs λret many <A> (msgs: (List String)) (ret: A) : A =
use P = λx A match msgs {
use cons = λmsg λmsgs cons: (HVM/print msgs.head (HVM/print/many msgs.tail ret))
(HVM.print A msg (HVM.print.many A msgs ret)) nil: ret
use nil = ret }
(~msgs P cons nil)
//HVM.print.many
//: ∀(A: *) ∀(msgs: (List String)) ∀(ret: A) A
//= λA λmsgs λret
//use P = λx A
//use cons = λmsg λmsgs
//(HVM.print A msg (HVM.print.many A msgs ret))
//use nil = ret
//(~msgs P cons nil)

View File

@ -1,4 +1,9 @@
HVM.save save <A> (file: String) (data: String) (cont: A) : A =
: ∀(A: *) ∀(file: String) ∀(data: String) ∀(cont: A) cont
A
= λA λfile λdata λcont cont
//HVM.save
//: ∀(A: *) ∀(file: String) ∀(data: String) ∀(cont: A)
//A
//= λA λfile λdata λcont cont

View File

@ -1,10 +1,18 @@
IO data IO A
: ∀(A: *) * | print (text: String) (then: ∀(x: Unit) (IO A))
= λA | load (file: String) (then: ∀(x: String) (IO A))
$(self: (IO A)) | save (file: String) (text: String) (then: ∀(x: Unit) (IO A))
∀(P: ∀(x: (IO A)) *) | done (term: A)
∀(print: ∀(text: String) ∀(then: ∀(x: Unit) (IO A)) (P (IO.print A text then)))
∀(load: ∀(file: String) ∀(then: ∀(x: String) (IO A)) (P (IO.load A file then)))
∀(save: ∀(file: String) ∀(text: String) ∀(then: ∀(x: Unit) (IO A)) (P (IO.save A file text then)))
∀(done: ∀(term: A) (P (IO.done A term))) //IO
(P self) //: ∀(A: *) *
//= λA
//$(self: (IO A))
//∀(P: ∀(x: (IO A)) *)
//∀(print: ∀(text: String) ∀(then: ∀(x: Unit) (IO A)) (P (IO.print A text then)))
//∀(load: ∀(file: String) ∀(then: ∀(x: String) (IO A)) (P (IO.load A file then)))
//∀(save: ∀(file: String) ∀(text: String) ∀(then: ∀(x: Unit) (IO A)) (P (IO.save A file text then)))
//∀(done: ∀(term: A) (P (IO.done A term)))
//(P self)

View File

@ -1,21 +1,31 @@
IO.bind bind <A> <B> (a: (IO A)) (b: A -> (IO B)) : (IO B) =
: ∀(A: *) match a {
∀(B: *) IO/print: (IO/print B a.text λx (IO/bind (a.then x) b))
∀(a: (IO A)) IO/load: (IO/load B a.file λs (IO/bind (a.then s) b))
∀(b: ∀(x: A) (IO B)) IO/save: (IO/save B a.file a.text λx (IO/bind (a.then Unit/new) b))
(IO B) IO/done: (b a.term)
= λA λB λa λb }
use P = λx ∀(b: ∀(x: A) (IO B)) (IO B)
use print = λtext λthen λb
(IO.print B text λx (IO.bind A B (then x) b))
use load = λfile λthen λb //IO.bind
(IO.load B file λs (IO.bind A B (then s) b)) //: ∀(A: *)
use save = λfile λdata λthen λb //∀(B: *)
(IO.save //∀(a: (IO A))
B //∀(b: ∀(x: A) (IO B))
file //(IO B)
data //= λA λB λa λb
λx (IO.bind A B (then Unit.one) b) //use P = λx ∀(b: ∀(x: A) (IO B)) (IO B)
) //use print = λtext λthen λb
use done = λterm λb (b term) //(IO.print B text λx (IO.bind A B (then x) b))
(~a P print load save done b) //use load = λfile λthen λb
//(IO.load B file λs (IO.bind A B (then s) b))
//use save = λfile λdata λthen λb
//(IO.save
//B
//file
//data
//λx (IO.bind A B (then Unit.one) b)
//)
//use done = λterm λb (b term)
//(~a P print load save done b)

View File

@ -1,3 +1,8 @@
IO.load.do do (file: String) : (IO String) =
: ∀(file: String) (IO String) (IO/load String file λx (IO/done String x))
= λfile (IO.load String file λx (IO.done String x))
//IO.load.do
//: ∀(file: String) (IO String)
//= λfile (IO.load String file λx (IO.done String x))

View File

@ -1,3 +1,8 @@
IO.print.do do (text: String) : (IO Unit) =
: ∀(text: String) (IO Unit) (IO/print Unit text λx (IO/done Unit x))
= λtext (IO.print Unit text λx (IO.done Unit x))
//IO.print.do
//: ∀(text: String) (IO Unit)
//= λtext (IO.print Unit text λx (IO.done Unit x))

View File

@ -1,12 +1,22 @@
IO.run run <A> (x: (IO A)) : (IO A) =
: ∀(A: *) ∀(x: (IO A)) (IO A) match x {
= λA λx IO/print: (HVM/print x.text (run (x.then Unit/new)))
use P = λx (IO A) IO/load: (HVM/load x.file λs (IO/run (x.then s)))
use print = λtext λthen IO/save: (HVM/save x.file x.text (IO/run (x.then Unit/new)))
(HVM.print (IO A) text (IO.run A (then Unit.one))) IO/done: (IO/done A x.term)
use load = λfile λthen }
(HVM.load (IO A) file λs (IO.run A (then s)))
use save = λfile λtext λthen
(HVM.save (IO A) file text (IO.run A (then Unit.one)))
use done = λterm (IO.done A term) //IO.run
(~x P print load save done) //: ∀(A: *) ∀(x: (IO A)) (IO A)
//= λA λx
//use P = λx (IO A)
//use print = λtext λthen
//(HVM.print (IO A) text (IO.run A (then Unit.one)))
//use load = λfile λthen
//(HVM.load (IO A) file λs (IO.run A (then s)))
//use save = λfile λtext λthen
//(HVM.save (IO A) file text (IO.run A (then Unit.one)))
//use done = λterm (IO.done A term)
//(~x P print load save done)

View File

@ -1,4 +1,9 @@
IO.save.do do (file: String) (text: String) : (IO Unit) =
: ∀(file: String) ∀(text: String) (IO Unit) (IO/save Unit file text λx (IO/done Unit x))
= λfile λtext
(IO.save Unit file text λx (IO.done Unit x))
//IO.save.do
//: ∀(file: String) ∀(text: String) (IO Unit)
//= λfile λtext
//(IO.save Unit file text λx (IO.done Unit x))

8
book/List/and.kind2 Normal file
View File

@ -0,0 +1,8 @@
use List/{cons,nil}
use Bool/{true}
and (list: (List Bool)) : Bool =
fold list {
cons: (Bool/and list.head list.tail)
nil: true
}

11
book/List/drop.kind2 Normal file
View File

@ -0,0 +1,11 @@
use List/{cons,nil}
use Nat/{succ,zero}
drop <A> (n: Nat) (list: (List A)) : (List A) =
match n with (list: (List A)) {
zero: list
succ: match list {
cons: (drop n.pred list.tail)
nil: nil
}
}

10
book/List/filter.kind2 Normal file
View File

@ -0,0 +1,10 @@
use List/{cons,nil}
filter <A> (cond: A -> Bool) (list: (List A)) : (List A) =
match list {
nil: nil
cons:
(Bool/if (cond list.head) (List A)
(cons list.head (filter cond list.tail))
(filter cond list.tail))
}

8
book/List/or.kind2 Normal file
View File

@ -0,0 +1,8 @@
use List/{cons,nil}
use Bool/{false}
or (list: (List Bool)) : Bool =
fold list {
cons: (Bool/or list.head list.tail)
nil: false
}

11
book/List/take.kind2 Normal file
View File

@ -0,0 +1,11 @@
use List/{cons,nil}
use Nat/{succ,zero}
take <A> (n: Nat) (list: (List A)) : (List A) =
match n with (list: (List A)) {
zero: nil
succ: match list {
cons: (cons list.head (take n.pred list.tail))
nil: nil
}
}

10
book/List/zip.kind2 Normal file
View File

@ -0,0 +1,10 @@
use List/{cons,nil}
zip <A> <B> (as: (List A)) (bs: (List B)) : (List (Pair A B)) =
match as with (bs: (List B)) {
cons: match bs {
cons: (cons (Pair/new as.head bs.head) (zip as.tail bs.tail))
nil: nil
}
nil: nil
}

View File

@ -3,8 +3,8 @@ Monad
= λM = λM
$(self: (Monad M)) $(self: (Monad M))
∀(P: ∀(x: (Monad M)) *) ∀(P: ∀(x: (Monad M)) *)
∀(new: ∀(new:
∀(bind: ∀(bind:
∀(A: *) ∀(B: *) ∀(a: (M A)) ∀(b: ∀(a: A) (M B)) ∀(A: *) ∀(B: *) ∀(a: (M A)) ∀(b: ∀(a: A) (M B))
(M B) (M B)
) )
@ -12,3 +12,8 @@ Monad
(P (Monad/new M bind pure)) (P (Monad/new M bind pure))
) )
(P self) (P self)
//data Monad (M: * -> *)
//| new
//(bind: ∀(A: *) ∀(B: *) ∀(a: (M A)) ∀(b: ∀(a: A) (M B)) (M B))
//(pure: ∀(A: *) ∀(a: A) (M A))

11
book/Nat/is_gtn.kind2 Normal file
View File

@ -0,0 +1,11 @@
use Nat/{succ,zero}
use Bool/{true,false}
is_gtn (a: Nat) (b: Nat) : Bool =
match a with (b: Nat) {
zero: false
succ: match b {
zero: true
succ: (is_gtn a.pred b.pred)
}
}

14
book/Nat/is_ltn.kind2 Normal file
View File

@ -0,0 +1,14 @@
use Nat/{succ,zero}
use Bool/{true,false}
is_ltn (a: Nat) (b: Nat) : Bool =
match a with (b: Nat) {
zero: match b {
zero: false
succ: true
}
succ: match b {
zero: false
succ: (is_ltn a.pred b.pred)
}
}

View File

@ -0,0 +1,8 @@
use Nat/{succ,zero}
use Bool/{true,false}
is_ltn_or_eql (a: Nat) (b: Nat) : Bool =
match _ = (Nat/is_ltn a b) {
true: true
false: (Nat/equal a b)
}

7
book/Nat/mul.kind2 Normal file
View File

@ -0,0 +1,7 @@
use Nat/{succ,zero}
mul (a: Nat) (b: Nat) : Nat =
match b {
succ: (Nat/add a (Nat/mul a b.pred))
zero: zero
}

View File

@ -8,4 +8,4 @@ Parser.bind
use P = λx ∀(b: ∀(x: A) (Parser B)) (Parser.Result B) use P = λx ∀(b: ∀(x: A) (Parser B)) (Parser.Result B)
use done = λa.code λa.value λb (b a.value a.code) use done = λa.code λa.value λb (b a.value a.code)
use fail = λa.error λb (Parser.Result.fail B a.error) use fail = λa.error λb (Parser.Result.fail B a.error)
(~(a code) P done fail b) (~(a code) P done fail b)

View File

@ -8,4 +8,4 @@ Parser.char
use false = (Parser.Result.done Char tail head) use false = (Parser.Result.done Char tail head)
(~(Char.is_slash head) P true false) (~(Char.is_slash head) P true false)
use nil = (Parser.Result.fail Char "eof") use nil = (Parser.Result.fail Char "eof")
(~code P cons nil) (~code P cons nil)

View File

@ -1,4 +1,2 @@
String.Map.from_list from_list <V> (list: (List (Pair String V))) : (String/Map V) =
: ∀(V: *) ∀(list: (List (Pair String V))) (BBT/from_list String/cmp list)
(String.Map V)
= λV λlist (BBT.from_list String V String.cmp list)

View File

@ -1,4 +1,2 @@
String.Map.get get <V> (key: String) (map: (String/Map V)) : (Maybe V) =
: ∀(V: *) ∀(key: String) ∀(map: (String.Map V)) (BBT/get String/cmp key map)
(Maybe V)
= λV λkey λmap (BBT.get String V String.cmp key map)

View File

@ -1,4 +1,2 @@
String.Map.got got <V> (key: String) (map: (String/Map V)) : (Pair (Maybe V) (String/Map V)) =
: ∀(V: *) ∀(key: String) ∀(map: (String.Map V)) (BBT/got String/cmp key map)
(Pair (Maybe V) (String.Map V))
= λV λkey λmap (BBT.got String V String.cmp key map)

View File

@ -1,3 +1,2 @@
String.Map.has has <V> (key: String) (map: (BBT String V)) : Bool =
: ∀(V: *) ∀(key: String) ∀(map: (BBT String V)) Bool (BBT/has String/cmp key map)
= λV λkey λmap (BBT.has String V String.cmp key map)

View File

@ -1,5 +1,2 @@
String.Map.has.linear linear <V> (key: String) (map: (BBT String V)) : (Pair Bool (BBT String V)) =
: ∀(V: *) ∀(key: String) ∀(map: (BBT String V)) (BBT/has/linear String/cmp key map)
(Pair Bool (BBT String V))
= λV λkey λmap
(BBT.has.linear String V String.cmp key map)

View File

@ -1,3 +1 @@
String.Map.new new <V> : (String/Map V) = (BBT/tip String V)
: ∀(V: *) (String.Map V)
= λV (BBT.tip String V)

View File

@ -1,8 +1,2 @@
String.Map.set set <V> (key: String) (val: V) (map: (String/Map V)) : (String/Map V) =
: ∀(V: *) (BBT/set String/cmp key val map)
∀(key: String)
∀(val: V)
∀(map: (String.Map V))
(String.Map V)
= λV λkey λval λmap
(BBT.set String V String.cmp key val map)

View File

@ -1,4 +1,2 @@
String.Map.to_list to_list <V> (map: (String/Map V)) : (List (Pair String V)) =
: ∀(V: *) ∀(map: (String.Map V)) (BBT/to_list map)
(List (Pair String V))
= λV λmap (BBT.to_list String V map)

View File

@ -1,7 +1,15 @@
join join (sep: String) (strs: (List String)) : String =
: ∀(sep: String) ∀(strs: (List String)) String match strs {
= λsep λstrs List/cons: (String/concat strs.head (String/join/go sep strs.tail))
use P = λx String List/nil: String/nil
use cons = λh λt (String/concat h (String/join/go sep t)) }
use nil = String/nil
(~strs P cons nil)
//join
//: ∀(sep: String) ∀(strs: (List String)) String
//= λsep λstrs
//use P = λx String
//use cons = λh λt (String/concat h (String/join/go sep t))
//use nil = String/nil
//(~strs P cons nil)

View File

@ -1,11 +1,19 @@
go go (sep: String) (strs: (List String)) : String =
: ∀(sep: String) ∀(strs: (List String)) String match strs {
= λsep λstrs List/cons: (String/concat sep (String/concat strs.head (go sep strs.tail)))
use P = λx String List/nil: String/nil
use cons = λh λt }
(String/concat
sep
(String/concat h (go sep t))
) //go
use nil = String/nil //: ∀(sep: String) ∀(strs: (List String)) String
(~strs P cons nil) //= λsep λstrs
//use P = λx String
//use cons = λh λt
//(String/concat
//sep
//(String/concat h (go sep t))
//)
//use nil = String/nil
//(~strs P cons nil)

View File

@ -1,3 +1,5 @@
String/length length (s: String) : Nat = (List/length s)
: ∀(a: String) Nat
= λa (List/length a) //String/length
//: ∀(a: String) Nat
//= λa (List/length a)