Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/pyrocat101/w
Hindley-Milner type inference.
https://github.com/pyrocat101/w
Last synced: about 1 month ago
JSON representation
Hindley-Milner type inference.
- Host: GitHub
- URL: https://github.com/pyrocat101/w
- Owner: pyrocat101
- License: epl-1.0
- Created: 2014-02-22T06:56:29.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2014-03-31T08:10:08.000Z (over 10 years ago)
- Last Synced: 2024-04-15T02:49:05.220Z (7 months ago)
- Language: Clojure
- Homepage:
- Size: 243 KB
- Stars: 5
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Algorithm W
An implementation of algorithm W for Hindley-Milner polymorphic type inference.
Use the Source, Luke!
``` clojure
(infer '+)
; => (:int -> (:int -> :int))
(infer '(+ 1 1))
; => :int
(infer '(fn [x] x))
; => (t0 -> t0)
(infer '(fn [x y] (+ x y)))
; => (:int -> (:int -> :int))
(infer '(let [f (fn [x] x)] f))
; => (t0 -> t0)
(infer '(if true 42 (+ 333 333)))
; => :int
(infer '())
; => (:list t0)
(infer '(empty? ()))
; :bool
(infer '(empty? (cons 1 ())))
; => :bool
(infer '(cons 1 ()))
; => (:list :int)
(infer '(first (cons 1 ())))
; => :int
(infer '(rest (cons 1 ())))
; => (:list :int)
(infer
'(let [fact (fix
(fn [fact x]
(if (= x 1)
1
(* x (fact (- x 1))))))]
(fact 10)))
; => :int;; Tests used in `infer.ss` by Yin Wang
(infer '(fn [f x] (f x)))
; => ((t0 -> t1) -> (t0 -> t1))
(infer '(fn [f x] (f (f x))))
; => ((t0 -> t0) -> (t0 -> t0))
(infer '(fn [m n f x] ((m (n f)) x)))
;; => ((t0 -> (t1 -> t2)) -> ((t3 -> t0) -> (t3 -> (t1 -> t2))))
(infer '((fn [f] (f 1)) (fn [v] v)))
;; => :int
(def S '(fn [x y z] ((x z) (y z))))
(def K '(fn [x y] x))
(infer S)
; => ((t0 -> (t1 -> t2)) -> ((t0 -> t1) -> (t0 -> t2)))
(infer `(~S ~K))
; => ((t0 -> t1) -> (t0 -> t0))
(infer `((~S ~K) ~K))
; => (t0 -> t0)
```## References
1. Martin Grabmüller, Algorithm W Step by Step
2. Luis Damas and Robin Milner, Principal Type-Schemes for Functional Programs## License
Copyright © 2014 Linjie Ding
Distributed under the Eclipse Public License either version 1.0 or (at your
option) any later version.