Friday, September 12, 2025

OCAML FIBONACCI SEQUENCE

 REVISED: Friday, September 12, 2025




1. OCAML  FIBONACCI SEQUENCE

(*
  File: fibonacci_sequence.ml
  Author: Ron Tinnel
  Description: This script provides an interactive command-line interface
  for calculating and displaying a user-defined segment of the Fibonacci sequence.
  It demonstrates core OCaml concepts such as recursion, function definition,
  pattern matching, exception handling, and I/O operations.

  To run this script:
  1. Make sure you have OCaml installed.
  2. Save the code as `fibonacci_sequence.ml`.
  3. Compile it using: `ocamlc -o fib_seq fibonacci_sequence.ml`
  4. Execute the compiled program: `./fib_seq`
*)

(**
 * == Objective 2: Fibonacci Method ==
 * Computes the nth Fibonacci number.
 *
 * OCaml Usage Explained:
 * `let rec fib n`: This defines a recursive function named `fib` that takes one
 * argument, `n`. The `rec` keyword is essential for functions that call themselves.
 * `match n with`: This is OCaml's powerful pattern matching construct. It checks
 * the value of `n` against a series of patterns.
 * `| 0 -> 0`: The first pattern. If `n` is 0, the function returns 0.
 * `| 1 -> 1`: The second pattern. If `n` is 1, the function returns 1. These are
 * the base cases for the recursion, which prevent an infinite loop.
 * `| _ -> fib (n - 1) + fib (n - 2)`: The underscore `_` is a wildcard pattern
 * that matches any other value. If `n` is not 0 or 1, the function calls
 * itself with `n-1` and `n-2` and returns their sum. This is the recursive step.
 *
 * Note: This is a classic, simple implementation. For very large values of `n`,
 * it becomes inefficient due to re-calculating the same Fibonacci numbers
 * multiple times. A more optimized version would use memoization or an
 * iterative approach.
 *
 * @param n The index in the Fibonacci sequence (integer, should be non-negative).
 * @return The nth Fibonacci number.
 *)
let rec fib n =
  match n with
  | 0 -> 0
  | 1 -> 1
  | _ -> fib (n - 1) + fib (n - 2)

(**
 * == Objective 3: Printing the Sequence ==
 * Iterates from a starting number to an ending number, printing the
 * Fibonacci value for each index in the range.
 *
 * OCaml Usage Explained:
 * `let print_fib_sequence start_num end_num =`: Defines a function that takes two arguments.
 * `if start_num < 0 || end_num < 0 then ...`: A simple guard clause to handle invalid
 * negative input, as the Fibonacci sequence is typically defined for non-negative integers.
 * `else if start_num > end_num then ...`: Handles the case where the user mixes up
 * the start and end numbers.
 * `for i = start_num to end_num do ... done`: OCaml provides imperative-style
 * constructs like `for` loops, which are very useful for simple iterations
 * where a side-effect (like printing to the console) is the main goal.
 * `let result = fib i in`: Calls our `fib` function for the current loop index `i`
 * and binds the returned value to the name `result`.
 * `print_endline (...)`: A standard library function that prints a string to the
 * console, followed by a newline character.
 * `string_of_int i`: Converts an integer `i` to its string representation.
 * `^`: The string concatenation operator in OCaml.
 *
 * @param start_num The starting index for the sequence.
 * @param end_num The ending index for the sequence.
 *)
let print_fib_sequence start_num end_num =
  if start_num < 0 || end_num < 0 then
    print_endline "Error: Fibonacci numbers must be non-negative."
  else if start_num > end_num then
    print_endline "Error: The starting number cannot be greater than the ending number."
  else
    (* Loop from the start to the end number, inclusive *)
    for i = start_num to end_num do
      let result = fib i in
      (* Print the result in a formatted string *)
      print_endline ("Fib(" ^ (string_of_int i) ^ ") = " ^ (string_of_int result))
    done

(**
 * == Objective 1: Recursive Main Loop ==
 * This function creates a persistent loop that prompts the user for input,
 * processes it, and then calls itself to repeat the process.
 *
 * OCaml Usage Explained:
 * `let rec main_loop () =`: Defines a recursive function that takes `()` (pronounced "unit")
 * as its argument. Unit is used for functions that don't need a meaningful input,
 * often because they deal with side-effects like I/O.
 * `print_string "> ";`: Prints a prompt without a newline.
 * `flush stdout;`: This is important in interactive programs. It forces the program
 * to immediately print any text that is buffered. Without it, the "> " prompt
 * might not appear until after the user has already entered their input.
 * `let input = read_line () in`: Reads a line of text from the user's input
 * and binds it to the name `input`.
 * `match input with | "exit" -> ...`: We use pattern matching again. If the user
 * types exactly "exit", we print a goodbye message and the function simply returns,
 * ending the chain of recursive calls and thus exiting the program.
 * `| _ -> ...`: If the input is anything other than "exit", we proceed.
 * `try ... with ...`: This is OCaml's exception handling mechanism. We `try` to
 * execute a block of code that might fail (like converting a non-number string to an integer).
 * If an exception occurs, the `with` block catches it.
 * `Scanf.sscanf input "%d %d" print_fib_sequence`: This is a powerful function from the
 * `Scanf` module. It scans the `input` string, looks for a pattern of two integers
 * ("%d %d"), and if it finds them, it passes them as arguments to the `print_fib_sequence`
 * function. It's a very clean way to parse formatted input.
 * `with _ -> ...`: If `Scanf.sscanf` fails (e.g., the user typed "hello world"), it
 * raises an exception. We catch it with a wildcard `_` and print a helpful error message.
 * `main_loop ()`: The crucial final line. After handling the input (either successfully or
 * by catching an error), the function calls itself to start the loop over again.
 * This is how recursion is used to create loops in functional programming.
 *)
let rec main_loop () =
  print_string "Enter a start and end number (e.g., '5 10') or type 'exit': ";
  flush stdout; (* Ensure the prompt is displayed before waiting for input *)
  let input = read_line () in

  match input with
  | "exit" ->
    (* Base case for the recursion: user wants to exit, so we don't recurse. *)
    print_endline "Goodbye!"
  | _ ->
    (* Recursive step: process input, then call main_loop again. *)
    (try
      (* Attempt to parse two integers from the user's input string. *)
      (* If successful, call print_fib_sequence with the parsed numbers. *)
      Scanf.sscanf input "%d %d" print_fib_sequence
    with
    (* If Scanf.sscanf fails, it raises an exception. We catch it here. *)
    | _ -> print_endline "\nInvalid input. Please enter two integers separated by a space.\n");
    main_loop () (* The recursive call that continues the loop *)

(*
 * The entry point of the script.
 * OCaml scripts execute code at the top level from top to bottom.
 * This is the first code that will actually run.
 *)
let () =
  print_endline "--- OCaml Fibonacci Sequence Generator ---";
  main_loop ()

2. CONCLUSION

This script provides an interactive command-line interface  for calculating and displaying a user-defined segment of the Fibonacci sequence.  It demonstrates core OCaml concepts such as recursion, function definition,   pattern matching, exception handling, and I/O operations.

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.

Pierce, Benjamin C., et al., editors. Logical Foundations. Vol. 1 of Software Foundations. Electronic Textbook, 2024. Version 6.7, http://softwarefoundations.cis.upenn.edu.

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


Wednesday, September 10, 2025

OCAML MODULE

 REVISED: Thursday, September 11, 2025




1. OCAML MODULE

OCaml modules are a powerful feature for organizing and structuring your code. They allow you to group related definitions (like functions, types, and values) together, control which parts are public or private, and avoid naming conflicts.

Let's break it down using the Adder module from the previous tutorial as an example.

A. The Basic Structure: The Implementation (.ml file)

The fundamental syntax for creating a module is module ModuleName = struct ... end.

  • module ModuleName: This declares a new module. A key rule in OCaml is that module names must start with a capital letter. In the example, this is module Adder.

  • struct ... end: This block contains the implementation of the module. Everything defined between struct and end is part of that module.

In the previous tutorial, the implementation is:

module Adder = struct
  (** Adds two integers and returns the result. *)
  let add (a : int) (b : int) : int = a + b
end

Defined here is a module named Adder that contains a single function named add.

B. Accessing Module Contents

To use a function or value from a module, you use the "dot notation": ModuleName.function_name.

This is exactly what you see later in the main_loop function:

let result = Adder.add num1 num2

This tells the OCaml compiler to look inside the Adder module to find the add function. This prevents naming conflicts. For instance, you could have another module, Subtractor, with its own calculate function, and you would access it via Subtractor.calculate.

C. The Public API: The Interface (.mli file)

While the current script only uses one file (.ml), a crucial part of module programming in OCaml is the concept of an interface or signature. The interface defines which parts of the module are publicly accessible.

  • Interfaces are placed in a file with the same name as the implementation, but with a .mli extension.

  • For the adder.ml file, you could create an adder.mli file.

  • The interface file specifies the types of the values you want to expose, but not their implementation.

An interface for the Adder module would look like this in a file named adder.mli:

(** The public interface for the Adder module. *)
(** Exposes a function that adds two integers.
    @param a The first integer.
    @param b The second integer.
    @return The sum of a and b. *)
val add : int -> int -> int

What this does:

  • val add : int -> int -> int: This line declares that there is a value named add available to the public, and its type is a function that takes an int, then another int, and returns an int.

  • Encapsulation: If there were other "helper" functions inside the Adder module in adder.ml that you did not list in adder.mli, they would be private. Nobody outside the adder.ml file could see or use them. This is the core of encapsulation and information hiding in OCaml.

When you compile your code, the OCaml compiler automatically pairs adder.ml with adder.mli and ensures that the implementation correctly matches the public interface you've defined.

2. CONCLUSION

Define the Implementation: Create a module in your .ml file using module ModuleName = struct ... end. Module names must be capitalized.

Define the Public Interface (Optional but good practice): Create a corresponding .mli file to declare which parts of the module are public using val value_name : type.

Access the Contents: Use dot notation (ModuleName.function_name) to use the public parts of the module from other files or modules.

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.

Pierce, Benjamin C., et al., editors. Logical Foundations. Vol. 1 of Software Foundations. Electronic Textbook, 2024. Version 6.7, http://softwarefoundations.cis.upenn.edu.

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



OCAML

 REVISED: Thursday, September 11, 2025




1. OCAML

OCaml is a general-purpose programming language that combines functional, imperative, and object-oriented styles. OCaml belongs to the ML (MetaLanguage) family of languages and is known for its strong type system, expressive syntax, and efficient native code compilation.

2. KEY FEATUES

A. Core Characteristics

Functional programming first-class: Functions are values, higher-order functions are common, and recursion is central.

Strong, static type system: The compiler catches many errors at compile time. Types are inferred, so you rarely need to annotate.

Pattern matching: Powerful way to deconstruct data (like lists, trees, variants) and handle cases safely.

Immutable by default: Values are immutable unless explicitly declared mutable.

Efficient compilation: OCaml compiles to fast native code (via ocamlopt) or bytecode (via ocamlc).

B. Practical Features

Type inference: Lets you write concise code without losing safety.

Algebraic data types: Variants and records make modeling complex data elegant.

Modules and functors: Support modular, large-scale programming.

Garbage collection: Automatic memory management.

Multi-paradigm: You can mix functional, imperative (mutable state, loops), and object-oriented styles.

 C. Why People Use OCaml

Compilers and analyzers: It’s used for projects like Coq, F*, Flow, and the original Rust compiler.

Finance and industry: Some trading systems use it for reliability and performance.

Research and teaching: Clean semantics make it popular in academia.

Static analysis tools: Facebook/Meta’s Infer is written in OCaml.

 D. Compared to Other Languages

vs. Haskell: OCaml is strict (eager evaluation) by default, and more pragmatic with side effects.

vs. Python: OCaml is statically typed and much faster, but has a smaller ecosystem.

vs. C++/Rust: OCaml has less control over memory but far simpler semantics and safer defaults.

3. EXAMPLE
 
(*
  OCaml Adder Script

  This program demonstrates a simple module for integer addition and a main loop
  that continuously prompts the user for two numbers, calculates their sum,
  and prints the result. The program terminates when the user types "exit".
*)

(* 1. A module that adds two integers. *)
module Adder = struct
  (** Adds two integers and returns the result. *)
  let add (a : int) (b : int) : int = a + b
end

(**
 * The main recursive loop of the program. It prompts for user input,
 * processes it, and then calls itself to continue the loop.
*)
let rec main_loop () =
  try
    (* 2. Ask the user to input the value for the two integers. *)
    print_endline "Enter the first integer (or type 'exit' to quit):";
    let input1 = read_line () in

    (* 4. Run in a loop until the user types exit. *)
    if input1 = "exit" then
      (* If the user types 'exit', print a goodbye message and the program will end. *)
      print_endline "Goodbye!"
    else
      begin
        print_endline "Enter the second integer:";
        let input2 = read_line () in

        (* Convert the string inputs to integers.
           This will raise a 'Failure' exception if the input is not a valid integer. *)
        let num1 = int_of_string input1 in
        let num2 = int_of_string input2 in

        (* Call the 'add' function from our 'Adder' module. *)
        let result = Adder.add num1 num2 in

        (* 3. Print the results. Printf.printf allows for formatted output. *)
        Printf.printf "%d + %d = %d\n\n" num1 num2 result;

        (* Call the function again to continue the loop. *)
        main_loop ()
      end
  with
  | Failure _ ->
    (* This block catches errors from 'int_of_string' if the user enters non-numeric input. *)
    print_endline "\nError: Invalid input. Please enter integers only.\n";
    main_loop ()
  | End_of_file ->
    (* This block handles the End of File character (Ctrl+D), gracefully exiting. *)
    print_endline "\nExiting program."

(* The entry point of the program. This initial call starts the main loop. *)
let () = main_loop ()


4. CONCLUSION

OCaml is a high-level, statically typed, functional-first language with strong safety guarantees and efficient performance, well-suited to symbolic computation, compilers, and large-scale reliable systems.

5. 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.

Pierce, Benjamin C., et al., editors. Logical Foundations. Vol. 1 of Software Foundations. Electronic Textbook, 2024. Version 6.7, http://softwarefoundations.cis.upenn.edu.

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



Tuesday, December 17, 2024

COQ FILE NAMING CONVENTIONS

COQ FILE NAMING CONVENTIONS

REVISED: Saturday, September 6, 2025



1. COQ'S FILE NAMING CONVENTIONS

Coq doesn't require any special naming conventions for files to import or export modules. The connection between files and modules is established through the Require and Require Import commands within Coq code, not through filename matching.

Here's a breakdown:

File Organization: You can organize your Coq code into any files you like (conventionally using the .v extension). For example, you might have MyModule.v, AnotherModule.v, and MainFile.v.

Module Definition: Within each .v file, you define modules using the Module ... End The name you give to the module within the Module declaration is crucial, not the filename.

Importing Modules: To use a module defined in another file, you use the Require or Require Import commands.

Require MyModule.: This command loads the file containing the MyModule definition. It makes the module name MyModule available, but you still need to use the qualified name (e.g., MyModule.my_nat) to access its contents.

Require Import MyModule.: This command loads the file and also imports the module into the current scope. This allows you to use the unqualified names (e.g., my_nat) directly.

Example:

Let's say you have two files:

MyModule.v:

Module MyModule.
  Definition my_nat : nat := 5.
End MyModule.

MainFile.v:

Require Import MyModule. (* Or Require MyModule. *)

Compute my_nat. (* If you used Require Import *)
Compute MyModule.my_nat. (* Always works, even with just Require *)

In this example, the filename MyModule.v is arbitrary. You could rename it to SomethingElse.v as long as you change the Require command in MainFile.v accordingly:

(* If MyModule.v is renamed to SomethingElse.v *)
Require Import SomethingElse. (* Coq will look for SomethingElse.vo or SomethingElse.v *)
Compute MyModule.my_nat. (* Module name remains MyModule *)

Compilation:

When you compile your Coq project (using coqc), Coq creates compiled files (.vo files) that are used for faster loading. The Require commands instruct Coq to find these compiled files (or the source .v files if the .vo files are not available). Coq searches in the current directory and in directories specified by the -I flag (include paths).

2. CONCLUSION

Filenames are not directly used to resolve module names during import.

The Require (or Require Import) command, along with the module name specified in the Module declaration, is what connects files to modules.

Coq looks for .vo (compiled) or .v (source) files based on the required module name and the specified search paths.

This decoupling of filenames and module names provides flexibility in organizing your Coq projects.

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.

Pierce, Benjamin C., et al., editors. Logical Foundations. Vol. 1 of Software Foundations. Electronic Textbook, 2024. Version 6.7, http://softwarefoundations.cis.upenn.edu.

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




COQ MODULE SYSTEM PART 2

COQ MODULE SYSTEM PART 2

REVISED: Saturday, September 6, 2025



1. COQ'S MODULE SYSTEM PART 2

Require Import ZArith. 
Require Import Lia.

Module MyModule.

  (* A simple definition within the module. *)
  Definition my_nat : nat := 5.

  (* A function definition. *)
  Definition increment_by_two (n : nat) : nat := n + 2.

  (* A theorem (or lemma) within the module. *)
  Theorem add_two_positive : forall n : nat, increment_by_two n > n.
  Proof.
     intros n.
     simpl. (* Simplifies increment_by_two n to n + 2. *)
     lia. (* Uses the lia tactic to prove the inequality. *)
  Qed.

  (* Another definition, using the previous one. *)
  Definition my_other_nat : nat := increment_by_two my_nat.

  (* A record definition (like a struct or class). *)
  Record point : Type := {
     x : nat;
     y : nat
  }.

  (* A function operating on records. *)
  Definition move_point (p : point) (dx dy : nat) : point :=
     {| x := p.(x) + dx; y := p.(y) + dy |}.

  (* Example usage of the record and function. *)
  Definition my_point : point := {| x := 3; y := 7 |}.
  Definition moved_point : point := move_point my_point 1 2.

End MyModule.

(* Using the definitions from the module. *)
Compute MyModule.my_nat. (* Output: 5 *)
Compute MyModule.add_two 10. (* Output: 12 *)
Compute MyModule.my_other_nat. (* Output: 7 *)
Compute MyModule.moved_point. (* Output: {| x := 4; y := 9 |} : point *)

(* Demonstrating module inclusion/import. *)
Module AnotherModule.
     Import MyModule. (* Brings all definitions from MyModule into scope. *)

    Compute my_nat. (* Now accessible directly. Output: 5 *)
    Compute move_point my_point 5 5. (* Output: {| x := 8; y := 12 |} : point *)
  
  (* Showing that you can redefine things locally without affecting the original module. *)
    Definition my_nat : nat := 100. (* This only redefines my_nat *within* AnotherModule. *)
    Compute my_nat. (* Output: 100 *)
    Compute MyModule.my_nat. (* Output: 5 (MyModule is unchanged) *)

End AnotherModule.

(* Demonstrating module types (interfaces). *)
Module Type PointInterface.
     Parameter point : Type.
     Parameter make_point : nat -> nat -> point.
     Parameter get_x : point -> nat.
     Parameter get_y : point -> nat.
End PointInterface.

Module PointImplementation : PointInterface.
     Record point : Type := { x : nat; y : nat }.
     Definition make_point x y := {| x := x; y := y |}.
     Definition get_x p := p.(x).
     Definition get_y p := p.(y).
End PointImplementation.

Module AnotherPointImplementation : PointInterface.
     (* A different implementation of points using lists *)
     Definition point := list nat.
     Definition make_point x y := [x;y].
     Definition get_x p := List.nth 0 p 0. (* Default 0 if out of bounds *)
     Definition get_y p := List.nth 1 p 0.
End AnotherPointImplementation.

Explanation:

Module MyModule. and End MyModule.: These keywords define the beginning and end of a module named MyModule. Modules act as namespaces, grouping related definitions (constants, functions, theorems, records, etc.) together.

Definition my_nat : nat := 5.: This defines a constant my_nat of type nat (natural number) with the value 5.

Definition increment_by_two (n : nat) : nat := n + 2.: This defines a function increment_by_two that takes a natural number n as input and returns a natural number n + 2.

Theorem add_two_positive : forall n : nat, increment_by_two n > n.: This declares a theorem named add_two_positive. forall n : nat means "for all natural numbers n." The theorem states that for any natural number n, increment_by_two n is greater than n.

Proof. ... Qed.: This block contains the proof of the theorem.

intros n. introduces the universal quantifier n into the context.
simpl. simplifies the expression increment_by_two n to n + 2.
lia. is a powerful tactic that automatically proves inequalities in Presburger arithmetic (linear arithmetic over integers and natural numbers).
Record point : Type := { ... }.: This defines a record type called point. Records are similar to structs or classes in other programming languages. They allow you to group related data together. In this case, a point has two fields: x and y, both of type nat.

p.(x): This is the syntax for accessing the field x of a record p.

{| x := ...; y := ... |}: This is the syntax for constructing a record value.

Compute MyModule.my_nat.: This command computes the value of my_nat within the MyModule namespace. The output will be 5.

Import MyModule.: This statement within AnotherModule imports all definitions from MyModule into the current scope of AnotherModule, allowing you to use them directly without the MyModule. prefix.

Redefinition within a module: As shown in AnotherModule, you can redefine names (like my_nat) locally within a module after importing another. This creates a shadowing effect. The local definition takes precedence within the module where it's redefined, without altering the original definition in the imported module.

Module Types (Interfaces): Module Type PointInterface. defines an interface for a point module. It specifies the signatures (types) of the functions and parameters that a module implementing this interface must provide. Module PointImplementation : PointInterface. and Module AnotherPointImplementation : PointInterface. show two different ways of implementing such interface. This is very powerful for code organization, modularity, and abstraction, as it allows you to swap implementations without changing the code that uses these modules.

2. CONCLUSION

Coq modules are crucial for developing larger and more complex Coq projects, enabling better organization, maintainability, and reusability of code.

Key Takeaways about Coq Modules:

Namespaces: Modules provide namespaces to organize code and avoid name collisions.

Encapsulation: They encapsulate definitions, preventing accidental modification from outside the module (unless explicitly imported).

Code Reusability: Modules promote code reusability by allowing you to import and use definitions in other modules.

Abstraction and Interfaces: Module types enable abstraction by defining interfaces that different modules can implement, promoting modularity and code maintainability.

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.

Pierce, Benjamin C., et al., editors. Logical Foundations. Vol. 1 of Software Foundations. Electronic Textbook, 2024. Version 6.7, http://softwarefoundations.cis.upenn.edu.

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




Monday, December 16, 2024

COQ MODULE SYSTEM PART 1

COQ MODULE SYSTEM PART 1

REVISED: Saturday, September 6, 2025



1. COQ'S MODULE SYSTEM PART 1

Coq's module system is a powerful tool for organizing and structuring large-scale formal developments. By breaking down complex proofs and definitions into smaller, modular components, modules promote code reusability, maintainability, and modularity.

Basic Module Structure:

A Coq module typically consists of:

Module Declaration:

Module MyModule. (* START MYMODULE *)
(* Module Body *)
End MyModule. (* END MYMODULE*)

Module MyModule. (* START MYMODULE *)

Module Body:

Definitions: Definition, Fixpoint, Inductive
Theorems: Theorem, Lemma, Proposition
Tactics: Proof, Qed
Other Modules: Nested modules
Importing and Exporting Definitions:

Import:

Import MyModule.
Imports all definitions from MyModule into the current scope.

Export:

Export MyModule.
Exports all definitions from MyModule to any module that imports it.

Example: A Simple Module for Natural Numbers

End MyModule. (* END MYMODULE *)

Module NatModule. (* START NATMODULE *)

(* Inductive definition of natural numbers *)
Inductive nat : Type :=
   | O : nat
   | S : nat -> nat.

(* Definition of addition on natural numbers *)
Fixpoint plus (n m : nat) : nat :=
   match n with
   | O => m
   | S p => S (plus p m)
   end.

End NatModule. (* END NATMODULE *)

Using the Module:

Import NatModule.

(* Example usage of the `plus` function *)
Theorem plus_comm : forall n m : nat, plus n m = plus m n.
Proof.
  (* ... proof using the `plus` function from NatModule *)
Qed.

Advanced Module Features:

Module Parameters:

Module MyModule (A : Type).  (* START MYMODULE *)
(* Module body using A *)
End MyModule.  (* END MYMODULE *)

Allows for generic modules that can be instantiated with different types.

Module Signatures:

Define interfaces for modules, specifying the signatures of exported definitions.

Functors:

Higher-order modules that can transform other modules.

Example: A Generic List Module

Module ListModule (A : Type). (* START LISTMODULE *)

Inductive list : Type :=
   | nil : list
   | cons : A -> list -> list.

(* ... other list operations *)

End ListModule.  (* END LISTMODULE *)

Using the Generic List Module:

Import ListModule.

(* Instantiate the module for natural numbers *)
Module NatList := ListModule(nat).

(* Use the `nil` and `cons` constructors for natural number lists *)
Definition my_list : NatList.list := NatList.cons 1 (NatList.cons 2 NatList.nil).

2. CONCLUSION

By effectively utilizing Coq's module system, you can organize your formal developments into well-structured, reusable components, making your proofs more modular, maintainable, and easier to understand.

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.

Pierce, Benjamin C., et al., editors. Logical Foundations. Vol. 1 of Software Foundations. Electronic Textbook, 2024. Version 6.7, http://softwarefoundations.cis.upenn.edu.

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




Friday, December 13, 2024

COQ CONSTRUCTORS

COQ CONSTRUCTORS

REVISED:  Saturday, September 6, 2025





1. INTRODUCTION

What are Coq Constructors?

Building Blocks of Inductive Types

In Coq, inductive types define new data structures. Constructors are the fundamental components used to construct values (or terms) of these inductive types.

Defining New Data

Think of constructors as blueprints for creating specific instances of a data type. For example:

bool: 

This built-in inductive type has two constructors: true and false. These are the only ways to create a value of type bool.

nat: 

The natural numbers are defined inductively with two constructors:

O: Represents the number zero.

S: Represents the successor function (e.g., S O is 1, S (S O) is 2, and so on).

2. HOW TO USE CONSTRUCTORS

Define an Inductive Type:

Inductive my_list (A : Type) : Type :=
  | Nil : my_list A
  | Cons : A -> my_list A -> my_list A.
  
Nil is the constructor for an empty list.

Cons is the constructor to add an element (A) to the beginning of an existing list (my_list A).

This defines a new type called my_list. 

A is a type parameter, meaning my_list can be a list of any type.

Compiling:

Inductive my_list (A : Type) : Type :=
  | Nil : my_list A
  | Cons : A -> my_list A -> my_list A.

The Messages window of CoqIDE shows:

my_list is defined
my_list_rect is defined
my_list_ind is defined
my_list_rec is defined
my_list_sind is defined

When you define an inductive type in Coq, the system automatically generates several useful definitions and principles:

my_list_rect: This is the general recursion principle for my_list. It allows you to define functions recursively on my_list by providing cases for Nil and Cons.

my_list_ind: This is the induction principle for my_list. It's used to prove properties about my_list by induction on its structure.

my_list_rec: This is a specialized recursion principle for defining functions on my_list. It's often more convenient to use than my_list_rect in many cases.

my_list_sind: This is a specialized induction principle for proving properties about my_list. It's often more convenient to use than my_list_ind in many cases.

In essence, the my_list type is a fundamental building block for defining various list-based data structures and algorithms in Coq. 

Construct Values:

(* Example list of integers *)
Definition my_list_example : my_list nat := 
    Cons 1 (Cons 2 (Cons 3 Nil)). 
  
This creates a list containing the integers 1, 2, and 3.

Pattern Matching:

Constructors are essential for pattern matching in Coq, which allows you to define functions that operate on inductive types.

Fixpoint my_list_length (l : my_list A) : nat :=
   match l with
   | Nil => 0
   | Cons _ tl => S (my_list_length tl) 
   end.
  
This function calculates the length of a list. It uses pattern matching to distinguish between the Nil case (empty list) and the Cons case (non-empty list).

3. CONCLUSION

By understanding the inductive definition and the generated principles, you can effectively work with lists in Coq proofs and program development.

Constructors are fundamental for defining and working with inductive types in Coq. They provide a structured way to create and manipulate data. 

Pattern matching on constructors is crucial for defining functions that operate on inductive types.

Example:

Let's define a simple binary tree:

Inductive tree (A : Type) : Type :=
  | Leaf : tree A
  | Node : A -> tree A -> tree A -> tree A.
  
Leaf: Represents a leaf node (no children).

Node: Represents an internal node with a value (A) and two child subtrees.

4. 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.

Pierce, Benjamin C., et al., editors. Logical Foundations. Vol. 1 of Software Foundations. Electronic Textbook, 2024. Version 6.7,
http://softwarefoundations.cis.upenn.edu.

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