Search Results

Found 1 results for "06c8f8d886dfd5e3fd5aaa70246afeee" across all boards searching md5.

Anonymous /g/105652614#105683999
6/23/2025, 11:07:37 PM
Adds typechecking in your path
(use-modules (ice-9 match))

(define (lookup symbol table)
(cdr (assoc symbol table)))

(define-syntax check
(syntax-rules ()
[(_ program)
(test 'program '())]))

(define (test term table)
(match term
[('let (bind value) body)
(test body (cons (cons bind (test value table)) table))]
[('lambda (bind ': type) body)
(list type '-> (test body (cons (cons bind type) table)))]
[(f x)
(match (list (lookup f table) (test x table))
[((a '-> b) c)
(if (eq? a c)
b
(begin (format #t "type mismatch: ((~a -> ~a) ~a)~%" a b c) #f))])]
[('+ x y)
(match (list (test x table) (test y table))
[('Int 'Int) 'Int]
[('Real 'Real) 'Real]
[(a b) (format #t "type error: (+ ~a ~a)~%" a b) #f])]
[(? boolean?) 'Bool]
[(and (? number?) (? exact?)) 'Int]
[(and (? number?) (? inexact?)) 'Real]
[symbol (lookup symbol table)]))
(check 1) ;=> Int

(check (+ 1 #t)) ;=> type error: (+ Int Bool)

(check (+ 1.0 2.0)) ;=> Real

(check (lambda (x : Int) x)) ;=> (Int -> Int)
(check
(let (id (lambda (x : Int) x))
(id 2.0)))
;=> type mismatch: ((Int -> Int) Real)

(check
(let (x 1)
(let (y 2)
(let (double (lambda (x : Int) (+ x x)))
(+ x (double y))))))
;=> Int