Last active
August 21, 2020 20:50
-
-
Save ENvironmentSet/4012d3d2969d3b9ecaf7717cc32a56d5 to your computer and use it in GitHub Desktop.
Generating eliminator for given constraint in typescript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** If you want to know how to encoding HKTs in typescript, check this: | |
https://gist.github.com/ENvironmentSet/1662a140f99381bc85fd6be51ecdcbb5 | |
Sorry for messy names, this was only PoC. **/ | |
export interface HKT { | |
param: unknown; | |
result: unknown; | |
} | |
export type Apply<f extends HKT, x> | |
= (f & { param: x })['result']; | |
export type ElimWithConstraint<constraint extends HKT> = <R>(f: <A>(x: Apply<constraint, A>) => R) => R; | |
export function makeElimWithConstraint<constraint extends HKT>(): <A>(x: Apply<constraint, A>) => ElimWithConstraint<constraint> { | |
return x => f => f(x); | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { HKT, Apply, makeElimForConstraint } from './constraints'; | |
export interface ExcludeBottomConstraint extends HKT { | |
result: this['param'] extends undefined | null ? | |
never | |
: this['param']; | |
} | |
export const elimNonBottom = makeElimWithConstraint<ExcludeBottomConstraint>(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { HKT, Apply, makeElimForConstraint } from './constraints'; | |
interface IdentityConstraint extends HKT { | |
result: this['param']; | |
} | |
export const elimAny = makeElimWithConstraint<IdentityConstraint>(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { HKT, Apply, makeElimForConstraint } from './constraints'; | |
export interface TruthyConstraint extends HKT { | |
result: this['param'] extends undefined | null | 0 | '' | false | typeof NaN ? | |
never | |
: this['param']; | |
} | |
export const elimTruthy = makeElimWithConstraint<TruthyConstraint>(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment