REVISED: Tuesday, November 5, 2024
1. COQ'S FUNCTIONAL PROGRAMMING LANGUAGE
We type in the CoqIDF buffer window using Coq keyword vernacular commands.
As shown below, the Inductive type day : is defined to be := the list of day items or constructors separated by | vertical bars. The list of constructors ends with a period .
Inductive day : Type :=
| monday
| tuesday
| wednesday
| thursday
| friday
| saturday
| sunday.
We move the cursor to the right of the period and click the CoqIDE down-arrow to compile the above Type. If the compile is successful the line color changes to green.
After the successful compile, CoqIDE responds in the Messages' window with
day is defined
day_rect is defined
day_ind is defined
day_rec is defined
day_sind is defined
Next, we write the function next_weekday based on the above day type. The function uses pattern matching, matching the argument d and the branch or case type it returns will be : day type. The branch or case is separated by right arrows pointing to the matching argument. The function ends with end and a period .
Definition next_weekday (d:day) : day :=
match d with
| monday => tuesday
| tuesday => wednesday
| wednesday => thursday
| thursday => friday
| friday => monday
| saturday => monday
| sunday => monday
end.
We move the cursor to the right of the period after end and click the CoqIDE down-arrow to compile the function next_weekday. If the compile is successful the function color changes to green.
After the successful compile, CoqIDE responds in the Messages' window with
next_weekday is defined
To call a function, we use the vernacular command Compute.
Compute (next_weekday friday).
After the successful compile, CoqIDE responds in the Messages window with
= monday
: day
We test our next_weekday function using the vernacular command Example.
Example test_next_weekday :
(next_weekday (next_weekday saturday)) = tuesday.
After a successful compile, CoqIDE responds in the Goal's window by telling us we must first prove the new test_next_weekday function before we can use it.
1 goal
______________________________________(1/1)
next_weekday (next_weekday saturday) =
tuesday
CoqIDF wants us to prove 1 goal the test_next_weekday function.
We use the simpl tactic to simplify the goal we are trying to prove.
Proof. simpl. reflexivity. Qed.
After Proof we compile period by period one at a time. After the successful compile of simple, CoqIDE responds in the Goal's window with
1 goal
______________________________________(1/1)
tuesday = tuesday
Notice 1 goal has changed, now both sides of = are the same.
When both sides of = are the same we can use the tactic reflexivity. After the successful compile of reflexivity, CoqIDE responds in the Goal's window with
No more goals.
Now when we compile Qed. all windows are blank and CoqIDE accepts the Proof as proven.
Compute (next_weekday (next_weekday saturday)).
After the successful compile, CoqIDE responds in the Messages window with
= tuesday
: day
Notice the above Compute is passing function arguments from right to left. For example, next_weekday saturday computes to monday which is passed as an argument to the function on the left next_weekday which uses it to compute the final answer tuesday.
2. CONCLUSION
The Coq functional programming Language is easy to use.
3. 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.