Skip to content

Commit

Permalink
Merge pull request #245 from rolyp/primitive-id
Browse files Browse the repository at this point in the history
Index of primitive results depend only on arguments, not calling context. Underscore patterns.
  • Loading branch information
rolyp authored Nov 2, 2019
2 parents a96b57a + f3012bb commit 5eb01ba
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 17 deletions.
8 changes: 4 additions & 4 deletions fluid/example/mergeSort.fld
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ letrec
fun split {
[] → ([], []);
[x, ...xs] →
match split xs as
match split xs as
(ys, zs) → ([x, ...zs], ys)
};

fun merge xs ys →
match (xs, ys) as {
([], ys') → ys;
([x, ...xs'], []) → xs;
([], _) → ys;
([x, ...xs'], []) → xs;
([x, ...xs'], [y, ...ys']) →
match x < y as {
True → [x, ...merge xs' ys];
Expand All @@ -21,7 +21,7 @@ letrec
match xs as {
[] → xs;
[x] → xs;
[x, y, ...xs'] →
[x, y, ..._] →
match split xs as
(ys, zs) → merge (mergesort ys) (mergesort zs)
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rolyp/fluid",
"version": "0.1.11",
"version": "0.1.12",
"description": "Fluid",
"dependencies": {
"@types/three": "0.93.28",
Expand Down
3 changes: 2 additions & 1 deletion src/Eval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ export function eval_ (ρ: Env, e: Expr): ExplValue {
[tv1, tv2]: [ExplValue, ExplValue] = [eval_(ρ, e.e1), eval_(ρ, e.e2)],
[v1, v2]: [Value, Value] = [tv1.v, tv2.v]
if ((v1 instanceof Num || v1 instanceof Str) && (v2 instanceof Num || v2 instanceof Str)) {
return explValue(Expl.binaryApp(tv1 as ExplValue<PrimValue>, e.opName, tv2 as ExplValue<PrimValue>)(kₜ), op.op(v1, v2)(kᵥ))
const k: MemoId = memoId(op.op, [v1, v2])
return explValue(Expl.binaryApp(tv1 as ExplValue<PrimValue>, e.opName, tv2 as ExplValue<PrimValue>)(kₜ), op.op(v1, v2)(k))
} else {
return error(`Applying "${e.opName}" to non-primitive value.`, v1, v2)
}
Expand Down
3 changes: 2 additions & 1 deletion src/Match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ export abstract class Elim<K extends Cont = Cont> extends DataValue<"Elim"> {

function apply_<K extends Cont> (σ: Elim<K>, tv: ExplValue, : MatchPrefix): [Env, Match<K>] {
if (VarElim.is(σ)) {
return [Env.singleton(σ.x, tv), match(, σ.κ)]
const ρ: Env = σ.x.val === "_" ? emptyEnv() : Env.singleton(σ.x, tv)
return [ρ, match(, σ.κ)]
} else
if (DataElim.is(σ)) {
const v: Value = tv.v,
Expand Down
8 changes: 7 additions & 1 deletion src/Parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const lexer = moo.compile({
ident: {
match: /[a-zA-Z_][0-9a-zA-Z_]*'*/, // greedy
type: moo.keywords({
keyword: ["as", "match", "fun", "in", "let", "letrec", "primitive", "typematch"],
keyword: ["_", "as", "match", "fun", "in", "let", "letrec", "primitive", "typematch"],
})
},
whitespace: {
Expand Down Expand Up @@ -339,6 +339,12 @@ const grammar: Grammar = {
{"name": "pattern", "symbols": ["pair_pattern"], "postprocess": id},
{"name": "pattern", "symbols": ["list_pattern"], "postprocess": id},
{"name": "pattern", "symbols": ["constr_pattern"], "postprocess": id},
{"name": "variable_pattern$macrocall$2", "symbols": [{"literal":"_"}]},
{"name": "variable_pattern$macrocall$1$macrocall$2", "symbols": ["variable_pattern$macrocall$2"]},
{"name": "variable_pattern$macrocall$1$macrocall$1", "symbols": ["variable_pattern$macrocall$1$macrocall$2"], "postprocess": id},
{"name": "variable_pattern$macrocall$1$macrocall$1", "symbols": ["variable_pattern$macrocall$1$macrocall$2", "_"], "postprocess": ([x, ]) => x},
{"name": "variable_pattern$macrocall$1", "symbols": ["variable_pattern$macrocall$1$macrocall$1"]},
{"name": "variable_pattern", "symbols": ["variable_pattern$macrocall$1"], "postprocess": () => (κ: Cont) => varElim(str("_")(ν()), κ)(ν())},
{"name": "variable_pattern", "symbols": ["var"], "postprocess": ([x]) => (κ: Cont) => varElim(x, κ)(ν())},
{"name": "pair_pattern$macrocall$2", "symbols": [{"literal":"("}]},
{"name": "pair_pattern$macrocall$1", "symbols": ["pair_pattern$macrocall$2"], "postprocess": id},
Expand Down
2 changes: 1 addition & 1 deletion src/Value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function taggedId<Tag extends string> (k: Id, prop: Tag): TaggedId<Tag> {
return make(TaggedId, k, prop) as TaggedId<Tag>
}

export function memoId (f: Function, : IArguments): MemoId {
export function memoId (f: Function, : Iterable<any>): MemoId {
const : FunctionId = functionId(f)
let k: MemoId =
for (let v of ) {
Expand Down
2 changes: 1 addition & 1 deletion src/app/IDE.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ import { openWithImports } from "../Module"
import "../app/GraphicsRenderer"
import { Editor } from "./Editor"

new Editor(openWithImports("flatten"))
new Editor(openWithImports("map"))
9 changes: 6 additions & 3 deletions src/grammar.ne
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const lexer = moo.compile({
ident: {
match: /[a-zA-Z_][0-9a-zA-Z_]*'*/, // greedy
type: moo.keywords({
keyword: ["as", "match", "fun", "in", "let", "letrec", "primitive", "typematch"],
keyword: ["_", "as", "match", "fun", "in", "let", "letrec", "primitive", "typematch"],
})
},
whitespace: {
Expand Down Expand Up @@ -62,6 +62,7 @@ rootExpr ->
lexeme[X] ->
$X {% id %} |
$X _ {% ([x, ]) => x %}

keyword[X] -> lexeme[$X]

_ -> (%whitespace | %singleLineComment):+
Expand Down Expand Up @@ -261,8 +262,10 @@ pattern ->
list_pattern {% id %} |
constr_pattern {% id %}

variable_pattern ->
var
variable_pattern ->
keyword["_"]
{% () =>: Cont) => varElim(str("_")(ν()), κ)(ν()) %} |
var
{% ([x]) =>: Cont) => varElim(x, κ)(ν()) %}

pair_pattern ->
Expand Down
8 changes: 4 additions & 4 deletions test/Edit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,16 @@ describe("edit", () => {
}

expect (here: ExplValueCursor) {
here = here.isChanged({ val: { before: 661, after: 39.125 } })
here = here.isNew()
.toTerminal()
here.toBinaryArg2("/").isNew()
here = here.toBinaryArg1("/").isNew()
.toTerminal()
here.toBinaryArg1("+").isUnchanged()
here = here.toBinaryArg2("+").isChanged({ val: { before: 625, after: 42.25 } })
here = here.toBinaryArg2("+").isNew()
.toTerminal()
here.toBinaryArg1("*").isChanged({ val: { before: 25, after: 6.5 } })
here.toBinaryArg2("*").isChanged({ val: { before: 25, after: 6.5 } })
here.toBinaryArg1("*").isNew()
here.toBinaryArg2("*").isNew()
}
})(e)
})
Expand Down
6 changes: 6 additions & 0 deletions test/util/Core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,19 @@ export class BwdSlice {

export class Edit {
constructor (e: Expr, ρ: Env = emptyEnv()) {
if (flags.get(Flags.Visualise)) {
new Editor(e, ρ).render()
}
if (flags.get(Flags.Edit)) {
Eval.eval_(ρ, e)
newRevision()
this.setup(new ExprCursor(e))
const tv: ExplValue = Eval.eval_(ρ, e)
this.expect(ExplValueCursor.descendant(null, tv))
}
if (flags.get(Flags.Visualise)) {
new Editor(e, ρ).render()
}
}

setup (here: ExprCursor): void {
Expand Down

0 comments on commit 5eb01ba

Please sign in to comment.