mirror of
https://github.com/HigherOrderCO/Kind.git
synced 2024-09-11 17:56:48 +03:00
242 lines
5.1 KiB
Plaintext
242 lines
5.1 KiB
Plaintext
|
|
//// Sums a binary tree in parallel, using fold
|
|
//// __(1)__
|
|
//// / \ __(1)__
|
|
//// (1) (2) => / \ => (14)
|
|
//// / \ / \ (4) (9)
|
|
//// (1) (2) (3) (4)
|
|
|
|
//use Tree/{node,leaf}
|
|
|
|
//data Tree A
|
|
//| node (val: A) (lft: (Tree A)) (rgt: (Tree A))
|
|
//| leaf
|
|
|
|
//sum (x: (Tree U60)) : U60 =
|
|
//fold x {
|
|
//node: (+ x.val (+ x.lft x.rgt))
|
|
//leaf: 0
|
|
//}
|
|
|
|
//gen (n: U60) (x: U60) : (Tree U60) =
|
|
//switch n {
|
|
//0: leaf
|
|
//_: (node x (gen n-1 (+ (* x 2) 1)) (gen n-1 (+ (* x 2) 2)))
|
|
//}
|
|
|
|
//main = (sum (gen 16 0))
|
|
|
|
use Tree/{node,leaf,gen,sum}
|
|
|
|
_main: U60 =
|
|
(sum (gen 16 0))
|
|
|
|
//use Nat.{succ,zero}
|
|
|
|
//_main: (List U60) =
|
|
//(List.map _ _ (List.cons _ 1 (List.cons _ 2 (List.cons _ 3 (List.nil _)))) λx(+ x 1))
|
|
|
|
//_main : (Maybe U60) =
|
|
//(Maybe.bind _ _ (Maybe.some _ 1) λx
|
|
//(Maybe.bind _ _ (Maybe.some _ 2) λy
|
|
//(Maybe.some _ (+ x y))))
|
|
|
|
|
|
//main : (Maybe U60) = {
|
|
//x = !bar
|
|
//y = 80
|
|
//for i in [1,2,!(foo y)]:
|
|
//for j in [10,20,30]:
|
|
//x += i
|
|
//if x > j:
|
|
//return x
|
|
//return x + y
|
|
//}
|
|
|
|
|
|
//fold xs:
|
|
//cons:
|
|
//xs.head + xs.tail
|
|
//nil:
|
|
//0
|
|
|
|
//for x in xs:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//_main (a: Nat) (b: Nat) (e: (Equal A a b)) : (Equal A a b) =
|
|
//switch a {
|
|
//succ: switch b {
|
|
//succ: e
|
|
//zero: e
|
|
//}
|
|
//zero: switch b {
|
|
//succ: e
|
|
//zero: ?D
|
|
//}
|
|
//}
|
|
|
|
//λa λb λe
|
|
//use a.P = _
|
|
//use a.succ = λa.pred λb λe ?A
|
|
//use a.zero = λb λe ?B
|
|
//({ (~a a.P a.succ a.zero)
|
|
//: ∀(b: Nat) ∀(e: (Equal A a b)) _}
|
|
//b e)
|
|
|
|
//({(~a
|
|
//_
|
|
//λa.pred λe2 ?A
|
|
//λe2 ?B
|
|
//): ∀(e2: (Equal A a b)) _} e)
|
|
|
|
//_main (a: Nat) (b: Nat) (e: (Equal A a b)) : (Equal A a b) =
|
|
//switch a {
|
|
//succ: switch b {
|
|
//succ: ?A
|
|
//zero: ?B
|
|
//}
|
|
//zero: switch b {
|
|
//succ: ?C
|
|
//zero: ?D
|
|
//}
|
|
//}
|
|
|
|
|
|
//λa (~a _
|
|
//λap λb (~b _
|
|
//λbp λe ?SS
|
|
//λe ?SZ
|
|
//)
|
|
//λb (~b _
|
|
//λbp λe ?ZS
|
|
//λe ?ZZ
|
|
//)
|
|
//)
|
|
|
|
//switch x {
|
|
//}
|
|
|
|
//_main
|
|
//: ∀(A0: *) ∀(X: *) ∀(R: *)
|
|
//use F = ∀(x: X) ∀(a0: A0) R
|
|
//use G = ∀(a0: A0) ∀(x: X) R
|
|
//∀(f: F) G
|
|
//= λA0 λX λR λf
|
|
//λa0 λx
|
|
//(f x a0)
|
|
|
|
//_main
|
|
//: ∀(A0: *) ∀(A1: *) ∀(X: *) ∀(R: *)
|
|
//use F = ∀(x: X) ∀(a0: A0) R
|
|
//use G = ∀(a0: A0) ∀(a1: A1) ∀(x: X) R
|
|
//∀(f: F) G
|
|
//= λA0 λA1 λX λR λf
|
|
//λa0 λa1 λx
|
|
//(f x a0 a1)
|
|
|
|
//_main : ∀(a: Nat) ∀(b: Nat) ∀(e: (Equal A a b)) (Equal A a b) =
|
|
//λa (~a _
|
|
//λap λb (~b _
|
|
//λbp λe ?A
|
|
//λe ?B)
|
|
//λb (~b _
|
|
//λbp λe ?C
|
|
//λe ?D)
|
|
//)
|
|
|
|
|
|
//_main : ∀(a: A) ∀(b: A) ∀(e: (Equal A a b)) (Equal A a b) =
|
|
//λa (~a _
|
|
//λat λb (~b _
|
|
//λbt λe ?SS
|
|
//λbt λe ?SZ
|
|
//)
|
|
//λat λb (~b _
|
|
//λbt λe ?ZS
|
|
//λbt λe ?ZZ
|
|
//)
|
|
//)
|
|
|
|
//_main : ∀(a: A) ∀(b: A) ∀(e: (Equal A a b)) (Equal A a b) =
|
|
//λa (~a _
|
|
//λat λb (at (~b
|
|
//λbt λap (bt λbp (REC ap bp))
|
|
//_
|
|
//))
|
|
//λat λb ?b
|
|
//)
|
|
|
|
//_main : ∀(a: (List Nat)) ∀(b: (List Nat)) ∀(e: (Equal (List Nat) a b)) (Equal (List Nat) a b) =
|
|
//λa
|
|
//(~a _
|
|
//λa.head λa.tail λb (~b _
|
|
//λb.head λb.tail λe ?A
|
|
//λe ?B)
|
|
//λb λe ?C
|
|
//)
|
|
|
|
//_main : ∀(a: (List Nat)) ∀(b: (List Nat)) ∀(e: (Equal (List Nat) a b)) (Equal (List Nat) a b) =
|
|
//λa
|
|
//(~a _
|
|
//λa.fields λb (a.fields (~b _
|
|
//λb.head λb.tail λa.head λa.tail λe ?A
|
|
//λe ?B))
|
|
//λb λe ?C
|
|
//)
|
|
|
|
|
|
//switch a {
|
|
//cons: switch b {
|
|
//cons: ... a.tail ...
|
|
//nil: ... a.tail ...
|
|
//}
|
|
//nil: switch b {
|
|
//cons:
|
|
//nil:
|
|
//}
|
|
//}
|
|
|
|
//_main
|
|
//: use list =
|
|
//(List.cons _ Nat.zero
|
|
//(List.cons _ 1
|
|
//(List.cons _ (Nat.succ (Nat.succ Nat.zero))
|
|
//(List.nil _))))
|
|
//(The (List Nat) list)
|
|
//= ?A
|
|
|
|
|
|
//C0
|
|
//All { nam: "len", inp: Var { nam: "Nat" }, bod:
|
|
//All { nam: "head", inp: Var { nam: "A" }, bod:
|
|
//All { nam: "tail", inp: App { fun: App { fun: Var { nam: "Vector" }, arg: Var { nam: "A" } }, arg: Var { nam: "len" } }, bod:
|
|
//App { fun: App { fun: Var { nam: "P" }, arg: App { fun: Var { nam: "Nat.succ" }, arg: Var { nam: "len" } } }, arg: App { fun: App { fun: App { fun: App { fun: Var { nam: "Vector.cons" }, arg: Var { nam: "A" } }, arg: Var { nam: "len" } }, arg: Var { nam: "head" } }, arg: Var { nam: "tail" } } } } } }
|
|
|
|
//ADT {
|
|
//name: "Vector",
|
|
//pars: ["A"],
|
|
//idxs: [("len", Var { nam: "Nat" })],
|
|
//ctrs: [
|
|
//Constructor {
|
|
//name: "cons",
|
|
//flds: [("len", Var { nam: "Nat" }), ("head", Var { nam: "A" }), ("tail", App { fun: App { fun: Var { nam: "Vector" }, arg: Var { nam: "A" } }, arg: Var { nam: "len" } })],
|
|
//idxs: [App { fun: Var { nam: "Nat.succ" }, arg: Var { nam: "len" } }, App { fun: App { fun: App { fun: App { fun: Var { nam: "Vector.cons" }, arg: Var { nam: "A" } }, arg: Var { nam: "len" } }, arg: Var { nam: "head" } }, arg: Var { nam: "tail" } }]
|
|
//},
|
|
//Constructor {
|
|
//name: "nil",
|
|
//flds: [],
|
|
//idxs: [
|
|
//Var { nam: "Nat.zero" },
|
|
//App { fun: Var { nam: "Vector.nil" }, arg: Var { nam: "A" } }
|
|
//]
|
|
//}
|
|
//]
|
|
//}
|