• Stars
    star
    181
  • Rank 205,111 (Top 5 %)
  • Language
    OCaml
  • Created over 9 years ago
  • Updated 11 months ago

Reviews

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

Repository Details

Prototype type inference engine

This is a prototype type inferencer for an ML-like language with subtyping and polymorphism. It's written in OCaml, and building it requires Menhir.

It accepts lines containing programs written in a very limited subset of OCaml (just lambdas, unit and let), and spews some debugging output and their principal type if it likes them, and an unceremonious exception if it does not.

Some examples, and their inferred types:

The identity function is given type v0 -> v0. All free variables in inferred types are considered universally quantified, as is the custom in these parts.

fun x -> x
(v0 -> v0)

The inferencer actually spits out two types: the original and a simplified one. The second one is a simplified but equivalent rendering of the first. The simplifier is not currently very good.

Type ascriptions can be used, and check polymorphic subsumption:

(fun x -> x : a -> a)
(v0 -> v0)

The ascription may be less general than the inferred type:

(fun x -> x : unit -> unit)
(unit -> unit)

But the inferencer won't like a more general or unrelated type:

(fun x -> x : a -> b)
<error>

On to something more interesting. Self-application can be typed in this system:

fun x -> x x
(((v1 -> v0) & v1) -> v0)

The argument x must be both a v1 -> v0 and a v1. One thing you can do with the self-application function is apply it to itself (why you would do this is outside the scope of this work):

(fun x -> x x) (fun x -> x x)
Bot

This is the bottom type, equivalent to just a (universally quantified). We can check this with a type ascription:

((fun x -> x x) (fun x -> x x) : a)
Bot

The Y combinator can be typed by this system (here showing the simplified type, the first attempt is a bit uglier):

(fun f -> (fun x -> f (x x)) (fun x -> f (x x)))
((v0 -> v0) -> v0)

This version of the Y combinator doesn't work in strict languages. The CBV-safe version eta-expands, giving:

(fun f -> (fun x -> f (fun v -> (x x) v)) (fun x -> f (fun v -> (x x) v)))
(((v1 -> v0) -> ((v1 -> v0) & v2)) -> v2)

This is slightly different from the expected ((a -> b) -> (a -> b)) -> (a -> b). An ascription check shows that it is more general:

(fun f -> (fun x -> f (fun v -> (x x) v)) (fun x -> f (fun v -> (x x) v)) :
  ((a -> b) -> (a -> b)) -> (a -> b))
(((v1 -> v0) -> (v1 -> v0)) -> (v1 -> v0))

The extra generality comes from allowing v2 to be more general than v1 -> v0, which comes into play if -> has subtypes.

We can use fixpoint combinators to make values with recursive types, such as a function that takes arbitrarily many arguments:

(fun f -> (fun x -> f (fun v -> (x x) v)) (fun x -> f (fun v -> (x x) v))) 
   (fun f -> fun x -> f)
(Top -> rec v0 = (Top -> v0))

This type could be folded into the simpler rec a = Top -> a; but the (rather stupid) simplifier can't figure that out yet.

More Repositories

1

jq

Command-line JSON processor
C
21,280
star
2

malfunction

Malfunctional Programming
OCaml
318
star
3

counterexamples

Counterexamples in Type Systems
JavaScript
281
star
4

crowbar

Property fuzzing for OCaml
OCaml
174
star
5

bf.sed

A brainfuck compiler, written in sed
154
star
6

ppx_stage

Staged metaprogramming in stock OCaml
OCaml
142
star
7

idris-malfunction

Experimental Malfunction backend for Idris
Haskell
82
star
8

caml-oxide

safe FFI between OCaml and Rust (experimental)
Rust
64
star
9

minhttp

A tiny and surprisingly featureful webserver
C
49
star
10

git-ls

List files, annotated by git status
Python
36
star
11

pathfinder

pythonic filesystem library
Python
35
star
12

hax

Random scripts, snippets of code, whatever.
Java
23
star
13

libfib

A library for high-performance lightweight threads
C++
19
star
14

llvm

Context switching for LLVM
C++
15
star
15

linkage

easy-to-use wrapper for OCaml's Dynlink
OCaml
15
star
16

ocaml-afl-persistent

persistent-mode afl-fuzz for ocaml
OCaml
14
star
17

Sandbox

Sandbox for running native code under Linux at full speed, without allowing it to do anything other than read/write stdio
C
12
star
18

bzseek

Random access library for bzip2 files
C
8
star
19

cshore

Call C from Python without writing bindings
C
8
star
20

moria

Embedded language for construction of DWARF
OCaml
7
star
21

with-gdb

Attaches gdb to crashed processes
C
7
star
22

nd

Netsoc LDAP directory
Python
6
star
23

wikiflow

random walks through human knowledge
5
star
24

fyp

Final year project
Haskell
5
star
25

python-monkeypatch

Bringing Ruby best practices to Python
Python
5
star
26

Altitude

Debugging above C level
C
4
star
27

clang

Clang C/C++ compiler, with patches to support context-switching
C++
4
star
28

sequent-subtyping

Coq
3
star
29

irclinks

IRC network visualisation
Python
3
star
30

seqbuf

Sequential buffer access
OCaml
3
star
31

sneeky

Steganography through bad spelling
Python
2
star
32

bandgraph

Scale-space visualisation of time-series data
Python
2
star