summaryrefslogtreecommitdiff
path: root/derived/derived-instances.scm
blob: 2c650844555d3b2a134debe968a6a62abd14e4c9 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
;;; Basic DI structure:
;;;  a. Create the set of instances
;;;  b. Expand the context of each potential instance.
;;;  c. Once b. reaches a fixpoint, fill in the ast for the generated instances

(define *di-context-changed* '#f)

(define (add-derived-instances modules)
  (let ((insts '()))
    (walk-modules modules
     (lambda () (setf insts (append (find-derivable-instances) insts))))
    (compute-di-fixpoint insts)
    (dolist (inst insts)
      (when (instance-ok? inst)
        (create-instance-fns inst)
	(push inst (module-instance-defs
		    (table-entry *modules*
				 (def-module (instance-algdata inst)))))))))

(define (compute-di-fixpoint insts)
  (setf *di-context-changed* '#f)
  (dolist (inst insts)
    (propagate-di-context inst))
  (when *di-context-changed* (compute-di-fixpoint insts)))

;;; Create instance decls for all derived instances in a module.  Filter
;;; out underivable instances (Ix & Enum only)

(define (find-derivable-instances)
  (let ((algs (module-alg-defs *module*))
	(insts '()))
    (dolist (alg algs)
      (dolist (class (algdata-deriving alg))
	 (cond ((memq class (list (core-symbol "Eq")
				  (core-symbol "Ord")
				  (core-symbol "Text")
				  (core-symbol "Binary")))
		(setf insts (add-derivable-instance insts alg class '#f)))
	       ((eq? class *printer-class*)
		(setf insts (add-derivable-instance
			     insts alg (core-symbol "Text") '#t)))
	       ((eq? class (core-symbol "Ix"))
		(if (or (algdata-enum? alg)
			(algdata-tuple? alg))
		    (setf insts (add-derivable-instance insts alg class '#f))
		    (signal-cant-derive-ix alg)))
	       ((eq? class (core-symbol "Enum"))
		(if (algdata-enum? alg)
		    (setf insts (add-derivable-instance insts alg class '#f))
		    (signal-cant-derive-enum alg)))
	       (else
		(signal-not-derivable class)))))
    insts))


(define (signal-cant-derive-ix alg)
  (phase-error 'cant-derive-IX
    "An Ix instance for ~A cannot be derived.  It is not an enumeration~%~
     or single-constructor datatype."
    alg))

(define (signal-cant-derive-enum alg)
  (phase-error 'cant-derive-Enum
    "An Enum instance for ~A cannot be derived.  It is not an enumeration."
    alg))

(define (signal-not-derivable class)
  (recoverable-error 'not-derivable
    "Class ~A is not one of the classes that permits derived instances."
    class))


;; This adds a provisional instance template.  Of course, there may already
;;; be an instance (error!)

(define (add-derivable-instance insts alg cls sp)
  (let ((existing-inst (lookup-instance alg cls)))
    (cond ((eq? existing-inst '#f)
	   (let ((inst (new-instance cls alg (algdata-tyvars alg))))
	     (setf (instance-context inst) (algdata-context alg))
	     (setf (instance-decls inst) '())
	     (setf (instance-ok? inst) '#t)
	     (setf (instance-suppress-readers? inst) sp)
	     (cons inst insts)))
	  (else
	   (signal-instance-exists alg cls)
	   insts))))

(define (signal-instance-exists alg cls)
  (recoverable-error 'instance-exists
    "An instance for type ~A in class ~A already exists;~%~
     the deriving clause is being ignored."
    alg cls))

;;; This updates all instance contexts for an algdata.  Each derivable
;;; instance generates a recursive context for every field.  If a
;;; component cannot satisfy the desired context, the ok? field is set to
;;; #f to mark the instance as bogus.

(define (propagate-di-context inst)
  (when (instance-ok? inst)
    (propagate-constructor-contexts inst
			   (algdata-constrs (instance-algdata inst)))))

;;; These two functions propagate the context to ever field of every
;;; constructor

(define (propagate-constructor-contexts inst constrs)
  (or (null? constrs)
      (and (propagate-contexts inst (instance-class inst)
			       (con-types (car constrs)))
	   (propagate-constructor-contexts inst (cdr constrs)))))

(define (propagate-contexts inst class types)
  (or (null? types)
      (and (propagate-type-context inst class (car types))
	   (propagate-contexts inst class (cdr types)))))

;;; This propagates a context out to a given type.  The type can only contain
;;; the tyvars which are args to the algdata.

(define (propagate-type-context inst class type)
  (cond ((tyvar? type)
	 (cond ((single-ast-context-implies?
		   (instance-context inst) class (tyvar-name type))
		'#t)
	       (else
		(setf *di-context-changed* '#t)
		(setf (instance-context inst)
		      (augment-context (instance-context inst) class
				       (tyvar-name type)))
		'#t)))
	((synonym? (tycon-def type))
	 (propagate-type-context inst class (expand-synonym type)))
	(else
	 (let* ((algdata (tycon-def type))  ; must be a algdata
	        (args (tycon-args type))
		(new-inst (lookup-instance algdata class)))
	   (cond ((or (eq? new-inst '#f)
		      (not (instance-ok? new-inst)))
		  (signal-cannot-derive-instance
		    (instance-class inst) (instance-algdata inst))
		  (setf (instance-ok? inst) '#f)
		  (setf *di-context-changed* '#t)
		  '#f)
		 (else
		  (propagate-instance-contexts inst 
				      (instance-context new-inst)
				      (instance-tyvars new-inst)
				      args)))))))


(define (single-ast-context-implies? ast-context class tyvar)
  (cond ((null? ast-context)
	 '#f)
	((eq? tyvar (context-tyvar (car ast-context)))
	 (let ((class1 (class-ref-class (context-class (car ast-context)))))
	   (or (eq? class1 class)
	       (memq class (class-super* class1))
	       (single-ast-context-implies? (cdr ast-context) class tyvar))))
	(else
	 (single-ast-context-implies? (cdr ast-context) class tyvar))))

;;; *** This message makes no sense to me.  What is the problem that
;;; *** makes it impossible to derive the instance?

(define (signal-cannot-derive-instance class alg)
  (phase-error 'cannot-derive-instance
    "Instance ~A(~A) cannot be derived."
    class alg))


;;; This propagates contexts into structure components.  The context
;;; changes due to the context associated with the various instance
;;; decls encountered.

;;; Here's the plan for expanding Cls(Alg t1 t2 .. tn) using
;;; instance (Cls1(vx),Cls2(vy),...) => Cls(Alg(v1 v2 .. vn))
;;;   for each Clsx in the instance context, propagate Clsx to the
;;;   ti corresponding to vx, where vx must be in the set vi.

(define (propagate-instance-contexts inst contexts tyvars args)
  (or (null? contexts)
      (and (propagate-type-context inst
	      (class-ref-class (context-class (car contexts)))
	      (find-corresponding-tyvar
	       (context-tyvar (car contexts)) tyvars args))
	   (propagate-instance-contexts inst (cdr contexts) tyvars args))))

;;; Given the t(i) and the v(i), return the t corresponding to a v.

(define (find-corresponding-tyvar tyvar tyvars args)
  (if (eq? tyvar (car tyvars))
      (car args)
      (find-corresponding-tyvar tyvar (cdr tyvars) (cdr args))))

;;; 1 level type synonym expansion

(define (expand-synonym type)
  (let* ((synonym (tycon-def type))
	 (args (synonym-args synonym))
	 (body (synonym-body synonym)))
  (let ((alist (map (lambda (tyvar arg) (tuple tyvar arg))
		    args (tycon-args type))))
    (copy-synonym-body body alist))))

(define (copy-synonym-body type alist)
  (if (tyvar? type)
      (tuple-2-2 (assq (tyvar-name type) alist))
      (make tycon (def (tycon-def type))
	          (name (tycon-name type))
		  (args (map (lambda (ty)
			       (copy-synonym-body ty alist))
			     (tycon-args type))))))

;;; This fills in the body decls for an instance function.

(define (create-instance-fns inst)
  (let ((class (instance-class inst))
	(alg (instance-algdata inst)))
    (cond ((eq? class (core-symbol "Eq"))
	   (add-instance inst (eq-fns alg)))
	  ((eq? class (core-symbol "Ord"))
	   (add-instance inst (ord-fns alg)))
	  ((eq? class (core-symbol "Ix"))
	   (add-instance inst (ix-fns alg)))
	  ((eq? class (core-symbol "Enum"))
	   (add-instance inst (enum-fns alg)))
	  ((eq? class (core-symbol "Text"))
	   (add-instance inst (text-fns alg (instance-suppress-readers? inst))))
	  ((eq? class (core-symbol "Binary"))
	   (add-instance inst (binary-fns alg))))))

(define (add-instance inst decls)
  (setf (instance-decls inst) decls))

;;; Add class(var) to a context, removing any contexts made redundant by
;;; the new addition.  Example: adding Ord a to (Eq a, Eq b) would yield
;;; (Ord a,Eq b).

(define (augment-context contexts cl var)
  (cons (**context (**class/def cl) var)
	(remove-implied-contexts cl var contexts)))

(define (remove-implied-contexts class1 tyvar1 contexts)
  (if (null? contexts)
      '#f
      (with-slots context (class tyvar) (car contexts)
	(let ((rest (remove-implied-contexts class1 tyvar1 (cdr contexts)))
	      (class2 (class-ref-class class)))
	  (if (and (eq? tyvar1 tyvar)
		   (memq class2 (class-super* class1)))
	      rest
	      (cons (car contexts) rest))))))