Chemlogic

A Logic Programming Computer Chemistry System

CWSF 2014, 2015

Chemlogic is a logic program that helps users solve chemistry problems, including stoichiometric calculations, equation balancing, reaction type analysis and conversions between chemical names and formulas. The program can be applied as a study tool for high school chemistry and a calculation tool in the lab. Guidance is provided for correcting and understanding errors in user input. Input is parsed using grammatical rules, then solutions are found by transforming the syntax tree and applying logical constraints. Chemlogic is available on the Web and on Android.

Balancing a chemical equation

Use Cases

  • High school and first-year university students can use Chemlogic to solve homework problems and work on lab reports. When they make mistakes in applying chemical notation and nomenclature, or their queries contain logic errors, the program will provide guidance that clearly explains the error.
    • For teachers and students in British Columbia, a list of concepts from the science curriculum that are implemented in the program is available.
  • Anyone who writes documents involving chemical equations, formulas, and calculations can save time and frustration by using Chemlogic to generate LaTeX and HTML output.
  • chemcli (the command-line interface) can be used to perform calculations based on input from scripts or sensors in a hardware project. The program can run on a Raspberry Pi.
  • The app and web interface can be used in the lab to quickly balance equations, convert chemical quantities and perform stoichiometric calculations.

Design and Implementation

The architecture of Chemlogic, 2ed.
The architecture of Chemlogic

The program is implemented in Prolog and makes extensive use of its features, including definite clause grammars (DCGs), difference lists, syntactic unification to manipulate ASTs, constraint logic programming (CLPq) and metaprogramming. It is important to note that Prolog backtracks to solve problems and tries to find alternate solutions for queries — the implementation of the program is based on this model.

The Web interface is built using SWI-Prolog's HTTP/HTML libraries, which provide an elegant model for web applications, fully integrated with Prolog. The CLI interface is a simple domain-specific language realized using Prolog's support for defining operators.

The Android application merits special discussion. Its interface is written in Java, using the Android SDK. It communicates with an embedded copy of Chemlogic, running on a cross-compiled copy of SWI-Prolog 6. The toolchain developed by the Angstrom Distribution project is used to perform the cross-compilation and an Android emulator is used to bootstrap the system.

The entire system is extensively documentation. For more details on the design and implementation of the system, please see the paper written for the 2015 Canada-Wide Science Fair: Chemlogic: A Logic Programming Computer Chemistry System, 2ed. Further information is provided in the 2014 paper, extended discussion, conference poster, and development notes.

At the Canada-Wide Science Fair

In high school, I had the great privilege of competing in the Canada-Wide Science Fair, the national competition for youth scientific research in Canada. I met many close friends at the fair and had the opportunity to receive advice from experts in Computer Science.

At the 2015 fair in Fredericton, New Brunswick, I won a Silver medal. That year, I also received the BC Innovation Council Young Innovators Award. In 2014, at the fair in Windsor, Ontario, I won a Bronze medal.

A blog written by our delegate, gives a great impression of my experience at the CWSF. An article appeared in the Nelson Star profiling the West Kootenay delegation to the CWSF.

Installation

You can download the latest release of Chemlogic or check out the git repository. Chemlogic requires a copy of SWI-Prolog to be installed on the system. Once you have compiled the program using make (see the README for instructions), you can simply run chemcli for the CLI or chemweb for the web interface. The web interface will be at http://localhost:8000/chemlogic/ by default.

Building the Android app is somewhat difficult. Check out the git repository from Github. Assuming you have obtained the dependencies, host environment and Android emulator, the process is fully-automated.

Features

  • Chemical formulas (incl. hydrates)
  • Chemical names
    • Retained names
    • Acids
    • Ionic compounds
    • Covalent compounds
    • Some organic compounds
    • Pure substances/allotropes
    • Common names
  • Symbolic equations
  • Word equations
  • Stoichiometric calculations
    • Limiting reactant analysis
    • Excess quantity calculations
    • Support for concentration units
  • Reaction type analysis
    • Completion of neutralization, double replacement and single replacement reactions.
    • Predicts whether reactions of any of the above types will occur (using the reactivity series.)
    • Identification of neutralization, double replacement, single replacement, decomposition and synthesis reactions.

Further Development

  • Enable support for stoichiometry in the Android app.
  • Implement support for equilibrium problems (Keq, solubility, acid/base, etc.)
  • Develop a quiz feature that can generate a variety of random practice problems.
  • Create a proof-of-concept hardware project that uses Chemlogic to process data.
  • Upgrade app design to use the new Material Design style.