REVISED: Monday, February 12, 2024
Haskell Modules No Data Hiding.
I. HASKELL DATA MODULES
You are reading "Haskell Modules No Data Hiding", the first half of the "Haskell Data Modules" tutorial.
"Haskell Modules Data Hiding" is the second half.
The two parts of this tutorial are a very brief outline of "Haskell Programming Tutorial 6 - Modules" by Sankha Mukherjee.
A. SKELETON
We start off with the following file containing both the main function and the vector functions:
main = do
print $ (2,4,5) · (0,1,0)
print $ (2,4,5) × (0,1,0)
(·) :: (Float, Float, Float) -> (Float, Float, Float) -> Float
(·) (x1, x2, x3)(y1, y2, y3) = x1*y1+x2*y2+x3*y3
(×) :: (Float, Float, Float) -> (Float, Float, Float) -> (Float, Float, Float)
(×) (x1, x2, x3)(y1, y2, y3) = (x2*y3-x3*y2, x3*y1-x1*y3, x1*y2-x2*y1)
(·) :: (Float, Float, Float) -> (Float, Float, Float) -> Float
(·) (x1, x2, x3)(y1, y2, y3) = x1*y1+x2*y2+x3*y3
(×) :: (Float, Float, Float) -> (Float, Float, Float) -> (Float, Float, Float)
(×) (x1, x2, x3)(y1, y2, y3) = (x2*y3-x3*y2, x3*y1-x1*y3, x1*y2-x2*y1)
What happens if we need these vector functions in hundreds of other programs? One solution would be to copy past them into each of the other programs.
However, what happens if we want to change a function; for example, changing from a three element tuple to a two element tuple? Changes might have to be made to hundreds of files that were copy pasted.
However, what happens if we want to change a function; for example, changing from a three element tuple to a two element tuple? Changes might have to be made to hundreds of files that were copy pasted.
A good programming maintenance technique is to use modules.
One module for each group of related functions.
One module containing the main function.
B. FLESH ON BONES
1. SAVE
Save has two steps. "Save Step1" saves the module containing the vector functions. "Save Step 2" saves the main function.
SAVE STEP 1
Use your editor to save MyVectorsF.hs the vector dot product, and vector cross product, function signatures and definitions. Notice both of these functions are tied to a particular data type; i.e., Float. Also notice each vector function assumes a "three element tuple". What do you do when you need the dot product or cross product of a vector which has less tuple elements or more tuple elements? That question will be answered in "Haskell Data Hiding".
(·) :: (Float, Float, Float) -> (Float, Float, Float) -> Float
(·) (x1, x2, x3)(y1, y2, y3) = x1*y1+x2*y2+x3*y3
(×) :: (Float, Float, Float) -> (Float, Float, Float) -> (Float, Float, Float)
(×) (x1, x2, x3)(y1, y2, y3) = (x2*y3-x3*y2, x3*y1-x1*y3, x1*y2-x2*y1)
SAVE STEP 2
Use your editor to save MyVetorsM.hs the main function. Notice by importing MyVectorsF you do not have to copy paste the vector functions into the main function.
import MyVectorsF
main = do
print $ (2,4,5) · (0,1,0)
print $ (2,4,5) × (0,1,0)
2. LOAD
Load has two steps. "Load Step 1" loads the vector functions. "Load Step 2" loads the main function.
LOAD STEP 1
As shown below, load the above MyVectorsF.hs file into GHCi:
Prelude> :load MyVectorsF
[1 of 1] Compiling MyVectorsF ( MyVectorsF.hs, interpreted )
Ok, modules loaded: MyVectorsF.
Prelude>
LOAD STEP 2
As shown below, load the above MyVectorsM.hs file into GHCi:
Prelude> :load MyVectorsM
[1 of 2] Compiling MyVectorsF ( MyVectorsF.hs, interpreted )
[2 of 2] Compiling Main ( MyVectorsM.hs, interpreted )
Ok, modules loaded: Main, MyVectorsF.
Prelude>
Now that we have loaded Main, we can run functions in GHCi that were defined in Main.
3. RUN
As shown below, run the main function, in GHCi:
Prelude> main
4.0(-5.0,0.0,2.0)
Prelude>
4. COMPILE
As shown below, compile MyVectorsM.hs in GHC:
Prelude> :! ghc --make "*MyVectorsM"
[1 of 2] Compiling MyVectorsF ( MyVectorsF.hs, MyVectorsF.o )
[2 of 2] Compiling Main ( MyVectorsM.hs, MyVectorsM.o )
Linking MyVectorsM.exe ...
Prelude>
The compile is shown so you know it compiles without error.
C. COMMENTS
Use the program shown above as an example. Rewrite the example and make it your own program.
Repeat 1. thru 3. above until your new program works the way you want it to work. Each time you make changes in your editor to your program, make sure you save the file and load it in GHCi.
II. CONCLUSION
You have just received an introduced to "Haskell Modules No Data Hiding", the first half of the "Haskell Data Modules" tutorial. "Haskell Modules Data Hiding" is the second half of "Haskell Data Modules".
III. REFERENCE