Browse Source

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

pull/20/head
nisstyre56 10 years ago
parent
commit
cd23678c5a
  1. 7
      environments.js
  2. 7
      free_vars.js
  3. 13
      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)
*/
// returns the new environment after mutating it
// values = [(identifier, JLambda expression)]
var errors = require("./errors.js");
var rep = require("./representation.js");
/*
* returns the new environment after mutating it
* values = [(identifier, JLambda expression)]
*/
function extend(env, values) {
var new_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:
* the bound variables in a function or let
* the free variables in a function or let
@ -19,10 +19,8 @@
*/
var rep = require("./representation.js");
var env = require("./environments.js");
var errors = require("./errors.js");
var parser = require("./parse.js");
var pprint = require("./pprint.js");
var $ = require("./tools.js");
var _ = require("underscore");
@ -106,6 +104,9 @@ function annotate_fvs(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) {
var closure;
switch (stx.exprType) {

13
parse.js

@ -367,12 +367,17 @@ function parseDataType(tokens, linenum, charnum) {
typeName.charnum,
"Expected a type operator in data type definition");
}
parameters = parseMany(parse,
if (fst(tokens)[0] !== "right_paren") {
parameters = parseMany(parse,
validName,
validFormPar,
tokens,
charnum,
linenum);
}
else {
parameters = [];
}
if (!tokens || (fst(tokens)[0]) !== "right_paren") {
throw error.JSyntaxError(_.last(parameters).linenum,
_.last(parameters).charnum,
@ -402,7 +407,7 @@ function parseDefType(tokens, linenum, charnum) {
}
if (fst(tokens)[0] === "left_paren") {
/* It's an actual data type definition
* i.e. not just a newtype
* i.e. not just an alias
*/
tokens.pop();
return parseDataType(tokens, linenum, charnum);
@ -424,7 +429,7 @@ function parseDefType(tokens, linenum, charnum) {
if (lhs.exprType !== "TypeOperator") {
throw error.JSyntaxError(lhs.linenum,
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);
@ -432,7 +437,7 @@ function parseDefType(tokens, linenum, charnum) {
rhs.exprType !== "TypeOperator") {
throw error.JSyntaxError(rhs.linenum,
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);
return result;

19
prelude.jl

@ -4,24 +4,29 @@
;; 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)
(Empty |
(Cons a (List a)))
deftype (Bottom)
Undefined
deftype (Maybe a)
(Nothing |
(Just a))

21
representation.js

@ -368,28 +368,11 @@ function makeGensym() {
var gensym = makeGensym();
//console.log(isTypeExpr(new Name("T")));
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"],
/*":" : [1, "Left"],
"$" : [1, "Left"],
"." : [1, "Left"],*/
"," : [1, "Left"],
"->" : [1, "Right"]};
"->" : [1, "Right"]
};
module.exports =
{

8
typecheck.js

@ -13,7 +13,7 @@
var rep = require("./representation.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