Browse Source

fix semantic ambiguity with type aliases vs. data types with no parameters

pull/20/head
nisstyre56 11 years ago
parent
commit
cd23678c5a
  1. 7
      environments.js
  2. 7
      free_vars.js
  3. 11
      parse.js
  4. 19
      prelude.jl
  5. 21
      representation.js
  6. 8
      typecheck.js

7
environments.js

@ -3,13 +3,14 @@
* with a few built-in (a standard Prelude environment) * with a few built-in (a standard Prelude environment)
*/ */
// returns the new environment after mutating it
// values = [(identifier, JLambda expression)]
var errors = require("./errors.js"); var errors = require("./errors.js");
var rep = require("./representation.js"); var rep = require("./representation.js");
/*
* returns the new environment after mutating it
* values = [(identifier, JLambda expression)]
*/
function extend(env, values) { function extend(env, values) {
var new_env = {}; var new_env = {};
var env_keys = Object.keys(env); var env_keys = Object.keys(env);

7
free_vars.js

@ -1,4 +1,4 @@
/* Takes an AST and converts all of the functions into closures. /* Takes an AST and converts all of the functions into "closures"
* A closure is a triple of: * A closure is a triple of:
* the bound variables in a function or let * the bound variables in a function or let
* the free variables in a function or let * the free variables in a function or let
@ -19,10 +19,8 @@
*/ */
var rep = require("./representation.js"); var rep = require("./representation.js");
var env = require("./environments.js");
var errors = require("./errors.js"); var errors = require("./errors.js");
var parser = require("./parse.js"); var parser = require("./parse.js");
var pprint = require("./pprint.js");
var $ = require("./tools.js"); var $ = require("./tools.js");
var _ = require("underscore"); var _ = require("underscore");
@ -106,6 +104,9 @@ function annotate_fvs(stx) {
return new rep.Closure(bound_vars, free_variables, stx, []); return new rep.Closure(bound_vars, free_variables, stx, []);
} }
/*
* This traverse the tree and gathers up all of the free variables of various functions/let bindings
*/
function annotate_fvs_all(stx) { function annotate_fvs_all(stx) {
var closure; var closure;
switch (stx.exprType) { switch (stx.exprType) {

11
parse.js

@ -367,12 +367,17 @@ function parseDataType(tokens, linenum, charnum) {
typeName.charnum, typeName.charnum,
"Expected a type operator in data type definition"); "Expected a type operator in data type definition");
} }
if (fst(tokens)[0] !== "right_paren") {
parameters = parseMany(parse, parameters = parseMany(parse,
validName, validName,
validFormPar, validFormPar,
tokens, tokens,
charnum, charnum,
linenum); linenum);
}
else {
parameters = [];
}
if (!tokens || (fst(tokens)[0]) !== "right_paren") { if (!tokens || (fst(tokens)[0]) !== "right_paren") {
throw error.JSyntaxError(_.last(parameters).linenum, throw error.JSyntaxError(_.last(parameters).linenum,
_.last(parameters).charnum, _.last(parameters).charnum,
@ -402,7 +407,7 @@ function parseDefType(tokens, linenum, charnum) {
} }
if (fst(tokens)[0] === "left_paren") { if (fst(tokens)[0] === "left_paren") {
/* It's an actual data type definition /* It's an actual data type definition
* i.e. not just a newtype * i.e. not just an alias
*/ */
tokens.pop(); tokens.pop();
return parseDataType(tokens, linenum, charnum); return parseDataType(tokens, linenum, charnum);
@ -424,7 +429,7 @@ function parseDefType(tokens, linenum, charnum) {
if (lhs.exprType !== "TypeOperator") { if (lhs.exprType !== "TypeOperator") {
throw error.JSyntaxError(lhs.linenum, throw error.JSyntaxError(lhs.linenum,
lhs.charnum, lhs.charnum,
"left-hand side of type definition was not a type operator"); "left-hand side of type alias was not a type operator");
} }
rhs = parse(tokens, linenum, charnum); rhs = parse(tokens, linenum, charnum);
@ -432,7 +437,7 @@ function parseDefType(tokens, linenum, charnum) {
rhs.exprType !== "TypeOperator") { rhs.exprType !== "TypeOperator") {
throw error.JSyntaxError(rhs.linenum, throw error.JSyntaxError(rhs.linenum,
rhs.charnum, rhs.charnum,
"was expecting an application or type operator on the right-hand side of a type definition"); "was expecting an application or type operator on the right-hand side of a type alias");
} }
result = addSrcPos(new typ.DefType(lhs, rhs), tokens, rhs.linenum, rhs.charnum); result = addSrcPos(new typ.DefType(lhs, rhs), tokens, rhs.linenum, rhs.charnum);
return result; return result;

19
prelude.jl

@ -4,24 +4,29 @@
;; Type definitions ;; Type definitions
deftype String (A300 Char) deftype String (Vector Char)
deftype Int Intrinsic deftype (Int) Intrinsic
deftype Float Intrinsic deftype (Float) Intrinsic
deftype Char Intrinsic deftype (Char) Intrinsic
deftype Byte Intrinsic deftype (Byte) Intrinsic
deftype Void Intrinsic deftype (Void) Intrinsic
deftype IO Intrinsic deftype (IO a) Intrinsic
deftype (Vector a) Intrinsic
deftype (List a) deftype (List a)
(Empty | (Empty |
(Cons a (List a))) (Cons a (List a)))
deftype (Bottom)
Undefined
deftype (Maybe a) deftype (Maybe a)
(Nothing | (Nothing |
(Just a)) (Just a))

21
representation.js

@ -368,28 +368,11 @@ function makeGensym() {
var gensym = makeGensym(); var gensym = makeGensym();
//console.log(isTypeExpr(new Name("T")));
OPInfo = { OPInfo = {
/*"+" : [3, "Left"],
"-" : [3, "Left"],
"*" : [4, "Left"],
"/" : [4, "Left"],
"^" : [5, "Right"]
"++" : [3, "Left"],
"==" : [2, "Left"],
">" : [2, "Left"],
">=" : [2, "Left"],
"<" : [2, "Left"],
"<=" : [2, "Left"],
"&&" : [2, "Left"],
"||" : [2, "Left"],*/
"::" : [2, "Left"], "::" : [2, "Left"],
/*":" : [1, "Left"],
"$" : [1, "Left"],
"." : [1, "Left"],*/
"," : [1, "Left"], "," : [1, "Left"],
"->" : [1, "Right"]}; "->" : [1, "Right"]
};
module.exports = module.exports =
{ {

8
typecheck.js

@ -13,7 +13,7 @@
var rep = require("./representation.js"); var rep = require("./representation.js");
var env = require("./environments.js"); var env = require("./environments.js");
var TypeOp = rep.TypeOp; /*
var TypeVar = rep.TypeVar; * Map all bindings with explicit type annotations in the environment
*/
function gather_annotations(stx) {

Loading…
Cancel
Save