{"id":18008581,"url":"https://github.com/mgree/mgt","last_synced_at":"2025-04-04T12:11:08.383Z","repository":{"id":48556583,"uuid":"279703846","full_name":"mgree/mgt","owner":"mgree","description":"Implementation of \"Migrating Gradual Types\" by Campora et al.","archived":false,"fork":false,"pushed_at":"2021-07-20T15:54:24.000Z","size":495,"stargazers_count":1,"open_issues_count":1,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-02-09T21:43:52.380Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mgree.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}},"created_at":"2020-07-14T22:12:20.000Z","updated_at":"2021-07-20T15:54:29.000Z","dependencies_parsed_at":"2022-09-01T00:11:26.462Z","dependency_job_id":null,"html_url":"https://github.com/mgree/mgt","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mgree%2Fmgt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mgree%2Fmgt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mgree%2Fmgt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mgree%2Fmgt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mgree","download_url":"https://codeload.github.com/mgree/mgt/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247174456,"owners_count":20896078,"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":[],"created_at":"2024-10-30T01:19:11.203Z","updated_at":"2025-04-04T12:11:08.347Z","avatar_url":"https://github.com/mgree.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# mgt\n[![Main workflow](https://github.com/mgree/mgt/actions/workflows/build.yml/badge.svg)](https://github.com/mgree/mgt/actions/workflows/build.yml)\n\nImplementation of \"Migrating Gradual Types\" by Campora, Chen, Erwig, and Walkingshaw ([POPL 2018](https://dl.acm.org/doi/10.1145/3158103), [author PDF](http://web.engr.oregonstate.edu/~walkiner/papers/popl18-migrating-gradual-types.pdf)).\n\nClosely follows the formalism, where the [paper-formalism](https://github.com/mgree/mgt/releases/tag/paper-formalism) tag is closest to the paper. There are several additions and changes:\n\n  - Some tiny bug fixes and divergences from the paper.\n  - Mostly imperative implementation of constraint generation and unification.\n  - Constraint generation takes a term optionally annotated with gradual types and returns a term fully annotated with migrational types.\n  - Operator overloading.\n\nTo use the ocaml compiler, you need to install the `mgt` runtime package via OPAM. That's easiest to do via pinning:\n\n```ShellSession\n$ cd ocaml\n$ opam pin add .\n```\n\nAfter that, `cargo build` and `cargo run` should work just fine to run the `mgt` compiler.\n\n## Overloading\n\nConsider a source operation like `==` in JS:\n\n```\ntrue == true\n1    == 1\n0    == false\n```\n\nEach of these returns `true`. In the target language, there are really three\nunderlying operations:\n\n```\n==b : bool -\u003e bool -\u003e bool\n==i : int  -\u003e int  -\u003e bool\n==? : ?    -\u003e ?    -\u003e bool\n```\n\nAny use of `==` in the source language ought to turn into one of these\nthree operators. But which one? If you know the argument types and they\nfit, you should prefer `==b` and `==i`. The rest of the time, it's gotta\nbe `==?`.\n\nThe situation is even worse for something like `+`, where you have in\nthe target language:\n\n```\n+i : int    -\u003e int    -\u003e int\n+s : string -\u003e string -\u003e string\n+? : ?      -\u003e ?      -\u003e ?\n```\n\n(Recalling that `5 +? hi` is `\"5hi\"` and `true +? \"love\"` is `\"truelove\"`, just\nfor lols.) Type inference itself depends on overloading resolution.\n\n### Current algorithm\n\nOnly `==` and `+` (with addition `+i` and string concatenation `+s`) are handled\nright now. There are two new ingredients:\n\n  1. Biased choice. You can specify that you'd prefer a given choice to be made\n     if possible. When computing valid eliminators, biased choice will keep the\n     biased side and ignore the unbiased one---so long as there's a solution.\n  2. Ground constraints. You can require that a type _really_ be of some\n     particular ground type. For now, that's just base types, but it should be\n     possible to extend this.\n\nTaken together, an overloaded operation uses biased choice to try to select\nconcrete implementations, but the arguments are given ground constraints so that\nwe don't over-narrow things.\n\nSome examples (in `eg/`) clarify things well. In particular:\n\n```\nlet eq = \\x:?. \\y:?. x == y in \neq 5 true\n```\n\nWill infer `x:int` and `y:bool`, but correctly select `==?`. Inference\ngeneralizes correctly:\n\n```\nlet eq = \\x:?. \\y:?. x == y in \neq 5 true \u0026\u0026 eq 0 0 \u0026\u0026 eq false false\n```\n\nWill infer `x:?` and `y:?` and select `==?`.\n\nCritically, writing `0 == 0 \u0026\u0026 true == false` will correctly select `==i` and\n`==b`, respectively.\n\n## Assumptions/holes\n\nThe `assume x [: t] in e` form lets you add bindings at arbitrary types without\ndefinitions. It uses typed holes `__[name]` generate fresh type variables.\n\nIt's tempting to have a typed hole have type `?`, but that doesn't work like\nother bits of the inference. If you want something to be `?`, write `assume foo\n: ? in bar`. Writing `assume foo in bar` will try to infer `foo`'s type.\n\n## TODO\n\n- Language features\n  + [ ] Let polymorphism and type schemes (need to separate unification\n        variables and true type variables). First cut: just have `Ctx` track\n        type schemes, instantiating at every variable. Most things will be\n        monomorphic, but assumes can give us polymorphism. Operation resolution\n        may need to yield type schemes rather than types, too\n    * [x] Dynamic interpretation of polymorphism\n    * [ ] Cf. Henglein and Rehof's coercion parameters (update CLI params/options)\n\n- [ ] Ability to use explicit operations like `+i` in the source language\n\n- [ ] Ability to use explicit operations like `+i` in the source language\n\n- [ ] Implement Rastogi et al.'s \"The Ins and Outs of Gradual Type Inference\".\n\n- [ ] Herder-style scoring?\n\n- [ ] Interpreter (for testing)\n\n- [ ] Better compiler output \n  + [ ] save explicit code before OCaml\n  + [ ] comment indicating variation\n  + [x] print result\n\n- [ ] Ensure that choices don't show up in the final, eliminated AST\n     + [x] Using assertion, or...\n     + [ ] `eliminate` operation that changes types to really have no choice\n           left (would need more than one `Expr`, trait)\n\n- [ ] Refactor pretty printing (separate trait, nicer arithmetic output)\n\n## Optimization\n\n- [ ] Make things mutable\n- [ ] Go by reference in inference, type checking, etc.?\n\n# Acknowledgments\n\nConversations with [Arjun Guha](https://khoury.northeastern.edu/~arjunguha), [Colin\nGordon](https://twitter.com/csgordon/), [Ron\nGarcia](https://twitter.com/rg9119), and [Jens\nPalsberg](http://web.cs.ucla.edu/~palsberg/) were helpful.\n[@jorendorff](https://twitter.com/jorendorff) gave a tip on concrete syntax.\n  \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmgree%2Fmgt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmgree%2Fmgt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmgree%2Fmgt/lists"}