1

I am working on a practice sheet for a final tomorow and I am a bit confused trying to figure out what the question is asking and how to resolve it. I wanted to check here and see if the good people of stack overflow can help me resolve it.

The question is set up like this:

Show a derivation tree (which is a trace of typecheck) demonstrating that the following expression has a type :

(appC  (lamC ‘x   ???  (plusC (idC ‘x) (numC 1)))
  (numC 2) )

It is based on this definition of type check:

(define (typecheck [a : ExprC] [tenv : TypeEnv])
  (type-case ExprC a
  [numC (n) (numT)]
[plusC (l r) (typecheck-nums l r tenv)]
[multC (l r) (typecheck-nums l r tenv)]
[idC (n) (type-lookup n tenv)]
[lamC (n arg-type body)
      (arrowT arg-type
              (typecheck body 
                         (extend-env (tbind n arg-type)
                                     tenv)))]
[appC (fun arg)
      (type-case Type (typecheck fun tenv)
        [arrowT (arg-type result-type)
                (if (equal? arg-type
                            (typecheck arg tenv))
                    result-type
                    (type-error arg
                                (to-string arg-type)))]
        [else (type-error fun "function")])]))

(define (typecheck-nums l r tenv)
  (type-case Type (typecheck l tenv)
[numT ()
      (type-case Type (typecheck r tenv)
        [numT () (numT)]
        [else (type-error r "num")])]
[else (type-error l "num")]))

(define (type-error a msg)
(error 'typecheck (string-append
                 "no type: "
                 (string-append
                  (to-string a)
                  (string-append " not "
                                 msg)))))

When I run this in racket :

(typecheck (appC 
 (lamC 'x (numT) (plusC (idC 'x) (numC 1)))
 (numC 2))
mt-env) 

I do get:

- Type
(numT)
- Type
(numT) 

At the bottom it states :

"You check that the type for 'x must be : numT"

So it is coming up with numT but I am confused about the tracing part, I know this question is long but this is the one out of all of the practice questions that is confusing me the most. Any advice/help is appreciated

Christophorus
  • 154
  • 12

1 Answers1

0

The question: "Show a derivation tree demonstrating that the following expression has a type" means that you need to make a proof that the expression has the given type.

Consider this example (unrelated to your typecheck example):

The result of evaluating the expression (+ 1 2) has type int. Why:

The subexpression 1 has type  int
The subexpression 2 has type  int
The subexpression + has type  int int -> int

Due to the type above the rule for application states that applying a function of type int int -> int to two arguments of type int, has a result of type int.

A type checker examines an expression recursively. It determines the types of the subexpressions first, and then use their types to infer the types of compound expressions.

In order to get a proof that a given expression has a certain type, one can use the trace of running the type checker. To get the trace, modify typechecker to print the types as they are inferred.

Untested modification:

(define (typecheck [a : ExprC] [tenv : TypeEnv])
  (define (tell result-type)
    (display "The type of ")
    (display a)
    (display " is: ")
    (display result-type)
    (newline))

  (tell
   (type-case ExprC a
     [numC (n)               (numT)]
     [plusC (l r)            (typecheck-nums l r tenv)]
     [multC (l r)            (typecheck-nums l r tenv)]
     [idC (n)                (type-lookup n tenv)]
     [lamC (n arg-type body) (arrowT arg-type
                                     (typecheck body 
                                                (extend-env (tbind n arg-type)
                                                            tenv)))]
     [appC (fun arg)         (type-case Type (typecheck fun tenv)
                               [arrowT (arg-type result-type)
                                       (if (equal? arg-type
                                                   (typecheck arg tenv))
                                           result-type
                                           (type-error arg
                                                       (to-string arg-type)))]
                               [else (type-error fun "function")])])))
soegaard
  • 30,661
  • 4
  • 57
  • 106