{"id":26818884,"url":"https://github.com/milesbarr/hindley-milner-in-python","last_synced_at":"2025-08-31T11:46:12.199Z","repository":{"id":219509493,"uuid":"336429286","full_name":"milesbarr/hindley-milner-in-python","owner":"milesbarr","description":"Hindley–Milner type inference implemented in Python.","archived":false,"fork":false,"pushed_at":"2023-06-07T13:19:17.000Z","size":6,"stargazers_count":13,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-30T05:15:30.512Z","etag":null,"topics":["hindley-milner","programming-language-design","programming-language-theory","python","type-system","type-systems"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/milesbarr.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2021-02-06T01:25:16.000Z","updated_at":"2025-03-29T17:44:56.000Z","dependencies_parsed_at":"2024-03-21T09:00:23.242Z","dependency_job_id":null,"html_url":"https://github.com/milesbarr/hindley-milner-in-python","commit_stats":null,"previous_names":["milesbarr/hindley-milner-in-python"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/milesbarr%2Fhindley-milner-in-python","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/milesbarr%2Fhindley-milner-in-python/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/milesbarr%2Fhindley-milner-in-python/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/milesbarr%2Fhindley-milner-in-python/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/milesbarr","download_url":"https://codeload.github.com/milesbarr/hindley-milner-in-python/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250540956,"owners_count":21447426,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["hindley-milner","programming-language-design","programming-language-theory","python","type-system","type-systems"],"created_at":"2025-03-30T05:15:35.136Z","updated_at":"2025-04-24T00:48:45.764Z","avatar_url":"https://github.com/milesbarr.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Hindley–Milner in Python\n\nA Hindley–Milner type system allows a programming language's types to be inferred with few or no explicit type annotations. A unique property of Hindley–Milner type systems is that type constraints are propagated both forward and backward throughout a program. More concretely, that means variables both inherit type constraints from assigned expressions and impose type constraints on assigned expressions based on how the variables are used. The body of a function (or lambda) imposes type constraints on its parameters and return value. When calling a function, the function's constraints are duplicated onto the arguments and the expression where the return value is used. As a result, a Hindley–Milner type system forms a system of type constraints across an entire program.\n\nAlthough a Hindley–Milner type system can sound daunting, its implementation can be quite simple. This repository contains a minimal example of a Hindley–Milner type system implemented in Python. The example is intended as a learning tool to more easily understand the implementation of a Hindley–Milner type system. One could extrapolate the same concepts from the example to implement a Hindley–Milner type system for a full-fledged programming language.\n\n## Examples\n\n### Identity Function\n\nPython representation:\n```py\nidentity = lambda x: x\nprint(type(identity(True)))\n```\n\nHindley–Milner type inference:\n```py\nprint(infer(\n    [\"let\", \"identity\", [\"lambda\", \"x\", [\"identifier\", \"x\"]],\n        [\"apply\", [\"identifier\", \"identity\"], [\"bool\", True]]], {}))\n```\n\n### Factorial Function\n\nPython representation:\n```py\ndef factorial(n):\n    if n == 0:\n        return 1\n    else:\n        return n * factorial(n - 1)\n\nprint(type(factorial(1)))\n```\n\nHindley–Milner type inference:\n```py\nif_type = [\"any\"]\nprint(infer(\n    [\"let\", \"factorial\", [\"lambda\", \"n\",\n        [\"apply\", [\"apply\", [\"apply\", [\"identifier\", \"if\"],\n            [\"apply\", [\"identifier\", \"zero?\"], [\"identifier\", \"n\"]]],\n            [\"int\", 1]],\n            [\"apply\", [\"apply\", [\"identifier\", \"*\"],\n                [\"identifier\", \"n\"]],\n                [\"apply\", [\"identifier\", \"factorial\"],\n                    [\"apply\", [\"apply\", [\"identifier\", \"-\"],\n                        [\"identifier\", \"n\"]], [\"int\", 1]]]]]],\n        [\"apply\", [\"identifier\", \"factorial\"], [\"int\", 123]]], {\n    \"if\": [\"lambda\", [\"bool\"],\n        [\"lambda\", if_type, [\"lambda\", if_type, if_type]]],\n    \"zero?\": [\"lambda\", [\"int\"], [\"bool\"]],\n    \"*\": [\"lambda\", [\"int\"], [\"lambda\", [\"int\"], [\"int\"]]],\n    \"-\": [\"lambda\", [\"int\"], [\"lambda\", [\"int\"], [\"int\"]]]\n}))\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmilesbarr%2Fhindley-milner-in-python","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmilesbarr%2Fhindley-milner-in-python","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmilesbarr%2Fhindley-milner-in-python/lists"}