change current-join to throw error on fail

- returns join type on success
This commit is contained in:
Stephen Chang 2016-04-20 15:42:52 -04:00
parent 9b9c1dda96
commit 1fc2b3f538
3 changed files with 20 additions and 16 deletions

View File

@ -51,7 +51,15 @@
( (or e- ...) : Bool)])
(begin-for-syntax
(define current-join (make-parameter (λ (x y) x))))
(define current-join
(make-parameter
(λ (x y)
(unless (typecheck? x y)
(type-error
#:src x
#:msg "branches have incompatible types: ~a and ~a" x y))
x))))
(define-typed-syntax if
[(~and ifstx (_ e_tst e1 e2))
#:with τ-expected (get-expected-type #'ifstx)
@ -62,10 +70,6 @@
#:with (e1- τ1) (infer+erase #'e1_ann)
#:with (e2- τ2) (infer+erase #'e2_ann)
#:with τ-out ((current-join) #'τ1 #'τ2)
#:fail-unless (and (typecheck? #'τ1 #'τ-out)
(typecheck? #'τ2 #'τ-out))
(format "branches have incompatible types: ~a and ~a"
(type->str #'τ1) (type->str #'τ2))
( (if e_tst- e1- e2-) : τ-out)])
(define-base-type Unit)

View File

@ -45,11 +45,4 @@
#'([l τl] ...))]
[_ #f])))
(current-sub? sub?)
(current-typecheck-relation (current-sub?))
(define (join t1 t2)
(cond
[((current-sub?) t1 t2) t2]
[((current-sub?) t2 t1) t1]
[else #'Top]))
(current-join join))
(current-typecheck-relation (current-sub?)))

View File

@ -1,7 +1,8 @@
#lang s-exp "typecheck.rkt"
(extends "stlc+lit.rkt" #:except #%datum +)
(reuse Bool String add1 #:from "ext-stlc.rkt")
(require (prefix-in ext: (only-in "ext-stlc.rkt" #%datum)))
(require (prefix-in ext: (only-in "ext-stlc.rkt" #%datum))
(only-in "ext-stlc.rkt" current-join))
(provide (for-syntax subs? current-sub?))
;; Simply-Typed Lambda Calculus, plus subtyping
@ -88,5 +89,11 @@
(define-sub-relation Nat <: Int)
(define-sub-relation Int <: Num)
(define-sub-relation t1 <: s1 ... s2 <: t2 => ( s1 ... s2) <: ( t1 ... t2)))
(define-sub-relation t1 <: s1 ... s2 <: t2 => ( s1 ... s2) <: ( t1 ... t2))
(define (join t1 t2)
(cond
[((current-sub?) t1 t2) t2]
[((current-sub?) t2 t1) t1]
[else #'Top]))
(current-join join))