PredScript is a functional programming language with first-class types.
- functional and data driven language
- functions are polymorphic and dispatch on types of all arguments
- types are arbitrary functions that return booleans (predicates)
- types can have subtypes
!! BEWARE: WIP !!
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
.
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": "[email protected]"
});
- Types can be arbitrary predicates (like
isEmailString
andisPositiveInt
) - 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
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
$ npm test
(C) Divyansh Prakash, 2023-2024