summaryrefslogtreecommitdiff
path: root/type/default.scm
blob: 529f4f8a8c0bf9d6929e0e0f6d864dcfe5c3e012 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
;;; This handles the default rule.

(define (maybe-default-ambiguous-tyvar type def module)
  (let ((classes (ntyvar-context type)))
   (and (not (null? classes)) ; this happens only during cleanup after an error
    (let ((non-standard? '#f)
	  (numeric? '#f))
      (dolist (class classes)
	(cond ((eq? (class-kind class) 'numeric)
	       (setf numeric? '#t))
	      ((not (eq? (class-kind class) 'standard))
	       (setf non-standard? '#t))))
      (cond ((or non-standard? (not numeric?))
	     (remember-context def
 	       (phase-error 'Non-defaultable-ambiguous-context
"An ambiguous context, ~A, cannot be defaulted.~%Ambiguity in call to ~A~%"
                  classes def))
	     '#f)
	    (else
	     (find-default-type type classes classes
			(tuple-2-2 (assq module *default-decls*)))))))))

(define (find-default-type tyvar classes all-classes defaults)
  (cond ((null? defaults)
	 (phase-error 'no-default-applies
	    "Ambiguous context: ~A~%No default applies.~%"
	    all-classes)
	 '#f)
	((null? classes)
	 (instantiate-tyvar tyvar (car defaults))
	 '#t)
	((type-in-class? (car defaults) (car classes))
	 (find-default-type tyvar (cdr classes) all-classes defaults))
	(else
	 (find-default-type tyvar all-classes all-classes (cdr defaults)))))
	 
(define (type-in-class? ntype class)
  (let* ((ntype (expand-ntype-synonym ntype))
	 (alg (ntycon-tycon ntype))
	 (inst (lookup-instance alg class)))
    (if (eq? inst '#f)
	'#f
	(let ((res '#t))
	  (do-contexts (c (instance-context inst)) (ty (ntycon-args ntype))
	    (when (not (type-in-class? ty c))
	      (setf res '#f)))
	  res))))