{"id":30808598,"url":"https://github.com/ecyrbe/lean-units","last_synced_at":"2026-02-14T01:37:29.913Z","repository":{"id":311835091,"uuid":"1045255881","full_name":"ecyrbe/lean-units","owner":"ecyrbe","description":"lean physical unit system, SI international","archived":false,"fork":false,"pushed_at":"2025-09-04T10:02:35.000Z","size":126,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-09-04T12:11:44.668Z","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":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ecyrbe.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-08-26T22:16:07.000Z","updated_at":"2025-09-04T10:02:39.000Z","dependencies_parsed_at":"2025-08-27T07:10:50.351Z","dependency_job_id":"63440a9b-f2ca-49a7-b7b3-cbdb44ed539b","html_url":"https://github.com/ecyrbe/lean-units","commit_stats":null,"previous_names":["ecyrbe/lean-units"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ecyrbe/lean-units","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ecyrbe%2Flean-units","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ecyrbe%2Flean-units/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ecyrbe%2Flean-units/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ecyrbe%2Flean-units/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ecyrbe","download_url":"https://codeload.github.com/ecyrbe/lean-units/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ecyrbe%2Flean-units/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":273854756,"owners_count":25180011,"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","status":"online","status_checked_at":"2025-09-06T02:00:13.247Z","response_time":2576,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":"2025-09-06T03:48:42.494Z","updated_at":"2026-02-14T01:37:25.226Z","avatar_url":"https://github.com/ecyrbe.png","language":"Lean","funding_links":[],"categories":[],"sub_categories":[],"readme":"# lean-units\n\nlean-units is a small Lean library that provides:\n- dimension definitions (length, time, mass, temperature, ...),\n- unit definitions for SI, ExtraSI and Imperial systems,\n- quantities built on top of units so you can represent typed physical values.\n\n## Build / install\n- Build the project with Lake:\n```sh\nlake build\n```\nSee [lakefile.toml](lakefile.toml).\n\nQuick overview\n- A compile-time checked dimensional analysis framework.\n- Compute with physical quantities in a type-safe manner.\n- Support for safe compile-time checked conversions\n- Allow Formal verification of physics statements relying on Dimensional Analysis (more work on this part to be done)\n\n## Basic usage\n\n- Compute with physical quantities:\n\n```lean\nimport LeanUnits.Systems.SI\n\nopen Units\n\ndef solar_mass_kepler_formula\n(period : SI Unit.second) (semi_major_axis : SI Unit.meter): SI Unit.kilogram :=\n  ↑(4.0 • pi^2 • semi_major_axis³ / (G * period²))\n\n#eval solar_mass_kepler_formula year earth_semi_major_axis\n-- 1.9884098707065004e30 (kg)\n```\n- Converting between units:\n\n```lean\n\nabbrev MeV : SI Unit.electronvolt := ⟨10^6⟩ -- 1 MeV\n\ndef electron_mass_ev := (0.51099895069 • MeV / c²)\n#eval electron_mass_ev -- 510998.950690 (c⁻²•eV)\n#eval electron_mass_ev.units -- c⁻²•eV\n#eval electron_mass_ev.dimension -- M\n\n--explicit conversion to kg\ndef electron_mass_kg := electron_mass_ev.into Unit.kilogram\n#eval electron_mass_kg -- 9.109383701528e-31 (kg)\n#eval electron_mass_kg.units -- kg\n#eval electron_mass_kg.dimension -- M\n```\n\n- defining new dimensions :\n\n```lean\n-- base dimensions\ndef_base_dimension Length := \"L\"\ndef_base_dimension Time := \"T\"\ndef_base_dimension Mass := \"M\"\n\n-- derived dimensions\ndef_derived_dimension Acceleration := Length / Time^2\ndef_derived_dimension Force := Mass * Acceleration\n```\n\n- defining base units:\n\n```lean\n-- base SI units\ndef_base_unit meter := \"m\" from Dimension.Length\ndef_base_unit second := \"s\" from Dimension.Time\ndef_base_unit kilogram := \"kg\" from Dimension.Mass\n\n-- derived SI units\ndef_derived_unit newton := \"N\" from kilogram*meter/second^2\n-- derived unit with conversion\ndef_derived_unit celsius := \"°C\" from kelvin with Conversion.translate (27315/100)\n```\n- defining new quantities:\n\n```lean\nabbrev SI (units : Unit) := Quantity units Float\n\ndef ly : SI light_year := ⟨1.0⟩\n\ndef distance_to_alpha_centauri : SI light_year := 4.367 • ly\n#eval distance_to_alpha_centauri -- 4.367 (ly)\n#eval distance_to_alpha_centauri.units -- ly\n#eval distance_to_alpha_centauri.into Unit.meter -- 4.132e16 (m)\n#eval distance_to_alpha_centauri.dimension -- L\n```\n\n## Casting between units or dimensions\n\n### Dimensions\n\nThe library provides a way to cast between two dimensions that guaranty that their dimensions are propositionaly the same by using the tactic `auto_equiv`.\n\n### Units\n\nThe library provides a way to cast between two units that guaranty that :\n- their dimensions are the same (propositionaly)\n- their conversions are the same (propositionaly)\nThese two rules form an equivalence relation on units.\n\nIt uses by default the tactic `auto_equiv` to automatically prove that the units are the equivalent.\nYou can provide your own proof if needed.\n\n## Conversion between units\n\nThe library provides a way to convert between two units that guaranty that :\n- their dimensions are the same (propositionaly)\n\nTheir conversions don't have to be the same, since the conversion is what we want to compute.\n\nIt uses by default the tactic `auto_dim` to automatically prove that the dimensions are the same.\nYou can provide your own proof if needed.\n\n## Internal representation\n\nInternal representation of units and dimensions use Mathlib's `DFinsupp` (dependent finitely supported functions).\nThis allows to represent dimension or units as products of base dimensions or units raised to rational powers.\n\n### Dimensions\n\nA dimension is a product of base dimensions raised to rational powers.\nFor example, the dimension of force is `Mass•Length•Time⁻²` and is represented as:\n- `force = {\"M\" ↦ 1, \"L\" ↦ 1, \"T\" ↦ -2}`\n\nDFinsupp then gives us a natural way to combine dimensions by addition of the exponents since we get a `AddCommGroup` structure on dimensions.\n\nAnd we can define as many base dimensions as we want, since they are just string identifiers mapped to rational exponents.\n\n### Units\n\nUnits represent a choice of metric in a given dimension.\n\nChoosing a metric involves choosing\n- a power factor (it's one for base and derived units)\n- an affine conversion (to convert between units of the same dimension)\n- a dimension (to ensure dimensional correctness)\n\nA unit is a product of base units raised to rational powers.\nFor example, the unit of force in the SI system is `kg•m/s²`\ncan be represented as :\n- `force₁ = {\"kg\" ↦ (1,0,Mass), \"m\" ↦ (1,0,Length), \"s\" ↦ (-2,0,Time⁻²)}`\n\nor when using derived units:\n- `force₂={\"N\" ↦ (1,0,Mass•Length•Time⁻²)}`\n\nConverting between these two representations is possible because\ntheir dimensions are the same under product:\n- `Π 𝒟(force₁) = Mass•Length•Time⁻² = Π 𝒟(force₂)`\n\n### Quantities\n\nA quantity is a value associated with a Unit or a Dimension.\nLean units allows you to work with quantities associated with either a unit or a dimension. Both ways are represented by the same structure `Quantity` allowing to transition your proofs on dimensional analysis to computations with physical quantities with a real unit system (like SI standard).\n\n#### For engineering computations\n\nWhen computing a quantity, you usually want to work with a value associated with a unit.\nFor example, `9.81 (m/s²)` is a quantity representing the acceleration due to gravity at the surface of the Earth.\n\nUsually, in engineering you mostly only work with quantities associated with units.\nAnd units are just a way to ensure dimensional correctness and perform conversions.\n\n#### For formal proofs\n\nWhen doing formal proofs, you usually want to work with quantities associated with a dimension. And don't care about the units.\nFormal proof side of the library is still a work in progress, but you can already define dimensions and prove statements about them.\n\n## Examples\n- See the example files for working code and common tasks:\n  - Basic computations: [Examples/compute.lean](Examples/compute.lean)\n  - Unit conversions and derived units: [Examples/conversion.lean](Examples/conversion.lean)\n  - Using the library in formal proofs / tactics: [Examples/formal.lean](Examples/formal.lean)\n\n## Notes and pointers\n- The library separates systems (SI, ExtraSI, Imperial, Natural). SI units and prefixes are implemented under `LeanUnits.Systems.SI.*`.\n- If you need to define a new unit or derived unit, inspect the framework in [LeanUnits/Framework/Units/Basic.lean](LeanUnits/Framework/Units/Basic.lean).\n\n## LICENSE\n- The project is licensed under the MIT License. See [LICENSE](LICENSE).\n- Copyright 2025 ecyrbe\n\n## Acknowledgements\n- Thanks to the authors of Mathlib for providing a solid foundation for formalizing mathematics in Lean.\n- Thanks to the Lean community for their support and contributions to the ecosystem.\n- Special thanks to [Terrence Tao](https://github.com/teorth) for the formal [source code here](https://github.com/teorth/analysis/blob/18d4fd7253ff17a05133d9b6b120b5f08f5ce6ad/analysis/Analysis/Misc/UnitsSystem.lean). He gave the permission to use his lemmas and definitions as a starting point for the formal side of the library.\n  The adaptation can be seen in [LeanUnits/Framework/Quantities/Lemmas.lean](LeanUnits/Framework/Quantities/Lemmas.lean).","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fecyrbe%2Flean-units","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fecyrbe%2Flean-units","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fecyrbe%2Flean-units/lists"}