random-types.lsp 13 KB
Newer Older
1 2 3 4 5 6 7
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Mon Oct  6 05:04:45 2003
;;;; Contains: Generating random types and testing relationships on them

(in-package :cl-test)

pfdietz's avatar
pfdietz committed
8
(compile-and-load "types-aux.lsp")
9 10 11
(compile-and-load "random-aux.lsp")
(compile-and-load "random-int-form.lsp")

12 13
(defparameter *random-types* nil)

14 15 16 17 18 19
(defun make-random-type (size)
  (if (<= size 1)
      (rcase
       (1 nil)
       (1 t)
       (1 `(eql ,(let ((r (ash 1 (random 45))))
sds's avatar
sds committed
20
                   (random-from-interval r (- r)))))
21
       (1 (random-from-seq #(integer unsigned-byte ratio rational real float
sds's avatar
sds committed
22 23
                             short-float single-float double-float
                             long-float complex symbol cons function)))
24
       (1
sds's avatar
sds committed
25 26 27 28 29 30 31 32
        (let* ((len (random *maximum-random-int-bits*))
               (r1 (ash 1 len))
               (r2 (+ r1 r1))
               (x (- (random r2) r1))
               (y (- (random r2) r1))
               (lo (min x y))
               (hi (max x y)))
          `(integer ,lo ,hi)))
33
       (1 (make-random-real-type))
pfdietz's avatar
pfdietz committed
34
       ;; (1 (make-random-complex-type))
35 36
       )
    (rcase
37
     (2 (let* ((op (random-from-seq #(cons cons and or)))
sds's avatar
sds committed
38 39 40 41
               (nargs (if (eq op 'cons) 2
                        (1+ (random (min size 4)))))
               (sizes (random-partition (1- size) nargs)))
          `(,op ,@(mapcar #'make-random-type sizes))))
42
     (1 `(not ,(make-random-type (1- size))))
43
     ; (1 (make-random-function-type size))
44 45
     )))

46 47 48
(defun make-random-real-type ()
  (rcase
   (1 (random-from-seq '(integer unsigned-byte short-float single-float
sds's avatar
sds committed
49
                                 double-float long-float rational real)))
50
   (1 (destructuring-bind (lo hi)
sds's avatar
sds committed
51 52 53 54 55 56
          (make-random-integer-range)
        (rcase
         (4 `(integer ,lo ,hi))
         (1 `(integer ,lo))
         (1 `(integer ,lo *))
         (2 `(integer * ,hi)))))
57
   (1 (let ((r1 (random-real))
sds's avatar
sds committed
58 59
            (r2 (random-real)))
        `(real ,(min r1 r2) ,(max r2 r2))))
60 61 62 63 64 65
   ;;; Add more cases here
   ))

(defun make-random-complex-type ()
  `(complex ,(make-random-real-type)))

pfdietz's avatar
pfdietz committed
66 67
(defun make-random-function-type (size)
  (let* ((sizes (random-partition (1- size) 2))
sds's avatar
sds committed
68
         (types (mapcar #'make-random-type sizes)))
pfdietz's avatar
pfdietz committed
69 70
    `(function (,(car types)) ,(cadr types))))

pfdietz's avatar
pfdietz committed
71 72 73
(defun size-of-type (type)
  (if (consp type)
      (case (car type)
sds's avatar
sds committed
74 75 76 77 78 79 80
        (complex (1+ (size-of-type (cadr type))))
        ((array simple-array) (1+ (size-of-type (cadr type))))
        (vector (1+ (size-of-type (cadr type))))
        (complex (1+ (size-of-type (cadr type))))
        ((cons or and not) (reduce #'+ (cdr type) :initial-value 1
                                   :key #'size-of-type))
        (t 1))
pfdietz's avatar
pfdietz committed
81 82 83 84
      1))

(defun mutate-type (type)
  (let* ((size (size-of-type type))
sds's avatar
sds committed
85
         (r (random size)))
pfdietz's avatar
pfdietz committed
86
    (flet ((%f ()
sds's avatar
sds committed
87 88 89 90 91 92 93 94 95 96
             (rcase
              (6 (make-random-type (random (1+ size))))
              (2 `(not ,type))
              (1 `(and ,(make-random-type 1) ,type))
              (1 `(and ,type ,(make-random-type 1)))
              (1 `(or ,(make-random-type 1) ,type))
              (1 `(or ,type ,(make-random-type 1)))))
           (%random-int ()
             (let ((bits (1+ (min (random 20) (random 20)))))
               (- (ash 1 bits) (random (ash 1 (1+ bits)))))))
pfdietz's avatar
pfdietz committed
97
      (if (or (and (= r 0) (coin)) (not (consp type)))
sds's avatar
sds committed
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
          (%f)
          (case (car type)
            ((and or not cons complex)
             (let ((sizes (mapcar #'size-of-type (cdr type))))
               (loop with sum = 0
                  for e on sizes
                  for ctype in (cdr type)
                  for i from 0
                  do (setf sum (incf (car e) sum))
                  when (>= sum r)
                  return (rcase
                          (1 ctype) ;; replace with component type
                          (1 (cons (car type)
                                   (append (subseq (cdr type) 0 i)
                                           (list (mutate-type ctype))
                                           (subseq (cdr type) (1+ i)))))))))
            ((array simple-array vector)
             (let ((ctype (if (cdr type) (cadr type) t)))
               (rcase
                (1 (if (eql ctype *) t ctype))
                (1 (cons (car type)
                         (cons (mutate-type ctype)
                               (cddr type)))))))
            ((unsigned-byte)
             (if (integerp (cadr type))
                 (rcase
                  (1 'unsigned-byte)
                  (1 `(unsigned-byte (+ (cadr type) (- 10 (random 20))))))
                 (%f)))
pfdietz's avatar
pfdietz committed
127

sds's avatar
sds committed
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
            ((integer)
             (let ((lo-delta (%random-int))
                   (hi-delta (%random-int))
                   (old-lo (or (cadr type) '*))
                   (old-hi (or (caddr type) '*)))
               (flet ((%inc (old delta)
                        (if (or (coin) (not (integerp old)))
                            delta
                            (+ old delta))))
                 (rcase
                  (1 `(integer ,old-lo *))
                  (1 `(integer * ,old-hi))
                  (1 (let ((new-lo (%inc old-lo lo-delta)))
                       (if (or (null (cdr type))
                               (null (cddr type))
                               (not (integerp old-hi)))
                           `(integer ,new-lo ,@(cddr type))
                           ;; caddr is integer
                           (if (<= new-lo old-hi)
                               `(integer ,new-lo ,old-hi)
                               `(integer ,old-hi ,new-lo)))))
                  (1 (let ((new-hi (%inc old-hi hi-delta)))
                       (if (or (null (cdr type))
                               (null (cddr type))
                               (not (integerp old-lo)))
                           `(integer ,old-lo ,new-hi)
                           (if (<= old-lo new-hi)
                               `(integer ,old-lo ,new-hi)
                               `(integer ,new-hi ,old-lo)))))
                  (1 (let ((new-lo (%inc old-lo lo-delta))
                           (new-hi (%inc old-hi hi-delta)))
                       (if (<= new-lo new-hi)
                           `(integer ,new-lo ,new-hi)
                           `(integer ,new-hi ,new-lo))))))))

            (t (%f)))))))
pfdietz's avatar
pfdietz committed
164

165 166
(defun test-random-types (n size)
  (loop for t1 = (make-random-type size)
sds's avatar
sds committed
167 168 169
        for t2 = (make-random-type size)
        for i from 0 below n
        ;; do (print (list t1 t2))
170
        do (setf *random-types* (list t1 t2))
sds's avatar
sds committed
171 172 173 174 175
        do (when (and (= (mod i 100) 0) (> i 0))
             (format t "~A " i) (finish-output *standard-output*))
        when (test-types t1 t2)
        collect (list t1 t2)
        finally (terpri)))
pfdietz's avatar
pfdietz committed
176

pfdietz's avatar
pfdietz committed
177
(defun test-random-mutated-types (n size &key (reps 1))
pfdietz's avatar
pfdietz committed
178
  (loop for t1 = (make-random-type size)
sds's avatar
sds committed
179 180 181 182
        for t2 = (let ((x t1)) (loop repeat reps
                                     do (setq x (mutate-type x))) x)
        for i from 0 below n
        ;; do (print (list t1 t2))
pfdietz's avatar
pfdietz committed
183
        do (setf *random-types* (list t1 t2))
sds's avatar
sds committed
184 185 186 187 188
        do (when (and (= (mod i 100) 0) (> i 0))
             (format t "~A " i) (finish-output *standard-output*))
        when (test-types t1 t2)
        collect (list t1 t2)
        finally (terpri)))
189 190 191 192 193 194

(defun test-types (t1 t2)
  (multiple-value-bind (sub success)
      (subtypep t1 t2)
    (when success
      (if sub
sds's avatar
sds committed
195 196 197 198
          (check-all-subtypep t1 t2)
        (let ((nt1 `(not ,t1))
              (nt2 `(not ,t2)))
          (subtypep nt2 nt1))))))
199 200 201 202 203 204 205 206 207 208 209 210 211

(defun prune-type (tp try-fn)
  (declare (type function try-fn))
  (flet ((try (x) (funcall try-fn x)))
    (cond
     ((member tp '(nil t)))
     ((symbolp tp)
      (try nil)
      (try t))
     ((consp tp)
      (try nil)
      (try t)
      (let ((op (first tp))
sds's avatar
sds committed
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
            (args (rest tp)))
        (case op
          ((cons)
           (try 'cons)
           (prune-list args
                       #'prune-type
                       #'(lambda (args) (try `(cons ,@args)))))
          ((integer)
           (try op)
           (try '(eql 0))
           (when (= (length args) 2)
             (let ((arg1 (first args))
                   (arg2 (second args)))
               (when (and (integerp arg1) (integerp arg2))
                 (try `(eql ,arg1))
                 (try `(eql ,arg2))
                 (when (and (< arg1 0) (<= 0 arg2))
                   (try `(integer 0 ,arg2)))
                 (when (and (<= arg1 0) (< 0 arg2))
                   (try `(integer ,arg1 0)))
                 (when (> (- arg2 arg1) 1)
                   (try `(integer ,(+ arg1 (floor (- arg2 arg1) 2)) ,arg2))
                   (try `(integer ,arg1 ,(- arg2 (floor (- arg2 arg1) 2)))))))))

          ((real float ratio single-float double-float short-float long-float)
           (try op))

          ((or and)
           (mapc try-fn args)
           (loop for i from 0 below (length args)
                 do (try `(,op ,@(subseq args 0 i)
                               ,@(subseq args (1+ i)))))
           (prune-list args
                       #'prune-type
                       #'(lambda (args) (try (cons op args)))))
          ((not)
           (let ((arg (first args)))
             (try arg)
             (when (and (consp arg)
                        (eq (car arg) 'not))
               (try (second arg)))
             (prune-type arg #'(lambda (arg) (try `(not ,arg))))))
254

sds's avatar
sds committed
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
          ((member)
           (dolist (arg (cdr tp))
             (try `(eql ,arg)))
           (when (cddr tp)
           (try `(member ,@(cddr tp)))))

          ((eql)
           (assert (= (length args) 1))
           (let ((arg (first args)))
             (unless (= arg 0)
               (try `(eql 0))
               (cond
                ((< arg -1)
                 (try `(eql ,(ceiling arg 2))))
                ((> arg 1)
                 (try `(eql ,(floor arg 2))))))))

          )))))
273 274
  (values))

275 276 277
(defun prune-type-pair (pair &optional (fn #'test-types))
  (declare (type function fn))
  (let ((t1 (first pair))
sds's avatar
sds committed
278 279
        (t2 (second pair))
        changed)
280 281
    (loop
     do (flet ((%try2 (new-tp)
sds's avatar
sds committed
282 283 284 285 286 287 288 289
                      (when (funcall fn t1 new-tp)
                        (print "Success in first loop")
                        (print new-tp)
                        (setq t2 new-tp
                              changed t)
                        (throw 'success nil))))
          (catch 'success
            (prune-type t2 #'%try2)))
290
     do (flet ((%try1 (new-tp)
sds's avatar
sds committed
291 292 293 294 295 296 297 298
                      (when (funcall fn new-tp t2)
                        (print "Success in second loop")
                        (print new-tp)
                        (setq t1 new-tp
                              changed t)
                        (throw 'success nil))))
          (catch 'success
            (prune-type t1 #'%try1)))
299
     while changed
300 301
     do (setq changed nil))
    (list t1 t2)))
302

303 304
(defun test-type-triple (t1 t2 t3)
  ;; Returns non-nil if a problem is found
305 306
  (catch 'problem
    (multiple-value-bind (sub1 success1)
sds's avatar
sds committed
307
        (subtypep t1 t2)
308
      (when success1
sds's avatar
sds committed
309 310 311 312 313 314
        (if sub1
            (append
             (check-all-subtypep t1 `(or ,t2 ,t3))
             (check-all-subtypep `(and ,t1 ,t3) t2))
            (or (subtypep `(or ,t1 ,t3) t2)
                (subtypep t1 `(and ,t2 ,t3))))))))
315 316 317

(defun test-random-types3 (n size)
  (loop for t1 = (make-random-type (1+ (random size)))
sds's avatar
sds committed
318 319 320 321
        for t2 = (make-random-type (1+ (random size)))
        for t3 = (make-random-type (1+ (random size)))
        for i from 1 to n
        ;; do (print (list t1 t2))
322
        do (setf *random-types* (list t1 t2 t3))
sds's avatar
sds committed
323 324 325 326 327
        do (when (and (= (mod i 100) 0) (> i 0))
             (format t "~A " i) (finish-output *standard-output*))
        when (test-type-triple t1 t2 t3)
        collect (list t1 t2 t3)
        finally (terpri)))
328 329 330 331

(defun prune-type-triple (pair &optional (fn #'test-type-triple))
  (declare (type function fn))
  (let ((t1 (first pair))
sds's avatar
sds committed
332 333 334
        (t2 (second pair))
        (t3 (third pair))
        changed)
335 336
    (loop
     do (flet ((%try2 (new-tp)
sds's avatar
sds committed
337 338 339 340 341 342 343 344
                      (when (funcall fn t1 new-tp t3)
                        (print "Success in first loop")
                        (print new-tp)
                        (setq t2 new-tp
                              changed t)
                        (throw 'success nil))))
          (catch 'success
            (prune-type t2 #'%try2)))
345
     do (flet ((%try1 (new-tp)
sds's avatar
sds committed
346 347 348 349 350 351 352 353
                      (when (funcall fn new-tp t2 t3)
                        (print "Success in second loop")
                        (print new-tp)
                        (setq t1 new-tp
                              changed t)
                        (throw 'success nil))))
          (catch 'success
            (prune-type t1 #'%try1)))
354
     do (flet ((%try3 (new-tp)
sds's avatar
sds committed
355 356 357 358 359 360 361 362
                      (when (funcall fn t1 t2 new-tp)
                        (print "Success in second loop")
                        (print new-tp)
                        (setq t3 new-tp
                              changed t)
                        (throw 'success nil))))
          (catch 'success
            (prune-type t3 #'%try3)))
363 364 365
     while changed
     do (setq changed nil))
    (list t1 t2 t3)))
366 367 368



sds's avatar
sds committed
369