ebooksgratis.com

See also ebooksgratis.com: no banners, no cookies, totally FREE.

CLASSICISTRANIERI HOME PAGE - YOUTUBE CHANNEL
Privacy Policy Cookie Policy Terms and Conditions
Hylomorphism (computer science) - Wikipedia, the free encyclopedia

Hylomorphism (computer science)

From Wikipedia, the free encyclopedia

In computer science, and in particular functional programming, a hylomorphism is a recursive function, corresponding to the composition of an anamorphism (which first builds a set of results; also known as 'unfolding') and a catamorphism (which then folds these results into a final return value). Fusion of these two recursive computations into a single recursive pattern then avoids building the intermediate data structure. This is a particular form of the optimizing program transformation techniques collectively known as deforestation. The categorical dual of a hylomorphism is called a metamorphism, and is a catamorphism followed by an anamorphism.

Contents

[edit] Formal definition

A hylomorphism h\ : A \rightarrow C can be defined in terms of its separate anamorphic and catamorphic parts.

The anamorphic part can be defined in terms of a unary function g\ : A \rightarrow B||A defining the list, and a predicate p\ : A \rightarrow Boolean providing the terminating condition.

The catamorphic part can be defined as a combination of an initial value c\ \in C for the fold and a binary operator \oplus : B||C \rightarrow C used to perform the fold.

Thus a hylomorphism


h\ a = \begin{cases}
                c & \mbox{if } pa
              \\b \oplus ha' & \mbox{otherwise}
        \end{cases}

(where (b,\ a') = ga) may be defined (assuming appropriate definitions of p, g, h).

[edit] Notation

An abbreviated notation for the above hylomorphism is h = [\![(c, \oplus),(g, p)]\!].

[edit] Hylomorphisms in practice

[edit] Lists

Lists are common data structures (as they naturally arise whenever a linear process must be applied to a number of inputs); as such, it is sometimes necessary (or, at least, preferable) to generate a temporary list of intermediate results before performing an operation to reduce this list to a single final results.

One example of a commonly encountered hylomorphism is the canonical factorial function.

 factorial :: Integer -> Integer
 factorial n | n == 0 = 1
             | n > 0  = n * factorial (n - 1)

In the previous example (written in Haskell, a purely functional programming language) it can be seen that this function, applied to any given valid input, will generate a linear call tree isomorphic to a list. For example, given n = 5 it will produce the following:

 factorial 5 = 5 * (factorial 4) = 120
   factorial 4 = 4 * (factorial 3) = 24
     factorial 3 = 3 * (factorial 2) = 6
       factorial 2 = 2 * (factorial 1) = 2
         factorial 1 = 1 * (factorial 0) = 1
           factorial 0 = 1

In this example, the anamorphic part of the process is the generation of the call tree which is isomorphic to the list [1, 1, 2, 3, 4, 5]. The catamorphism, then, is the calculation of the product of the elements of this list. Thus, in the notation given above, the factorial function may be written \mathit{factorial} = [\![(1,\times),(g, p)]\!] where g\ n = (n, n - 1) and p\ n = (n = 0).

[edit] Trees

However, it should be noted that the term 'hylomorphism' does not apply solely to functions acting upon isomorphisms of lists. For example, a hylomorphism may also be defined by generating a non-linear call tree which is then collapsed. An example of such a function is the function to generate the nth term of the Fibonacci sequence.

 fibonacci :: Integer -> Integer
 fibonacci n | n == 0 = 0
             | n == 1 = 1
             | n > 1 = fibonacci (n - 2) + fibonacci (n - 1)

This function, again applied to any valid input, will generate a call tree which is non-linear. In the following example, the call tree generated by applying the fibonacci function to the input 4.

                    fib 4 <- 1 + 2 = 3
                   /     \
 0 + 1 = 1 -> fib 2       fib 3 <- 1 + 1 = 2
              /  \        /   \
          fib 0  fib 1 fib 1  fib 2 <- 0 + 1 = 1
            |     |    |      /\
            0     1    1     /  \
                          fib 0  fib 1
                           |      |
                           0      1

This time, the anamorphism is the generation of the call tree isomorphic to the tree with leaf nodes 0, 1, 1, 0, 1 and the catamorphism the summation of these leaf nodes.

[edit] See also

[edit] References


aa - ab - af - ak - als - am - an - ang - ar - arc - as - ast - av - ay - az - ba - bar - bat_smg - bcl - be - be_x_old - bg - bh - bi - bm - bn - bo - bpy - br - bs - bug - bxr - ca - cbk_zam - cdo - ce - ceb - ch - cho - chr - chy - co - cr - crh - cs - csb - cu - cv - cy - da - de - diq - dsb - dv - dz - ee - el - eml - en - eo - es - et - eu - ext - fa - ff - fi - fiu_vro - fj - fo - fr - frp - fur - fy - ga - gan - gd - gl - glk - gn - got - gu - gv - ha - hak - haw - he - hi - hif - ho - hr - hsb - ht - hu - hy - hz - ia - id - ie - ig - ii - ik - ilo - io - is - it - iu - ja - jbo - jv - ka - kaa - kab - kg - ki - kj - kk - kl - km - kn - ko - kr - ks - ksh - ku - kv - kw - ky - la - lad - lb - lbe - lg - li - lij - lmo - ln - lo - lt - lv - map_bms - mdf - mg - mh - mi - mk - ml - mn - mo - mr - mt - mus - my - myv - mzn - na - nah - nap - nds - nds_nl - ne - new - ng - nl - nn - no - nov - nrm - nv - ny - oc - om - or - os - pa - pag - pam - pap - pdc - pi - pih - pl - pms - ps - pt - qu - quality - rm - rmy - rn - ro - roa_rup - roa_tara - ru - rw - sa - sah - sc - scn - sco - sd - se - sg - sh - si - simple - sk - sl - sm - sn - so - sr - srn - ss - st - stq - su - sv - sw - szl - ta - te - tet - tg - th - ti - tk - tl - tlh - tn - to - tpi - tr - ts - tt - tum - tw - ty - udm - ug - uk - ur - uz - ve - vec - vi - vls - vo - wa - war - wo - wuu - xal - xh - yi - yo - za - zea - zh - zh_classical - zh_min_nan - zh_yue - zu -