Leanpub Header

Skip to main content

Little Elpier

Higher-Order Logic, Types and λProlog, One Question at a Time

Little Elpier

What eats a fly? Ask the pond. λProlog, types and higher-order logic from zero, one question at a time.

Free With Membership

With Membership

Free!

$22.00

You pay

Author earns

$

Also available for 1 book credit with a Reader Membership

PDF
EPUB
About

About

About the Book

What eats a fly? Ask the pond.

A small pond: algae, snails and flies, frogs and fish, a duck, a heron and an otter. Eleven short lines say who eats whom. Type a question, eats X fly, and a program answers: the frog, then the fish. It does not search the web or guess. It answers only from what the lines say, and it tells you when they say nothing.

That is a logic program: a list of things that are true, and a way to ask what follows. This little book teaches the idea from zero, in the question-and-answer style of The Little Schemer. Each page is two columns, a short question on the left and a short answer on the right. Cover the answers with a card, try, then look. Now and then a box states a Maxim, a short rule the questions above it have earned, from "A fact says what is true. A question asks whether it follows." to "A typing rule is a clause. A type checker is a logic program, and it runs both ways."

The language is λProlog, a younger and stranger relative of Prolog. It adds three things: types, so the computer turns away nonsense before it runs; predicates you pass around like numbers; and two small words, pi and =>, that let a program say "for any new thing" and "suppose this were true". Those three additions are what higher-order logic programming means. With them, a type checker for a little programming language fits in seven short clauses, and the same seven clauses infer types they were never told. That is where the book ends.

What you will learn

  1. Facts and Questions. Constants, predicates, capital letters as holes, and what "no" really means.
  2. Rules and Recursion. :- as "if", recursion that knows when to stop, and the search tree behind every answer.
  3. Terms and Unification. Numbers built as trees, and how the engine makes two terms the same, or fails.
  4. Lists. One append, run three ways: join, check and split. A relation has no inputs and outputs until you ask it something.
  5. Types. Kinds, types and modes, and a checker that reads the program before it runs.
  6. Predicates as Values. One rule for "above", "upstream of" and "older than"; map, filter and fold.
  7. Lambdas. Predicates without names, terms with holes, and binders that cannot be captured.
  8. Pi and Implication. What-if questions about a pond that is not quite this one.
  9. The Little Type Checker. Seven clauses that check the types of a little language and, asked with a hole, infer them.

Who it is for

Students and programmers who have never taken a logic course. You need some programming in any language, a pencil, and patience. No mathematics, no Prolog and no Lean are assumed. You do not need to know what ∀ means; by Chapter 8 you will have written it. A chapter is an evening's work.

What you get

  • 9 short chapters, 541 questions with their answers, and 11 Maxims collected on one page
  • 35 exercises graded ★ to ★★★, each with a solution that runs
  • 14 diagrams: the food web, search trees, term trees, and binders
  • A two-page cheat sheet and a guided "Where to Go Next"
  • The Apache-2.0 companion code at github.com/Volland/little-elpier: every program in the book, with every printed output kept as a golden file that a test checks

Every example runs

The engine is lean-elpi, an implementation of the ELPI dialect of λProlog written in Lean 4. pip install leanelpi gives you the whole engine with nothing to compile, on macOS, Linux and Windows with WSL; a Docker image and a build from source work too. The book's site, volland.github.io/little-elpier, has a sample of the frames.

Share this book

Author

About the Author

Volodymyr Pavlyshyn

Hey I am Volodymyr 

Seasoned Developer's Journey from COBOL to Web 3.0, SSI, Privacy First Edge AI, and Beyond

 As a seasoned developer with over 20 years of experience, I have worked with various programming languages, including some that are considered "dead," such as COBOL and Smalltalk. However, my passion for innovation and embracing cutting-edge technology has led me to focus on the emerging fields of Web 5.0, Self-Sovereign Identity (SSI),AI Agents, Knowledge Graphs, Agentiic memory systems, and the architecture of a decentralized world that empowers data democratization.

A firm believer in the potential of agent systems and the concept of a "soft" internet, I am dedicated to exploring and promoting these transformative ideas. In addition to writing, I also enjoy sharing my knowledge and insights through videoblogging. Most of my Medium posts serve as supplementary content to the videos on my YouTube channel, which you can explore here: https://www.youtube.com/c/VolodymyrPavlyshyn. 

Join me on this exciting journey as we delve into the future of technology and the possibilities it holds.

Contents

Table of Contents

Preface: most programs are recipes; a logic program is a list of things that are true. What λProlog adds to Prolog (types, predicates as values, pi and =>), why a Little book, and the engine, lean-elpi. Before You Begin: how to read frames (cover the right column with a card), Maxims, asides, graded exercises and the Notes pages; three ways to run the code (Python only, Docker, from source); running a program and the prompt. The chapters Facts and Questions (45 frames): a pond of eight critters and eleven lines that say who eats whom. Facts, constants and predicates; yes-or-no questions; capital letters as holes; several answers and More? (y/N); "and" in a question; why "no" means "the pond does not say so". The First Maxim: A fact says what is true. A question asks whether it follows. The Second Maxim: A capital letter is a hole. The answer fills the holes. Rules and Recursion (67 frames): "Is the heron above the algae?" No line says so. Rules with :-, "and" and "or", a rule that uses itself, how the engine searches (goals left to right, clauses top to bottom, backtracking), and the trap of a rule that loops. The Third Maxim: Read :- as "if" and the comma as "and". Two clauses mean "or". The Fourth Maxim: Every recursion needs a way out, and every step must move toward it. Terms and Unification (56 frames): the frog keeps a tally of the flies it ate, and the tally is a term. Numbers from two words, terms as trees, making two terms the same, a term inside itself (the occurs check), and plus in two clauses, run forwards and then asked for every split. The Fifth Maxim: Unification makes two terms the same, or it fails. There is no halfway. Lists (69 frames): the heron's menu is not one thing; it is a list. Head and tail, mem, append run three ways (join, check, split), counting, collecting answers with std.findall, reversing, and why the library's append will not split. The Sixth Maxim: A relation has no inputs and outputs until you ask it something. Ask it backwards. Types (68 frames): someone types eats 3 fly, and the pond refuses to load. Kinds and types, arrows, prop, type errors as friends, polymorphism, modes (i: and o:), and what types do not catch. The Seventh Maxim: Give every name a type. The checker reads the program before it runs. Predicates as Values (56 frames): the same rule three times, for "above", "upstream of" and "older than", becomes one. A relation as an argument, half a question (eats heron), std.filter, std.map, std.forall, std.exists, std.fold, and what higher-order means. The Eighth Maxim: A predicate is a value. Pass it where you would pass a number. Lambdas (54 frames): "critters that eat flies" as a predicate without a name. x\ eats x fly, lambdas as terms, solving for a function, and a little expression language whose let binds with a lambda: no variable names, no capture bugs. The Ninth Maxim: A lambda is a term with a hole in it. Applying it fills the hole. Pi and Implication (61 frames): "If the duck ate frogs, would the duck be above flies?" => adds a fact for one goal; pi makes a fresh name; together they walk under a binder. Reading the logic: pi is ∀ and => is "implies". The Tenth Maxim: pi makes a name nobody has used. => adds a fact for one goal only. The Little Type Checker (65 frames): add tt (num 1) is nonsense; who decides? A little language with numbers, booleans and functions, its typing rules written as clauses, checking and inferring with the same seven clauses, running a type backwards to find terms, and what the checker cannot do. The Eleventh Maxim: A typing rule is a clause. A type checker is a logic program, and it runs both ways. Every chapter ends with "What We Learned" and four exercises graded ★ to ★★★ (three in Chapter 1). Back matter: The Maxims (all eleven on one page) · The Little Elpier's Card (a cheat sheet: programs, declarations, logic, the prompt) · Solutions (every exercise, every one runs) · Where to Go Next Full Table of Contents The section-level list, as Leanpub generates it from the # and ## headings. Check the book page against it after each publish. Preface Why a Little Book · The Engine · What You Need Before You Begin Frames · Maxims, Asides, Exercises, and Notes · What You Need · Running a Program · Conventions 1. Facts and Questions The Pond · Facts · Questions · Holes · And · What We Learned · Exercises 2. Rules and Recursion Rules · And, Or · Rules That Use Themselves · How the Engine Searches · The Trap · What We Learned · Exercises 3. Terms and Unification Counting with Two Words · Terms Are Trees · Making Two Terms the Same · A Term Inside Itself · Adding · What We Learned · Exercises 4. Lists Two Pieces · Member · Append · Counting · Collecting · Reversing · The Library's Lists · What We Learned · Exercises 5. Types Kinds and Types · Arrows · Prop · When the Checker Says No · Any Type · Who Fills the Holes · What Types Do Not Catch · What We Learned · Exercises 6. Predicates as Values The Same Rule Three Times · A Relation as an Argument · Half a Question · Filter · Map, Forall, Exists and Fold · What Higher-Order Means · What We Learned · Exercises 7. Lambdas A Predicate Without a Name · Lambdas as Terms · Solving for a Function · Names That Point Back · What We Learned · Exercises 8. Pi and Implication Suppose · A Name Nobody Has Used · Together · Walking Under a Binder · Reading the Logic · What We Learned · Exercises 9. The Little Type Checker A Little Language · Rules as Clauses · Functions · Checking and Inferring · Running Backwards · What It Cannot Do · What We Learned · Exercises The Maxims The Little Elpier's Card Programs · Declarations · Logic · At the Prompt Solutions One section per chapter, 1 to 9 Where to Go Next

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub