Skip to content

Commit 76821cb

Browse files
committed
Adjust the expansion of def to permit recursive top-level definitions
The order of definitions is much more relevant at the top level, since it evaluates forms one by one, so expand to the definition of a typed identifier before the definition of the value itself. Reported by @iitalics in #74.
1 parent bdcccf7 commit 76821cb

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

hackett-lib/hackett/private/base.rkt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -392,10 +392,10 @@
392392
#:with id- (generate-temporary #'id)
393393
#:with t_reduced (if (attribute exact?) #'t.expansion (type-reduce-context #'t.expansion))
394394
#`(begin-
395-
(define- id- (:/use #,((attribute t.scoped-binding-introducer) #'e) t_reduced #:exact))
396395
#,(indirect-infix-definition
397396
#'(define-syntax- id (make-typed-var-transformer #'id- (quote-syntax t_reduced)))
398-
(attribute fixity.fixity)))]
397+
(attribute fixity.fixity))
398+
(define- id- (:/use #,((attribute t.scoped-binding-introducer) #'e) t_reduced #:exact)))]
399399
[(_ id:id
400400
{~optional fixity:fixity-annotation}
401401
e:expr)
@@ -404,11 +404,10 @@
404404
#:do [(match-define-values [(list id-) e-] (τ⇐/λ! #'e #'t_e (list (cons #'id #'t_e))))]
405405
#:with t_gen (type-reduce-context (generalize (apply-current-subst #'t_e)))
406406
#`(begin-
407-
(define- #,id- #,e-)
408407
#,(indirect-infix-definition
409-
#`(define-syntax- id
410-
(make-typed-var-transformer (quote-syntax #,id-) (quote-syntax t_gen)))
411-
(attribute fixity.fixity)))])
408+
#`(define-syntax- id (make-typed-var-transformer (quote-syntax #,id-) (quote-syntax t_gen)))
409+
(attribute fixity.fixity))
410+
(define- #,id- #,e-))])
412411

413412
(begin-for-syntax
414413
(struct todo-item (full summary) #:prefab))

hackett-test/info.rkt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
(define deps
66
'("base"
77
"hackett-lib"
8+
"rackunit-lib"
9+
"sandbox-lib"
810
"testing-util-lib"))
911
(define build-deps
1012
'())
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#lang racket/base
2+
3+
; Ensure recursive definitions work at the top level.
4+
5+
(require racket/sandbox
6+
rackunit
7+
syntax/strip-context)
8+
9+
(define hackett-evaluator (make-evaluator 'hackett))
10+
(define (hackett-eval stx) (hackett-evaluator (strip-context stx)))
11+
12+
(hackett-eval #'(require hackett))
13+
(hackett-eval #'(defn fac : {Integer -> Integer}
14+
[[0] 1]
15+
[[x] {x * (fac {x - 1})}]))
16+
17+
(check-equal? (hackett-eval #'(fac 6)) (hackett-eval #'720))

0 commit comments

Comments
 (0)