This project provides an OCaml implementation for term manipulation including variable substitution, composition, and computing the Most General Unifier (MGU). The project is built around the main module in term_array.ml.
- term_array.ml: Implements the term data structure, unification, substitution (both regular and in-place), editing of terms, and related functionalities.
- Tests:
- term_array_tests.ml: Basic test cases for term manipulation.
- additional_tests.ml: Advanced and comprehensive tests covering edge cases.
- OCaml compiler (e.g., ocamlc)
- GNU Make
-
Compile the Tests:
- Run
maketo compile the project using the provided Makefile.
- Run
-
Run Basic Tests:
- Execute the basic tests with:
make run_tc
- Execute the basic tests with:
-
Run Advanced Tests:
- Execute the advanced tests with:
make run_advanced
- Execute the advanced tests with:
-
Run All Tests:
- To run all available tests:
make run_all
- To run all available tests:
-
Clean Compiled Files:
- Remove generated files with:
make clean
- Remove generated files with:
The core functionality is implemented in term_array.ml. Key components include:
-
Term Representation:
Terms are defined as either a variable (V string) or a node (Node(symbol * term array)).
• A symbol is a pair of its name and arity.
• Terms are built using arrays to efficiently handle children. -
Signature Validation:
A signature is an array of symbols. The function checks that each symbol has a non-negative arity and ensures no duplicate symbol names exist. -
Well-formed Term Checking:
The functionwftermverifies that a term conforms to a given signature by checking that the symbol’s provided arity matches both the expected arity and the number of child terms. -
Unification (MGU):
Themgufunction computes the Most General Unifier of two terms. It recursively applies substitutions, ensuring no circular bindings occur by using an occurs-check. Two exceptions,NEGATIVE_ARITYandNOT_UNIFIABLE, signal errors related to signature and unification failures, respectively. -
Substitution Mechanisms:
• Regular Substitution:
Implemented via thesubstfunction, this applies an association list mapping variables to terms.
• Substitution Composition:
Thecompose_substfunction combines substitutions, ensuring that mappings are applied in the correct order.
• In-place Substitution:
Through a mutable reference (term_ref), thesubst_in_placefunction updates a term directly, offering an alternative to traditional substitution. -
Editing Terms:
Theeditfunction replaces a subterm at a specified position (given as a list of indices) with a new term, enabling dynamic term modifications. -
Supporting Utility Functions:
Additional functions compute the term's height (ht), size (size), and gather its variables (vars).
String conversion functions help in visualizing both terms and substitutions.
Happy coding!