git.haldean.org ubik / b0e44b1
ctor and type names can't collide Haldean Brown 4 years ago
1 changed file(s) with 19 addition(s) and 19 deletion(s). Raw diff Collapse all Expand all
44 ^ IV = Good = Bad
55
66 ^ Bag
7 = Bag Number Number
7 = BagC Number Number
88
99 # accessors for members, to give them names
10 : n ^ Bag -> Number = \x -> ? x { . Bag n * => n }
11 : good ^ Bag -> Number = \x -> ? x { . Bag * good => good }
10 : n ^ Bag -> Number = \x -> ? x { . BagC n * => n }
11 : good ^ Bag -> Number = \x -> ? x { . BagC * good => good }
1212
1313 # make new bags with a good or a bad value removed
1414 : sub-good ^ Bag -> Bag
15 = \x -> Bag (- (n x) 1) (- (good x) 1)
15 = \x -> BagC (- (n x) 1) (- (good x) 1)
1616 : sub-bad ^ Bag -> Bag
17 = \x -> Bag (- (n x) 1) (good x)
17 = \x -> BagC (- (n x) 1) (good x)
1818
1919 # this should be a tuple...
20 ^ TreePair = TreePair IV Tree
21 : tree ^ TreePair -> Tree = \tp -> ? tp { . TreePair * t => t }
20 ^ TreePair = TreePairC IV Tree
21 : tree ^ TreePair -> Tree = \tp -> ? tp { . TreePairC * t => t }
2222
2323 # ...and this should be an alias
24 ^ Tree = Tree (List TreePair)
24 ^ Tree = TreeC (List TreePair)
2525 # accessor for children
26 : children ^ Tree -> List TreePair = \tree -> ? tree { . Tree l => l }
26 : children ^ Tree -> List TreePair = \tree -> ? tree { . TreeC l => l }
2727
2828 # create a tree by choosing k IVs out of bag b
2929 : make ^ Number -> Bag -> Tree
3030 = \k b -> ? {
31 . eq k 0 => Tree Nil
32 . => Tree ({
31 . eq k 0 => TreeC Nil
32 . => TreeC ({
3333 : ivs = map (\num -> ? {
3434 . math:lt num (good b) => Good
3535 . => Bad
3636 }) (range 0 (n b))
3737 ! map (\iv -> ? iv {
38 . Good => TreePair Good (make (- k 1) (sub-good b))
39 . Bad => TreePair Bad (make (- k 1) (sub-bad b))
38 . Good => TreePairC Good (make (- k 1) (sub-good b))
39 . Bad => TreePairC Bad (make (- k 1) (sub-bad b))
4040 }) ivs
4141 })
4242 }
43 ?: make 0 (Bag 1 1) = Tree Nil
44 ?: make 1 (Bag 1 1) = Tree (Cons (TreePair Good (Tree Nil)) Nil)
45 ?: make 1 (Bag 2 1) = Tree (Cons (TreePair Good (Tree Nil))
46 (Cons (TreePair Bad (Tree Nil)) Nil))
43 ?: make 0 (BagC 1 1) = TreeC Nil
44 ?: make 1 (BagC 1 1) = TreeC (Cons (TreePairC Good (TreeC Nil)) Nil)
45 ?: make 1 (BagC 2 1) = TreeC (Cons (TreePairC Good (TreeC Nil))
46 (Cons (TreePairC Bad (TreeC Nil)) Nil))
4747
4848 # the probability that you would get i good IVs when drawing from the tree.
4949 : prob ^ Tree -> Number -> Number
5555 . Cons * * =>
5656 reduce + 0 (map
5757 (\child -> * (/ 1 nc) (prob (tree child) (? child {
58 . TreePair * iv => ? iv {
58 . TreePairC * iv => ? iv {
5959 . Good => (- i 1)
6060 . Bad => i
6161 }
6565 }
6666
6767 ! {
68 : t = make 2 (Bag 2 1)
68 : t = make 2 (BagC 2 1)
6969 ! emit (humanize (prob t 1))
7070 }