DEV Community

Cover image for Phel: the Lisp that compiles to PHP 🐘
Jose Maria Valera Reales
Jose Maria Valera Reales

Posted on • Updated on • Originally published at chemaclass.es

Phel: the Lisp that compiles to PHP 🐘

The new functional language. Fully interoperable with PHP πŸš€

The Phel Language

Phel is a Functional Programming (FP) language that compiles to PHP. It is a dialect of Lisp inspired by Clojure and Janet.

Features

  • Built on PHP’s ecosystem
  • Good error reporting
  • Different Datastructures (Arrays, Tables, and Tuples)
  • Macros
  • Recursive functions
  • Powerful but simple Syntax
  • REPL

Why Phel?

Phel is a creation of Jens Haase, and as he mentions on the website, β€œit is the result of many failed attempts to do functional programming in PHP”. He was looking for something with these characteristics:

  • A LISP-inspired
  • Functional Programming language
  • That runs on cheap hosting providers
  • And is easy to write and debug

From my point, it was ending May 2020 when I finished the book β€œSeven Languages in Seven Weeks: A Pragmatic Guide to Learning Programming Languages”, in which I learned a bit of Prolog, Erlang, Clojure, Haskell… I even created a repository in Github to store my progress over the weeks with these learnings.

Alt Text

Suddenly, one day beginning of June 2020, I saw a post in Reddit's PHP official channel, with a message of someone mentioning this β€œPhel” project, claiming to be a β€œfunctional programming language written in PHP”! (or something like that).

I work with PHP for around 8 years so far, and I always try to combine OOP with FP as much as possible (because I believe both paradigms are better combined; they are not necessarily exclusive), and a new dialect of Lisp fully written in native PHP 7.4 sounded really exciting. I decided to take a look at it. I wanted to see the code.

The fresh readings from Mathias NoBack books such as Object Design Style Guide, Principles of Package Design, and Advanced Web Application Architecture, gave me the courage to try out what I learned and put it for real in a project. This was (and still is) a beautiful challenge, which helps me to test myself and grow professionally at the same time.

My first commit was done on 6 June 2020, and since then I work on this project a bit (almost) every day in my free time. It helps me to learn more about FP, languages internal design, and best practices on testing and software design architecture in general.

I started applying minor changes until I ended refactoring the whole compiler structure (among other things) in order to make the modules easier to read and understand. Psalm, strict types, good test coverage, unifying code style… These were some of my favorite topics.

Alt Text

Example of Phel code

The following example gives a short impression of how Phel looks like:

# Define a namespace
(ns my\example)
# Define a variable with name "my-name" and value "world"
(def my-name "world")
# Define a func with name "print-name" and one argument "your-name"
(defn print-name [your-name]
  (print "hello" your-name))
# Call the function
(print-name my-name)
Enter fullscreen mode Exit fullscreen mode

Current status of Phel

Check out the official website with all documentation of the Phel Language:

https://phel-lang.org/

Alt Text

Also recommended

Originally written in https://chemaclass.es/blog/phel-first-release/

Top comments (0)