Skip to content

divs1210/PredScript

Repository files navigation

License: MIT tests

PredScript

What

PredScript is a functional programming language with first-class types.

Features

!! BEWARE: WIP !!

Why

If a language's type system was built around predicates - abitrary functions of one argument that return booleans - we could define types like isPositiveInt, isEmailString etc.

In the statically typed world, these kind of types are called dependent types. Dependently typed languages like Agda, Idris, etc. often involve writing a lot of proofs which makes them not very well suited for general purpose programming.

PredScript is an attempt to make a flexible dependently-typed functional language with familiar (easy / mainstream / JS-like) syntax that lies somewhere on the spectrum of Dynamically Typed → Statically Typed.

Code Example

interface isEmailString extends isString;
function isEmailString(s: isString): isBool {
    /^\S+@\S+\.\S+$/
    .test($this, s)
}

let isUser: isPred = gen_isRecord({ 
    "id": isInt,
    "username": isString,
    "email": isEmailString
});

let u: isUser = as(isUser, {
    "id": 1,
    "username": "johndoe",
    "email": "johndoe@email.com"
});

Design goals

  • Types can be arbitrary predicates (like isEmailString and isPositiveInt)
  • Be amenable to static type checking / type linting
  • Dynamically type check at runtime
    • do it efficiently
    • runtime can be directed to skip arbitrary checks (for optimization)
  • Polymorphic
  • Compile to JS
  • Interop with JS
  • Look and feel similar to JS

Usage

Install

PredScript is developed against node 20.6.1. Use nvm to install it.

$ nvm install 20.6.1
$ nvm use 20.6.1

Download and install PredScript

$ git clone https://github.com/divs1210/PredScript
$ cd PredScript
$ npm i

Compile and run examples

$ node compile.js examples/poly.ps
$ node dist/index.js

Run tests

$ npm test

License

MIT License

(C) Divyansh Prakash, 2023-2024