Fix map definitions

This commit is contained in:
caotic123 2021-06-22 17:20:18 -03:00
parent 89e0a7e4f7
commit ce6733d6fc
137 changed files with 436 additions and 421 deletions

View File

@ -39,7 +39,7 @@ type App.KL.Game.Phase {
App.KL.Game.start: App.KL.Game
let phase = App.KL.Game.Phase.draft
let players = Map.new!
let board = Hexagonal.Axial.BBL.new!
let board = Hexagonal.Axial.BBT.new!
let tick = 0#64
let casts = []
App.KL.Game.new(tick, phase, players, board, casts)

View File

@ -1,12 +1,12 @@
App.KL.Game.Board: Type
Hexagonal.Axial.BBL<App.KL.Game.Tile>
Hexagonal.Axial.BBT<App.KL.Game.Tile>
// get tile from board
App.KL.Game.Board.get(
coord: Hexagonal.Axial,
board: App.KL.Game.Board
): Maybe<App.KL.Game.Tile>
Hexagonal.Axial.BBL.get!(coord, board)
Hexagonal.Axial.BBT.get!(coord, board)
// set tile in some coord on board
App.KL.Game.Board.set(
@ -14,7 +14,7 @@ App.KL.Game.Board.set(
tile: App.KL.Game.Tile,
board:App.KL.Game.Board
): App.KL.Game.Board
Hexagonal.Axial.BBL.insert!(coord, tile, board)
Hexagonal.Axial.BBT.set!(coord, tile, board)
// push entity to board
App.KL.Game.Board.push(
@ -60,7 +60,7 @@ App.KL.Game.Board.is_occupied(coord: Hexagonal.Axial, board: App.KL.Game.Board):
// Returns a map with the player Address as the key to its respective coordinate
App.KL.Game.Board.find_players(board: App.KL.Game.Board): Map<Hexagonal.Axial>
let tile_list = Hexagonal.Axial.BBL.to_list!(board)
let tile_list = Hexagonal.Axial.BBT.to_list!(board)
let players = []
for coord_tile in tile_list with players:
let {coord,tile} = coord_tile

View File

@ -4,7 +4,7 @@ App.KL.Game.Board.arena: App.KL.Game.Board
board_size = App.KL.Constants.board_size
n_board_size = I32.neg(board_size)
board = Hexagonal.Axial.BBL.new<App.KL.Game.Tile>
board = Hexagonal.Axial.BBT.new<App.KL.Game.Tile>
// TODO: Define this elsewhere?
// Defines all possible images inside a tile

View File

@ -7,5 +7,5 @@
//(coord, map)
//App.KL.Game.Map.push(coord, App.KL.Game.Map.Entity.animation(App.KL.Game.Animation.new(some(45), App.KL.Game.Sprite.fire)),
//map), coords)
//App.KL.Game.Effect.Result.new<Unit>(Unit.new, map, [], NatMap.new!)
//App.KL.Game.Effect.Result.new<Unit>(Unit.new, map, [], Nat.NatMap.new!)

View File

@ -1,8 +1,8 @@
App.KL.Game.Field.get_by_id(id: Nat): Maybe<App.KL.Game.Field>
NatMap.get!(id, App.KL.Game.Field.get_by_id.map)
Nat.NatMap.get!(id, App.KL.Game.Field.get_by_id.map)
App.KL.Game.Field.get_by_id.map: NatMap<App.KL.Game.Field>
NatMap.from_list!(List.imap!!((i,x) {i, x}, App.KL.Game.Field.list))
App.KL.Game.Field.get_by_id.map: Nat.NatMap<App.KL.Game.Field>
Nat.NatMap.from_list!(List.imap!!((i,x) {i, x}, App.KL.Game.Field.list))
App.KL.Game.Field.get_by_id.default(id: Nat): App.KL.Game.Field
App.KL.Game.Field.get_by_id(id) <> App.KL.Game.Field.Grass.field

View File

@ -1,5 +1,5 @@
App.KL.Game.Hero.get_by_id(id: Nat): Maybe<App.KL.Game.Hero>
NatMap.get!(id, App.KL.Game.Hero.get_by_id.map)
Nat.NatMap.get!(id, App.KL.Game.Hero.get_by_id.map)
App.KL.Game.Hero.get_by_id.map: NatMap<App.KL.Game.Hero>
NatMap.from_list!(List.imap!!((i,x) {i, x}, App.KL.Game.Hero.list))
App.KL.Game.Hero.get_by_id.map: Nat.NatMap<App.KL.Game.Hero>
Nat.NatMap.from_list!(List.imap!!((i,x) {i, x}, App.KL.Game.Hero.list))

View File

@ -47,7 +47,7 @@ App.KL.Game.Phase.Play.draw.board(
mouse: Pair<U32, U32>
img: VoxBox
): VoxBox
map = Hexagonal.Axial.BBL.to_list!(map)
map = Hexagonal.Axial.BBT.to_list!(map)
mouse_coord = Hexagonal.Axial.from_screen_xy(mouse, App.KL.Constants.hexagon_radius, App.KL.Constants.center_x, App.KL.Constants.center_y)
for pos in map with img:
{coord, tile} = pos

View File

@ -1,10 +0,0 @@
App.Kaelin.Event.Buffer.monad.bind<A : Type,
B : Type>
(x : App.Kaelin.Event.Buffer<A>,
f : A -> App.Kaelin.Event.Buffer<B>
) : App.Kaelin.Event.Buffer<B>
(ls) Parser {
get y = x(ls)
get c = f(Pair.snd!!(y), Pair.fst!!(y))
Parser.pure!({ls ++ Pair.fst!!(c), Pair.snd!!(c)})
}

View File

@ -1,2 +0,0 @@
App.Kaelin.Event.Buffer.monad.pure<A : Type>(x : A) : App.Kaelin.Event.Buffer<A>
(ls) Parser.pure!({ls, x})

View File

@ -1,5 +0,0 @@
App.Kaelin.Event.Buffer.monad.run(A : Type, buffer : App.Kaelin.Event.Buffer<A>, str : String) : Maybe<A>
Parser.run!(Parser {
get b = buffer([])
return Pair.snd!!(b)
}, str)

View File

@ -1,4 +0,0 @@
type BBL <K: Type, V: Type> {
tip,
bin(size: U32, key: K, val: V, left: BBL<K,V>, right: BBL<K,V>)
}

View File

@ -1,6 +0,0 @@
BBL.assign(A : Type,
B : Type,
cmp : A -> A -> Cmp
) : BBL<A, B> -> BBL<A, B> -> BBL<A, B>
BBL.foldr_with_key(A, B, BBL<A, B>, (key, value, map) BBL.insert!!(cmp, key, value, map))

View File

@ -1,10 +0,0 @@
BBL.delete_min<K: Type, V: Type>(map: BBL<K,V>): BBL<K,V>
case map {
tip: map,
bin: case map.left {
tip: map.right,
bin:
let new_left = BBL.delete_min<K,V>(map.left)
BBL.balance<K,V>(map.key, map.val, new_left, map.right)
}
}

View File

@ -1,2 +0,0 @@
BBL.foldr<K: Type, V: Type, Z: Type>(f: V -> Z -> Z, z: Z, map : BBL<K,V>): Z
BBL.foldr.go<K,V,Z>(f, z, map)

View File

@ -1,8 +0,0 @@
BBL.foldr.go<K: Type, V: Type, Z: Type>(f: V -> Z -> Z, z: Z, map: BBL<K,V>): Z
case map {
tip: z,
bin:
let right_folded = BBL.foldr.go<K,V,Z>(f, z, map.right)
let new_z = f(map.val, right_folded)
BBL.foldr.go<K,V,Z>(f, new_z, map.left)
}

View File

@ -1,2 +0,0 @@
BBL.foldr_with_key<K: Type, V: Type, Z: Type>(f: K -> V -> Z -> Z, z: Z, map: BBL<K,V>): Z
BBL.foldr_with_key.go<K,V,Z>(f, z, map)

View File

@ -1,12 +0,0 @@
BBL.foldr_with_key.go<K: Type, V: Type, Z: Type>(
f : K -> V -> Z -> Z,
z : Z,
map : BBL<K,V>
) : Z
case map {
tip: z,
bin:
let right_folded = BBL.foldr_with_key.go<K,V,Z>(f, z, map.right)
let new_z = f(map.key, map.val, right_folded)
BBL.foldr_with_key.go<K,V,Z>(f, new_z, map.left)
}

View File

@ -1,4 +0,0 @@
// TODO optmize?
BBL.for<K: Type, V: Type>(map: BBL<K,V>)<A: Type>(a: A, f: K -> V -> A -> A): A
let list = BBL.to_list!!(map)
List.for!(list)!(a, (x, y) f(x@fst, x@snd, y))

View File

@ -1,5 +0,0 @@
BBL.from_list<K: Type, V: Type>(
cmp : K -> K -> Cmp,
xs : List<Pair<K,V>>
) : BBL<K,V>
BBL.from_list.go<K,V>(cmp, BBL.tip<K,V>, xs)

View File

@ -1,13 +0,0 @@
BBL.from_list.go<K: Type, V: Type>(
cmp : K -> K -> Cmp,
acc : BBL<K,V>,
xs : List<Pair<K,V>>
) : BBL<K,V>
case xs {
nil : acc,
cons:
let key = Pair.fst<K,V>(xs.head)
let val = Pair.snd<K,V>(xs.head)
let new_acc = BBL.insert<K,V>(cmp, key, val, acc)
BBL.from_list.go<K,V>(cmp, new_acc, xs.tail)
}

View File

@ -1,9 +0,0 @@
BBL.lookup<K: Type, V: Type>(cmp: K -> K -> Cmp, key: K, map: BBL<K,V>): Maybe<V>
case map {
tip: Maybe.none<V>,
bin: case cmp(key, map.key) {
ltn: BBL.lookup<K,V>(cmp, key, map.left),
eql: Maybe.some<V>(map.val),
gtn: BBL.lookup<K,V>(cmp, key, map.right)
}
}

View File

@ -1,10 +0,0 @@
BBL.lookup_with_default<K: Type, V: Type>(
cmp : K -> K -> Cmp,
key : K,
dft : V,
map : BBL<K,V>
) : V
case BBL.lookup<K,V>(cmp, key, map) as maybe_val {
none: dft,
some: maybe_val.value
}

View File

@ -1,9 +0,0 @@
BBL.map<K: Type, V: Type, Z: Type>(f: V -> Z, map: BBL<K,V>): BBL<K,Z>
case map {
tip: BBL.tip<K,Z>,
bin:
let new_val = f(map.val)
let new_left = BBL.map<K,V,Z>(f, map.left)
let new_right = BBL.map<K,V,Z>(f, map.right)
BBL.node<K,Z>(map.key, new_val, new_left, new_right)
}

View File

@ -1,9 +0,0 @@
BBL.member<K: Type, V: Type>(cmp: K -> K -> Cmp, key: K, map: BBL<K,V>): Bool
case map {
tip: Bool.false,
bin: case cmp(key, map.key) {
ltn: BBL.member<K,V>(cmp, key, map.left),
eql: Bool.true,
gtn: BBL.member<K,V>(cmp, key, map.right)
}
}

View File

@ -1,5 +0,0 @@
BBL.node<K: Type, V: Type>(key: K, val: V, left: BBL<K,V>, right: BBL<K,V>): BBL<K,V>
let size_left = BBL.size!!(left)
let size_right = BBL.size!!(right)
let new_size = U32.add(1u, U32.add(size_left, size_right))
BBL.bin!!(new_size, key, val, left, right)

View File

@ -1,2 +0,0 @@
BBL.not_member<K: Type, V: Type>(cmp: K -> K -> Cmp, key: K, map: BBL<K,V>): Bool
Bool.not(BBL.member<K,V>(cmp, key, map))

View File

@ -1,6 +0,0 @@
BBL.show_tree<K: Type, V: Type>(
tree: BBL<K,V>,
key_show: K -> String,
val_show: V -> String
) : String
BBL.show_tree.go!!(tree, key_show, val_show, 0)

View File

@ -1,2 +0,0 @@
BBL.singleton<K: Type, V: Type>(key: K, val: V): BBL<K,V>
BBL.bin<K,V>(1#32, key, val, BBL.tip!!, BBL.tip!!)

View File

@ -1,14 +0,0 @@
BBL.split_gtn<K: Type, V: Type>(cmp: K -> K-> Cmp, cut: K, map: BBL<K,V>): BBL<K,V>
case map {
tip: map,
bin: case cmp(cut, map.key) {
ltn:
let key = map.key
let val = map.val
let left = BBL.split_gtn<K,V>(cmp, cut, map.left)
let right = map.right
BBL.concat3<K,V>(cmp, key, val, left, right),
eql: map.right,
gtn: BBL.split_gtn<K,V>(cmp, cut, map.right)
}
}

View File

@ -1,14 +0,0 @@
BBL.split_ltn<K: Type, V: Type>(cmp: K -> K-> Cmp, cut: K, map: BBL<K,V>): BBL<K,V>
case map {
tip: map,
bin: case cmp(cut, map.key) {
ltn: BBL.split_ltn<K,V>(cmp, cut, map.left),
eql: map.left,
gtn:
let key = map.key
let val = map.val
let left = map.left
let right = BBL.split_ltn<K,V>(cmp, cut, map.right)
BBL.concat3<K,V>(cmp, key, val, left, right)
}
}

View File

@ -1,9 +0,0 @@
BBL.tests.1 : IO<Unit>
let kvs = [
Pair.new!!(1,"a")
Pair.new!!(2,"b")
Pair.new!!(3,"c")
]
let map = BBL.from_list!!(Nat.cmp, kvs)
let map = BBL.show!!(Nat.show, String.show, map)
IO.print(map)

View File

@ -1,6 +0,0 @@
BBL.to_list<K: Type, V: Type>(map: BBL<K,V>): List<Pair<K,V>>
BBL.foldr_with_key<K,V,List<Pair<K,V>>>(
(k, v, kvs) List.cons<Pair<K,V>>(Pair.new<K,V>(k,v), kvs),
List.nil<Pair<K,V>>,
map
)

View File

@ -1,23 +0,0 @@
BBL.union<K: Type, V: Type>(
cmp : K -> K -> Cmp,
map_a: BBL<K,V>,
map_b: BBL<K,V>
) : BBL<K,V>
case map_a {
tip: case map_b {
tip: BBL.tip<K,V>,
bin: map_b
},
bin: case map_b {
tip: map_a,
bin:
let key = map_b.key
let val = map_b.val
let ltn = BBL.split_ltn<K,V>(cmp, map_b.key, map_a)
let gtn = BBL.split_gtn<K,V>(cmp, map_b.key, map_a)
let left = BBL.union<K,V>(cmp, ltn, map_b.left)
let right = BBL.union<K,V>(cmp, gtn, map_b.right)
BBL.concat3<K,V>(cmp, key, val, left, right)
}
}

View File

@ -1,2 +0,0 @@
BBL.w : U32
3#32

4
base/BBT.kind Normal file
View File

@ -0,0 +1,4 @@
type BBT <K: Type, V: Type> {
tip,
bin(size: U32, key: K, val: V, left: BBT<K,V>, right: BBT<K,V>)
}

View File

@ -1,10 +1,10 @@
BBL.adjust<K: Type, V: Type>(
BBT.adjust<K: Type, V: Type>(
cmp : K -> K -> Cmp,
f : V -> V,
key : K,
map : BBL<K,V>
) : BBL<K,V>
BBL.adjust_with_key<K,V>(
map : BBT<K,V>
) : BBT<K,V>
BBT.adjust_with_key<K,V>(
cmp,
(k, v) f(v),
key,

View File

@ -1,27 +1,27 @@
BBL.adjust_with_key<K: Type, V: Type>(
BBT.adjust_with_key<K: Type, V: Type>(
cmp : K -> K -> Cmp,
f : K -> V -> V,
k : K,
map : BBL<K,V>
) : BBL<K,V>
map : BBT<K,V>
) : BBT<K,V>
case map {
tip: map,
bin:
case cmp(k, map.key) {
// k < map.key, we check the left subtree
ltn:
let new_left = BBL.adjust_with_key<K,V>(cmp, f, k, map.left)
BBL.node<K,V>(map.key, map.val, new_left, map.right),
let new_left = BBT.adjust_with_key<K,V>(cmp, f, k, map.left)
BBT.node<K,V>(map.key, map.val, new_left, map.right),
// we found the key we were looking for, so just
// apply the function
eql:
let new_val = f(map.key, map.val)
BBL.node<K,V>(map.key, new_val, map.left, map.right),
BBT.node<K,V>(map.key, new_val, map.left, map.right),
// k > map.key, we check in the right subtree
gtn:
let new_right = BBL.adjust_with_key<K,V>(cmp, f, k, map.right)
BBL.node<K,V>(map.key, map.val, map.left, map.right)
let new_right = BBT.adjust_with_key<K,V>(cmp, f, k, map.right)
BBT.node<K,V>(map.key, map.val, map.left, map.right)
}
}

6
base/BBT/assign.kind Normal file
View File

@ -0,0 +1,6 @@
BBT.assign(A : Type,
B : Type,
cmp : A -> A -> Cmp
) : BBT<A, B> -> BBT<A, B> -> BBT<A, B>
BBT.foldr_with_key(A, B, BBT<A, B>, (key, value, map) BBT.insert!!(cmp, key, value, map))

View File

@ -1,41 +1,41 @@
BBL.balance<K: Type, V: Type>(k: K, v: V, l: BBL<K,V>, r: BBL<K,V>): BBL<K,V>
let size_l = BBL.size!!(l)
let size_r = BBL.size!!(r)
BBT.balance<K: Type, V: Type>(k: K, v: V, l: BBT<K,V>, r: BBT<K,V>): BBT<K,V>
let size_l = BBT.size!!(l)
let size_r = BBT.size!!(r)
let size_l_plus_size_r = U32.add(size_l, size_r)
let w_x_size_l = U32.mul(BBL.w, size_l)
let w_x_size_r = U32.mul(BBL.w, size_r)
let w_x_size_l = U32.mul(BBT.w, size_l)
let w_x_size_r = U32.mul(BBT.w, size_r)
// size_l_plus_size_r < 2 means that one subtree is empty and the
// other contains only one element, hence the tree cannot be balanced
if U32.ltn(size_l_plus_size_r, 2#32) then
BBL.node!!(k, v, l, r)
BBT.node!!(k, v, l, r)
// size_r > w * size_l means the right subtree is too heavy
else if U32.gtn(size_r, w_x_size_l) then
case r {
// this is an impossible case because we already know that
// the right subtree is too heavy
// question: how to deal with it?
tip: BBL.node!!(k, v, l, r),
tip: BBT.node!!(k, v, l, r),
bin:
let size_rl = BBL.size<K,V>(r.left)
let size_rr = BBL.size<K,V>(r.right)
let size_rl = BBT.size<K,V>(r.left)
let size_rr = BBT.size<K,V>(r.right)
// size_rl < size_rr means we should perform a single left rotation
if U32.ltn(size_rl, size_rr) then
let new_key = r.key
let new_val = r.val
let new_left = BBL.node!!(k, v, l, r.left)
let new_left = BBT.node!!(k, v, l, r.left)
let new_right = r.right
BBL.node!!(new_key, new_val, new_left, new_right)
BBT.node!!(new_key, new_val, new_left, new_right)
// size_rl > size_rr means we should perform a double left rotation
else
// we need to branch on the left subtree of r in order to extract its components
case r.left {
tip: BBL.node!!(k, v, l, r), // impossible case
tip: BBT.node!!(k, v, l, r), // impossible case
bin:
let new_key = r.left.key
let new_val = r.left.val
let new_left = BBL.node!!(k, v, l, r.left.left)
let new_right = BBL.node!!(r.key, r.val, r.left.right, r.right)
BBL.node!!(new_key, new_val, new_left, new_right)
let new_left = BBT.node!!(k, v, l, r.left.left)
let new_right = BBT.node!!(r.key, r.val, r.left.right, r.right)
BBT.node!!(new_key, new_val, new_left, new_right)
}
}
@ -45,30 +45,30 @@ BBL.balance<K: Type, V: Type>(k: K, v: V, l: BBL<K,V>, r: BBL<K,V>): BBL<K,V>
// this is an impossible case because we already know that
// the left subtree is too heavy
// question: how to deal with it?
tip: BBL.node!!(k, v, l, r),
tip: BBT.node!!(k, v, l, r),
bin:
let size_ll = BBL.size<K,V>(l.left)
let size_lr = BBL.size<K,V>(l.right)
let size_ll = BBT.size<K,V>(l.left)
let size_lr = BBT.size<K,V>(l.right)
// size_lr < size_lln means we should perform a single right rotation
if U32.ltn(size_lr, size_ll) then
let new_key = l.key
let new_val = l.val
let new_left = l.left
let new_right = BBL.node!!(k, v, l.right, r)
BBL.node!!(new_key, new_val, new_left, new_right)
let new_right = BBT.node!!(k, v, l.right, r)
BBT.node!!(new_key, new_val, new_left, new_right)
// size_lr > size_ll means we should perform a double right rotation
else
// we need to branch on the right subtree of l in order to
// extract its components and move them to the right
case l.right {
tip: BBL.node!!(k, v, l, r), // impossible case
tip: BBT.node!!(k, v, l, r), // impossible case
bin:
let new_key = l.right.key
let new_val = l.right.val
let new_left = BBL.node!!(l.key, l.val, l.left, l.right.left)
let new_right = BBL.node!!(k, v, l.right.right, r)
BBL.node!!(new_key, new_val, new_left, new_right)
let new_left = BBT.node!!(l.key, l.val, l.left, l.right.left)
let new_right = BBT.node!!(k, v, l.right.right, r)
BBT.node!!(new_key, new_val, new_left, new_right)
}
}
else // neither subtrees are too heavy, no balancing is needed
BBL.node!!(k, v, l, r)
BBT.node!!(k, v, l, r)

View File

@ -1,42 +1,42 @@
BBL.concat3<K: Type, V: Type>(
BBT.concat3<K: Type, V: Type>(
cmp : K -> K -> Cmp,
key : K,
val : V,
left : BBL<K,V>,
right: BBL<K,V>
) : BBL<K,V>
left : BBT<K,V>,
right: BBT<K,V>
) : BBT<K,V>
case left {
tip: case right {
tip: BBL.singleton<K,V>(key, val), // trivial case, both trees are empty
bin: BBL.insert<K,V>(cmp, key, val, right) // if left is empty, we just insert (key,val) in the right tree
tip: BBT.singleton<K,V>(key, val), // trivial case, both trees are empty
bin: BBT.insert<K,V>(cmp, key, val, right) // if left is empty, we just insert (key,val) in the right tree
},
bin: case right {
tip: BBL.insert<K,V>(cmp, key, val, left), // if right is empety, we just insert (key,val) in the left tree
tip: BBT.insert<K,V>(cmp, key, val, left), // if right is empety, we just insert (key,val) in the left tree
// if none of the trees are empty, we need to check if we can
// make a balanced tree by using (key,val) as a new node
// if not, we need to find the largest subtree on the side
// "facing" the smaller tree that is small enough to balance
// with the smaller tree
bin:
let right_is_heavier = U32.ltn(U32.mul(BBL.w, left.size), right.size)
let left_is_heavier = U32.ltn(U32.mul(BBL.w, right.size), left.size)
let right_is_heavier = U32.ltn(U32.mul(BBT.w, left.size), right.size)
let left_is_heavier = U32.ltn(U32.mul(BBT.w, right.size), left.size)
if right_is_heavier then
let new_key = right.key
let new_val = right.val
let new_left = BBL.concat3<K,V>(cmp, key, val, left, right.left)
let new_left = BBT.concat3<K,V>(cmp, key, val, left, right.left)
let new_right = right.right
BBL.balance<K,V>(new_key, new_val, new_left, new_right)
BBT.balance<K,V>(new_key, new_val, new_left, new_right)
else if left_is_heavier then
let new_key = left.key
let new_val = left.val
let new_left = left.left
let new_right = BBL.concat3<K,V>(cmp, key, val, left.right, right)
BBL.balance<K,V>(new_key, new_val, new_left, new_right)
let new_right = BBT.concat3<K,V>(cmp, key, val, left.right, right)
BBT.balance<K,V>(new_key, new_val, new_left, new_right)
else
BBL.node<K,V>(key, val, left, right)
BBT.node<K,V>(key, val, left, right)
}
}

View File

@ -1,26 +1,26 @@
BBL.delete<K: Type, V: Type>(
BBT.delete<K: Type, V: Type>(
cmp : K -> K -> Cmp
key: K,
map: BBL<K, V>,
) : BBL<K, V>
map: BBT<K, V>,
) : BBT<K, V>
case map {
tip: BBL.tip!!
tip: BBT.tip!!
bin:
case cmp(key, map.key) {
// when recursion comes back rebalance the node
ltn: BBL.balance!!(map.key, map.val, BBL.delete!!(cmp, key, map.left), map.right)
ltn: BBT.balance!!(map.key, map.val, BBT.delete!!(cmp, key, map.left), map.right)
// when recursion comes back rebalance the node
gtn: BBL.balance!!(map.key, map.val, map.left, BBL.delete!!(cmp, key, map.right))
gtn: BBT.balance!!(map.key, map.val, map.left, BBT.delete!!(cmp, key, map.right))
eql:
// get min value from right subtree
let min = BBL.min!!(map.right)
let min = BBT.min!!(map.right)
case min {
//no nodes on right, return left; if left is empty then will return empty anyway
none: map.left
some:
open min.value
let right_without_min = BBL.delete_min!!(map.right) // deletes right's min, rebalancing this subtree
BBL.balance!!(min.value.fst, min.value.snd, map.left, right_without_min) // rebalance his own node with the new right tree
let right_without_min = BBT.delete_min!!(map.right) // deletes right's min, rebalancing this subtree
BBT.balance!!(min.value.fst, min.value.snd, map.left, right_without_min) // rebalance his own node with the new right tree
}!
}!
}!

10
base/BBT/delete_min.kind Normal file
View File

@ -0,0 +1,10 @@
BBT.delete_min<K: Type, V: Type>(map: BBT<K,V>): BBT<K,V>
case map {
tip: map,
bin: case map.left {
tip: map.right,
bin:
let new_left = BBT.delete_min<K,V>(map.left)
BBT.balance<K,V>(map.key, map.val, new_left, map.right)
}
}

2
base/BBT/foldr.kind Normal file
View File

@ -0,0 +1,2 @@
BBT.foldr<K: Type, V: Type, Z: Type>(f: V -> Z -> Z, z: Z, map : BBT<K,V>): Z
BBT.foldr.go<K,V,Z>(f, z, map)

8
base/BBT/foldr/go.kind Normal file
View File

@ -0,0 +1,8 @@
BBT.foldr.go<K: Type, V: Type, Z: Type>(f: V -> Z -> Z, z: Z, map: BBT<K,V>): Z
case map {
tip: z,
bin:
let right_folded = BBT.foldr.go<K,V,Z>(f, z, map.right)
let new_z = f(map.val, right_folded)
BBT.foldr.go<K,V,Z>(f, new_z, map.left)
}

View File

@ -0,0 +1,2 @@
BBT.foldr_with_key<K: Type, V: Type, Z: Type>(f: K -> V -> Z -> Z, z: Z, map: BBT<K,V>): Z
BBT.foldr_with_key.go<K,V,Z>(f, z, map)

View File

@ -0,0 +1,12 @@
BBT.foldr_with_key.go<K: Type, V: Type, Z: Type>(
f : K -> V -> Z -> Z,
z : Z,
map : BBT<K,V>
) : Z
case map {
tip: z,
bin:
let right_folded = BBT.foldr_with_key.go<K,V,Z>(f, z, map.right)
let new_z = f(map.key, map.val, right_folded)
BBT.foldr_with_key.go<K,V,Z>(f, new_z, map.left)
}

4
base/BBT/for.kind Normal file
View File

@ -0,0 +1,4 @@
// TODO optmize?
BBT.for<K: Type, V: Type>(map: BBT<K,V>)<A: Type>(a: A, f: K -> V -> A -> A): A
let list = BBT.to_list!!(map)
List.for!(list)!(a, (x, y) f(x@fst, x@snd, y))

5
base/BBT/from_list.kind Normal file
View File

@ -0,0 +1,5 @@
BBT.from_list<K: Type, V: Type>(
cmp : K -> K -> Cmp,
xs : List<Pair<K,V>>
) : BBT<K,V>
BBT.from_list.go<K,V>(cmp, BBT.tip<K,V>, xs)

View File

@ -0,0 +1,13 @@
BBT.from_list.go<K: Type, V: Type>(
cmp : K -> K -> Cmp,
acc : BBT<K,V>,
xs : List<Pair<K,V>>
) : BBT<K,V>
case xs {
nil : acc,
cons:
let key = Pair.fst<K,V>(xs.head)
let val = Pair.snd<K,V>(xs.head)
let new_acc = BBT.insert<K,V>(cmp, key, val, acc)
BBT.from_list.go<K,V>(cmp, new_acc, xs.tail)
}

View File

@ -1,32 +1,32 @@
BBL.insert<K: Type, V: Type>(
BBT.insert<K: Type, V: Type>(
cmp : K -> K -> Cmp,
key : K,
val : V,
map : BBL<K,V>
) : BBL<K,V>
map : BBT<K,V>
) : BBT<K,V>
case map {
tip: BBL.singleton<K,V>(key, val),
tip: BBT.singleton<K,V>(key, val),
bin: case cmp(key, map.key) {
// if key < map.key, the new value should go into the left subtree
ltn:
let new_key = map.key
let new_val = map.val
let new_left = BBL.insert<K,V>(cmp, key, val, map.left)
let new_left = BBT.insert<K,V>(cmp, key, val, map.left)
let new_right = map.right
BBL.balance<K,V>(new_key, new_val, new_left, new_right),
BBT.balance<K,V>(new_key, new_val, new_left, new_right),
// `val` is already in the map, so how do we proceed?
// if we decide to simply return the map unchanged, we are
// ignoring the case where other fields of `val`, which are not
// used in the comparison function, might have changed
// this comes at some extra cost, of course
eql: BBL.node<K,V>(key, val, map.left, map.right),
eql: BBT.node<K,V>(key, val, map.left, map.right),
// if key > map.key, the new value should go into the right subtree
gtn:
let new_key = map.key
let new_val = map.val
let new_left = map.left
let new_right = BBL.insert<K,V>(cmp, key, val, map.right)
BBL.balance<K,V>(new_key, new_val, new_left, new_right)
let new_right = BBT.insert<K,V>(cmp, key, val, map.right)
BBT.balance<K,V>(new_key, new_val, new_left, new_right)
}
}

View File

@ -1,19 +1,19 @@
BBL.is_balanced<K: Type, V: Type>(map: BBL<K,V>): Bool
BBT.is_balanced<K: Type, V: Type>(map: BBT<K,V>): Bool
case map {
tip: Bool.true,
bin:
let size_l = BBL.size!!(map.left)
let size_r = BBL.size!!(map.right)
let max_size_l = U32.mul(BBL.w, size_r)
let max_size_r = U32.mul(BBL.w, size_l)
let size_l = BBT.size!!(map.left)
let size_r = BBT.size!!(map.right)
let max_size_l = U32.mul(BBT.w, size_r)
let max_size_r = U32.mul(BBT.w, size_l)
let size_l_plus_r = U32.add(size_l, size_r)
let sizes_dont_exceed = List.and([
U32.lte(size_l, max_size_r),
U32.lte(size_r, max_size_l)
])
let subtrees_sizes_ok = Bool.or(sizes_dont_exceed, U32.lte(size_l_plus_r, 1#32))
let left_is_balanced = BBL.is_balanced!!(map.left)
let right_is_balanced = BBL.is_balanced!!(map.right)
let left_is_balanced = BBT.is_balanced!!(map.left)
let right_is_balanced = BBT.is_balanced!!(map.right)
List.and([subtrees_sizes_ok, left_is_balanced, right_is_balanced])
}

9
base/BBT/lookup.kind Normal file
View File

@ -0,0 +1,9 @@
BBT.lookup<K: Type, V: Type>(cmp: K -> K -> Cmp, key: K, map: BBT<K,V>): Maybe<V>
case map {
tip: Maybe.none<V>,
bin: case cmp(key, map.key) {
ltn: BBT.lookup<K,V>(cmp, key, map.left),
eql: Maybe.some<V>(map.val),
gtn: BBT.lookup<K,V>(cmp, key, map.right)
}
}

View File

@ -0,0 +1,10 @@
BBT.lookup_with_default<K: Type, V: Type>(
cmp : K -> K -> Cmp,
key : K,
dft : V,
map : BBT<K,V>
) : V
case BBT.lookup<K,V>(cmp, key, map) as maybe_val {
none: dft,
some: maybe_val.value
}

9
base/BBT/map.kind Normal file
View File

@ -0,0 +1,9 @@
BBT.map<K: Type, V: Type, Z: Type>(f: V -> Z, map: BBT<K,V>): BBT<K,Z>
case map {
tip: BBT.tip<K,Z>,
bin:
let new_val = f(map.val)
let new_left = BBT.map<K,V,Z>(f, map.left)
let new_right = BBT.map<K,V,Z>(f, map.right)
BBT.node<K,Z>(map.key, new_val, new_left, new_right)
}

9
base/BBT/member.kind Normal file
View File

@ -0,0 +1,9 @@
BBT.member<K: Type, V: Type>(cmp: K -> K -> Cmp, key: K, map: BBT<K,V>): Bool
case map {
tip: Bool.false,
bin: case cmp(key, map.key) {
ltn: BBT.member<K,V>(cmp, key, map.left),
eql: Bool.true,
gtn: BBT.member<K,V>(cmp, key, map.right)
}
}

View File

@ -1,8 +1,8 @@
BBL.min<K: Type, V: Type>(map: BBL<K,V>): Maybe<Pair<K,V>>
BBT.min<K: Type, V: Type>(map: BBT<K,V>): Maybe<Pair<K,V>>
case map {
tip: Maybe.none<Pair<K,V>>,
bin: case map.left {
tip: Maybe.some<Pair<K,V>>(Pair.new<K,V>(map.key, map.val)),
bin: BBL.min<K,V>(map.left)
bin: BBT.min<K,V>(map.left)
}
}

5
base/BBT/node.kind Normal file
View File

@ -0,0 +1,5 @@
BBT.node<K: Type, V: Type>(key: K, val: V, left: BBT<K,V>, right: BBT<K,V>): BBT<K,V>
let size_left = BBT.size!!(left)
let size_right = BBT.size!!(right)
let new_size = U32.add(1u, U32.add(size_left, size_right))
BBT.bin!!(new_size, key, val, left, right)

2
base/BBT/not_member.kind Normal file
View File

@ -0,0 +1,2 @@
BBT.not_member<K: Type, V: Type>(cmp: K -> K -> Cmp, key: K, map: BBT<K,V>): Bool
Bool.not(BBT.member<K,V>(cmp, key, map))

View File

@ -1,4 +1,4 @@
BBL.null<K: Type, V: Type>(map: BBL<K,V>): Bool
BBT.null<K: Type, V: Type>(map: BBT<K,V>): Bool
case map {
tip: Bool.true,
bin: Bool.false

View File

@ -1,9 +1,9 @@
BBL.show<K: Type, V: Type>(
BBT.show<K: Type, V: Type>(
show_key: K -> String,
show_val: V -> String,
map : BBL<K,V>
map : BBT<K,V>
): String
let show_pair = Pair.show<K,V>(show_key, show_val)
let kvs = BBL.to_list<K,V>(map)
let kvs = BBT.to_list<K,V>(map)
let kvs = List.map<Pair<K,V>, String>(show_pair, kvs)
List.show<String>(String.show, kvs)

6
base/BBT/show_tree.kind Normal file
View File

@ -0,0 +1,6 @@
BBT.show_tree<K: Type, V: Type>(
tree: BBT<K,V>,
key_show: K -> String,
val_show: V -> String
) : String
BBT.show_tree.go!!(tree, key_show, val_show, 0)

View File

@ -1,5 +1,5 @@
BBL.show_tree.go<K: Type, V: Type>(
tree: BBL<K,V>,
BBT.show_tree.go<K: Type, V: Type>(
tree: BBT<K,V>,
key_show: K -> String,
val_show: V -> String,
nivel: Nat
@ -13,6 +13,6 @@ BBL.show_tree.go<K: Type, V: Type>(
| val_show(tree.val)
| "\n"
result
| BBL.show_tree.go!!(tree.left, key_show, val_show, nivel + 1)
| BBL.show_tree.go!!(tree.right, key_show, val_show, nivel + 1)
| BBT.show_tree.go!!(tree.left, key_show, val_show, nivel + 1)
| BBT.show_tree.go!!(tree.right, key_show, val_show, nivel + 1)
}!

2
base/BBT/singleton.kind Normal file
View File

@ -0,0 +1,2 @@
BBT.singleton<K: Type, V: Type>(key: K, val: V): BBT<K,V>
BBT.bin<K,V>(1#32, key, val, BBT.tip!!, BBT.tip!!)

View File

@ -1,4 +1,4 @@
BBL.size<K: Type, V: Type>(map: BBL<K,V>): U32
BBT.size<K: Type, V: Type>(map: BBT<K,V>): U32
case map {
tip: 0#32,
bin: map.size

14
base/BBT/split_gtn.kind Normal file
View File

@ -0,0 +1,14 @@
BBT.split_gtn<K: Type, V: Type>(cmp: K -> K-> Cmp, cut: K, map: BBT<K,V>): BBT<K,V>
case map {
tip: map,
bin: case cmp(cut, map.key) {
ltn:
let key = map.key
let val = map.val
let left = BBT.split_gtn<K,V>(cmp, cut, map.left)
let right = map.right
BBT.concat3<K,V>(cmp, key, val, left, right),
eql: map.right,
gtn: BBT.split_gtn<K,V>(cmp, cut, map.right)
}
}

14
base/BBT/split_ltn.kind Normal file
View File

@ -0,0 +1,14 @@
BBT.split_ltn<K: Type, V: Type>(cmp: K -> K-> Cmp, cut: K, map: BBT<K,V>): BBT<K,V>
case map {
tip: map,
bin: case cmp(cut, map.key) {
ltn: BBT.split_ltn<K,V>(cmp, cut, map.left),
eql: map.left,
gtn:
let key = map.key
let val = map.val
let left = map.left
let right = BBT.split_ltn<K,V>(cmp, cut, map.right)
BBT.concat3<K,V>(cmp, key, val, left, right)
}
}

9
base/BBT/tests/1.kind Normal file
View File

@ -0,0 +1,9 @@
BBT.tests.1 : IO<Unit>
let kvs = [
Pair.new!!(1,"a")
Pair.new!!(2,"b")
Pair.new!!(3,"c")
]
let map = BBT.from_list!!(Nat.cmp, kvs)
let map = BBT.show!!(Nat.show, String.show, map)
IO.print(map)

6
base/BBT/to_list.kind Normal file
View File

@ -0,0 +1,6 @@
BBT.to_list<K: Type, V: Type>(map: BBT<K,V>): List<Pair<K,V>>
BBT.foldr_with_key<K,V,List<Pair<K,V>>>(
(k, v, kvs) List.cons<Pair<K,V>>(Pair.new<K,V>(k,v), kvs),
List.nil<Pair<K,V>>,
map
)

23
base/BBT/union.kind Normal file
View File

@ -0,0 +1,23 @@
BBT.union<K: Type, V: Type>(
cmp : K -> K -> Cmp,
map_a: BBT<K,V>,
map_b: BBT<K,V>
) : BBT<K,V>
case map_a {
tip: case map_b {
tip: BBT.tip<K,V>,
bin: map_b
},
bin: case map_b {
tip: map_a,
bin:
let key = map_b.key
let val = map_b.val
let ltn = BBT.split_ltn<K,V>(cmp, map_b.key, map_a)
let gtn = BBT.split_gtn<K,V>(cmp, map_b.key, map_a)
let left = BBT.union<K,V>(cmp, ltn, map_b.left)
let right = BBT.union<K,V>(cmp, gtn, map_b.right)
BBT.concat3<K,V>(cmp, key, val, left, right)
}
}

2
base/BBT/w.kind Normal file
View File

@ -0,0 +1,2 @@
BBT.w : U32
3#32

12
base/BList.kind Normal file
View File

@ -0,0 +1,12 @@
type BList.BTree(A : Type) {
Leaf(value : A)
Node(size : U32, left : BList.BTree<A>, right : BList.BTree<A>)
}
type BList.Digit<A : Type> {
Zero
One(tree : BList.BTree<A>)
}
BList<A : Type> : Type
List<BList.Digit<A>>

5
base/BList/delete.kind Normal file
View File

@ -0,0 +1,5 @@
BList.delete(A : Type, blist : BList<A>) : Pair<BList.BTree<A>, BList<A>>
case blist {
nil : ?a
cons : ?b
}

11
base/BList/insert.kind Normal file
View File

@ -0,0 +1,11 @@
BList.consTree(A : Type, e : BList.BTree<A>, tree : BList<A>) : BList<A>
case tree {
nil : [BList.Digit.One!(e)]
cons : case tree.head as value {
Zero : BList.Digit.One!(e) & tree.tail
One : BList.Digit.Zero! & BList.consTree!(BList.merge!(e, value.tree), tree.tail)
}
}
BList.insert(A : Type, v : A, tree : BList<A>) : BList<A>
BList.consTree!(BList.BTree.Leaf!(v), tree)

2
base/BList/merge.kind Normal file
View File

@ -0,0 +1,2 @@
BList.merge(A : Type, x : BList.BTree<A>, y : BList.BTree<A>) : BList.BTree<A>
BList.BTree.Node!(BList.size!(x) + BList.size!(y), x, y)

5
base/BList/size.kind Normal file
View File

@ -0,0 +1,5 @@
BList.size(A : Type, tree : BList.BTree<A>) : U32
case tree {
Leaf : 1
Node : tree.size
}

View File

@ -1,7 +0,0 @@
// Deletes a value in a BBL tree
Hexagonal.Axial.BBL.delete<A : Type>(
key : Hexagonal.Axial,
map : Hexagonal.Axial.BBL<A>
): Hexagonal.Axial.BBL<A>
BBL.delete!!(Hexagonal.Axial.cmp, key, map)

View File

@ -1,7 +0,0 @@
// Gets a value in a BBL tree
Hexagonal.Axial.BBL.get<A : Type>(
key : Hexagonal.Axial,
map : Hexagonal.Axial.BBL<A>
): Maybe<A>
BBL.lookup!!(Hexagonal.Axial.cmp, key map)

View File

@ -1,8 +0,0 @@
// Inserts a value to a BBL tree
Hexagonal.Axial.BBL.insert<A : Type>(
key : Hexagonal.Axial,
val : A,
map : Hexagonal.Axial.BBL<A>
): Hexagonal.Axial.BBL<A>
BBL.insert!!(Hexagonal.Axial.cmp, key, val, map)

View File

@ -1,6 +0,0 @@
// Generates a list of Hexagonal Axial coordinates from BBL Keys
Hexagonal.Axial.BBL.keys<A : Type>(
map : Hexagonal.Axial.BBL<A>
): List<Hexagonal.Axial>
BBL.foldr_with_key!!!((key, value, list) key & list, [], map)

View File

@ -1,7 +0,0 @@
// Applies to all values in a A Type map the f Function
Hexagonal.Axial.BBL.map<A : Type, B : Type>(
f : A -> B,
map : Hexagonal.Axial.BBL<A>
) : Hexagonal.Axial.BBL<B>
BBL.map!!!(f, map)

View File

@ -1,3 +0,0 @@
// A new Hexagonal Axial BBL tree
Hexagonal.Axial.BBL.new<A : Type> : Hexagonal.Axial.BBL<A>
BBL.tip!!

View File

@ -1,6 +0,0 @@
// Converts the BBL Map into a List of Hexagonal Axial coordinates
Hexagonal.Axial.BBL.to_list<A: Type>(
map: Hexagonal.Axial.BBL<A>
): List<Pair<Hexagonal.Axial,A>>
BBL.to_list!!(map)

View File

@ -1,6 +0,0 @@
// Unites two results of Actions in a BBL tree
Hexagonal.Axial.BBL.union<A : Type>(
map : Hexagonal.Axial.BBL<A>
): Hexagonal.Axial.BBL<A> -> Hexagonal.Axial.BBL<A>
BBL.union!!(Hexagonal.Axial.cmp, map)

View File

@ -1,6 +0,0 @@
// Gets all values in a map and returns as a List
Hexagonal.Axial.BBL.values<A : Type>(
map : Hexagonal.Axial.BBL<A>
): List<A>
BBL.foldr_with_key!!!((key, value, list) value & list, [], map)

View File

@ -1,3 +1,3 @@
// Type for Balanced Binary Tree in an Hexagonal Axial environment
Hexagonal.Axial.BBL<A : Type> : Type
BBL<Hexagonal.Axial, A>
Hexagonal.Axial.BBT<A : Type> : Type
BBT<Hexagonal.Axial, A>

View File

@ -0,0 +1,7 @@
// Deletes a value in a Map tree
Hexagonal.Axial.BBT.delete<A : Type>(
key : Hexagonal.Axial,
map : Hexagonal.Axial.BBT<A>
): Hexagonal.Axial.BBT<A>
BBT.delete!!(Hexagonal.Axial.cmp, key, map)

View File

@ -1,7 +1,7 @@
// Converts a List of Pair A, B which A is an Hexagonal Axial Coordinate and B a
// value of B type
Hexagonal.Axial.BBL.from_list<A: Type>(
Hexagonal.Axial.BBT.from_list<A: Type>(
list: List<Pair<Hexagonal.Axial,A>>
): Hexagonal.Axial.BBL<A>
): Hexagonal.Axial.BBT<A>
BBL.from_list!!(Hexagonal.Axial.cmp, list)
BBT.from_list!!(Hexagonal.Axial.cmp, list)

View File

@ -0,0 +1,7 @@
// Gets a value in a BBT tree
Hexagonal.Axial.BBT.get<A : Type>(
key : Hexagonal.Axial,
map : Hexagonal.Axial.BBT<A>
): Maybe<A>
BBT.lookup!!(Hexagonal.Axial.cmp, key map)

View File

@ -0,0 +1,6 @@
// Generates a list of Hexagonal Axial coordinates from BBT Keys
Hexagonal.Axial.BBT.keys<A : Type>(
map : Hexagonal.Axial.BBT<A>
): List<Hexagonal.Axial>
BBT.foldr_with_key!!!((key, value, list) key & list, [], map)

View File

@ -0,0 +1,7 @@
// Applies to all values in a A Type map the f Function
Hexagonal.Axial.BBT.map<A : Type, B : Type>(
f : A -> B,
map : Hexagonal.Axial.BBT<A>
) : Hexagonal.Axial.BBT<B>
BBT.map!!!(f, map)

View File

@ -0,0 +1,3 @@
// A new Hexagonal Axial BBT tree
Hexagonal.Axial.BBT.new<A : Type> : Hexagonal.Axial.BBT<A>
BBT.tip!!

View File

@ -0,0 +1,8 @@
// Inserts a value to a BBT tree
Hexagonal.Axial.BBT.set<A : Type>(
key : Hexagonal.Axial,
val : A,
map : Hexagonal.Axial.BBT<A>
): Hexagonal.Axial.BBT<A>
BBT.insert!!(Hexagonal.Axial.cmp, key, val, map)

View File

@ -0,0 +1,6 @@
// Converts the BBT Map into a List of Hexagonal Axial coordinates
Hexagonal.Axial.BBT.to_list<A: Type>(
map: Hexagonal.Axial.BBT<A>
): List<Pair<Hexagonal.Axial,A>>
BBT.to_list!!(map)

View File

@ -0,0 +1,6 @@
// Unites two results of Actions in a BBT tree
Hexagonal.Axial.BBT.union<A : Type>(
map : Hexagonal.Axial.BBT<A>
): Hexagonal.Axial.BBT<A> -> Hexagonal.Axial.BBT<A>
BBT.union!!(Hexagonal.Axial.cmp, map)

View File

@ -0,0 +1,6 @@
// Gets all values in a map and returns as a List
Hexagonal.Axial.BBT.values<A : Type>(
map : Hexagonal.Axial.BBT<A>
): List<A>
BBT.foldr_with_key!!!((key, value, list) value & list, [], map)

View File

@ -1,3 +0,0 @@
Map(V: Type): Type
BBL<String, V>

View File

@ -1,4 +0,0 @@
// Copies all key and vals from b to a
Map.assign<A: Type>(a: Map(A), b: Map(A)): Map(A)
BBL.assign(String, A, String.cmp, b, a)

View File

@ -1,2 +0,0 @@
Map.keys<A: Type>(xs: Map<A>): List<String>
BBL.foldr_with_key!!!((key, value, list) key & list, [], xs)

View File

@ -1,2 +0,0 @@
Map.map<A: Type, B: Type>(fn: A -> B, map: Map(A)): Map(B)
BBL.map!!!(fn, map)

View File

@ -1,2 +0,0 @@
Map.new<A: Type>: Map(A)
BBL.tip!!

Some files were not shown because too many files have changed in this diff Show More