Safe Haskell | Safe |
---|---|
Language | Haskell2010 |
Depth indexed perfect tree as data family.
Synopsis
- data family Tree (n :: Nat) a
- singleton :: a -> Tree 'Z a
- toList :: forall n a. SNatI n => Tree n a -> [a]
- reverse :: forall n a. SNatI n => Tree n a -> Tree n a
- (!) :: SNatI n => Tree n a -> Wrd n -> a
- tabulate :: SNatI n => (Wrd n -> a) -> Tree n a
- leftmost :: SNatI n => Tree n a -> a
- rightmost :: SNatI n => Tree n a -> a
- foldMap :: forall a n m. (Monoid m, SNatI n) => (a -> m) -> Tree n a -> m
- foldMap1 :: forall s a n. (Semigroup s, SNatI n) => (a -> s) -> Tree n a -> s
- ifoldMap :: forall a n m. (Monoid m, SNatI n) => (Wrd n -> a -> m) -> Tree n a -> m
- ifoldMap1 :: forall a n s. (Semigroup s, SNatI n) => (Wrd ('S n) -> a -> s) -> Tree ('S n) a -> s
- foldr :: forall a b n. SNatI n => (a -> b -> b) -> b -> Tree n a -> b
- ifoldr :: forall a b n. SNatI n => (Wrd n -> a -> b -> b) -> b -> Tree n a -> b
- foldr1Map :: forall a b n. SNatI n => (a -> b -> b) -> (a -> b) -> Tree n a -> b
- ifoldr1Map :: (Wrd n -> a -> b -> b) -> (Wrd n -> a -> b) -> Tree n a -> b
- foldl :: forall a b n. SNatI n => (b -> a -> b) -> b -> Tree n a -> b
- ifoldl :: forall a b n. SNatI n => (Wrd n -> b -> a -> b) -> b -> Tree n a -> b
- length :: forall n a. SNatI n => Tree n a -> Int
- null :: Tree n a -> Bool
- sum :: (Num a, SNatI n) => Tree n a -> a
- product :: (Num a, SNatI n) => Tree n a -> a
- map :: forall a b n. SNatI n => (a -> b) -> Tree n a -> Tree n b
- imap :: SNatI n => (Wrd n -> a -> b) -> Tree n a -> Tree n b
- traverse :: forall n f a b. (Applicative f, SNatI n) => (a -> f b) -> Tree n a -> f (Tree n b)
- itraverse :: forall n f a b. (Applicative f, SNatI n) => (Wrd n -> a -> f b) -> Tree n a -> f (Tree n b)
- traverse1 :: forall n f a b. (Apply f, SNatI n) => (a -> f b) -> Tree n a -> f (Tree n b)
- itraverse1 :: forall n f a b. (Apply f, SNatI n) => (Wrd n -> a -> f b) -> Tree n a -> f (Tree n b)
- itraverse_ :: forall n f a b. (Applicative f, SNatI n) => (Wrd n -> a -> f b) -> Tree n a -> f ()
- zipWith :: forall a b c n. SNatI n => (a -> b -> c) -> Tree n a -> Tree n b -> Tree n c
- izipWith :: SNatI n => (Wrd n -> a -> b -> c) -> Tree n a -> Tree n b -> Tree n c
- repeat :: SNatI n => x -> Tree n x
- universe :: SNatI n => Tree n (Wrd n)
- liftArbitrary :: forall n a. SNatI n => Gen a -> Gen (Tree n a)
- liftShrink :: forall n a. SNatI n => (a -> [a]) -> Tree n a -> [Tree n a]
- ensureSpine :: SNatI n => Tree n a -> Tree n a
Documentation
data family Tree (n :: Nat) a Source #
Instances
Construction
Conversions
toList :: forall n a. SNatI n => Tree n a -> [a] Source #
Convert Tree
to list.
>>>
toList $ Node (Node (Leaf 'f') (Leaf 'o')) (Node (Leaf 'o') (Leaf 'd'))
"food"
reverse :: forall n a. SNatI n => Tree n a -> Tree n a Source #
Reverse Tree
.
>>>
let t = Node (Node (Leaf 'a') (Leaf 'b')) (Node (Leaf 'c') (Leaf 'd'))
>>>
reverse t
Node (Node (Leaf 'd') (Leaf 'c')) (Node (Leaf 'b') (Leaf 'a'))
Indexing
(!) :: SNatI n => Tree n a -> Wrd n -> a Source #
Indexing.
>>>
let t = Node (Node (Leaf 'a') (Leaf 'b')) (Node (Leaf 'c') (Leaf 'd'))
>>>
t ! W1 (W0 WE)
'c'
tabulate :: SNatI n => (Wrd n -> a) -> Tree n a Source #
Tabulating, inverse of !
.
>>>
tabulate id :: Tree N.Nat2 (Wrd N.Nat2)
Node (Node (Leaf 0b00) (Leaf 0b01)) (Node (Leaf 0b10) (Leaf 0b11))
Folds
foldMap1 :: forall s a n. (Semigroup s, SNatI n) => (a -> s) -> Tree n a -> s Source #
See Foldable1
.
ifoldMap :: forall a n m. (Monoid m, SNatI n) => (Wrd n -> a -> m) -> Tree n a -> m Source #
See FoldableWithIndex
.
ifoldMap1 :: forall a n s. (Semigroup s, SNatI n) => (Wrd ('S n) -> a -> s) -> Tree ('S n) a -> s Source #
There is no type-class for this :(
ifoldr :: forall a b n. SNatI n => (Wrd n -> a -> b -> b) -> b -> Tree n a -> b Source #
Right fold with an index.
ifoldl :: forall a b n. SNatI n => (Wrd n -> b -> a -> b) -> b -> Tree n a -> b Source #
Left fold with an index.
Special folds
Mapping
map :: forall a b n. SNatI n => (a -> b) -> Tree n a -> Tree n b Source #
>>>
map not $ Node (Leaf True) (Leaf False)
Node (Leaf False) (Leaf True)
imap :: SNatI n => (Wrd n -> a -> b) -> Tree n a -> Tree n b Source #
>>>
let t = Node (Node (Leaf 'a') (Leaf 'b')) (Node (Leaf 'c') (Leaf 'd'))
>>>
imap (,) t
Node (Node (Leaf (0b00,'a')) (Leaf (0b01,'b'))) (Node (Leaf (0b10,'c')) (Leaf (0b11,'d')))
traverse :: forall n f a b. (Applicative f, SNatI n) => (a -> f b) -> Tree n a -> f (Tree n b) Source #
itraverse :: forall n f a b. (Applicative f, SNatI n) => (Wrd n -> a -> f b) -> Tree n a -> f (Tree n b) Source #
itraverse1 :: forall n f a b. (Apply f, SNatI n) => (Wrd n -> a -> f b) -> Tree n a -> f (Tree n b) Source #
itraverse_ :: forall n f a b. (Applicative f, SNatI n) => (Wrd n -> a -> f b) -> Tree n a -> f () Source #
Apply an action to every element of a Tree
and its index, ignoring the results.
Zipping
zipWith :: forall a b c n. SNatI n => (a -> b -> c) -> Tree n a -> Tree n b -> Tree n c Source #
Zip two Tree
s with a function.
izipWith :: SNatI n => (Wrd n -> a -> b -> c) -> Tree n a -> Tree n b -> Tree n c Source #
Zip two Tree
s. with a function that also takes the elements' indices.
repeat :: SNatI n => x -> Tree n x Source #
Repeat value
>>>
repeat 'x' :: Tree N.Nat2 Char
Node (Node (Leaf 'x') (Leaf 'x')) (Node (Leaf 'x') (Leaf 'x'))
Universe
QuickCheck
Ensure spine
ensureSpine :: SNatI n => Tree n a -> Tree n a Source #
Ensure spine.
If we have an undefined Tree
,
>>>
let v = error "err" :: Tree N.Nat2 Char
And insert data into it later:
>>>
let setHead :: a -> Tree N.Nat2 a -> Tree N.Nat2 a; setHead x (Node (Node _ u) v) = Node (Node (Leaf x) u) v
Then without a spine, it will fail:
>>>
leftmost $ setHead 'x' v
*** Exception: err ...
But with the spine, it won't:
>>>
leftmost $ setHead 'x' $ ensureSpine v
'x'