├── README.md ├── package.json ├── LICENSE ├── .gitignore └── lib └── index.js /README.md: -------------------------------------------------------------------------------- 1 | # hindley-milner-vanilla-js 2 | Algorithm W implementation for type inference and parametric polymorphism in Vanilla JS 3 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "hindley-milner", 3 | "version": "0.1.0", 4 | "description": "Algorithm W implementation for type inference and parametric polymorphism in Vanilla JS", 5 | "main": "lib/index.js", 6 | "repository": "https://github.com/haskellcamargo/hindley-milner-vanilla-js.git", 7 | "author": "Marcelo Camargo ", 8 | "license": "MIT", 9 | "keywords": [ 10 | "type-inference", 11 | "hindley-milner", 12 | "polymorphism", 13 | "algorithm-w", 14 | "parametric-polymorphism", 15 | "type-theory", 16 | "damas-milner" 17 | ] 18 | } 19 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Marcelo Camargo 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | *.pid.lock 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage 19 | 20 | # nyc test coverage 21 | .nyc_output 22 | 23 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 24 | .grunt 25 | 26 | # Bower dependency directory (https://bower.io/) 27 | bower_components 28 | 29 | # node-waf configuration 30 | .lock-wscript 31 | 32 | # Compiled binary addons (http://nodejs.org/api/addons.html) 33 | build/Release 34 | 35 | # Dependency directories 36 | node_modules/ 37 | jspm_packages/ 38 | 39 | # Typescript v1 declaration files 40 | typings/ 41 | 42 | # Optional npm cache directory 43 | .npm 44 | 45 | # Optional eslint cache 46 | .eslintcache 47 | 48 | # Optional REPL history 49 | .node_repl_history 50 | 51 | # Output of 'npm pack' 52 | *.tgz 53 | 54 | # Yarn Integrity file 55 | .yarn-integrity 56 | 57 | # dotenv environment variables file 58 | .env 59 | 60 | -------------------------------------------------------------------------------- /lib/index.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | /** 3 | * This module is an extensively documented walkthrough for typechecking a 4 | * basic functional language using the Hindley-Damas-Milner algorithm. 5 | * 6 | * This is a port of the same algorithm from Haskell to JS. It can be found 7 | * under https://github.com/quchen/articles/blob/master/hindley-milner. 8 | * 9 | * In the end, we'll be able to infer the type of expressions like 10 | * 11 | * @ 12 | * find (λx. (>) x 0) 13 | * :: [Integer] → Either () Integer 14 | * @ 15 | * 16 | * It can be used in multiple different forms: 17 | * 18 | * * The source is written in literal programming style, so you can almost 19 | * read it from to bottom, minus some few references to later topics. 20 | * * /Loads/ of doctests (runnable and verified code examples) are included 21 | * * The code is runnable in NodeJS, all definitions are exposed. 22 | * * A small main module that gives many examples of what you might try out in 23 | * Node interactive mode is also included. 24 | */ 25 | 26 | 27 | 28 | // ############################################################################# 29 | // ############################################################################# 30 | // * Preliminaries 31 | // ############################################################################# 32 | // ############################################################################# 33 | 34 | 35 | 36 | // ############################################################################# 37 | // ** Names 38 | // ############################################################################# 39 | 40 | 41 | 42 | /** 43 | * A 'name' is an identifier in the language we're going to typecheck. 44 | * Variables on both the term and type leve have 'Name's, for example. 45 | */ 46 | const Name = String 47 | 48 | 49 | /** 50 | * Pretty visualization for 'Name'. It'll be further implemented to other 51 | * structures 52 | */ 53 | Name.prototype.ppr = function () { 54 | return this.toString() 55 | } 56 | 57 | 58 | 59 | // ############################################################################# 60 | // ** Monotypes 61 | // ############################################################################# 62 | 63 | 64 | 65 | /** 66 | * A monotype is an unquantified/unparametric type, in other words it contains 67 | * no @forall@s. Monotypes are the inner building blocks of all types. Examples 68 | * of monotypes are @Int@, @a@, @a → b@. 69 | * 70 | * In formal notation, 'MType's are often called τ (tau) types. 71 | */ 72 | // TVar :: MType 73 | const TVar = function (name) { // ^ @a@ 74 | this.name = name 75 | } 76 | 77 | 78 | // TFun :: MType 79 | const TFun = function (parameter, returns) { // ^ @a → b@ 80 | this.parameter = parameter 81 | this.returns = returns 82 | } 83 | 84 | 85 | // TConst :: MType 86 | const TConst = function (name) { // ^ @Int@, @()@, … 87 | this.name = name 88 | } 89 | 90 | 91 | /** 92 | * Since we can't declare our own types in our simple type system here, we'll 93 | * hard-code certain basic ones so we can typecheck some familiar functions 94 | * that use them later. 95 | */ 96 | // TList :: MType 97 | const TList = function (type) { // ^ @[a]@ 98 | this.type = type 99 | } 100 | 101 | 102 | // TEither :: MType 103 | const TEither = function (left, right) { // ^ @Either a b@ 104 | this.left = left 105 | this.right = right 106 | } 107 | 108 | 109 | // TTuple :: MType 110 | const TTuple = function (first, second) { // ^ @(a, b)@ 111 | this.first = first 112 | this.second = second 113 | } 114 | 115 | 116 | /** 117 | * >>> console.log(new TFun(new TEither(new TVar('a'), new TVar('b')), new TFun(new TVar('c'), new TVar('d'))).ppr()) 118 | * Either a b → c → d 119 | */ 120 | TVar.prototype.ppr = function () { 121 | return this.name 122 | } 123 | 124 | 125 | TList.prototype.ppr = function () { 126 | return `[${this.type.ppr()}]` 127 | } 128 | 129 | 130 | TEither.prototype.ppr = function () { 131 | return `Either ${this.left.ppr()} ${this.right.ppr()}` 132 | } 133 | 134 | 135 | TTuple.prototype.ppr = function () { 136 | return `Tuple ${this.first.ppr()} ${this.second.ppr()}` 137 | } 138 | 139 | 140 | TConst.prototype.ppr = function () { 141 | return this.name.ppr() 142 | } 143 | 144 | 145 | TFun.prototype.ppr = function (parenthesize = false) { 146 | const lhs = this.parameter.ppr(true) 147 | const rhs = this.returns.ppr() 148 | 149 | return parenthesize 150 | ? `(${lhs} → ${rhs})` 151 | : `${lhs} → ${rhs}` 152 | } 153 | 154 | 155 | 156 | /** 157 | * The individual variables of an 'MType'. This is simply the collection of all the 158 | * individual type variables ocurring inside of it. 159 | * 160 | * __Example:__ The free variables of @a → b@ are @a@ and @b@. 161 | */ 162 | // freeMType :: MType → Set Name 163 | TVar.prototype.freeMType = function () { 164 | return new Set([this.name]) 165 | } 166 | 167 | 168 | TFun.prototype.freeMType = function () { 169 | return new Set([...this.parameter.freeMType(), ...this.returns.freeMType()]) 170 | } 171 | 172 | 173 | TList.prototype.freeMType = function () { 174 | return this.type.freeMType() 175 | } 176 | 177 | 178 | TEither.prototype.freeMType = function () { 179 | return new Set([...this.left.freeMType(), ...this.right.freeMType()]) 180 | } 181 | 182 | 183 | TTuple.prototype.freeMType = function () { 184 | return new Set([...this.first.freeMType(), ...this.second.freeMType()]) 185 | } 186 | 187 | 188 | TConst.prototype.freeMType = function () { 189 | return new Set() 190 | } 191 | 192 | --------------------------------------------------------------------------------