CAPITULO V: DISEÑO DE UN SISTEMA DE CONTROL DE STOCKS PARA LOS
5.3 PROPUESTA PARA EL CONTROL EN LA RECEPCIÓN Y ENTREGA DE
5.4.1 Métodos propuestos para la gestión de materiales en base al flujo de entradas y
Recursion schemes are defined in terms of the basic types and combina- tors in Figures 2.4 and 2.5, presented in previous sections. The recursion
schemes used in this thesis are catamorphisms, anamorphisms and hylo- morphisms. A catamorphism is a recursive function that uses primitive recursion to consume an input data type, e.g. adding all the elements of a list of integers. An anamorphism is the dual function, that takes an input value and generates an output data type, e.g. generating a list of numbers from zero up to some value. Ahylomorphism is adivide and conquer recur- sive function, where the divide part can be split into an anamorphism, and the combine part into a catamorphism. The map function for polymorphic recursive datatypes can be defined in terms of catamorphisms.
F-Algebras(Coalgebras) Given a functor F, an F-algebra is a pair (A, f), where A is an object, and f is a morphism f : F A → A. For example, in the context of the basic types and combinators, consider the following functor L:
L X = 1 +Int×X
This functor takes a type, e.g. A, to the type 1 +Int×A. An L-algebra in the context of types is a pair of a type A, and a function f :L A →A. Consider the following function:
sum :LInt→Int sum(inj1 ()) = 0
sum(inj2 (i, j)) =i+j
The pair (Int,sum) is anL-algebra.
The dual concept is that of acoalgebra. Given a functorG, aG-coalgebra is a pair (B, g), where B is an object, and g is a morphism g :B →G B. An example in terms of types and functions, consider again the functor L. The pair (Int,next) is an L-coalgebra, where the function next is defined below:
next :Int→LInt
nextn =if n= 0 then inj1 ()
else inj2 (n, n−1)
Homomorphisms A homomorphism between algebras (A, f) and (B, g) is a function h :A →B such that
2.3. STRUCTURED RECURSION 45
Dually, a homomorphism between coalgebras (A, f) and (B, g) is a function
h:A→B such that
F h◦f =g◦h.
Initial(Final) (Co-)Algebra AnF-algebra (A, f) isinitial if, for anyF- algebra (B, g), there is a unique homomorphism from (A, f) to (B, g). There may be more than one initial algebra, but they are all equivalent, since there must be a unique homomorphism between them. An F-coalgebra (A, f) is terminal if there is a unique homomorphism from any other F-coalgebra (A, g) to (A, f). In the types and basic combinators defined in Figures 2.4 and 2.5, the pair (µF,inF) is an initialF-algebra:
inF :F µF →µF.
Dually, the pair (µF,outF) is a terminal coalgebra, where
outF :µF →F µF.
Note that there is no distinction between “greatest” or “least” fixpoint. In the semantic model of CPO, datatypes (i.e. those formed from initial alge- bras) and co-datatypes (i.e. those built from terminal co-algebras) coincide, this is a property calledalgebraic compactness, and makes it possible to com- bine datatypes and co-datatypes, with some limitations that are discussed in Chapter 3. This implies that inF and outF are inverses:
inF ◦outF =id outF ◦inF =id.
Catamorphisms Given the initialF-algebra (µF,inF), there is a unique
homomorphism to any other F-algebra (B, g). By the definition of homo- morphisms, this implies that there must be a function h : µF → B such that
h◦inF =g◦F h,
and this function h must be unique. This h is a catamorphism:
cataF g =h
cataL sum (inL (inj2 (3,inL (inj2 (2,inL (inj1 ()))))))
= {By the definition ofcataL } (sum◦L(cataLsum)◦outL)
(inL(inj2 (3,inL(inj2 (2,inL(inj1 ()))))))
= {inL and outL are inverses}
(sum◦L(cataLsum)) (inj2 (3,inL (inj2 (2,inL (inj1 ())))))
= {Definition of Lfunctor }
sum (inj2 (3,cataLsum(inL(inj2 (2,inL(inj1 ()))))))
= {Repeatedly applyingcataL}
sum (inj2 (3,sum(inj2 (2,sum(inj1 ()))))) = {By the definition ofsum}
sum (inj2 (3,sum(inj2 (2,0)))) = {By the definition ofsum}
sum (inj2 (3,2))
= {By the definition ofsum}
5
Figure 2.6: Example of application of cataL sumto the list [2,3].
Since inF and outF are inverses, this can be simplified to the following
expression:
cataF g =h
whereh=g◦F h◦outF
Essentially, cataF g is a recursive function that takes a recursive datatype,
µF, and returns a value of type B. This is done by first applying the
outF operation, and then applyingcataF g to the recursive positions of the
resulting F µF. This will turn F µF to F B. This output of type F B is the passed to g, that returns the final value of type B. For example, recall the function sum:
sum :LInt→Int sum(inj1 ()) = 0
sum(inj2 (i, j)) =i+j
2.3. STRUCTURED RECURSION 47
and returns the value that results of adding all of them. For example, the list [3,2] is represented in terms of the initial L-algebra as follows:
[3,2] =inL(inj2 (3,inL (inj2 (2,inL (inj1 ())))))
Applying cataL sumto this list proceeds as we show in Figure 2.6.
Polymorphic datatypes As was previously stated, the following defini- tion of F is also a functor:
F A=µ(G A)
This is a well-known fact (see e.g. [Gib02b]) that can be shown by defining the function F f for any given function f : A → B. This function F f
is going to be called mapF f, which is more common in the functional programming community. Given a function f :A→B,
F f =mapF f =cataG A (inG ◦ G f id)
In order to show that mapF f defines a functor, it must respect identities
mapF id =cataG A (inG ◦ Gid id) = cataG AinG=id,
and compositions,
mapF (f ◦g) =cataG A (inG ◦ G(f ◦g)id)
=cataG A (inG ◦ G f id◦ G g id)
=cataG B (inG ◦ G f id)◦ cataG A(inG ◦ G g id)
=mapF f ◦ mapF g
Anamorphisms Given the terminal F-coalgebra (µF,outF), there is a
unique homomorphism from any otherF-coalgebra (A, f). By the definition of homomorphisms, that implies that there must be a function h:A→µF
such that
F h◦f =outF ◦h,
and this function h must be unique. This h is an anamorphism:
anaF f =h
Again, this can be simplified, since inF ◦outF =id:
anaF f =h
whereh=inF ◦F h◦f.
As an example of anamorphism, recall the function next:
next :Int→LInt
nextn =if n= 0 then inj1 ()
else inj2 (n, n−1)
The anamorphism anaL next takes an integer i as input, and generates a
list of all numbers from i to 0. Note that if i < 0, this function would generate an infinite list.