{"id":18952621,"url":"https://github.com/jdan/compiler.lean","last_synced_at":"2025-10-19T21:58:33.332Z","repository":{"id":54598439,"uuid":"316872240","full_name":"jdan/compiler.lean","owner":"jdan","description":"A formally verified compiler for a simple language with numbers and sums","archived":false,"fork":false,"pushed_at":"2020-11-29T17:33:04.000Z","size":8,"stargazers_count":26,"open_issues_count":0,"forks_count":2,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-01-01T02:41:37.682Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Lean","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jdan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-11-29T04:01:03.000Z","updated_at":"2024-10-27T02:49:14.000Z","dependencies_parsed_at":"2022-08-13T20:50:43.894Z","dependency_job_id":null,"html_url":"https://github.com/jdan/compiler.lean","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jdan%2Fcompiler.lean","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jdan%2Fcompiler.lean/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jdan%2Fcompiler.lean/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jdan%2Fcompiler.lean/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jdan","download_url":"https://codeload.github.com/jdan/compiler.lean/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239952598,"owners_count":19723922,"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-11-08T13:34:08.314Z","updated_at":"2025-10-19T21:58:28.275Z","avatar_url":"https://github.com/jdan.png","language":"Lean","funding_links":[],"categories":[],"sub_categories":[],"readme":"## compiler.lean\n\nInspired by the wonderful [Program Correctness](https://www.youtube.com/watch?v=T_IINWzQhow) video over on [Computerphile](https://www.youtube.com/channel/UC9-y-6csu5WGm29I7JiwpnA), this repo contains a formally verified \"compiler\" for a language with natural number values and add expressions. It is written using the [Lean theorem prover](https://leanprover-community.github.io/).\n\nWe define an expression type for our language and an instruction type for the stack machine which our compiler targets.\n\n```\ninductive Expr\n| Val : ℕ -\u003e Expr\n| Add : Expr -\u003e Expr -\u003e Expr\n\ninductive Instr\n| PUSH : ℕ -\u003e Instr\n| ADD : Instr\n```\n\n### `exec_compile_eq_eval (e : Expr) : exec (compile e) [] = [eval e]`\n\n[[Source]](/src/compiler.lean)\n\nAll expressions `e` when compiled and executed on an empty stack produce the same value as `eval`.\n\nI'm still new to lean so my proofs aren't great. Suggestions welcome!\n\n### `eval : Expr -\u003e ℕ`\n\nEvaluates an `Expr` to produce a natural number\n\n```\neval (Val 5)\n=\u003e 5\neval (Add (Add (Val 10) (Val 20))\n          (Val 30))\n=\u003e 60\n```\n\n### `compile : Expr -\u003e list Instr`\n\nCompiles an `Expr` to produce a list of instructions\n\n```\ncompile (Val 42)\n=\u003e [PUSH 42]\ncompile (Add (Add (Val 10) (Val 20))\n             (Val 30))\n=\u003e [PUSH 10, PUSH 20, ADD, PUSH 30, ADD]\n```\n\n### `exec : list Instr -\u003e list ℕ -\u003e list ℕ`\n\nExecutes a list of instructions on a stack\n\n```\nexec [PUSH 42] []\n=\u003e [42]\nexec [PUSH 10, PUSH 20, ADD, PUSH 30, ADD][]\n=\u003e [60]\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjdan%2Fcompiler.lean","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjdan%2Fcompiler.lean","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjdan%2Fcompiler.lean/lists"}