{"id":13749197,"url":"https://github.com/lorepozo/program-induction","last_synced_at":"2025-05-09T12:30:50.593Z","repository":{"id":62442965,"uuid":"123941174","full_name":"lorepozo/program-induction","owner":"lorepozo","description":"A library for program induction and learning representations.","archived":false,"fork":false,"pushed_at":"2023-12-18T13:52:30.000Z","size":819,"stargazers_count":29,"open_issues_count":12,"forks_count":7,"subscribers_count":5,"default_branch":"main","last_synced_at":"2024-10-29T15:32:19.446Z","etag":null,"topics":["bayesian-inference","genetic-programming","lambda-calculus","pcfg","program-induction","representation-learning"],"latest_commit_sha":null,"homepage":"","language":"Rust","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/lorepozo.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,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-03-05T15:41:49.000Z","updated_at":"2024-09-21T11:18:35.000Z","dependencies_parsed_at":"2023-12-18T13:42:50.387Z","dependency_job_id":"7100a628-a92d-45e5-b2d9-fbee49494ae7","html_url":"https://github.com/lorepozo/program-induction","commit_stats":{"total_commits":421,"total_committers":5,"mean_commits":84.2,"dds":0.2660332541567696,"last_synced_commit":"41933cd46a22ba5d78ba3f2d0f2fca18a4ada375"},"previous_names":["lucasem/program-induction"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lorepozo%2Fprogram-induction","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lorepozo%2Fprogram-induction/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lorepozo%2Fprogram-induction/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lorepozo%2Fprogram-induction/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lorepozo","download_url":"https://codeload.github.com/lorepozo/program-induction/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224859694,"owners_count":17381676,"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":["bayesian-inference","genetic-programming","lambda-calculus","pcfg","program-induction","representation-learning"],"created_at":"2024-08-03T07:00:56.812Z","updated_at":"2024-11-15T23:32:15.416Z","avatar_url":"https://github.com/lorepozo.png","language":"Rust","readme":"# program-induction\n\n[![crates.io](https://img.shields.io/crates/v/programinduction.svg)](https://crates.io/crates/programinduction)\n[![docs.rs](https://docs.rs/programinduction/badge.svg)](https://docs.rs/programinduction)\n[![CI](https://github.com/lorepozo/program-induction/actions/workflows/ci.yaml/badge.svg)](https://github.com/lorepozo/program-induction/actions/workflows/ci.yaml)\n\nA library for program induction and learning representations.\n\nImplements Bayesian program learning and genetic programming.\nSee the [docs](https://docs.rs/programinduction) for more information.\n\n## Installation\n\nInstall [rust](https://rust-lang.org) and ensure you're up to date (`rustup update`).\nIn a new or existing project, add the following to your `Cargo.toml`:\n\n```toml\n[dependencies]\nprograminduction = \"0.9\"\n# many examples also depend on polytype for the tp! and ptp! macros:\npolytype = \"7.0\"\n```\n\nThe documentation requires a custom HTML header to include KaTeX for math\nsupport. This isn't supported by `cargo doc`, so to build the documentation\nyou may use:\n\n```sh\ncargo rustdoc -- --html-in-header rustdoc-include-katex-header.html\n```\n\n## Usage\n\nSpecify a probabilistic context-free grammar (PCFG; see `pcfg::Grammar`) and\ninduce a sentence that matches an example:\n\n```rust\nuse polytype::tp;\nuse programinduction::pcfg::{task_by_evaluation, Grammar, Rule};\nuse programinduction::{ECParams, EC};\n\nfn evaluate(name: \u0026str, inps: \u0026[i32]) -\u003e Result\u003ci32, ()\u003e {\n    match name {\n        \"0\" =\u003e Ok(0),\n        \"1\" =\u003e Ok(1),\n        \"plus\" =\u003e Ok(inps[0] + inps[1]),\n        _ =\u003e unreachable!(),\n    }\n}\n\nfn main() {\n    let g = Grammar::new(\n        tp!(EXPR),\n        vec![\n            Rule::new(\"0\", tp!(EXPR), 1.0),\n            Rule::new(\"1\", tp!(EXPR), 1.0),\n            Rule::new(\"plus\", tp!(@arrow[tp!(EXPR), tp!(EXPR), tp!(EXPR)]), 1.0),\n        ],\n    );\n    let ec_params = ECParams {\n        frontier_limit: 1,\n        search_limit_timeout: None,\n        search_limit_description_length: Some(8.0),\n    };\n    // task: the number 4\n    let task = task_by_evaluation(\u0026evaluate, \u00264, tp!(EXPR));\n\n    let frontiers = g.explore(\u0026ec_params, \u0026[task]);\n    let sol = \u0026frontiers[0].best_solution().unwrap().0;\n    println!(\"{}\", g.display(sol));\n}\n```\n\nThe Exploration-Compression (EC) algorithm iteratively learns a better\nrepresentation by finding common structure in induced programs. We can run\nthe EC algorithm with a polymorphically-typed lambda calculus representation\n`lambda::Language` in a Boolean circuit domain:\n\n```rust\nuse polytype::{ptp, tp};\nuse programinduction::{domains, lambda, ECParams, EC};\n\nfn main() {\n    // circuit DSL\n    let dsl = lambda::Language::uniform(vec![\n        // NAND takes two bools and returns a bool\n        (\"nand\", ptp!(@arrow[tp!(bool), tp!(bool), tp!(bool)])),\n    ]);\n    // parameters\n    let lambda_params = lambda::CompressionParams::default();\n    let ec_params = ECParams {\n        frontier_limit: 1,\n        search_limit_timeout: Some(std::time::Duration::new(1, 0)),\n        search_limit_description_length: None,\n    };\n    // randomly sample 250 circuit tasks\n    let rng = \u0026mut rand::thread_rng();\n    let tasks = domains::circuits::make_tasks(rng, 250);\n\n    // one iteration of EC:\n    let (new_dsl, _solutions) = dsl.ec(\u0026ec_params, \u0026lambda_params, \u0026tasks);\n    // print the new concepts it invented, based on common structure:\n    for (expr, _, _) in \u0026new_dsl.invented {\n        println!(\"invented {}\", new_dsl.display(expr))\n        // one of the inventions was \"(λ (nand $0 $0))\",\n        // which is the common and useful NOT operation!\n    }\n}\n```\n\nYou may have noted the above use of `domains::circuits`. Some domains are\nalready implemented for you. Currently, this only consists of _circuits_ and\n_strings_.\n\n## TODO\n\n(you could be the one who does one of these!)\n\n- [x] First-class function evaluation within Rust (and remove lisp\n      interpreters).\n- [x] Add task generation function in `domains::strings`\n- [x] Fallible evaluation (e.g. see how `domains::strings` handles `slice`).\n- [x] Lazy evaluation.\n- [x] `impl GP for pcfg::Grammar` is not yet complete.\n- [ ] Eta-long sidestepping (so `f` gets enumerated instead of `(λ (f $0))`)\n- [ ] Consolidate lazy/non-lazy evaluation (for ergonomics).\n- [ ] Permit non-`\u0026'static str`-named `Type`/`TypeScheme`.\n- [ ] Ability to include recursive primitives in `lambda` representation.\n- [ ] Faster lambda calculus evaluation (less cloning; bubble up whether\n      beta reduction happened rather than ultimate equality comparison).\n- [ ] PCFG compression is currently only estimating parameters, not actually\n      learning pieces of programs. An [adaptor\n      grammar](http://cocosci.berkeley.edu/tom/papers/adaptornips.pdf)\n      approach seems like a good direction to go, perhaps minus the Bayesian\n      non-parametrics.\n- [ ] Add more learning traits (like `EC` or `GP`)\n- [ ] Add more representations\n- [ ] Add more domains\n\n","funding_links":[],"categories":["Projects"],"sub_categories":["Libraries"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Florepozo%2Fprogram-induction","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Florepozo%2Fprogram-induction","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Florepozo%2Fprogram-induction/lists"}