Skip to content

Commit

Permalink
Fix effect and multiple value explorers
Browse files Browse the repository at this point in the history
  • Loading branch information
davazp committed Apr 3, 2020
1 parent 69948ac commit a654cae
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 34 deletions.
4 changes: 4 additions & 0 deletions packages/delisp-core/src/type-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import { generateUniqueTVar } from "./type-generate";
import * as T from "./types";
import { flatten, flatMap, last, unique } from "./utils";

export function isEmtpyRow(t: T.Type): boolean {
return t.node.tag === "empty-row";
}

export function isTVar(t: T.Type): t is T.Var {
return t.node.tag === "type-variable";
}
Expand Down
94 changes: 60 additions & 34 deletions packages/liphe/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,57 @@ export const RecordExplorer: React.FC<{
);
};

function isUnconstraint(t: Delisp.Type, container: Delisp.Type) {
return Delisp.isTVar(t) && Delisp.countTypeOccurrences(t, container) <= 1;
}

export const EffectTypeExplorer: React.FC<{
type: Delisp.Type;
contextType: Delisp.Type;
}> = ({ type, contextType }) => {
const effects = Delisp.normalizeEffect(type);

return (
<ul>
{effects.fields.map((eff, effPosition) => {
return (
<li className={styles.effectItem} key={effPosition}>
{eff.label}
</li>
);
})}
{Delisp.isEmtpyRow(effects.extends) ||
isUnconstraint(effects.extends, contextType) ? null : (
<li>
... <TypeExplorer type={effects.extends} />
</li>
)}
</ul>
);
};

export const ValuesTypeExplorer: React.FC<{
type: Delisp.Type;
}> = ({ type }) => {
const row = Delisp.normalizeOutput(type);
return (
<ul>
{row.fields.map((value, valuePosition) => {
return (
<li className={styles.effectItem} key={valuePosition}>
<TypeExplorer type={value.labelType}></TypeExplorer>
</li>
);
})}
{Delisp.isEmtpyRow(row.extends) ? null : (
<li>
... <TypeExplorer type={row.extends} />
</li>
)}
</ul>
);
};

export const FunctionExplorer: React.FC<{ fn: Delisp.SFunction<Typed> }> = ({
fn,
}) => {
Expand All @@ -157,23 +208,11 @@ export const FunctionExplorer: React.FC<{ fn: Delisp.SFunction<Typed> }> = ({
const [, ...args] = fn.node.lambdaList.positionalArguments;
const [contextType, ...argsTypes] = type.args;

const isUnconstraintContext =
Delisp.isTVar(contextType) &&
Delisp.countTypeOccurrences(contextType, selfType) === 1;

const effects = Delisp.normalizeEffect(type.effect).fields.map(
(f) => f.label
);

const outputTypes = Delisp.normalizeOutput(type.output).fields.map(
(f) => f.labelType
);

return (
<div className={styles.function}>
<span className={styles.functionLabel}>λ</span>

{isUnconstraintContext ? null : (
{isUnconstraint(contextType, selfType) ? null : (
<div>
<strong>*context*</strong> - <TypeExplorer type={contextType} />
</div>
Expand All @@ -193,28 +232,14 @@ export const FunctionExplorer: React.FC<{ fn: Delisp.SFunction<Typed> }> = ({
</ul>
</div>

{effects.length === 0 ? null : (
<div>
<strong>Effects:</strong>
<ul className={styles.effects}>
{effects.map((eff, effPosition) => {
return <li key={effPosition}>{eff}</li>;
})}
</ul>
</div>
)}
<div>
<strong>Effect:</strong>
<EffectTypeExplorer type={type.effect} contextType={selfType} />
</div>

<div>
<strong>Output:</strong>
<ul className={styles.outputs}>
{outputTypes.map((out, outPosition) => {
return (
<li key={outPosition}>
<TypeExplorer type={out} />
</li>
);
})}
</ul>
<ValuesTypeExplorer type={type.output} />
</div>

{fn.node.body.map((expr, i) => {
Expand Down Expand Up @@ -245,8 +270,9 @@ export const IdentifierExplorer: React.FC<{

export default function Homepage() {
const [code, setCode] = useState(`
(lambda (x)
(+ *context* x)
(lambda (f x)
(print x)
(f)
)`);

const [module, setModule] = useState<Delisp.Module<Typed>>();
Expand Down

0 comments on commit a654cae

Please sign in to comment.