(define impc:ti:maximize-generic-type
(lambda (string-type)
;; (println 'maxtypein: string-type)
(let* ((ptr-depth (impc:ir:get-ptr-depth string-type))
(p (impc:ti:split-namedtype string-type)))
;; first check of we are asking for a fully generic type definition (i.e. List*)
(if (and (null? (cdr p))
(impc:ti:get-generictype-candidate-types (car p))) ;; not generic!
(apply string-append (car p) ":" (symbol->string (impc:ti:get-generictype-candidate-types (car p)))
(make-list (impc:ir:get-ptr-depth string-type) "*"))
;; next check if type is already maximized!
(if (or (not (impc:ti:get-generictype-candidate-types (car p))) ;; not generic!
(and (not (regex:match? (cadr p) "({|!)"))
(not (string-contains? string-type "{"))))
string-type
;; otherwise we really do need to max type!
(let* ((name (car p))
(argstr (cadr p))
(ags
(cl:remove #f
(map (lambda (x)
(if (regex:match? x "^[A-Za-z0-9_]*{")
(impc:ti:maximize-generic-type x)
(if (regex:match? x (string-append "^" name "[^A-Za-z0-9_]"))
#f
x)))
(impc:ir:get-pretty-tuple-arg-strings argstr)))) ;)
(named_ags (cl:remove
#f
(map (lambda (x)
(if (regex:match? x "^[A-Za-z0-9_]*{")
(impc:ti:maximize-generic-type x)
#f))
(impc:ir:get-pretty-tuple-arg-strings argstr))))
(ags_a (cl:remove-duplicates (regex:match-all argstr "![A-Za-z_0-9]*")))
(gtype (symbol->string (impc:ti:get-generictype-candidate-types (car p))))
;; (plst (impc:ir:get-pretty-tuple-arg-strings gtype))
;; (plst (map (lambda (x)
;; (if (regex:match? x "^[A-Za-z0-9_]*{")
;; (impc:ti:maximize-generic-type x)
;; x))
;; (impc:ir:get-pretty-tuple-arg-strings gtype)))
(named_gags (cl:remove
#f
(map (lambda (x)
(if (regex:match? x "^[A-Za-z0-9_]*({|:<)")
(string-append "\\Q" x "\\E")
#f))
(impc:ir:get-pretty-tuple-arg-strings gtype))))
(gags (cl:remove-duplicates (regex:match-all gtype "![A-Za-z_0-9]*"))))
;; (println 'maximize: string-type 'gtype gtype 'ags ags 'gags gags 'named: named_ags named_gags)
(let* ((gt2 (if (<> (length named_gags)
(length named_ags))
gtype
(regex:replace-everything gtype named_gags named_ags)))
;; (lll (println 'gt2 gt2))
(newt (if (<> (length ags) (length gags))
gt2
(regex:replace-everything gt2 gags ags)))
;; (lllll (println 'newt newt))
(newt2 (map (lambda (x)
;; (println 'string-type string-type 'x x)
(if (regex:match? x "^[A-Za-z0-9_]*{")
(if (regex:match? x (string-append string-type "\\**"))
(regex:replace x "^([^{]*).+(\\*+)$" "$1$2")
(impc:ti:maximize-generic-type x))
x))
(impc:ir:get-pretty-tuple-arg-strings newt)))
;; (lllllllll (println 'newt2 newt2))
(newtype_c (apply string-append (car p) ":<" (string-join newt2 ",") ">"
(make-list ptr-depth "*"))))
;; (println 'maxtypeout: string-type newtype_c)
newtype_c)))))))