{"id":13726594,"url":"https://github.com/cristianoc/REInfer","last_synced_at":"2025-05-07T21:33:15.429Z","repository":{"id":44065003,"uuid":"135302963","full_name":"cristianoc/REInfer","owner":"cristianoc","description":"Runtime Extended Inference for json data.","archived":false,"fork":false,"pushed_at":"2023-01-04T22:11:14.000Z","size":1382,"stargazers_count":22,"open_issues_count":10,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-04T06:02:20.126Z","etag":null,"topics":["javascript","json","programming-language","reason","reasonml","type-inference","type-system"],"latest_commit_sha":null,"homepage":"","language":"Reason","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/cristianoc.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-05-29T13:48:35.000Z","updated_at":"2023-08-19T07:49:55.000Z","dependencies_parsed_at":"2023-02-02T21:31:46.249Z","dependency_job_id":null,"html_url":"https://github.com/cristianoc/REInfer","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/cristianoc%2FREInfer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cristianoc%2FREInfer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cristianoc%2FREInfer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cristianoc%2FREInfer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cristianoc","download_url":"https://codeload.github.com/cristianoc/REInfer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252957509,"owners_count":21831501,"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":["javascript","json","programming-language","reason","reasonml","type-inference","type-system"],"created_at":"2024-08-03T01:03:14.029Z","updated_at":"2025-05-07T21:33:14.990Z","avatar_url":"https://github.com/cristianoc.png","language":"Reason","funding_links":[],"categories":["Reason"],"sub_categories":[],"readme":"## REInfer: Runtime Extended Inference\n\nREInfer performs **R**untime **E**xtended type **Infer**ence on json data. Compared to traditional types, the extended types incorporate some numerical information, such as the number of times a field appears in the data, or the number of times it is null.\n\nA facility is provided to compare inferred types. This follows the idea of a diff algorithm which takes two values and returns the difference. The difference consists of a common part plus two deltas. Deltas are applied using a sum operation for extended types. The diff algorithm borrows and extends ideas from abduction for shape analysis, applied to type theory instead of program logic.\n\nA simple UI is provided to experiment with the primitives: it can be used to visualize inferred types and their difference.\n\nThere is a thought experiment exploring of the use of runtime type inference in conjunction with runtime type checking. This combination gives an instrumented semantics that can be used to execute programs. This instrumented semantics has the peculiar property that it can fail at run time in cases where the program is not statically typable. In contrast to ordinary testing, this also applies to programs that **do not fail** when executed under a normal semantics.\n\n\n## Run Project\n\n```sh\nnpm install\nnpm start\nnpm run webpack\n# in another tab\nnpm run serve\n```\n\nThen open the served page in the browser and edit `Demo.re`.\n\n\n### Example of type inference\n\n```\nval1:\n{\"x\":\"hello\"}\n\nval2:\n{\"x\":null, \"y\":0}\n```\n\nType inference will produce types `styp1` and `styp2`\n\n\u003cimg src=\"doc/inference.png\" width=\"400\"\u003e\n\nThe numerical information indicates that fied `x` occurs once. But in the second value it has optional type `? 1`, indicating that 1 (out of 1) value of x is null.\n\nNumbers begin to add up when using arrays, or when sampling multiple values.\nFor example, `[null,2,3,4]` has this type:\n\n\u003cimg src=\"doc/array.png\" width=\"170\"\u003e\n\n\n### Example of diff\n\nOnce the types for `val1` and `val2` have been computed, a difference algorithm computes a type `stypB`:\n\n\u003cimg src=\"doc/diffB.png\" width=\"200\"\u003e\n\nThe type highlights what sub-parts which are in common. Also, `lhs` highlights the subpart that the first type has in addition to the common part, and correspondingly for `rhs`.\n\nIt is also possible to look at `stypA1` and `stypA2` that indicate the overall difference between the common type and the two resulting ones:\n\n\u003cimg src=\"doc/diffA1A2.png\" width=\"400\"\u003e\n\n### Example of union types\n\nSome data formats allow differet types of data in the same position.\n\nFor example: `[ \"hell\", 0, \"world\"]` has this inferred type:\n\n\u003cimg src=\"doc/union.png\" width=\"150\"\u003e\n\nDiff is also supported with union types.\n\n### Example of singleton types\n\nWhile by default basic types are at the granularity of string/number/boolean, it's possible to turn on singleton types mode so each constant has a different type.\n\nFor example, `[\"h\", \"e\", \"l\", \"l\", \"o\", \"w\", \"o\", \"r\", \"l\", \"d\"]` in singleton type mode has inferred type:\n\n\u003cimg src=\"doc/singleton.png\" width=\"130\"\u003e\n\n\n## References\n\n* Sean Grove's talk at ReasonConf 2018 \u003chttps://www.youtube.com/watch?v=OT7xOpPCGds\u003e.\n\n* Tree view UI adapted from [react-treeview](https://github.com/chenglou/react-treeview).\n\n* Abduction in shape analysis \u003chttps://dl.acm.org/citation.cfm?id=2049700\u003e.\n\n## License\n\nThis project is [MIT-licensed](./LICENSE.md).\n\n\n## Formalisation\n\n\n### Values\n\n```\nval ::=\n  123 |\n  “abc” |\n  true | false |\n  null |\n  obj |\n  [ val1, …, valn ]\n\nobj ::= { x1:val, …, xn:val }\n```\n\n\n### Types\n\nTypes: `t` are ordinary types of a programming language, with `t?` an optional type.\n\n```\nt ::=\n  empty |\n  number |\n  string |\n  boolean |\n  t? |\n  {x1:t, …, xn:t} |\n  [ t ]\n```\n\n\n### Statistical Types\n\nStatistical types are a mutual definition of `styp` and `typ`.\n\n```\no ::= opt(p) | notOpt\n```\n\n```\nstyp ::= (typ,o)::p\n```\n\n```\ntyp ::=\n  empty |\n  number |\n  string |\n  boolean |\n  {x1:styp1, …, xn:stypn} |\n  [styp]\n```\n\nAbbreviation: write `typ?n::p` or `typ::p`.\n\n### Type checking for Types\n\n[Type checking for t](doc/TypeCheckingTypes.md): `|- val : t`\n\n\n### Erasure of Statistical Types\n\n[Erasure](doc/Erasure.md): `|- |styp| = t`\n\n\n### Type checking for Statistical Types\n\n[Type checking for styp](doc/TypeCheckingStyp.md):  `|- val : styp`\n\n\n### Sum of Statistical Types\n\n[Sum operations](doc/SumOperations.md): `|- styp1 + styp2 = styp` and `|- typ1 + typ2 = typ` and `|- o1 + o2 = o`.\n\nNotice this defines a partial commutative monoid.\n\n\n### Inference of Statistical Types from value samples\n\nGiven a set of sampled data `val1, …, valn` define a process of type inference `|- val1, …, valn -\u003e styp`. The process consists of using the existing inference for arrays:\n\n```\n|- [val1, …, valn] : [styp]\n———————————————————————————\n |- val1, …, valn -\u003e styp\n```\n\n\n### Abduction for Statistical Types\n\n[Abduction](doc/Abduction.md): `|- styp1 + \u003cstypA\u003e = styp2` and `|- typ1 + \u003ctypA\u003e = typ2` and `|- o1 + \u003coA\u003e = o2`.\n\nWith abduction, the inputs are `styp1` and `styp2`, and the output is `stypA`, the abduced value.\n\nThere are many solutions to the abduction question. We want smallest solution w.r.t. `\u003c=` where `styp1 \u003c= styp2` if there is `styp` such that `|- styp1 + styp = styp2`.\n\nAbduction aims to compute the smallest representation of the difference between statistical types.\n\nIt turns out that this form of difference is not sufficient because there are incomparable types and the negation corresponding to `+` does not exist. So another form of diff is required.\n\n\n### Diff for Statistical Types\n\n[Diff](doc/Diff.md): `|- \u003cstypA1,stypA2\u003e + \u003cstypB\u003e = styp1,styp2`\nand `|- \u003ctypA1,typA2\u003e + \u003ctypB\u003e = typ1,typ2` and `|- \u003coA1,oA2\u003e + \u003coB\u003e = o1,o2`.\n\nThe inputs are `styp1` and `styp2` and the outputs are `stypB` and `stypA1` and `stypA2`. The common part is `stypB` and the two deltas are `stypA1` and `stypA2`.\nThe correctness critera are that `stypA1 + stypB = styp1` and `stypA2 + stypB = styp2`.\n\nThere are many solutions to the diff question. We want the largest solution w.r.t. `\u003c=` for the `B` part, and smallest for the `A1` and `A2` parts, where `styp1 \u003c= styp2` if there is `styp` such that `|- styp1 + styp = styp2`.\n\n\n### Extension: Union Types\n\n```\nstypU ::= styp1 | ... | stypn\n\ntyp += union(stypU)\n```\n\nWrite `typ1 # typ2` when there is no `typ` such that `|- typ1 + typ2 = typ`, with the corresponding extension `styp1 # styp2`.\n\nThe sum of `stypU` written `|- stypU1 + stypU2 = stypU` is defined below.\nThe conversion `|- u(styp) = stypU` is also defined below.\n\n[Union Extension](doc/UnionExtension.md) of `|- styp1 + styp2 = styp` and `|- \u003cstypA1,stypA2\u003e + \u003cstypB\u003e = styp1,styp2`.\n\n\n### Extension: Singleton Types\n\n```\ntyp ::=\n  number | 0 | 1 | 2 | ...\n  string | \"hello\" | ...\n  boolean | true | false\n  ...\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcristianoc%2FREInfer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcristianoc%2FREInfer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcristianoc%2FREInfer/lists"}