micahcantor / stlc

Exploring an implementation of the STLC with extensions from TaPL

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

This repository contains an implementation for a small, typed Scheme-like language based on the presentation of the simply typed lambda calculus in the book Types and Programming Languages.

It uses a straight-forward environment-passing style for the evaluator and typechecker rather than the substitution model used in TaPL. The environment is mutable to enable recursive bindings and define statements. Its main features include single-parameter functions with automatic currying and product types (tuples). Some examples may be found below:

Examples

factorial

(define sub1 : (Int -> Int)
  (lambda (x : Int)
    (- x 1)))

(define factorial : (Int -> Int)
  (lambda (n : Int)
    (if (= n 1)
      1
      (* n (factorial (sub1 n))))))

(factorial 5)

tuples

(define intPair : (Int, Int)
  (1, 2))

(define fst : ((Int, Int) -> Int)
  (lambda (pair : (Int, Int))
    (get pair 0)))

(define snd : ((Int, Int) -> Int)
  (lambda (pair : (Int, Int))
    (get pair 1)))

(define addPoints : ((Int, Int) -> ((Int, Int) -> (Int, Int)))
  (lambda (p1 : (Int, Int), p2 : (Int, Int))
    ((+ (fst p1) (fst p2)), (+ (snd p1) (snd p2)))))

(addPoints intPair intPair) // (2, 4)

automatic currying

(define inc : (Int -> Int)
  (+ 1))

(inc 1) // 2

About

Exploring an implementation of the STLC with extensions from TaPL


Languages

Language:Haskell 100.0%