A fusion of OTP lib/dialyzer + lib/compiler for regular Erlang with type inference

Overview

Typed ERLC

The Problem

I have a dream, that one day there will be an Erlang compiler, which will generate high quality type-correct code from deduced value usages and -type and -specs. It will be able to print reasonable error messages about why the code is broken and will suggest how to fix the errors.

The current standard compiler, lib/compiler in OTP, and the type checking tool, Dialyzer in lib/dialyzer, are two separate projects:

  • The compiler processes programs one file at a time, and never does it look at the rest of the program. This is nice when you compile one module to drop a .beam file into a running program and have them work together. The majority of Erlang projects are built together, from the project root, as single collection of sources. They are packaged together as a single unit and all modules run together.
  • On the contrary, Dialyzer is what the compiler should be, it loads every file in the project together, and processes everything as a single program, checking all possible type usages and finding the type-related problems with high precision. Dialyzer does not compile Erlang code.

The typed_erlc Project

NOTE: This is an early stage work-in-progress.

This project is an alternate Erlang compiler, a fusion of OTP's lib/compiler and lib/dialyzer designed to load, analyze and compile all .erl files in your project together as a single unit, and perform live type deduction and type checking following the standard Erlang -spec() and -type() syntax.

Project File erlproject.toml

NOTE: The file format may and will change.

To perform the task, the tool needs to know all input files before the work begins. Hence the project file syntax is introduced (experimental). The default name for the project file is erlproject.toml, and the contents may look like:

[compiler_options]
include_paths = []          # default []

[inputs]
files = ["*.erl"]           # default ["*.erl"]
directories = ["src"]       # default ["."]
exclude_files = []          # TODO: not implemented; default []
exclude_directories = []    # TODO: not implemented; default []

You can use ** to match any portion of the path.

An empty erlproject.toml, is acceptable, consisting of comments, or no bytes at all. In this case entire current directory will be scanned for "*.erl" files, with all nested subdirectories.

Project Progress

NOTE: This is an early stage work-in-progress. The task list grows.

  • Project configuration
  • Preprocessor
    • Parse and interpret -define/-if*/-else/-endif directives by removing chunks of guarded code
    • Parse -include/include_lib directives
    • Perform inclusion
    • Parse and interpret -if(COND)
    • Substitute ?MACRO
    • Parse and interpret macros with arguments -define(MACRO(X, Y), ...)
  • Erlang syntax parser
    • Common keywords and constructs
    • Module attributes
    • Binary syntax
    • Scientific float syntax
    • Maps syntax
    • Records syntax
    • Typespec syntax
  • Type system and type inference engine
    • Define types, union types, special (any, none, ...)
    • Define expressions AST
    • Add support for types in the AST
    • Infer expression types
  • ???
  • Code generator
You might also like...
A bidirectional type checker

A bidirectional type inference system loosely based on Complete and Easy Bidirectional Typechecking for Higher-Rank Polymorphism.

The never type (the true one!) in stable Rust.

::never-say-never The ! type. In stable Rust. Since 1.14.0. Better than an enum Never {} definition would be, since an instance of type ! automagicall

sblade or switchblade it's a multitool in one capable of doing simple analysis with any type of data, attempting to speed up ethical hacking activities

sblade or switchblade it's a multitool in one capable of doing simple analysis with any type of data, attempting to speed up ethical hacking activities

Simplistic complier~virtual-machine that transforms AST into a Rust function, with basic type checking

rast-jit-vm rast-jit-vm is a simplistic, proof-of-concept~ish compiler / virtual machine that transforms syntax tree into a Rust function, type-checki

The official zeta compiler
The official zeta compiler

Torq What makes Torq the goto language for creating CLI's? Smaller Executables Inbuilt argument and flag parser Your code will work anywhere in any OS

dm-jitaux is a Rust-based JIT compiler using modified auxtools, dmasm and Inkwell LLVM wrapper for boosting Byond DM performance without any hassle!

dm-jitaux is a Rust-based JIT compiler using modified auxtools, dmasm and Inkwell LLVM wrapper for boosting Byond DM performance without any hassle (such as rewriting/refactroing your DM code).

The nightly_crimes!{} macro commits horrible crimes to allow you to enable nightly features on the stable compiler.

The nightly_crimes!{} macro commits horrible crimes to allow you to enable nightly features on the stable compiler.

An example of Brainf*** JIT-compiler with only the standard library.

jit-compiler An example of Brainf*** JIT-compiler with only the standard library. Prerequisite Rust(1.56.0-nightly or later, but it must work kind of

A compiler for the esoteric language ℂ.

The ℂ Programming Language It's a language where the only types are "complex number" and "matrix of complex numbers". In particular, this means you ca

Comments
  • Will typed_erlc support subtyping?

    Will typed_erlc support subtyping?

    (not a feature request, just curious)

    I'm asking because it seems there are untagged unions (which makes me suspect the plan is to have subtyping) but the type-checking seems Hindley-Milner-style, which doesn't have subtyping.

    Thanks for sharing this work.

    opened by mheiber 2
  • Think tank: Typed sends and receives

    Think tank: Typed sends and receives

    It looks like it should be possible to have typespec for pids, and have pid types carry all types that process can send or receive (or just receive).

    Support for raw Erlang: Scan the code reachable from the process spawn entry function, and all receive and send operators would contribute to the pid send/receive type.

    Support for gen_server: Scan the behaviour implementation, namely add the raw gen messages, and add messages processed by the handle_info callback.

    Of course disclaimer: This will only be valid during the compile-time, if the user reloads code to a different version, all type safety is gone and we're back to normal ordinary Erlang.

    question 
    opened by kvakvs 0
Owner
Dmytro Lytovchenko
Dmytro Lytovchenko
A simple, terminal-based Erlang dashboard written in Rust

erldash A simple, terminal-based Erlang dashboard. erldash connects to an Erlang node using the dynamic node name feature (since OTP-23) to collect me

Takeru Ohta 85 Dec 8, 2022
Erlang Language Platform. LSP server and CLI.

Erlang Language Platform (ELP) Description ELP integrates Erlang into modern IDEs via the language server protocol. ELP was inspired by rust-analyzer.

WhatsApp 31 Aug 7, 2023
Use explicit container types with Scrypto! Leverage the Rust compiler's type checking to increase security and productivity when developing Radix blueprints.

Scrypto Static Types Use explicit container types with Scrypto! Leverage the Rust compiler's type checking to increase security and productivity when

null 7 Aug 5, 2022
Run LLaMA inference on CPU, with Rust 🦀🚀🦙

LLaMA-rs Do the LLaMA thing, but now in Rust ?? ?? ?? Image by @darthdeus, using Stable Diffusion LLaMA-rs is a Rust port of the llama.cpp project. Th

Rustformers 2.7k Apr 17, 2023
Run LLaMA inference on CPU, with Rust 🦀🚀🦙

LLaMA-rs Do the LLaMA thing, but now in Rust ?? ?? ?? Image by @darthdeus, using Stable Diffusion LLaMA-rs is a Rust port of the llama.cpp project. Th

Rustformers 2.7k Apr 17, 2023
Efficent platform for inference and serving local LLMs including an OpenAI compatible API server.

candle-vllm Efficient platform for inference and serving local LLMs including an OpenAI compatible API server. Features OpenAI compatible API server p

Eric Buehler 21 Nov 15, 2023
Rust lib for Scaleway API

Notes This Scaleway API Crate is created and maintained by Qovery and used in production in the Qovery Engine. This project relies on OpenAPI Generato

Qovery 5 Nov 10, 2022
Sway-lib-core - Sway standard library core primitives.

lib-core This Sway project contains core operators and extremely primitive logic for use in the standard library of the Sway programming language. Usa

Fuel Labs 8 Mar 29, 2022
This crate provides a convenient macro that allows you to generate type wrappers that promise to always uphold arbitrary invariants that you specified.

prae This crate provides a convenient macro that allows you to generate type wrappers that promise to always uphold arbitrary invariants that you spec

null 96 Dec 4, 2022
A Rust framework for building context-sensitive type conversion.

Xylem is a stateful type conversion framework for Rust.

Jonathan Chan Kwan Yin 4 May 11, 2022