GithubHelp home page GithubHelp logo

lazear / types-and-programming-languages Goto Github PK

View Code? Open in Web Editor NEW
182.0 3.0 8.0 581 KB

Exercises from Benjamin Pierce's "Types and Programming Languages" textbook + extras!

License: MIT License

Rust 100.00%
types-and-programming-languages tapl types type-theory programming-languages lambda-calculus

types-and-programming-languages's Introduction

types-and-programming-languages

Several Rust implementations of exercises from Benjamin Pierce's "Types and Programming Languages" are organized into different folders, as described below:

  • arith is an implementation of the untyped lambda calculus extended with simple numeric operations

  • lambda is an implementation of the untyped lambda calculus as presented in chapter 5, 6, and 7.

  • typedarith is the arith project extended with simple types: Nat and Bool

  • stlc is an implementation of the simply typed lambda calculus, as discussed in chapters 9 and 10 of TAPL. This simply typed calculus has the types, Unit, Nat, Bool, T -> T (arrow), and Record types.

  • recon contains several implementations of Hindley-Milner based type reconstruction from the untyped lambda calculus to System F, with let-polymorphism. Both Algorithm W (the more common) and Algorithm J (the more efficient) are presented. For Alg. W, both a naive equality constraint solver, and a faster union-find (with path compression) solver are provided. Algorithm J makes use shared mutable references to promote type sharing instead.

  • system_f contains a parser, typechecker, and evaluator for the simply typed lambda calculus with parametric polymorphism (System F). The implementation of System F is the most complete so far, and I've tried to write a parser, typechecker and diagnostic system that can given meaningful messages

  • system_fw contains a parser for a high-level, Standard ML like source language that is desugared into an HIR, and then System F-omega. This extends system_f with type operators and higher-kinded types. This is where most of the ongoing work is located, as I'd like to make this the basis of a toy (but powerful, and useable) programming language. Ideally we will have some form of bidirectional type inference. Work on this has accidentally turned into a full fledged SML compiler, so it's likely that I will roll back the work on the system_fw project to just type checking

  • bidir is is an implementation of the bidirectional typechecker from 'Complete and Easy Bidirectional Typechecking', extended with booleans, product, and sum types. I make no claims on the correctness of the implementation for the extended features not present in the paper.

  • dependent is WIP, implementing a simple, dependently typed lambda calculus as discussed in ATAPL.

types-and-programming-languages's People

Contributors

lazear avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

types-and-programming-languages's Issues

Bug in System F evaluation code

Describe the bug
I was attempting to implement the PairADT existential type in the System Fw project (from Ch30.2) to test typechecking. I manually implemented it with macros, but the System F parser confirms that the macro was writtern correctly:

TyAbs(TyAbs(Abs(TyVar(1), Abs(TyVar(0), TyAbs(Abs((TyVar(2)->(TyVar(1)->TyVar(0))), App(App(Var(0), Var(2)), Var(1))))))))

It is currently not properly typechecking, so I decided to debug, and see if I could typecheck it in System F.

repl: \X \Y \x: X. \y: Y. \R \p: X->Y->R. p x y

| 1 \X \Y \x: X. \y: Y. \R \p: X->Y->R. p x y
                                       ^~~^ --- Type mismatch in application
                                       ^^ --- Abstraction requires type TyVar(2)
                                         ^^ --- Value has a type of TyVar(1)

The term parses correctly, but does not typecheck. Moving the \R type abstraction outside allows it to typecheck however.

\X \Y \R \x: X. \y: Y. \p: X->Y->R. p x y

During debugging of this, I discovered a secondary bug lurking somewhere in the evaluator. The following expression properly typechecks, but gives a bogus substitution during evaluation:

repl: (\x: Nat. \Y \y: Nat->Y. y x) 10 [Nat] succ
    -: Nat
---> (((λTy (λ_:(Nat->TyVar(0)). (#0 #1))) [Nat]) Succ)
---> ((λ_:(Nat->Nat). (#0 #0)) Succ) <--- erroneous term variable shifting here
---> (Succ Succ)
===> (Succ Succ)

This should evaluate to 11 instead

Evaluation of functions polymorphic over multiple types is bugged

Describe the bug
The term:

let poly = \X \Y (\func: X->Y. \val: X. func val) in poly [Nat][Bool]

evaluates to:

(λfunc: (Nat->Nat). (λval: Nat. (func val)))

when instead it should evaluate to:

(λfunc: (Nat->Bool). (λval: Nat. (func val)))

Note that the typechecker properly types this term as

((Nat->Bool)->(Nat->Bool))

To Reproduce
Steps to reproduce the behavior:

  1. Type the term in the REPL
  2. Evaluate

Expected behavior
Term should evaluate correctly

Additional context
It seems likely that the bug is residing in the TyTermSubst visitor. I attempted to model this off of Pierce's tmmap function, but upon further inspection I don't think it directly translates. The OCaml parser doesn't separate out TmVar and TyVar de Bruijn indices, and we do

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.