Sunday, February 22, 2015

HASKELL FOLDL INTRODUCTION

HASKELL FOLDL INTRODUCTION

REVISED: Friday, October 25, 2024




I. FOLDL 

foldl saves a lot of code. We could do a "Copy Paste" of the following two functions and then do a "File, Save As" facProd.hs to our working directory.

myProduct :: (Num a) => [a] -> a
myProduct = foldl (*) 1

myFactorial :: Int -> Int
myFactorial n = myProduct [1..n]

GHCi, version 9.8.1: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude>

Prelude>  :load facProd
[1 of 1] Compiling Main             ( facProd.hs, interpreted )
Ok, modules loaded: Main.

And then call the myFactorial function which in turn calls the myProduct function.

myFactorial :: Int -> Int

Prelude>  myFactorial 3
6
Prelude>

Or we can just call the myProduct function and obtain the factorial.

myProduct :: (Num a) => [a] -> a

Prelude>  myProduct [1,2,3]
6
Prelude> 

With a little upfront thinking we could have written myProduct as myFactorial.

myFactorial :: (Num a) => [a] -> a
myFactorial = foldl (*) 1

However, notice what happened. Looking closely at both function signatures if we wanted the factorial using both of our original functions we only have to enter the function name and the one integer we want the factorial of.

myFactorial 3

Now we have to enter the function name and a list of all the integers that are part of the factorial sequence.

myProduct [1,2,3]

Our first choice using two functions was really the best choice. However, the point I am trying to make is, often foldl is used and we do not even know it is being used. It can be a behind the scenes player that does a lot of work for us.

Based on comments from the tutorial audience, use foldl' from the Data.List. It apparently does the same thing as the Prelude foldl; however, it appears to be more efficient. Use foldr on lists that might be infinite or where the fold is building up a data structure. Use foldl' if the list is known to be finite and comes down to a single value.

II. CONCLUSION

In this tutorial you have been introduced to the advantages of using Haskell foldl.

III. REFERENCES

Bird, R. (2015). Thinking Functionally with Haskell. Cambridge, England: Cambridge University Press.

Davie, A. (1992). Introduction to Functional Programming Systems Using Haskell. Cambridge, England: Cambridge University Press.

Goerzen, J. & O'Sullivan, B. &  Stewart, D. (2008). Real World Haskell. Sebastopol, CA: O'Reilly Media, Inc.

Hutton, G. (2007). Programming in Haskell. New York: Cambridge University Press.

Lipovača, M. (2011). Learn You a Haskell for Great Good!: A Beginner's Guide. San Francisco, CA: No Starch Press, Inc.

Thompson, S. (2011). The Craft of Functional Programming. Edinburgh Gate, Harlow, England: Pearson Education Limited.




Saturday, February 21, 2015

IS ⊥ BOTTOM THE HASKELL RED-HEADED STEPCHILD?

IS ⊥ BOTTOM THE HASKELL RED-HEADED STEPCHILD?

REVISED: Thursday, February 8, 2024




Haskell And Bottom.

I. IS ⊥ BOTTOM THE HASKELL RED-HEADED STEPCHILD?

Undefined or bottom written with an up tack or falsum  refers to a computation which never completes successfully. Think of bottom as representing a computation which will not halt, it does not terminate. The glyph of the up tack appears as an upside down tee symbol, and is sometimes called eet, the word tee in reverse. Bottom or whatever you prefer to call it represents a computation which causes our program to fail. However, the  bottom type is a sub-type of all types! For example, the type Bool does not just include True, False it really includes True, , False. The type () with only one element actually has two: , (). The type Integer really includes [, 0, 1, 2, 3..]. The same inclusion can be said of all types. Adding to a set of values is called lifting. All normal Haskell types are lifted.

Why do we rarely see discussions devoted to bottom? Maybe it is related to our technological evolution. In the past we appeared to need information on large objects we could see with the unaided eye, we did not seem as interested in the very small, the bottom. Our technology has evolved, now we need information on both the very large and the very small. We need to rethink the importance of bottom. Granted Haskell does not have sub-types; however, bottom should be thought of as more than just not terminating or an error message. Seeing a bottom should spark our creative thought processes. There is more to a bottom than meets our unaided eye.

One area of creative thought processes is the math field of calculus which calculates limits and derivatives. How often have you recently seen Haskell used to solve non lambda calculus, calculus problems? These of course are problems calculating numbers approaching infinity or zero. What is infinity, is infinity bottom? What is zero? We need Haskell to help us with the creative what is ... problem types. Haskell is based on math; however, I have seldom seen easy to understand, Haskell non lambda calculus, calculus limits or derivative functions outside of Haskell Cabal. We have a lot of work ahead of us. 

I love Haskell, it is my favorite programming language; however, is the  bottom type the Haskell red-headed stepchild?

II. CONCLUSION

In this tutorial, you have been introduced to Haskell ⊥ bottom.

III. REFERENCES

Bird, R. (2015). Thinking Functionally with Haskell. Cambridge, England: Cambridge University Press.

Davie, A. (1992). Introduction to Functional Programming Systems Using Haskell. Cambridge, England: Cambridge University Press.

Goerzen, J. & O'Sullivan, B. &  Stewart, D. (2008). Real World Haskell. Sebastopol, CA: O'Reilly Media, Inc.

Hutton, G. (2007). Programming in Haskell. New York: Cambridge University Press.

Lipovača, M. (2011). Learn You a Haskell for Great Good!: A Beginner's Guide. San Francisco, CA: No Starch Press, Inc.

Thompson, S. (2011). The Craft of Functional Programming. Edinburgh Gate, Harlow, England: Pearson Education Limited.




Saturday, February 14, 2015

HASKELL SIEVE OF ERATOSTHENES

HASKELL SIEVE OF ERATOSTHENES

REVISED: Thursday, February 8, 2024




Haskell And Prime Numbers.

I. SIEVE OF ERATOSTHENES

Eratosthenes of  Cyrene (275-194 B.C.) was a Greek mathematician who created the Sieve of Eratosthenes, an algorithm for rapidly locating all prime numbers within a range of integers.

"Copy Paste" the following program into your text editor and then "File, Save As" Eratosthenes.hs to your working directory:

sieve :: Integral a => [a] -> [a]
sieve (p : xs) = p : sieve [x | x <- xs, x `mod` p /= 0]

primes :: [Integer]
primes = sieve [2..]

main = print $ take 100 primes

Load the example into GHCi as follows:

GHCi, version 9.8.1: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude>

Prelude>  :load Eratosthenes
[1 of 1] Compiling Main             ( Eratosthenes.hs, interpreted )
Ok, modules loaded: Main.
Prelude>

Call the main function, take the first 100 prime numbers generated by our program, and print them as follows:

Prelude>  main
[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97, 101,103,107,109,113,127,131,137,139,149,151,157,163,167,173, 179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263, 269,271,277,281,283,293,307,311,313,317,331,337,347,349,353,359, 367,373,379,383,389,397,401,409,419,421,431,433,439,443,449,457, 461,463,467,479,487,491,499,503,509,521,523,541]
Prelude>

II. PRIME NUMBER DEFINITION

A prime number is a natural number, a counting number, greater than 1 with no positive divisors or factors other than 1 and itself.

III. PRIMES

primes is a function name, primes is not a Haskell keyword.

primes :: [Integer]
primes = sieve [2..]

The initial prime generated by sieve is 2. 

IV. LIST COMPREHENSION

The following sieve function is a list comprehension which takes, as shown in its type signature, a list as its input and produces a list as its output.

sieve :: Integral a => [a] -> [a]
sieve (p : xs) = p : sieve [x | x <- xs, x `mod` p /= 0]

The sieve function obtains its arguments (p : xs) by deconstructing the beginning of the list sieve, to get its head p and tail xs.

After we have the arguments, read the sieve function from right to left.

The list comprehension iterates over xs, binding each element to x, which it returns if x is not (/=) evenly divisible by p.

, comma before the predicate is pronounced, "such that".

<- arrow is pronounced, "drawn from". A generator expression binds a variable on the left of a <- arrow to an element of the list on the right.

| pipe is pronounced, "such that". The part before the | pipe, x, is called the output function. The expression on the left of the | pipe is evaluated for each combination of generator expressions on the right.

p : sieve puts p on the front of a new list, which is constructed by calling sieve.

The sieve function determines whether or not the head of the list is a prime, and then calls itself recursively on the tail of the list. Therefore, sieve is a tail recursive function.

sieve is a function name, sieve is not a Haskell keyword.

The function sieve is defined in terms of itself; therefore, sieve is recursive. Haskell is lazily evaluated, and it will do no more work than is necessary, and stop once the take 100 primes have been calculated.

As shown below [2..] could produce an infinite number of integers starting from the number 2 if it were allowed to do so. When it is run as part of the total program we are saved from the imperative programming results of an infinite loop by the lazy nature of Haskell's functional programming language in which elements of the range object are only generated when they are needed.

Prelude>  [2..]
[2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25, ...

The sieve function takes a single list of integers as its argument, and assumes that the first integer in the list is a prime. The sieve function then removes from the list integers that are not prime.

V. TAKE

take is a Haskell Prelude function name.

The Prelude take function returns a sublist consisting of the first k elements from the sublist. Therefore, we are working with several lists, the [2..] list of integers, the take sublist of prime numbers, and each new list created from the integers which could not be divided by the last prime division.

VI. ANALYSIS

When our Sieve of Eratosthenes starts [2..] provides us with the prime number, 2 and a list of xs.

p is bound to 2.

x is bound to 2.

In other words:

First, sieve is passed a list of all integers greater than or equal to 2.

Second, sieve uses 2 as the first prime and passes itself a list of all remaining integers not divisible by 2 ([3, 5, 7, 9, 11, 13, ...]).

Third, sieve uses the 3 as the second prime and passes itself a list of all remaining integers not divisible by 3 ([5, 7, 11, 13, ...]).

Fourth, this process proceeds recursively until the number of primes necessary to satisfy the take requirement of 100 prime numbers is met.

The lists discussed above are list generators, functions which calculate only on demand the next number in a list.

VII. CONCLUSION

In this tutorial, you have been introduced to using Haskell to find prime numbers.

VIII. REFERENCES

Bird, R. (2015). Thinking Functionally with Haskell. Cambridge, England: Cambridge University Press.

Davie, A. (1992). Introduction to Functional Programming Systems Using Haskell. Cambridge, England: Cambridge University Press.

Goerzen, J. & O'Sullivan, B. &  Stewart, D. (2008). Real World Haskell. Sebastopol, CA: O'Reilly Media, Inc.

Hutton, G. (2007). Programming in Haskell. New York: Cambridge University Press.

Lipovača, M. (2011). Learn You a Haskell for Great Good!: A Beginner's Guide. San Francisco, CA: No Starch Press, Inc.

Thompson, S. (2011). The Craft of Functional Programming. Edinburgh Gate, Harlow, England: Pearson Education Limited.




Wednesday, January 15, 2014

HASKELL DIFFERENTIAL CALCULUS DERIVATIVE EQUATIONS

HASKELL DIFFERENTIAL CALCULUS DERIVATIVE EQUATIONS

REVISED: Thursday, February 8, 2024




Haskell And Derivatives.  (Note this blog is a work in progress.)

Differential Calculus cuts something into small pieces to find how it changes.

I. DERIVATIVES

A derivative is the slope of a curve, and the rate of change.

The derivative of f (x) is the function

d/dx f(x) = lim h→0 ( f(x + h) − f (x) ) / h = f'(x).

If this limit does not exist (DNE) for a given value of x, then f(x) is not differentiable at x.

Differentiability implies continuity; if f(x) is a differentiable function at x = a, then f(x) is continuous at x = a.

A. First Derivative Test

Suppose that f(x) is continuous on an interval, and that f′(a) = 0 for some value of a in that interval.

(a) If f′(x) > 0 to the left of a and f′(x) < 0 to the right of a, then f (a) is a
local maximum.

(b) If f′(x) < 0 to the left of a and f′(x) > 0 to the right of a, then f (a) is a
local minimum.

(c) If f′(x) has the same sign to the left and right of f′(a), then f′(a) is not a
local extremum.

B. Second Derivative Test

Suppose that f'' (x) is continuous on an open interval and that f' a = 0 for some value of a in that interval.

(a) If f′′(a) < 0, then f (x) has a local maximum at a.

(b) If f′′(a) > 0, then f (x) has a local minimum at a.

(c) If f′′(a) = 0, then the test is inconclusive. In this case, f (x) may or may not have a local extremum at x = a.

C. Trigonometric Function Derivatives

(a) d/dxsin(x) = cos(x).

(b) d/dxcos(x) = − sin(x).

(c) d/dxtan(x) = sec2(x).

(d) d/dxsec(x) = sec(x) tan(x).

(e) d/dxcsc(x) = − csc(x) cot(x).

(f) d/dxcot(x) = − csc2(x).

D. Inverse Trigonometric Function Derivatives

(a) d/darcsin(y) = 1/√(1-y2).

(b) d/darccos(y) = −1/√(1-y2).

(c) d/darctan(y) = 1/(1-y2).

(d) d/darcsec(y) = 1/(|y| √(y2-1)) for |y| > 1.

(e) d/darccsc(y) = −1/(|y| √(y2-1)) for |y| > 1.

(f) d/darccot(y) = −1/(1 + y2).

II. DERIVATIVE RULES

A. CONSTANT RULE

Given a constant c.

d/dx c = 0.

A constant is not changing.

B. SUM RULE

If f(x) and g(x) are differentiable and c is a constant, then:

(a) d/dx (f(x) + g(x)) = f'(x) + g'(x)

(b) d/dx (f(x) - g(x)) = f'(x) - g'(x)

(c) d/dx (c * f(x)) = c * f'(x)

The derivative of a sum is the sum of the derivatives, and the derivative of a difference is the difference of the derivatives.

C. PRODUCT RULE

Let h(x) = f(x) * g(x).

If f and g are differentiable at a, then

h'(a) = f'(a) * g(a) + f(a) * g'(a).

In other words:

d/dx(f(x) * g(x)) = (d/dx f(x)) * g(x) + f(x) * (d/dx g(x))

The derivative of a product is the derivative of the first times the second, plus the first times the derivative of the second.

D. QUOTIENT RULE

Let h(x) = f(x)/g(x).

If g(a) /= 0, and

f and g are differentiable at a, then

h'(a) = ((g(a) * f'(a)) - (f(a) * g'(a)))/g(a)2

The derivative of a quotient is the derivative of the top times the bottom, minus the top times the derivative of the bottom, all over the bottom squared.

E. POWER RULE

For any real number n,

d/dx xn = nxn-1.

Multiply by the power and reduce the power by one.

f x n = n * x ** (n - 1)

F. CHAIN RULE

If f(x) and g(x) are differentiable, then

d/dx f(g(x)) = f′(g(x))g′(x).

We identify the “inside function” and the “outside function”.  We then differentiate the outside function leaving the inside function alone and multiply all of this by the derivative of the inside function.

(a) Look for parentheses.

Dx (un)

(b) Bring exponent to front.

Dx (un) = n(un)

(c) Subtract exponent by one.

Dx (un) = n(un-1)

(d) Multiply by inside derivative.

Dx (un) = n(un-1)Dx (u)

(e) Do not have any negative exponents in answer.

(f) Answer must be as simplified as possible.

(g) √x = x1/2

(h) x-1 = 1/x  

A negative exponent in the numerator means inverse, which becomes a positive exponent in the denominator.

(i) a2 * a3 = a5 

Whenever you multiply two terms with the same base, you can add the exponents.

(j) (a 2) 3 = a6 

Whenever you have an exponent expression that is raised to a power, you can multiply the exponent and power.

(k) (xy2)3 = x3y6 

If you have a product inside parentheses, and a power on the parentheses, then the power goes on each element inside.

(l) Anything to the power zero is just "1".

III. HASKELL DERIVATIVE EXAMPLES

A. CONSTANT RULE



B. SUM RULE



C. PRODUCT RULE



D. QUOTIENT RULE



E. POWER RULE

f x n = n * x ** (n - 1)

Prelude>  let f x n = n * x ** (n - 1)
Prelude>

Prelude>  f 2 3
12.0
Prelude>

F. CHAIN RULE



IV. HASKELL DERIVATIVE THEOREMS

A. DERIVATIVE OF EX THEOREM

d/dx ex = ex

B. FERMAT'S THEOREM

If f(x) has a local extremum at x = a and f(x) is differentiable at a, then f′(a) = 0.

C. DERIVATIVE OF THE NATURAL LOGRITHM THEOREM

d/dx ln(x) = 1/x.

V. PROCEDURE FOR SKETCHING THE PLOTS OF FUNCTIONS

(a) Find the y-intercept, this is the point (0, f (0)). Place this point on your graph.

(b) Find candidates for vertical asymptotes, these are points where f (x) is undefined.

If lim x→a f(x) = ±∞, lim x→a+ f(x) = ±∞, or lim x→a− f(x) = ±∞,

then the line x = a is a vertical asymptote of f (x).

(c) Compute f′(x) and f′′(x).

(d) Find the critical points, the points where f′(x) = 0 or f′(x) is undefined.

(e) Use the second derivative test to identify local extrema and/or find the intervals where your function is increasing/decreasing.

(f) Find the candidates for inflection points, the points where f′′(x) = 0 or f′′(x) is undefined.

(g) Identify inflection points and concavity.

(h) If possible find the x-intercepts, the points where f(x) = 0. Place these points on your graph.

(i) Find horizontal asymptotes.

If lim x→∞ f(x) = L or lim x→−∞ f(x) = L,

then the line y = L is a horizontal asymptote of f(x).

(j) Determine an interval that shows all relevant behavior. At this point you should be able to sketch the plot of your function.

VI. STRAIGHT LINE EQUATIONS

A. SLOPE-INTERCEPT FORM

y = mx + b

This is called the slope-intercept form because "m" is the slope and "b" gives the y-intercept.

B. POINT-SLOPE FORM

Given a point (x1, y1) and a slope m.

y – y1 = m(x – x1)

VII. CONCLUSION

In this tutorial, you have been introduced to using Haskell to solve differential calculus derivative equations.

VIII. REFERENCES

Bird, R. (2015). Thinking Functionally with Haskell. Cambridge, England: Cambridge University Press.

Davie, A. (1992). Introduction to Functional Programming Systems Using Haskell. Cambridge, England: Cambridge University Press.

Goerzen, J. & O'Sullivan, B. &  Stewart, D. (2008). Real World Haskell. Sebastopol, CA: O'Reilly Media, Inc.

Hutton, G. (2007). Programming in Haskell. New York: Cambridge University Press.

Lipovača, M. (2011). Learn You a Haskell for Great Good!: A Beginner's Guide. San Francisco, CA: No Starch Press, Inc.

Thompson, S. (2011). The Craft of Functional Programming. Edinburgh Gate, Harlow, England: Pearson Education Limited.




Monday, January 13, 2014

HASKELL INTEGRAL CALCULUS LIMIT EQUATIONS

HASKELL CALCULUS LIMIT EQUATIONS

REVISED: Thursday, February 8, 2024




Haskell and Limits.  (Note: This blog is a work in progress.)

The word Calculus comes from Latin meaning "small stone". Integral Calculus joins in other words integrates all of the small pieces together to determine the total.

Differential Calculus and Integral Calculus are like inverses of each other, just like multiplication and division are inverses.

I. LIMIT LAWS

Given:

The lim x→a is a special way of saying, ignoring what happens when the value x gets to the value a as the value x moves closer and closer to the value of a the value x becomes a closer and closer approximation of the value of a

A limit is a value that a function approaches as the input x approaches some value.

As shown below, as x approaches a x→a then f(x) approaches L; and as x approaches a x→a then g(x) approaches M.

lim x→a f(x) = L,

lim x→a g(x) = M,

k is some constant,

and n is a positive integer.

A. CONSTANT LAW

lim x→a k f(x) = k (lim x→a f(x)) = k L.

The limit of a constant is that constant provided that limit exists.

B. SUM LAW

lim x→a (f(x) + g(x)) = lim x→a f(x) + lim x→a g(x) = L + M.

The limit of the sums is the sum of the limits provided those limits exist.

C. PRODUCT LAW

lim x→a (f(x) g(x)) = lim x→a f(x) · lim x→a g(x) = L M.

The limit of the products is the product of the limits provided those limits exist.

D. QUOTIENT LAW

lim x→a (f(x) / g(x)) = (lim x→a f(x))/(lim x→a g(x)) = L / M, if M  /=  0.

The limit of the quotients is the quotient of the limits provided those limits exist and the denominator is nonzero.

E. POWER LAW

lim x→a f(x)n = (lim x→a f(x))n = Ln.

F. ROOT LAW

lim x→a n√f(x) = n√lim x→a f(x) = n√L provided if n is even, then f(x) ≥ 0 near a.

G. COMPOSITION LAW

If lim x→a g(x) = M and lim x→M f(x) = f(M), then lim x→a f(g(x)) = f(M).

II. LIMIT DEFINITIONS

A. OFFICIAL LIMIT DEFINITION 

ε is epsilon the lower case of the fifth letter of the Greek alphabet.

δ is delta the lower case of the fourth letter of the Greek alphabet.

lim xa f(x) = L means

for all ε > 0,

there is δ > 0,

so that if x /= a and x is within δ of a ( δ is measuring how close x is to a )

then f(x) is within ε of L ( ε is measuring how close f(x) is to L )

If no such value of L can be found, then we say that the lim xa f(x) does not exist (DNE). 

B. ONE-SIDED LIMIT DEFINITION

We say that the limit of f(x) as x goes to a from the left is L,

lim x→a− f(x) = L

if for every ε > 0 there is a δ > 0 so that whenever x < a and

a − δ < x we have |f(x) − L| < ε.

We say that the limit of f(x) as x goes to a from the right is L,

lim x→a+ f(x) = L

if for every ε > 0 there is a δ > 0 so that whenever x > a and

x < a + δ we have |f(x) − L| < ε.

III. USEFUL HASKELL TRIGONOMETRIC FUNCTIONS

Remember Calculus is computed using radians not degrees.

To convert degrees into radians multiply the degrees by pi/180o.

For example, using Haskell 200converted into radians equals 3.490658503988659 radians as shown using the function below.

f x = x * pi / 180

Some useful built in Haskell Trigonometric functions are as follows:

sin

cos

tan

asin

atan

acos

sinh

tanh

cosh

asinh

atanh

acosh

IV. HASKELL LIMIT EXAMPLES

A. CONSTANT LAW



B. SUM LAW



C. PRODUCT LAW



D. QUOTIENT LAW



E. POWER LAW



F. ROOT LAW



G. COMPOSITION LAW



V. HASKELL LIMIT THEOREMS

A. INTERMEDIATE VALUE THEOREM

If f(x) is a continuous function for all x in the closed interval [a,b] and d is in between f(a) and f(b), then there is a number c in [a,b] such that f(c) = d.

B. SQUEEZE THEOREM

Suppose that g(x) ≤ f(x) ≤ h(x) for all x close to a but not necessarily equal to a. If

lim x→a g(x) = L = lim x→a h(x),

then lim x→a f(x) = L.

VI. CONCLUSION

In this tutorial, you have been introduced to using Haskell to solve integral calculus limit equations.

VII. REFERENCES

Bird, R. (2015). Thinking Functionally with Haskell. Cambridge, England: Cambridge University Press.

Davie, A. (1992). Introduction to Functional Programming Systems Using Haskell. Cambridge, England: Cambridge University Press.

Goerzen, J. & O'Sullivan, B. &  Stewart, D. (2008). Real World Haskell. Sebastopol, CA: O'Reilly Media, Inc.

Hutton, G. (2007). Programming in Haskell. New York: Cambridge University Press.

Lipovača, M. (2011). Learn You a Haskell for Great Good!: A Beginner's Guide. San Francisco, CA: No Starch Press, Inc.

Thompson, S. (2011). The Craft of Functional Programming. Edinburgh Gate, Harlow, England: Pearson Education Limited.




Thursday, January 9, 2014

HASKELL INVERSE FUNCTIONS

HASKELL INVERSE FUNCTIONS

REVISED: Thursday, February 8, 2024




Haskell Inverse Functions.

I. FUNCTION

A function is a relation between sets where for each input there is exactly one output. We call the set a function is mapping from the domain or source the input; and we call the set a function is mapping to the range or codomain the output.

Let f be a continuous real injective and surjective function defined on a closed interval [(x1,y1),(x2,y2)].

Using Haskell:

f x = y

An injective function is one such that no two elements in the domain map to the same value in the codomain or range.

A one-to-one function is one such that for every value in the codomain or range there is exactly one value in the domain.

A surjective function is one such that for each element in the codomain or range there is at least one element in the domain that maps to it.

A bijective function is both injective and surjective.

In order for a function to have an inverse function it must be bijective and one-to-one.

A. FUNCTION INVERSE

A plot gives y as a function of x if every vertical line crosses the plot at most once, this is commonly known as the vertical line test. A function is one-to-one if every horizontal line crosses the plot at most once, which is commonly known as the horizontal line test. We can only find an inverse to a function when it is one-to-one otherwise we must restrict the domain.

If a function f maps every input to exactly one output an inverse of that function maps every output to exactly one input. The inverse of f is well-defined if and only if f is bijective and one-to-one.

A multiplicative inverse or reciprocal for a number x denoted by 1/x or x-1 is a number which when multiplied by x yields the multiplicative identity, 1.

The derivative of a function f(x) at x is the slope of the tangent line at x.

Using Haskell to find the slope of a line.

"Copy Paste" the following module into your text editor and "File, Save As" Slopes.hs to your working directory:

module Slopes where

slopew :: Fractional a => (a, a) -> (a, a) -> a
slopew (x1,y1) (x2,y2) = dy/dx
                                          where dy = y2 - y1
                                                      dx = x2 - x1

slopel :: Fractional a => (a, a) -> (a, a) -> a
slopel (x1,y1) (x2,y2) = let dy = y2 - y1
                                              dx = x2 - x1
                                          in dy/dx

GHCi, version 9.8.1: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude>

Load the module Slopes into GHCi as follows:

Prelude>  :load Slopes.hs
[1 of 1] Compiling Main             ( Slopes.hs, interpreted )
Ok, modules loaded: Main.
Prelude>

Call the function slopew as follows:

Prelude>  slopew (1,1) (2,2)
1.0
it :: Fractional a => a
Prelude>

Call the function slopel as follows:

Prelude>   slopel (1,1) (2,2)
1.0
it :: Fractional a => a
Prelude>  

The range of the function f is the interval [(x1,y1),(x2,y2)], or [(x2,y2),(x1,y1)] if (x2,y2) < (x1,y1).

The function which maps the range or codomain y to the domain or source x is called the inverse of the function f.

For example, suppose you are filling the gas tank of your car and there are 4 gallons of gas in your tank when you start. The volume of gas in gallons after t seconds of filling your gas tank at the rate of 2 gallons per second is given by volume as a function of time v(t):

v(t) = 2t + 4
v(t) = 2(t + 2)
v = 2(t + 2)

Using Haskell, volume as a function f time v(t), in this case is:

v :: Num a => a -> a
v t = 2 * ( t + 2 )

GHCi, version 9.8.1: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude>

Prelude>  let v t = 2 * ( t + 2 )
v :: Num a => a -> a
Prelude>

Prelude>  v 0     -- Time is zero when you start pumping the gas.
4                          --  At time zero you start with 4 gallons of gas in your tank.
it :: Num a => a
Prelude>  

What is the inverse of the function v(t); and what does the inverse of the function v(t) tell us? While v(t) tells us how many total gallons of gas are in the tank after a period of time in seconds the inverse of v(t) tells us how much time in seconds must be spent to fill our tank with a given total volume of gas in gallons.

To compute the inverse of volume as a function of time v(t) first set v = v(t) and then compute time as a function of volume t(v) as follows:

v(t) = 2(t + 2)
v = 2(t + 2)

Solving for t:

v /2 = t + 2
v /2 - 2 = t
t = v /2 - 2
t = v/2 - 4/2
t = 1/2(v - 4)

This is a function that maps volume in gallons to time in seconds:

t(v) = 1/2(v – 4)
t = 1/2(v – 4)

Using Haskell time as a function of volume t(v) is:

t :: Fractional a => a -> a
t v = 1/2 * (v – 4)

GHCi, version 9.8.1: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude>

Prelude>  let t v = 1/2 * (v - 4)
t :: Fractional a => a -> a
Prelude>

Prelude>  t 4   -- You start with 4 gallons of gas in your tank.
0.0                    -- You start at time zero.
it :: Fractional a => a
Prelude>

Therefore, as shown in Haskell the inverse of volume as a function of time v(t):

v :: Num a => a -> a
v t = 2 * ( t + 2 )

is time as a function of volume t(v):

t :: Fractional a => a -> a
t v = 1/2 * (v – 4)

III. INVERSE FUNCTION THEOREM

If f(x) is a differentiable function, and f'(x) is continuous, and f'(a) /= 0, then

(a) f-1(y) is defined for y near f(a),

(b) f-1(y) is differentiable near f(a),

(c) d/dy f-1(y) is continuous near f(a), and

(d) d/dy f-1f(y) = 1/(f'(f-1(y))).

IV. CONCLUSION

In this tutorial you have been introduced to Haskell inverse functions.

V. REFERENCES

Bird, R. (2015). Thinking Functionally with Haskell. Cambridge, England: Cambridge University Press.

Davie, A. (1992). Introduction to Functional Programming Systems Using Haskell. Cambridge, England: Cambridge University Press.

Goerzen, J. & O'Sullivan, B. &  Stewart, D. (2008). Real World Haskell. Sebastopol, CA: O'Reilly Media, Inc.

Hutton, G. (2007). Programming in Haskell. New York: Cambridge University Press.

Lipovača, M. (2011). Learn You a Haskell for Great Good!: A Beginner's Guide. San Francisco, CA: No Starch Press, Inc.

Thompson, S. (2011). The Craft of Functional Programming. Edinburgh Gate, Harlow, England: Pearson Education Limited.