From a6ff70d09e1e5f161e401a3665bfe44b2abfc44e Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Wed, 11 Jan 2023 14:26:54 +0530 Subject: fix: minor fixes and refactors --- examples/custom-effect.ts | 3 ++- examples/test-runner.ts | 2 +- src/eval.ts | 32 ++++++++++++++++++++------------ stdlib/effect.ts | 2 ++ stdlib/util.ts | 8 ++++---- tsconfig.json | 2 +- 6 files changed, 30 insertions(+), 19 deletions(-) diff --git a/examples/custom-effect.ts b/examples/custom-effect.ts index f87f242..74c96b0 100644 --- a/examples/custom-effect.ts +++ b/examples/custom-effect.ts @@ -1,4 +1,5 @@ -import { Bind, DefineEffect, Effect, Kind1 } from '../stdlib/effect' +import { Bind, Effect, Kind1 } from '../stdlib/effect' +import { DefineEffect } from '../stdlib/sys' import { Print } from '../stdlib/stdio' interface Mathemagic<_A, _B> extends Effect {} diff --git a/examples/test-runner.ts b/examples/test-runner.ts index b06b010..7093bf7 100644 --- a/examples/test-runner.ts +++ b/examples/test-runner.ts @@ -1,5 +1,5 @@ import { Do, Effect } from '../stdlib/effect' -import { Print, PutString, PutStringLn } from '../stdlib/stdio' +import { Print, PutStringLn } from '../stdlib/stdio' import { DefineEffect } from '../stdlib/sys' type Testi = [ diff --git a/src/eval.ts b/src/eval.ts index f6eabf1..54036be 100644 --- a/src/eval.ts +++ b/src/eval.ts @@ -24,9 +24,11 @@ export const evaluateType = async ( ): Promise => { const name = effTyp.getSymbol()?.getName() + const args = effTyp.getTypeArguments() + return match(name, { DefineEffect: async () => { - const [nameTyp, exprTyp] = effTyp.getTypeArguments() + const [nameTyp, exprTyp] = args const name = nameTyp?.getLiteralValue() as string const exprStr = exprTyp?.getLiteralValue() as string @@ -34,13 +36,19 @@ export const evaluateType = async ( return [] }, + Pure: async () => { + const [valTyp] = args + const [resultKey, _] = ctx.createResult(ctx.typeToString(valTyp)) + return [resultKey] + }, + Print: async () => { - console.log(...effTyp.getTypeArguments().map(ctx.typeToString)) + console.log(...args.map(ctx.typeToString)) return [] }, PutString: async () => { - const [strinTyp] = effTyp.getTypeArguments() + const [strinTyp] = args const typString = ctx.typeToString(strinTyp) const string = JSON.parse( !typString.startsWith('"') ? `"${typString}"` : typString @@ -50,7 +58,7 @@ export const evaluateType = async ( }, Debug: async () => { - const [labelTyp, valueTyp] = effTyp.getTypeArguments() + const [labelTyp, valueTyp] = args const label = JSON.parse(ctx.typeToString(labelTyp)) const value = ctx.typeToString(valueTyp) console.log(label, value) @@ -59,7 +67,7 @@ export const evaluateType = async ( }, ReadFile: async () => { - const [pathTyp] = effTyp.getTypeArguments() + const [pathTyp] = args const filePath = JSON.parse(ctx.typeToString(pathTyp)) const contents = await fs.readFile(filePath, 'utf-8') const [resultKey, _] = ctx.createResult(JSON.stringify(contents)) @@ -67,7 +75,7 @@ export const evaluateType = async ( }, WriteFile: async () => { - const [pathTyp, contentsTyp] = effTyp.getTypeArguments() + const [pathTyp, contentsTyp] = args const filePath = JSON.parse(ctx.typeToString(pathTyp)) const contents = JSON.parse(ctx.typeToString(contentsTyp)) await fs.writeFile(filePath, contents) @@ -75,7 +83,7 @@ export const evaluateType = async ( }, Bind: async () => { - const [inputTyp, chainToKind] = effTyp.getTypeArguments() + const [inputTyp, chainToKind] = args const [resultKey] = inputTyp ? await evaluateType(ctx, inputTyp) : [] const [_, compNode] = ctx.createResult( @@ -93,7 +101,7 @@ export const evaluateType = async ( }, GetEnv: async () => { - const [envTyp] = effTyp.getTypeArguments() + const [envTyp] = args const envName = JSON.parse(ctx.typeToString(envTyp)) const [resultKey, _] = ctx.createResult( `${JSON.stringify(process.env[envName] ?? '')}` @@ -115,7 +123,7 @@ export const evaluateType = async ( }, JsExpr: async () => { - const [exprTyp] = effTyp.getTypeArguments() + const [exprTyp] = args const exprStr = JSON.parse(ctx.typeToString(exprTyp)) const result = eval(`JSON.stringify(${exprStr})`) const [resultKey, _] = ctx.createResult(`${result}`) @@ -123,7 +131,7 @@ export const evaluateType = async ( }, Seq: async () => { - const [effectTyps] = effTyp.getTypeArguments() + const [effectTyps] = args const effectResults = await evalList( ctx, effectTyps?.getTupleElements() ?? [] @@ -135,7 +143,7 @@ export const evaluateType = async ( }, Do: async () => { - const [effectTyps] = effTyp.getTypeArguments() + const [effectTyps] = args const effectResults = await evalList( ctx, effectTyps?.getTupleElements() ?? [] @@ -149,7 +157,7 @@ export const evaluateType = async ( _: async () => { if (name && ctx.hasCustomEffect(name)) { - return ctx.runCustomEffect(name, effTyp.getTypeArguments()) + return ctx.runCustomEffect(name, args) } console.log(`${name} effect is not handled`) diff --git a/stdlib/effect.ts b/stdlib/effect.ts index 5be1758..9fe3216 100644 --- a/stdlib/effect.ts +++ b/stdlib/effect.ts @@ -13,3 +13,5 @@ export interface Seq<_Effs extends Effect[]> extends Effect {} export interface Do<_Effs extends Effect[]> extends Effect {} +export interface Pure extends Effect {} + diff --git a/stdlib/util.ts b/stdlib/util.ts index ded9983..cf19589 100644 --- a/stdlib/util.ts +++ b/stdlib/util.ts @@ -8,15 +8,15 @@ export interface Id extends Kind1 { return: this['input'] } -type ADTTT = { _type: string; value: any } +type ADTDescr = { _type: string; value: any } -interface ADTConstructor extends Kind1 { +interface ADTConstructor extends Kind1 { return: this['input'] extends infer Inp ? A & { value: Inp } : A } -type Pat = Record +type Pat = Record export type ADT> = { [k in keyof D]: { _type: k; value: D[k] } } extends infer Rec extends Pat - ? Rec[keyof Rec] & { [k in keyof Rec]: ADTConstructor } + ? { t: Rec[keyof Rec] } & { [k in keyof Rec]: ADTConstructor } : never diff --git a/tsconfig.json b/tsconfig.json index 4174b25..7c7eece 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -21,5 +21,5 @@ "noUncheckedIndexedAccess": true, "skipLibCheck": false }, - "include": ["./src/**/*"] + "include": ["./src/**/*", "./stdlib/**/*", "./examples/**/*"] } -- cgit v1.3.1