• Stars
    star
    118
  • Rank 299,923 (Top 6 %)
  • Language
    Haskell
  • License
    MIT License
  • Created over 9 years ago
  • Updated about 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A small compiler for PCF

pcf

A one file compiler for PCF to C. It's currently about 275 lines of compiler and 75 lines of extremely boring instances. The compiler is fully explained in explanation.md.

What's PCF

PCF is a tiny typed, higher-order functional language. It has 3 main constructs,

  1. Natural Numbers

    In PCF there are two constants for natural numbers. Zero and Suc. Zero is pretty self explanatory. Suc e is the successor of a natural number, it's 1 + e in other languages. Finally, when given a natural number you can pattern match on it with ifz.

     ifz e {
        Zero  => ...
      | Suc x => ...
     }
    

    Here the first branch runs if e evaluates to zero and the second branch is run if e evaluates to Suc .... x is bound to the predecessor of e in the successor case.

  2. Functions

    PCF has functions. They can close over variables and are higher order. Their pretty much what you would expect from a functional language. The relevant words here are Lam and App. Note that functions must be annotated with their arguments type.

  3. General Recursion

    PCF has general recursion (and is thus Turing complete). It provides it in a slightly different way than you might be used to. In PCF you have the expression fix x : t in ... and in the ... x would be bound. The intuition here is that x stands for the whole fix x : t in ... expression. If you're a Haskell person you can just desugar this to fix $ \x : t -> ....

Example

For a quick example of how this all hangs together, here is how you would define plus in PCF

    plus =
      fix rec : nat -> nat -> nat in
        λ m : nat.
        λ n : nat.
          ifz m {
             Zero  => n
           | Suc x => Suc (rec x n)
          }

For this library we'd write this AST as

    let lam x e = Lam Nat $ abstract1 x e
        fix x e = Fix (Arr Nat (Arr Nat Nat)) $ abstract1 x e
        ifz i t x e = Ifz i t (abstract1 x e)
        plus = fix 1 $ lam 2 $ lam 3 $
                 ifz (V 2)
                     (V 3)
                     4 (Suc (App (V 1) (V 4) `App` (V 3)))
    in App (App plus (Suc Zero)) (Suc Zero)

We can then chuck this into the compiler and it will spit out the following C code

    tagged_ptr _21(tagged_ptr * _30)
    {
        tagged_ptr _31 = dec(_30[1]);
        tagged_ptr _35 = EMPTY;
        if (isZero(_30[1]))
        {
            _35 = _30[2];
        }
        else
        {
            tagged_ptr _32 = apply(_30[0], _31);
            tagged_ptr _33 = apply(_32, _30[2]);
            tagged_ptr _34 = inc(_33);
            _35 = _34;
        }
        return _35;
    }
    tagged_ptr _18(tagged_ptr * _36)
    {
        tagged_ptr _37 = mkClos(_21, 2, _36[0], _36[1]);
        return _37;
    }
    tagged_ptr _16(tagged_ptr * _38)
    {
        tagged_ptr _39 = mkClos(_18, 1, _38[0]);
        return _39;
    }
    tagged_ptr _29(tagged_ptr * _40)
    {
        tagged_ptr _41 = mkClos(_16, 0);
        tagged_ptr _42 = fixedPoint(_41);
        tagged_ptr _43 = mkZero();
        tagged_ptr _49 = inc(_43);
        tagged_ptr _50 = apply(_42, _49);
        tagged_ptr _51 = mkZero();
        tagged_ptr _56 = inc(_51);
        tagged_ptr _57 = apply(_50, _56);
        return _57;
    }
    int main()
    {
        call(_29);
    }

Which when run with preamble.c pasted on top it prints out 2. As you'd hope.

More Repositories

1

learn-tt

A collection of resources for learning type theory and type theory adjacent fields.
1,913
star
2

higher-order-unification

A small implementation of higher-order unification
Haskell
165
star
3

blott

An experimental type checker for a modal dependent type theory.
OCaml
110
star
4

nbe-for-mltt

Normalization by Evaluation for Martin-Löf Type Theory
OCaml
107
star
5

hm

A small implementation of type inference
Standard ML
74
star
6

miniprl

A small implementation of a proof refinement logic.
Standard ML
49
star
7

wiki-summary.el

Get summaries of wikipedia articles in Emacs
Emacs Lisp
27
star
8

undergraduate-thesis

A discouraging story.
TeX
15
star
9

graph-models

Notes on P-omega
TeX
14
star
10

modal

Modal logic in Haskell through Static Pointers
Haskell
8
star
11

sml-fingertree

The ugliness is worth it, the ugliness is worth it, the ugliness...
Standard ML
8
star
12

cooked-pi

What if we built the same frustrating type checker over and over again? For science.
Haskell
7
star
13

independence-of-the-continuum-hypothesis

Some notes on the independence of the continuum hypothesis.
TeX
7
star
14

c-dsl

a dsl for generating C
Haskell
7
star
15

register-alloc

WIP - A little doodle of a register allocator with an explanation of how it works.
Haskell
6
star
16

fibrational-semantics

Notes on simple fibrational semantics.
TeX
6
star
17

hlf

an implementation of LF
Haskell
5
star
18

hi

A toy interpreter for Haskell
Haskell
5
star
19

sml-kanren

Let's make another miniKanren.
Standard ML
5
star
20

ds-kanren

a tiny logic programming language
Haskell
4
star
21

concurrent-stack-with-helping

A case study in Iris formalizing a concurrent stack with helping.
TeX
4
star
22

emacs.d

my emacs config
Emacs Lisp
3
star
23

secret-notes

Containing small notes which aren't original or noteworthy, but which I don't want to lose.
TeX
3
star
24

hasquito

a dysfunctional compiler to javascript
Haskell
3
star
25

blog

the source for jozefg.bitbucket.org
HTML
3
star
26

sml-abt-unify

Simple unification for ABTs in SML
Standard ML
3
star
27

a-short-talk-on-iris

The notes for a short talk given on Iris in a seminar on concurrent separation logic at Carnegie Mellon.
TeX
2
star
28

bound-gen

Making bound play nice with monad-gen.
Haskell
2
star
29

regex

an exercise in decision procedures in Agda
Agda
2
star
30

folds-common

A conglomeration of folds.
Haskell
2
star
31

ctt.elf

Probably wrong formalization of computational type theory in Twelf.
2
star
32

classical-realizability

Notes on a realizability model for a classical logic
TeX
2
star
33

drafts

Drafts of posts for my blog
TeX
2
star
34

oplss

My notes from OPLSS and related pre-lecture-thingies
2
star
35

kripke

Modal logic and Kripke semantics in Twelf
2
star
36

hotc

Notes on higher-order typed compilation. Probably wrong
TeX
2
star
37

sml-higher-order-matching

A (probably broken) second order pattern matching
Standard ML
2
star
38

simple-abt

Nothing fancy, just binding without pain
Standard ML
2
star
39

generic-church

automatic church encodings
Haskell
2
star
40

monad-gen

a monad for fresh values
Haskell
2
star
41

f2js

A compiler in need of a better name
Haskell
2
star
42

jozefg.github.io

website
HTML
1
star
43

reified-records

automatic reification of records to maps
Haskell
1
star
44

c_of_scheme

a lousy scheme compiler
Haskell
1
star