mirror of
https://github.com/HigherOrderCO/Kind.git
synced 2024-09-11 16:36:13 +03:00
180 lines
5.5 KiB
Plaintext
180 lines
5.5 KiB
Plaintext
// Add_spaces
|
|
// : ∀(n: #U60)
|
|
// (String)
|
|
// = λn
|
|
// (U60.if #(== n #0) String (String.concat " " (Add_spaces #(- n #1))) "")
|
|
|
|
// U60.Map_show
|
|
// : ∀(map: (U60.Map String))
|
|
// ∀(depth: #U60)
|
|
// (String)
|
|
// = λmap λdepth
|
|
// let P = λx(String)
|
|
// let bin = λsize λkey λval λlft λrgt
|
|
// let cct = λa λb (String.concat a b)
|
|
// let spc = λx (cct (Add_spaces depth) x)
|
|
// let nnl = λx (cct String.newline x)
|
|
// let key = (U60.show key)
|
|
// let size = (U60.show size)
|
|
// let a = (nnl (spc (cct "key: " key)))
|
|
// let b = (cct ", size: " size)
|
|
// let c = (cct ", value: " val)
|
|
// let d = (nnl (spc (cct "left: " (U60.Map_show lft #(+ #1 depth)))))
|
|
// let e = (nnl (spc (cct "right: " (U60.Map_show rgt #(+ #1 depth)))))
|
|
// (cct a (cct b (cct c (cct d e ))))
|
|
// let tip = ""
|
|
// (~map P bin tip)
|
|
|
|
// U60.Map.gen
|
|
// : ∀(V: *)
|
|
// ∀(to_val: ∀(n: #U60) V)
|
|
// ∀(n: #U60)
|
|
// (U60.Map V)
|
|
// = λV λto_val λn
|
|
// (U60.Map.gen.go V to_val n (U60.Map.new V))
|
|
|
|
// U60.Map.gen.go
|
|
// : ∀(V: *)
|
|
// ∀(to_val: ∀(n: #U60) V)
|
|
// ∀(n: #U60)
|
|
// ∀(map: (U60.Map V))
|
|
// (U60.Map V)
|
|
// = λV λto_val λn λmap
|
|
// let P = λx ∀(map: (U60.Map String)) (U60.Map String)
|
|
// let true = λmap (U60.Map.set String n (to_val n) map)
|
|
// let false = λmap (U60.Map.gen.go V to_val #(- n #1) (U60.Map.set String n (to_val n) map))
|
|
// ((~(U60.to_bool #(== n #0)) P true false) map)
|
|
|
|
|
|
// U60.fold
|
|
// : ∀(A: *)
|
|
// ∀(f: ∀(n: #U60) ∀(acc: A) A)
|
|
// ∀(nil: A)
|
|
// ∀(n: #U60)
|
|
// A
|
|
// = λA λf λnil λn
|
|
// (U60.fold.go A f nil n)
|
|
|
|
// U60.fold.go
|
|
// : ∀(A: *)
|
|
// ∀(f: ∀(n: #U60) ∀(acc: A) A)
|
|
// ∀(acc: A)
|
|
// ∀(n: #U60)
|
|
// A
|
|
// = λA λf λacc λn
|
|
// (~(U60.to_bool n) λx(A) (U60.fold.go A f (f n acc) #(- n #1)) acc)
|
|
|
|
|
|
// AscendingMap
|
|
// : (U60.Map #U60)
|
|
// =
|
|
// let nil = (U60.Map.new #U60)
|
|
// let f = λn λacc (U60.Map.set #U60 #(- #11 n) #(- #11 n) acc)
|
|
// (U60.fold (U60.Map #U60) f nil #10)
|
|
|
|
// DescendingMap
|
|
// : (U60.Map #U60)
|
|
// =
|
|
// let nil = (U60.Map.new #U60)
|
|
// let f = λn λacc (U60.Map.set #U60 #(+ n #0) n acc)
|
|
// (U60.fold (U60.Map #U60) f nil #10)
|
|
|
|
// U60.Map.sum
|
|
// : ∀(map: (U60.Map #U60))
|
|
// (#U60)
|
|
// = λmap
|
|
// let P = λx(#U60)
|
|
// let bin = λsize λkey λval λlft λrgt
|
|
// let sum = #(+ #(+ key (U60.Map.sum lft)) (U60.Map.sum rgt))
|
|
// sum
|
|
// let tip = #0
|
|
// (~map P bin tip)
|
|
|
|
// Test1
|
|
// : (Maybe String)
|
|
// =
|
|
// let emptyMap = (BBT.tip #U60 String)
|
|
// let updatedMap = (U60.Map.set String #5 "value5" emptyMap)
|
|
// let value = (U60.Map.get String #5 updatedMap)
|
|
// let is_equal = (String.equal (~value λx(String) λsome(some) "none") "value5")
|
|
// (~is_equal λx(Maybe String) (Maybe.none String) (Maybe.some String "Test 1 Failed!"))
|
|
|
|
// Test2
|
|
// : (Maybe String)
|
|
// =
|
|
// let initialMap = (U60.Map.set String #10 "initialValue" (U60.Map.new String))
|
|
// let updatedMap = (U60.Map.set String #10 "updatedValue" initialMap)
|
|
// let value = (U60.Map.get String #10 updatedMap)
|
|
// let is_equal = (String.equal (~value λx(String) λsome(some) "none") "updatedValue")
|
|
// (~is_equal λx(Maybe String) (Maybe.none String) (Maybe.some String "Test 2 Failed!"))
|
|
|
|
// Test3
|
|
// : (Maybe String)
|
|
// =
|
|
// let map = (U60.Map.gen String U60.show #24)
|
|
// let value = (U60.Map.get String #7 map)
|
|
// let is_equal = (String.equal (~value λx(String) λsome(some) "none") "7")
|
|
// (~is_equal λx(Maybe String) (Maybe.none String) (Maybe.some String "Test 3 Failed!"))
|
|
|
|
// Test4
|
|
// : (Maybe String)
|
|
// =
|
|
// let map = (U60.Map.gen String U60.show #5)
|
|
// let value = (U60.Map.get String #6 map)
|
|
// let is_none = (~value λx(Bool) λval(Bool.false) Bool.true)
|
|
// (~is_none λx(Maybe String) (Maybe.none String) (Maybe.some String "Test 4 Failed!"))
|
|
|
|
// Test5
|
|
// : (Maybe String)
|
|
// =
|
|
// let map = AscendingMap
|
|
// let sum = (U60.Map.sum map)
|
|
// let expected_sum = #55
|
|
// let is_equal = (U60.equal sum expected_sum)
|
|
// (~is_equal λx(Maybe String) (Maybe.none String) (Maybe.some String "Test 5 Failed!"))
|
|
|
|
// Test6
|
|
// : (Maybe String)
|
|
// =
|
|
// let map = (U60.Map.gen #U60 λx(x) #20)
|
|
// let new = λfirst_value λmap
|
|
// let new = λlast_value λmap
|
|
// let is_first_correct = (U60.equal (~first_value λx(#U60) λsome(some) #0) #3)
|
|
// let is_last_correct = (U60.equal (~last_value λx(#U60) λsome(some) #0) #10)
|
|
// let are_both_correct = (Bool.and is_first_correct is_last_correct)
|
|
// (~are_both_correct λx(Maybe String) (Maybe.none String) (Maybe.some String "Test 6 Failed!"))
|
|
// (~(U60.Map.got #U60 #10 map) λx(Maybe String) new)
|
|
// (~(U60.Map.got #U60 #3 map) λx(Maybe String) new)
|
|
|
|
|
|
// Tests.run
|
|
// : ∀(tests: (List (Maybe String)))
|
|
// String
|
|
// = λtests
|
|
// let folder = (List.fold (Maybe String) tests)
|
|
// (folder String (λhead λtail (~head λx(String) λsome(some) tail)) "All tests passed!")
|
|
|
|
// RunTests
|
|
// : (String)
|
|
// =
|
|
// let tests = (List.nil (Maybe String))
|
|
// let tests = (List.cons (Maybe String) Test1 tests)
|
|
// let tests = (List.cons (Maybe String) Test2 tests)
|
|
// let tests = (List.cons (Maybe String) Test3 tests)
|
|
// let tests = (List.cons (Maybe String) Test4 tests)
|
|
// let tests = (List.cons (Maybe String) Test5 tests)
|
|
// let tests = (List.cons (Maybe String) Test6 tests)
|
|
// let solution = (Tests.run tests)
|
|
// solution
|
|
|
|
testBBT
|
|
: String
|
|
=
|
|
"
|
|
File commented for backward compatibility.
|
|
To run the tests, uncomment the code on:
|
|
testBBT.kind2
|
|
U60.Map
|
|
"
|
|
// RunTests
|