7 | {-------------------------------------------------------------------------------
8 | {-------------------------------------------------------------------------------
9 | This file defines the Algebra interface, which is a pragmatic implementation of
10 | the concept of F-algebra from category theory.
12 | It is pragmatic since it is not defined for an arbitrary category, but rather
13 | for the (vaguely defined) category of Idris types and functions.
15 | Instantiating it for 'other' categories is solved by exposing the carrier of the algebra at the type level, as it allow us to use interface constraints to specify the constraints on the carrier.
16 | This comes at the cost of needing to specify the carrier type explicitly at
17 | every call site.
18 | -------------------------------------------------------------------------------}
19 | -------------------------------------------------------------------------------}
21 | ||| Generalised sum operation
22 | ||| Categorically, an F-Algebra
28 | ||| In many instances below, we assume Num a defines a Rig structure on a
29 | ||| This means we assume the sum operation is both commutative and associative,
30 | ||| allowing us to define the Algebra instance for trees without any additional
31 | ||| assumptions, for instance (TODO unpack why)
37 | -- Does this work for any Applicative? I think not, because in trees we have to choose an order of summation. But that might not impact things?
38 | -- if the sum operation is commutative, then it should not impact things
43 | ||| Summing up leaves of a tree given by the Num a structure
44 | ||| This assumes that Num a is a Rig structure, meaning the sum operation is
45 | ||| assumed to be both commutative and associative.
46 | ||| Otherwise we would need to be expose the data of the ordering and
47 | ||| bracketing by which the summation below was performed
55 | ||| Summing up nodes of a tree given by the Num a structure
56 | ||| This assumes that Num a is a Rig structure, meaning the sum operation is
57 | ||| assumed to be both commutative and associative.
58 | ||| Otherwise we would need to be expose the data of the ordering and
59 | ||| bracketing by which the summation below was performed
67 | ||| Summing up nodes and leaves of a tree given by the Num a structure
68 | ||| This assumes that Num a is a Rig structure, meaning the sum operation is
69 | ||| assumed to be both commutative and associative.
70 | ||| Otherwise we would need to be expose the data of the ordering and
71 | ||| bracketing by which the summation below was performed
79 | -- public export
80 | -- [usualSum] Num a => Applicative f => Algebra BinTreeSame (f a) where
81 | -- reduce (Leaf leaf) = leaf
82 | -- reduce (Node node leftTree rightTree)
83 | -- = let lt = reduce {f=BinTreeSame} leftTree
84 | -- rt = reduce {f=BinTreeSame} rightTree
85 | -- in (uncurry (+)) <$> (liftA2 lt rt)
87 | -- can be simplified by uncommenting the Num (f a) instance in Num.idr
104 | -- public export
105 | -- [appSum] {shape : Vect n Nat} ->
106 | -- Num a => Applicative f =>
107 | -- Algebra (TensorA shape) (f a) using applicativeNum where
108 | -- reduce (TZ val) = val
109 | -- reduce (TS xs) = reduce (reduce <$> xs)
110 | --
111 | -- aa : Algebra (TensorA [2]) (TensorA [3] a) => a
115 | ||| Initial algebra for an endofunctor
118 | ||| One part of the isomorphism
121 | ||| Second part of the isomorphism
137 | ||| For some reason, inlining this function in `ana` causes a compiler error