@ -1,3 +1,6 @@
var errors = require ( "./errors.js" ) ;
var _ = require ( "underscore" ) ;
var Expression = {
var Expression = {
display :
display :
function ( ) {
function ( ) {
@ -6,18 +9,24 @@ var Expression = {
type :
type :
function ( ) {
function ( ) {
return this . exprType ;
return this . exprType ;
} ,
}
} ;
var TypeExpression = {
unify :
unify :
function ( t ) {
function ( t ) {
if ( this . exprType === t . exprType ) {
if ( this . expr === t . expr ) {
return t . exprType ;
return t . expr ;
}
}
else {
else {
console . log ( "Could not unify " + this . exprType + " with " + t . exprType ) ;
console . log ( "Could not unify " + this . expr + " with " + t . expr ) ;
}
}
}
} ,
isTypeExpr : true
} ;
} ;
var isTypeExpr = _ . property ( "isTypeExpr" ) ;
function Closure ( bound_vars , free_vars , body , env ) {
function Closure ( bound_vars , free_vars , body , env ) {
this . bound_vars = bound_vars ;
this . bound_vars = bound_vars ;
this . free_vars = free_vars ;
this . free_vars = free_vars ;
@ -163,17 +172,37 @@ function If(condition, thenexp, elseexp) {
}
}
function TypeVar ( name ) {
function TypeVar ( name ) {
this . exprtype = "TypeVar" ;
this . name = name ;
this . name = name ;
return this ;
return this ;
}
}
TypeVar . prototype = TypeExpression ;
function TypeOp ( name , params , body ) {
function TypeOp ( name , params , body ) {
if ( ! _ . every ( params , _ . compose (
_ . partial ( _ . isEqual , "TypeVar" ) ,
_ . property ( "exprtype" ) ) ) ) {
throw errors . JInternalError (
"Parameters to a type operator must be type variables"
) ;
}
this . name = name ;
this . name = name ;
this . params = params ;
this . params = params ;
this . body = body ;
this . body = body ;
return this ;
return this ;
}
}
TypeOp . prototype = TypeExpression ;
function TypeBinding ( expression , type ) {
this . expr = expression ;
this . type = type ;
return this ;
}
TypeBinding . prototype = TypeExpression ;
//Applies the function ``name'' to the list of parameters
//Applies the function ``name'' to the list of parameters
function makeApp ( name , parameters ) {
function makeApp ( name , parameters ) {
if ( parameters ) {
if ( parameters ) {
@ -184,7 +213,6 @@ function makeApp(name, parameters) {
else {
else {
return new App ( name ) ;
return new App ( name ) ;
}
}
}
}
function makeGensym ( ) {
function makeGensym ( ) {
@ -221,7 +249,8 @@ OPInfo = {"+" : [3, "Left"],
"," : [ 1 , "Left" ] } ;
"," : [ 1 , "Left" ] } ;
module . exports =
module . exports =
{ IntT : IntT ,
{
IntT : IntT ,
FloatT : FloatT ,
FloatT : FloatT ,
StrT : StrT ,
StrT : StrT ,
BoolT : BoolT ,
BoolT : BoolT ,
@ -241,5 +270,6 @@ module.exports =
gensym : gensym ,
gensym : gensym ,
TypeVar : TypeVar ,
TypeVar : TypeVar ,
TypeOp : TypeOp ,
TypeOp : TypeOp ,
TypeBinding : TypeBinding ,
Closure : Closure
Closure : Closure
} ;
} ;