• Stars
    star
    381
  • Rank 110,161 (Top 3 %)
  • Language
    TeX
  • Created over 8 years ago
  • Updated about 2 years ago

Reviews

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

Repository Details

Research on integrating datalog & lambda calculus via monotonicity types

FILES

paper/: ICFP 2016 paper.

src/: Implementation of Datafun in Racket. src/repl.rkt is most useful.

What follows is an extremely out-of-date description of Datafun's type theory. For more up-to-date information, here's a paper preprint; or you can clone the repository and run make in the paper/ directory to produce datafun.pdf.

Datafun

poset types     A,B ::= bool | nat | A × B | A → B | A →⁺ B | Set A | A + B
lattice types   L,M ::= bool | nat | L × M | A → L | A →⁺ L | Set A
expressions     e   ::= x | λx.e | e e
                      | (e, e) | πᵢ e
                      | true | false | if e then e else e
                      | inᵢ e | case e of in₁ x → e; in₂ x → e
                      | ∅ | e ∨ e | {e} | ⋁(x ∈ e) e
                      | fix x. e

contexts        Δ   ::= · | Δ,x:A
monotone ctxts  Γ   ::= · | Γ,x:A

Semantic intuition

Types correspond to partially ordered sets (posets):

  • bool is booleans; false < true.

  • nat is the naturals, ordered 0 < 1 < 2 < ...

  • A × B is pairs, ordered pointwise: (a₁,b₁) ≤ (a₂,b₂) iff a₁ ≤ a₂ and b₁ ≤ b₂.

  • A + B is sums, ordered disjointly. in₁ a₁ ≤ in₁ a₂ iff a₁ ≤ a₂, and likewise for in₂; but in₁ a and in₂ b are not comparable to one another.

  • Set A is finite sets of As, ordered by inclusion: x ≤ y iff ∀(a ∈ x) a ∈ y.

  • A → B are functions, ordered pointwise: f ≤ g iff ∀(x : B) f x ≤ g x.

  • A →⁺ B are monotone functions; for any f : A →⁺ B, given x,y : A such that x ≤ y we know that f x ≤ f y. The type system enforces this monotonicity. Monotone functions are ordered pointwise, just like regular functions.

Lattice types L are a subset of all types, defined so that every lattice type happens to be unital semilattices (usls) — that is, join-semilattices with a least element. Any lattice type is a type, but not all types are lattice types.

Semantics of expressions, in brief:

  • x, (e₁, e₂), πᵢ e, inᵢ e, if, true, false, and case all do what you'd expect.

  • λx.e and e e both do what you'd expect. However, it is left ambiguous whether they represent ordinary or monotone function creation/application.

    One could of course require the programmer to write ordinary and monotone functions differently (or even ordinary and monotone function applications differently). But for our purposes it's simplest to just give two typing rules (ordinary and monotone) for λx.e (and likewise e e).

    It is definitely possible to infer monotonicity in a bidirectional way, and possibly even in a Damas-Milner-ish way, but that's outside the scope of this README.

  • represents the least element of a lattice type.

  • e₁ ∨ e₂ represents the least upper bound ("lattice join") of e₁ and e₂.

  • {e} represents the singleton set containing e.

  • ⋁(x ∈ e₁) e₂ is set-comprehension. e₁ must have a finite set type; e₂ must have a lattice type. For each x in e₁, we compute e₂; then we lattice-join together all values of e₂ computed this way, and that is our result. This generalizes the "bind" operation of the finite-set monad.

  • fix x. e finds the least fixed-point of the monotone function λx. e.

Typing judgment: Δ;Γ ⊢ e : A

Our typing judgment is Δ;Γ ⊢ e : A

We call Δ our unrestricted context and Γ our monotone context. Both contexts obey the usual intuitionistic structural rules (weakening, exchange).

Typing rules

 Δ,x:A; Γ ⊢ e : B       Δ;Γ ⊢ e₁ : A → B   Δ;· ⊢ e₂ : A
------------------ λ    -------------------------------- app
Δ;Γ ⊢ λx.e : A → B             Δ;Γ ⊢ e₁ e₂ : B

 Δ; Γ,x:A ⊢ e : B       Δ;Γ ⊢ e₁ : A →⁺ B   Δ;Γ ⊢ e₂ : A
------------------- λ⁺  --------------------------------- app⁺
Δ;Γ ⊢ λx.e : A →⁺ B            Δ;Γ ⊢ e₁ e₂ : B

NB. The monotone context of e₂ in the rule app for applying ordinary functions must be empty! Since A → B represents an arbitrary function, we cannot rely on its output being monotone in its argument. Thus its argument must be, not monotone in Γ, but constant.

The typing rules for tuples, sums, and booleans are mostly boring:

    Δ;Γ ⊢ eᵢ : Aᵢ            Δ;Γ ⊢ e : A₁ × A₂
-----------------------      ------------------
Δ;Γ ⊢ (e₁,e₂) : A₁ × A₂       Δ;Γ ⊢ πᵢ e : Aᵢ

                                       Δ;Γ ⊢ e : bool    Δ;Γ ⊢ eᵢ : A
-----------------  ------------------  -------------------------------
Δ;Γ ⊢ true : bool  Δ;Γ ⊢ false : bool  Δ;Γ ⊢ if e then e₁ else e₂ : A

    Δ;Γ ⊢ e : Aᵢ
---------------------
Δ;Γ ⊢ inᵢ e : A₁ + A₂

However, there are two eliminators for sum types:

TODO

The typing rules get more interesting now:

                 Δ;Γ ⊢ eᵢ : L
-----------    -----------------
Δ;Γ ⊢ ∅ : L    Δ;Γ ⊢ e₁ ∨ e₂ : L

  Δ;· ⊢ e : A        Δ;Γ ⊢ e₁ : Set A  Δ,x:A; Γ ⊢ e₂ : L
-----------------    ------------------------------------
Δ;Γ ⊢ {e} : Set A          Δ;Γ ⊢ ⋁(x ∈ e₁) e₂ : L

Δ; Γ,x:L ⊢ e : L   L equality
----------------------------- fix
      Δ;Γ ⊢ fix x.e : L

In the last rule, for fix, the premise L equality means that the type L at which the fixed-point is computed must have decidable equality.

Two-layer formulation

Alternative, two-layer formulation:

set types       A,B ::= U P | A ⊗ B | A ⊕ B | A ⊃ B
poset types     P,Q ::= bool | nat | P × Q | P →⁺ Q | Set A
                      | Disc A | P + Q
lattice types   L,M ::= bool | nat | L × M | P →⁺ M | Set A
expressions     e   ::= x | λx.e | e e | (e, e) | πᵢ e
                      | inᵢ e | case e of in₁ x → e; in₂ x → e
                      | U u
lattice exprs   u   ::= x | λx.u | u u | (u, u) | πᵢ u
                      | ∅ | u ∨ u | {e} | ⋁(x ∈ u) u
                      | fix x. u
                      | D e | U⁻¹ e | let D x = u in u

Δ;· ⊢ u : P               Δ ⊢ e : U P
------------- U         --------------- U⁻¹
Δ ⊢ U u : U P           Δ;Γ ⊢ U⁻¹ e : P

  Δ ⊢ e : A             Δ;Γ ⊢ u₁ : D A   Δ,x:A; Γ ⊢ u₂ : P
--------------- D       ----------------------------------- let-D
Δ;Γ ⊢ D e : D A            Δ;Γ ⊢ let D x = u₁ in u₂ : P

I use and for set types not because they are linear, but simply to distinguish them from the × and + operations on poset types.

This version needs to be fleshed out more fully. In particular, we need some axioms to ensure that U (P + Q) = U P ⊕ U Q.

More Repositories

1

rotten

Demo of Ken Thompson's Reflections on Trusting Trust as a tiny lisp compiler
Python
96
star
2

minikanren-datalog

Datalog implemented in minikanren
Scheme
24
star
3

rust-sandbox

Playing around with rust
Rust
10
star
4

moxy

Language with monoidally extensible syntax
Racket
7
star
5

quick_macro

Quick macros for talon
Python
7
star
6

liberis

lisp bytecode vm
C
6
star
7

ukanren

Playing around with microKanren (in Haskell)
Haskell
6
star
8

rust-hashlife

Gosper's Hashlife algorithm implemented in Rust
Rust
6
star
9

stlc

Stuff about the simply typed lambda calculus
Agda
4
star
10

rntztex

Styles, a class, and a Makefile for LaTeX projects
Python
4
star
11

StreamSet

lazy potentially infinite streamable sets in Haskell
Haskell
3
star
12

ccc

a categorical compiler compiler
Racket
3
star
13

thesis

TeX
3
star
14

rust-fingertrees

Finger trees implemented in rust
Rust
3
star
15

indexed_clipboard

An indexed clipboard for talon.
Python
2
star
16

unidirectional-inference

Bidirectional type inference with wildcards, or "unidirectional type inference"
Haskell
2
star
17

quelle

datalog for queries and linear logic for state transitions
Racket
2
star
18

ttol

research re type theory of linking
Standard ML
2
star
19

plt-ideas-bot

Twitter bot generating PLT ideas by mashing up previous ideas
Python
1
star
20

minikanren-nbe

Normalisation by evaluation in minikanren
Scheme
1
star
21

ocpo

A language with omega-cpo semantics, inspired by Nick Rioux's Granite.
Haskell
1
star
22

emacs-talon

Some emacs talon integration stuff
Emacs Lisp
1
star
23

talon_emacs_draft

Using emacs as a draft window for talon.
Python
1
star
24

deriv-parsing

Exploring "Parsing with Derivatives"
Racket
1
star
25

fixflow

Investigating recursive dataflow for computing fixed points
Racket
1
star
26

curry-howard-slides

TeX
1
star
27

cam-rust

Simple functional VM based on Categorical Abstract Machine, implemented in Rust
Rust
1
star
28

datalog

datalog implementation in racket
Racket
1
star
29

xkcdhash

C
1
star
30

cps-talk

Python
1
star
31

datalog-experience-report

Scheme
1
star
32

infix-calculator

Arithmetic calculator with customizable infix operators
Haskell
1
star
33

hs-prolog

Simple pseudo-Prolog interpreter in Haskell
Haskell
1
star