This repository has been archived by the owner on Jul 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a2c1813
commit 7fc98f1
Showing
2 changed files
with
90 additions
and
88 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,61 @@ | ||
// Copyright (c) 2023 Adam Jones | ||
// | ||
// SPDX-License-Identifier: MIT | ||
|
||
import { generalise, instantiate, newTypeVar, Substitution, unify } from "./helper"; | ||
import { Context, Expression, makeContext, MonoType } from "./models"; | ||
import {Substitution, generalise, instantiate, newTypeVar, unify} from './helper'; | ||
import {Context, Expression, MonoType, makeContext} from './models'; | ||
|
||
export const M = (typEnv: Context, expr: Expression, type: MonoType): Substitution => { | ||
if (expr.type === "var") { | ||
console.log(`Variable ${expr.x}: expected to have type ${JSON.stringify(type)}`) | ||
if (expr.type === 'var') { | ||
console.log(`Variable ${expr.x}: expected to have type ${JSON.stringify(type)}`); | ||
|
||
const value = typEnv[expr.x]; | ||
if (value === undefined) throw new Error(`Undefined variable: ${expr.x}`); | ||
return unify(type, instantiate(value)) | ||
return unify(type, instantiate(value)); | ||
} | ||
|
||
if (expr.type === "abs") { | ||
if (expr.type === 'abs') { | ||
const beta1 = newTypeVar(); | ||
const beta2 = newTypeVar(); | ||
const s1 = unify(type, { | ||
type: 'ty-app', | ||
C: '->', | ||
mus: [beta1, beta2] | ||
}) | ||
mus: [beta1, beta2], | ||
}); | ||
const s2 = M( | ||
makeContext({ | ||
...s1(typEnv), | ||
[expr.x]: s1(beta1) | ||
[expr.x]: s1(beta1), | ||
}), | ||
expr.e, | ||
s1(beta2), | ||
s1(beta2) | ||
); | ||
return s2(s1); | ||
} | ||
|
||
if (expr.type === "app") { | ||
const beta = newTypeVar() | ||
if (expr.type === 'app') { | ||
const beta = newTypeVar(); | ||
const s1 = M(typEnv, expr.e1, { | ||
type: 'ty-app', | ||
C: '->', | ||
mus: [beta, type] | ||
}) | ||
const s2 = M(s1(typEnv), expr.e2, s1(beta)) | ||
return s2(s1) | ||
mus: [beta, type], | ||
}); | ||
const s2 = M(s1(typEnv), expr.e2, s1(beta)); | ||
return s2(s1); | ||
} | ||
|
||
if (expr.type === "let") { | ||
const beta = newTypeVar() | ||
const s1 = M(typEnv, expr.e1, beta) | ||
const s2 = M(makeContext({ | ||
...s1(typEnv), | ||
[expr.x]: generalise(s1(typEnv), s1(beta)) | ||
}), expr.e2, s1(type)) | ||
if (expr.type === 'let') { | ||
const beta = newTypeVar(); | ||
const s1 = M(typEnv, expr.e1, beta); | ||
const s2 = M( | ||
makeContext({ | ||
...s1(typEnv), | ||
[expr.x]: generalise(s1(typEnv), s1(beta)), | ||
}), | ||
expr.e2, | ||
s1(type) | ||
); | ||
return s2(s1); | ||
} | ||
|
||
throw new Error('Unknown expression type') | ||
} | ||
throw new Error('Unknown expression type'); | ||
}; |