Simple refinement types#638
Conversation
For design, see PistonDevelopers#636 - Added “typechk/refinement.dyon” - Added “typechk/refinement_2.dyon”
- Added “typechk/refinement_3.dyon”
- Added “typechk/refinement_4.dyon”
- Removed `Type::is_concrete` - Added “typechk/refinement_5.dyon”
- Fixed bug `a.goes_with(b)` => `b.goes_with(a)` - Added “typechk/refinement_6.dyon” - Added “typechk/refinement_7.dyon”
- Swap order of `Type::ambiguous` - Added “typechk/refinement_8.dyon” - Added “typechk/refinement_9.dyon”
- Added “typechk/refinement_10.dyon” - Added “typechk/refinement_11.dyon”
- Added “typechk/refinement_12.dyon” - Added “typechk/refinement_13.dyon”
- Added “typechk/refinement_14.dyon” - Added “typechk/refinement_15.dyon”
- Added “typechk/refinement_16.dyon” - Added “typechk/refinement_17.dyon”
- Added “typechk/refinement_18.dyon” - Added “typechk/refinement_19.dyon”
- Added “typechk/refinement_20.dyon” - Added “typechk/refinement_21.dyon”
- Added “typechk/refinement_22.dyon” - Added “typechk/refinement_23.dyon”
- Added “syntax/secret_fail.dyon”
… something - Added “typechk/void_refinement.dyon”
- Added “typechk/args_refinement.dyon”
- Added “typechk/refine_type_fail.dyon”
- Added “typechk/refinement_24.dyon” - Added “typechk/refinement_25.dyon”
- Added ambiguity check for wrapper types - Fixed doc comment on `Type::ambiguous` - Added “typechk/refinement_26.dyon” - Added “typechk/refinement_27.dyon”
- Added “typechk/refine_type_fail_2.dyon”
- Added “typechk/closure_ad_hoc.dyon”
- Added `UnOpEpression::into_expression`
- Added `Kind::Not`
- Added “typechk/arr_fail_2.dyon”
|
I'm happy with this PR now. I also tested the n_body benchmark on Before: real 0m3.152s
|
This case is covered by the type checker.
- Added `_` as printable character of objects
|
The performance drop was due to a bug in After: real 0m3.025s It is 4.4% faster now. |
|
Merging. |
Dyon is a rusty dynamically typed scripting language.
What are simple refinement types?
A simple refinement type system is an "extra layer" of types in addition to the normal type signature of functions. This is used to catch more errors before runtime.
The extra layer contains special knowledge about the function or alternative semantics from how the function is used.
Dyon supports ad-hoc types which lets you write e.g.
Unknown boolinstead of justbool. Simple refinement types in Dyon works seamlessly ad-hoc types.For example, one can use this to type check many-valued boolean logic:
The ad-hoc type
Unknown boolis constructed by a coin-flip. If you invert a coin-flip, you don't know whether it istrueorfalse, so it is stillUnknown bool.Dyon detects an error in the code above:
In this example, I used simple refinement types to reason about nondeterministic behavior of many-valued boolean logic. Instead of writing code explicitly as many-valued boolean logic, one can use ad-hoc types to lift the many-value logic to type level.
Design
For design, see #636
This PR adds support for simple refinement types in Dyon. This allows e.g. binary operators to be type checked as external functions.
stdnamespace for standard external functions (see Make namespacestdfor standard external functions #639)check__in_string_imports(type checker knowledge, see Addcheck__in_string_imports#646)unwrap_orImprovements in performance
There is a 4.4% improvement in performance on the n-body benchmark for
n=100_000. This is a longer run that is tested manually and measures performance on typical game-logic workloads.