DrRack
Hi,
For what its worth, it was just pointed out to me that one of my test programs:
{rec {fac : (number -> number) {fun {n}
{if {< n 2} 1{* n {fac {- n 1}}}}}}{fac 6}}is not syntactically correct.It should be:{rec {fac : (number -> number) {fun {n : number} {if {< n 2} 1 {* n {fac {- n 1}}}}}}{fac 6}}That is, I missed the type declaration for the fun parameter.I’m surprised no one mentioned this to me.Here are some comments I wrote in response to email questions. They might or might not be of interest.For the fun type check, you have some inputlike: {fun {x : number} {< x 10}}which becomes the TBOB tree:(fun ‘x (numType) (lt (id ‘x) (num 10)))So when you’re processing this in the get-type code, the variant[fun (arg type body) …has arg set to ‘x, type set to (numType),and body set to (lt (id ‘x) (num 10)).Now, looking at the input code, you should be able to say that the correct type of this expression is(funType (numType) (boolType)).How does your program figure this out?Since we’re typing a “fun” expression, we know that the answer will be a (funType …The domain type will be the type of the argument [which is “type” above], so the answer will be (funType type …and now we need to figure out the type of the body. Working by hand, we figured out boolType was the answer because the body is a “lt” tree, which returns a boolean value.So, the codomain type is the type of the body, i.e.(funType type (get-type body …Now, what do we want for the type environment.We need to figure out the type of an expression that involves “x”, so at some point will we want to lookup x in the type environment.We know the type of x from the input, so we extend the type environment to include this binding for the arg:(funType type (get-type body (aTypeSub arg type tenv)))=======================================The grammar describes what a type specification might look like:
| boolean
| (
So, the
You can see some of these types used in the sample programs. Your current code looks for fun, but theres nothing like that in the input. The type parser knows it has a function type when it finds a list rather than an atom. In that case, it calls itself recursively to process the domain specification, makes sure the second element in the list is -> and calls itself recursively to process the codomain specification.
Here are some examples of legal types:
number
boolean
( number -> number )
( number -> boolean )
( boolean -> boolean )
( boolean -> number )
( ( number -> boolean ) -> number )
( ( number -> number ) -> ( number -> boolean) )
( number -> ( number -> boolean ) )
etc.
Reviews
There are no reviews yet.