2 ------------------------------------------------------------------
3 -- A primop-table mangling program --
4 ------------------------------------------------------------------
14 import System ( getArgs )
15 import Maybe ( catMaybes )
18 main = getArgs >>= \args ->
19 if length args /= 1 || head args `notElem` known_args
20 then error ("usage: genprimopcode command < primops.txt > ...\n"
21 ++ " where command is one of\n"
22 ++ unlines (map (" "++) known_args)
27 Left err -> error ("parse error at " ++ (show err))
28 Right p_o_specs@(Info _ entries)
29 -> seq (sanityTop p_o_specs) (
33 -> putStr (gen_data_decl p_o_specs)
36 -> putStr (gen_switch_from_attribs
38 "primOpHasSideEffects" p_o_specs)
41 -> putStr (gen_switch_from_attribs
43 "primOpOutOfLine" p_o_specs)
46 -> putStr (gen_switch_from_attribs
48 "commutableOp" p_o_specs)
51 -> putStr (gen_switch_from_attribs
53 "primOpNeedsWrapper" p_o_specs)
56 -> putStr (gen_switch_from_attribs
58 "primOpCanFail" p_o_specs)
61 -> putStr (gen_switch_from_attribs
63 "primOpStrictness" p_o_specs)
65 "--primop-primop-info"
66 -> putStr (gen_primop_info p_o_specs)
69 -> putStr (gen_primop_tag p_o_specs)
72 -> putStr (gen_primop_list p_o_specs)
74 "--make-haskell-wrappers"
75 -> putStr (gen_wrappers p_o_specs)
77 "--make-haskell-source"
78 -> putStr (gen_hs_source p_o_specs)
80 "--make-ext-core-source"
81 -> putStr (gen_ext_core_source entries)
84 -> putStr (gen_latex_doc p_o_specs)
86 _ -> error "Should not happen, known_args out of sync?"
89 known_args :: [String]
98 "--primop-primop-info",
101 "--make-haskell-wrappers",
102 "--make-haskell-source",
103 "--make-ext-core-source",
107 ------------------------------------------------------------------
108 -- Code generators -----------------------------------------------
109 ------------------------------------------------------------------
111 gen_hs_source :: Info -> String
112 gen_hs_source (Info defaults entries) =
113 "-----------------------------------------------------------------------------\n"
115 ++ "-- Module : GHC.Prim\n"
117 ++ "-- Maintainer : cvs-ghc@haskell.org\n"
118 ++ "-- Stability : internal\n"
119 ++ "-- Portability : non-portable (GHC extensions)\n"
121 ++ "-- GHC\'s primitive types and operations.\n"
123 ++ "-----------------------------------------------------------------------------\n"
124 ++ "module GHC.Prim (\n"
125 ++ unlines (map (("\t" ++) . hdr) entries)
127 ++ unlines (map opt defaults) ++ "-}\n"
128 ++ unlines (map ent entries) ++ "\n\n\n"
129 where opt (OptionFalse n) = n ++ " = False"
130 opt (OptionTrue n) = n ++ " = True"
131 opt (OptionString n v) = n ++ " = { " ++ v ++ "}"
133 hdr s@(Section {}) = sec s
134 hdr (PrimOpSpec { name = n }) = wrapOp n ++ ","
135 hdr (PseudoOpSpec { name = n }) = wrapOp n ++ ","
136 hdr (PrimTypeSpec { ty = TyApp n _ }) = wrapTy n ++ ","
137 hdr (PrimTypeSpec {}) = error "Illegal type spec"
139 ent (Section {}) = ""
140 ent o@(PrimOpSpec {}) = spec o
141 ent o@(PrimTypeSpec {}) = spec o
142 ent o@(PseudoOpSpec {}) = spec o
144 sec s = "\n-- * " ++ escape (title s) ++ "\n"
145 ++ (unlines $ map ("-- " ++ ) $ lines $ unlatex $ escape $ "|" ++ desc s) ++ "\n"
147 spec o = comm ++ decl
148 where decl = case o of
149 PrimOpSpec { name = n, ty = t } -> wrapOp n ++ " :: " ++ pprTy t
150 PseudoOpSpec { name = n, ty = t } -> wrapOp n ++ " :: " ++ pprTy t
151 PrimTypeSpec { ty = t } -> "data " ++ pprTy t
154 comm = case (desc o) of
156 d -> "\n" ++ (unlines $ map ("-- " ++ ) $ lines $ unlatex $ escape $ "|" ++ d)
158 wrapOp nm | isAlpha (head nm) = nm
159 | otherwise = "(" ++ nm ++ ")"
160 wrapTy nm | isAlpha (head nm) = nm
161 | otherwise = "(" ++ nm ++ ")"
162 unlatex s = case s of
163 '\\':'t':'e':'x':'t':'t':'t':'{':cs -> markup "@" "@" cs
164 '{':'\\':'t':'t':cs -> markup "@" "@" cs
165 '{':'\\':'i':'t':cs -> markup "/" "/" cs
166 c : cs -> c : unlatex cs
168 markup s t xs = s ++ mk (dropWhile isSpace xs)
170 mk ('\n':cs) = ' ' : mk cs
171 mk ('}':cs) = t ++ unlatex cs
172 mk (c:cs) = c : mk cs
173 escape = concatMap (\c -> if c `elem` special then '\\':c:[] else c:[])
174 where special = "/'`\"@<"
178 pty (TyF t1 t2) = pbty t1 ++ " -> " ++ pty t2
180 pbty (TyApp tc ts) = tc ++ concat (map (' ' :) (map paty ts))
181 pbty (TyUTup ts) = "(# "
182 ++ concat (intersperse "," (map pty ts))
187 paty t = "(" ++ pty t ++ ")"
189 -- Generates the type environment that the stand-alone External Core tools use.
190 gen_ext_core_source :: [Entry] -> String
191 gen_ext_core_source entries =
192 "-----------------------------------------------------------------------\n"
193 ++ "-- This module is automatically generated by the GHC utility\n"
194 ++ "-- \"genprimopcode\". Do not edit!\n"
195 ++ "-----------------------------------------------------------------------\n"
196 ++ "module Language.Core.PrimEnv(primTcs, primVals, intLitTypes, ratLitTypes,"
197 ++ "\n charLitTypes, stringLitTypes) where\nimport Language.Core.Core"
198 ++ "\nimport Language.Core.Encoding\n\n"
199 ++ "primTcs :: [(Tcon, Kind)]\n"
201 ++ printList tcEnt entries
203 ++ "primVals :: [(Var, Ty)]\n"
205 ++ printList valEnt entries
207 ++ "intLitTypes :: [Ty]\n"
208 ++ "intLitTypes = [\n"
209 ++ printList tyEnt (intLitTys entries)
211 ++ "ratLitTypes :: [Ty]\n"
212 ++ "ratLitTypes = [\n"
213 ++ printList tyEnt (ratLitTys entries)
215 ++ "charLitTypes :: [Ty]\n"
216 ++ "charLitTypes = [\n"
217 ++ printList tyEnt (charLitTys entries)
219 ++ "stringLitTypes :: [Ty]\n"
220 ++ "stringLitTypes = [\n"
221 ++ printList tyEnt (stringLitTys entries)
224 where printList f = concat . intersperse ",\n" . filter (not . null) . map f
225 tcEnt (PrimTypeSpec {ty=t}) =
227 TyApp tc args -> parens tc (tcKind tc args)
228 _ -> error ("tcEnt: type in PrimTypeSpec is not a type"
229 ++ " constructor: " ++ show t)
232 -- The primops.txt.pp format doesn't have enough information in it to
233 -- print out some of the information that ext-core needs (like kinds,
234 -- and later on in this code, module names) so we special-case. An
235 -- alternative would be to refer to things indirectly and hard-wire
236 -- certain things (e.g., the kind of the Any constructor, here) into
237 -- ext-core's Prims module again.
238 tcKind "Any" _ = "Klifted"
239 tcKind tc [] | last tc == '#' = "Kunlifted"
240 tcKind tc [] | otherwise = "Klifted"
241 -- assumes that all type arguments are lifted (are they?)
242 tcKind tc (v:as) = "(Karrow Klifted " ++ tcKind tc as
244 valEnt (PseudoOpSpec {name=n, ty=t}) = valEntry n t
245 valEnt (PrimOpSpec {name=n, ty=t}) = valEntry n t
247 valEntry name ty = parens name (mkForallTy (freeTvars ty) (pty ty))
248 where pty (TyF t1 t2) = mkFunTy (pty t1) (pty t2)
249 pty (TyApp tc ts) = mkTconApp (mkTcon tc) (map pty ts)
250 pty (TyUTup ts) = mkUtupleTy (map pty ts)
251 pty (TyVar tv) = paren $ "Tvar \"" ++ tv ++ "\""
253 mkFunTy s1 s2 = "Tapp " ++ (paren ("Tapp (Tcon tcArrow)"
256 mkTconApp tc args = foldl tapp tc args
257 mkTcon tc = paren $ "Tcon " ++ paren (qualify True tc)
258 mkUtupleTy args = foldl tapp (tcUTuple (length args)) args
260 mkForallTy vs t = foldr
261 (\ v s -> "Tforall " ++
262 (paren (quot v ++ ", " ++ vKind v)) ++ " "
269 freeTvars (TyF t1 t2) = freeTvars t1 `union` freeTvars t2
270 freeTvars (TyApp _ tys) = freeTvarss tys
271 freeTvars (TyVar v) = [v]
272 freeTvars (TyUTup tys) = freeTvarss tys
273 freeTvarss = nub . concatMap freeTvars
275 tapp s nextArg = paren $ "Tapp " ++ s ++ " " ++ paren nextArg
276 tcUTuple n = paren $ "Tcon " ++ paren (qualify False $ "Z"
279 tyEnt (PrimTypeSpec {ty=(TyApp tc args)}) = " " ++ paren ("Tcon " ++
280 (paren (qualify True tc)))
283 -- more hacks. might be better to do this on the ext-core side,
284 -- as per earlier comment
285 qualify _ tc | tc == "Bool" = "Just boolMname" ++ ", "
287 qualify _ tc | tc == "()" = "Just baseMname" ++ ", "
289 qualify enc tc = "Just primMname" ++ ", " ++ (ze enc tc)
290 ze enc tc = (if enc then "zEncodeString " else "")
291 ++ "\"" ++ tc ++ "\""
293 intLitTys = prefixes ["Int", "Word", "Addr", "Char"]
294 ratLitTys = prefixes ["Float", "Double"]
295 charLitTys = prefixes ["Char"]
296 stringLitTys = prefixes ["Addr"]
297 prefixes ps = filter (\ t ->
299 (PrimTypeSpec {ty=(TyApp tc args)}) ->
300 any (\ p -> p `isPrefixOf` tc) ps
303 parens n ty = " (zEncodeString \"" ++ n ++ "\", " ++ ty ++ ")"
304 paren s = "(" ++ s ++ ")"
305 quot s = "\"" ++ s ++ "\""
307 gen_latex_doc :: Info -> String
308 gen_latex_doc (Info defaults entries)
309 = "\\primopdefaults{"
310 ++ mk_options defaults
312 ++ (concat (map mk_entry entries))
313 where mk_entry (PrimOpSpec {cons=constr,name=n,ty=t,cat=c,desc=d,opts=o}) =
315 ++ latex_encode constr ++ "}{"
316 ++ latex_encode n ++ "}{"
317 ++ latex_encode (zencode n) ++ "}{"
318 ++ latex_encode (show c) ++ "}{"
319 ++ latex_encode (mk_source_ty t) ++ "}{"
320 ++ latex_encode (mk_core_ty t) ++ "}{"
324 mk_entry (Section {title=ti,desc=d}) =
326 ++ latex_encode ti ++ "}{"
328 mk_entry (PrimTypeSpec {ty=t,desc=d,opts=o}) =
330 ++ latex_encode (mk_source_ty t) ++ "}{"
331 ++ latex_encode (mk_core_ty t) ++ "}{"
335 mk_entry (PseudoOpSpec {name=n,ty=t,desc=d,opts=o}) =
337 ++ latex_encode (zencode n) ++ "}{"
338 ++ latex_encode (mk_source_ty t) ++ "}{"
339 ++ latex_encode (mk_core_ty t) ++ "}{"
343 mk_source_ty typ = pty typ
344 where pty (TyF t1 t2) = pbty t1 ++ " -> " ++ pty t2
346 pbty (TyApp tc ts) = tc ++ (concat (map (' ':) (map paty ts)))
347 pbty (TyUTup ts) = "(# " ++ (concat (intersperse "," (map pty ts))) ++ " #)"
350 paty t = "(" ++ pty t ++ ")"
352 mk_core_ty typ = foralls ++ (pty typ)
353 where pty (TyF t1 t2) = pbty t1 ++ " -> " ++ pty t2
355 pbty (TyApp tc ts) = (zencode tc) ++ (concat (map (' ':) (map paty ts)))
356 pbty (TyUTup ts) = (zencode (utuplenm (length ts))) ++ (concat ((map (' ':) (map paty ts))))
358 paty (TyVar tv) = zencode tv
359 paty (TyApp tc []) = zencode tc
360 paty t = "(" ++ pty t ++ ")"
362 utuplenm n = "(#" ++ (replicate (n-1) ',') ++ "#)"
363 foralls = if tvars == [] then "" else "%forall " ++ (tbinds tvars)
366 tbinds ("o":tbs) = "(o::?) " ++ (tbinds tbs)
367 tbinds (tv:tbs) = tv ++ " " ++ (tbinds tbs)
368 tvars_of (TyF t1 t2) = tvars_of t1 `union` tvars_of t2
369 tvars_of (TyApp _ ts) = foldl union [] (map tvars_of ts)
370 tvars_of (TyUTup ts) = foldr union [] (map tvars_of ts)
371 tvars_of (TyVar tv) = [tv]
375 ++ mk_has_side_effects o ++ "}{"
376 ++ mk_out_of_line o ++ "}{"
377 ++ mk_commutable o ++ "}{"
378 ++ mk_needs_wrapper o ++ "}{"
379 ++ mk_can_fail o ++ "}{"
380 ++ latex_encode (mk_strictness o) ++ "}{"
383 mk_has_side_effects o = mk_bool_opt o "has_side_effects" "Has side effects." "Has no side effects."
384 mk_out_of_line o = mk_bool_opt o "out_of_line" "Implemented out of line." "Implemented in line."
385 mk_commutable o = mk_bool_opt o "commutable" "Commutable." "Not commutable."
386 mk_needs_wrapper o = mk_bool_opt o "needs_wrapper" "Needs wrapper." "Needs no wrapper."
387 mk_can_fail o = mk_bool_opt o "can_fail" "Can fail." "Cannot fail."
389 mk_bool_opt o opt_name if_true if_false =
390 case lookup_attrib opt_name o of
391 Just (OptionTrue _) -> if_true
392 Just (OptionFalse _) -> if_false
393 Just (OptionString _ _) -> error "String value for boolean option"
397 case lookup_attrib "strictness" o of
398 Just (OptionString _ s) -> s -- for now
399 Just _ -> error "Boolean value for strictness"
403 case maybe_tuple xs of
404 Just n -> n -- Tuples go to Z2T etc
405 Nothing -> concat (map encode_ch xs)
407 maybe_tuple "(# #)" = Just("Z1H")
408 maybe_tuple ('(' : '#' : cs) = case count_commas (0::Int) cs of
409 (n, '#' : ')' : _) -> Just ('Z' : shows (n+1) "H")
411 maybe_tuple "()" = Just("Z0T")
412 maybe_tuple ('(' : cs) = case count_commas (0::Int) cs of
413 (n, ')' : _) -> Just ('Z' : shows (n+1) "T")
415 maybe_tuple _ = Nothing
417 count_commas :: Int -> String -> (Int, String)
418 count_commas n (',' : cs) = count_commas (n+1) cs
419 count_commas n cs = (n,cs)
421 unencodedChar :: Char -> Bool -- True for chars that don't need encoding
422 unencodedChar 'Z' = False
423 unencodedChar 'z' = False
424 unencodedChar c = isAlphaNum c
426 encode_ch :: Char -> String
427 encode_ch c | unencodedChar c = [c] -- Common case first
430 encode_ch '(' = "ZL" -- Needed for things like (,), and (->)
431 encode_ch ')' = "ZR" -- For symmetry with (
451 encode_ch '\'' = "zq"
452 encode_ch '\\' = "zr"
457 encode_ch c = 'z' : shows (ord c) "U"
460 latex_encode (c:cs) | c `elem` "#$%&_^{}" = "\\" ++ c:(latex_encode cs)
461 latex_encode ('~':cs) = "\\verb!~!" ++ (latex_encode cs)
462 latex_encode ('\\':cs) = "$\\backslash$" ++ (latex_encode cs)
463 latex_encode (c:cs) = c:(latex_encode cs)
465 gen_wrappers :: Info -> String
466 gen_wrappers (Info _ entries)
467 = "{-# LANGUAGE NoImplicitPrelude, UnboxedTuples #-}\n"
468 -- Dependencies on Prelude must be explicit in libraries/base, but we
469 -- don't need the Prelude here so we add NoImplicitPrelude.
470 ++ "module GHC.PrimopWrappers where\n"
471 ++ "import qualified GHC.Prim\n"
472 ++ "import GHC.Bool (Bool)\n"
473 ++ "import GHC.Unit ()\n"
474 ++ "import GHC.Prim (" ++ types ++ ")\n"
475 ++ unlines (concatMap f specs)
477 specs = filter (not.dodgy) (filter is_primop entries)
478 tycons = foldr union [] $ map (tyconsIn . ty) specs
479 tycons' = filter (`notElem` ["()", "Bool"]) tycons
480 types = concat $ intersperse ", " tycons'
481 f spec = let args = map (\n -> "a" ++ show n) [1 .. arity (ty spec)]
482 src_name = wrap (name spec)
483 lhs = src_name ++ " " ++ unwords args
484 rhs = "(GHC.Prim." ++ name spec ++ ") " ++ unwords args
485 in ["{-# NOINLINE " ++ src_name ++ " #-}",
486 src_name ++ " :: " ++ pprTy (ty spec),
488 wrap nm | isLower (head nm) = nm
489 | otherwise = "(" ++ nm ++ ")"
493 [-- C code generator can't handle these
496 -- not interested in parallel support
497 "par#", "parGlobal#", "parLocal#", "parAt#",
498 "parAtAbs#", "parAtRel#", "parAtForNow#"
501 gen_primop_list :: Info -> String
502 gen_primop_list (Info _ entries)
504 [ " [" ++ cons first ]
506 map (\p -> " , " ++ cons p) rest
509 ) where (first:rest) = filter is_primop entries
511 gen_primop_tag :: Info -> String
512 gen_primop_tag (Info _ entries)
513 = unlines (max_def_type : max_def :
514 tagOf_type : zipWith f primop_entries [1 :: Int ..])
516 primop_entries = filter is_primop entries
517 tagOf_type = "tagOf_PrimOp :: PrimOp -> FastInt"
518 f i n = "tagOf_PrimOp " ++ cons i ++ " = _ILIT(" ++ show n ++ ")"
519 max_def_type = "maxPrimOpTag :: Int"
520 max_def = "maxPrimOpTag = " ++ show (length primop_entries)
522 gen_data_decl :: Info -> String
523 gen_data_decl (Info _ entries)
524 = let conss = map cons (filter is_primop entries)
525 in "data PrimOp\n = " ++ head conss ++ "\n"
526 ++ unlines (map (" | "++) (tail conss))
528 gen_switch_from_attribs :: String -> String -> Info -> String
529 gen_switch_from_attribs attrib_name fn_name (Info defaults entries)
530 = let defv = lookup_attrib attrib_name defaults
531 alternatives = catMaybes (map mkAlt (filter is_primop entries))
533 getAltRhs (OptionFalse _) = "False"
534 getAltRhs (OptionTrue _) = "True"
535 getAltRhs (OptionString _ s) = s
538 = case lookup_attrib attrib_name (opts po) of
540 Just xx -> Just (fn_name ++ " " ++ cons po ++ " = " ++ getAltRhs xx)
544 Nothing -> error ("gen_switch_from: " ++ attrib_name)
546 -> unlines alternatives
547 ++ fn_name ++ " _ = " ++ getAltRhs xx ++ "\n"
549 ------------------------------------------------------------------
550 -- Create PrimOpInfo text from PrimOpSpecs -----------------------
551 ------------------------------------------------------------------
553 gen_primop_info :: Info -> String
554 gen_primop_info (Info _ entries)
555 = unlines (map mkPOItext (filter is_primop entries))
557 mkPOItext :: Entry -> String
558 mkPOItext i = mkPOI_LHS_text i ++ mkPOI_RHS_text i
560 mkPOI_LHS_text :: Entry -> String
562 = "primOpInfo " ++ cons i ++ " = "
564 mkPOI_RHS_text :: Entry -> String
570 -> "mkCompare " ++ sl_name i ++ ppType t1
571 _ -> error "Type error in comparison op"
575 -> "mkMonadic " ++ sl_name i ++ ppType t1
576 _ -> error "Type error in monadic op"
580 -> "mkDyadic " ++ sl_name i ++ ppType t1
581 _ -> error "Type error in dyadic op"
583 -> let (argTys, resTy) = flatTys (ty i)
584 tvs = nub (tvsIn (ty i))
586 "mkGenPrimOp " ++ sl_name i ++ " "
587 ++ listify (map ppTyVar tvs) ++ " "
588 ++ listify (map ppType argTys) ++ " "
589 ++ "(" ++ ppType resTy ++ ")"
591 sl_name :: Entry -> String
592 sl_name i = "(fsLit \"" ++ name i ++ "\") "
594 ppTyVar :: String -> String
595 ppTyVar "a" = "alphaTyVar"
596 ppTyVar "b" = "betaTyVar"
597 ppTyVar "c" = "gammaTyVar"
598 ppTyVar "s" = "deltaTyVar"
599 ppTyVar "o" = "openAlphaTyVar"
600 ppTyVar _ = error "Unknown type var"
602 ppType :: Ty -> String
603 ppType (TyApp "Bool" []) = "boolTy"
605 ppType (TyApp "Int#" []) = "intPrimTy"
606 ppType (TyApp "Int32#" []) = "int32PrimTy"
607 ppType (TyApp "Int64#" []) = "int64PrimTy"
608 ppType (TyApp "Char#" []) = "charPrimTy"
609 ppType (TyApp "Word#" []) = "wordPrimTy"
610 ppType (TyApp "Word32#" []) = "word32PrimTy"
611 ppType (TyApp "Word64#" []) = "word64PrimTy"
612 ppType (TyApp "Addr#" []) = "addrPrimTy"
613 ppType (TyApp "Float#" []) = "floatPrimTy"
614 ppType (TyApp "Double#" []) = "doublePrimTy"
615 ppType (TyApp "ByteArray#" []) = "byteArrayPrimTy"
616 ppType (TyApp "RealWorld" []) = "realWorldTy"
617 ppType (TyApp "ThreadId#" []) = "threadIdPrimTy"
618 ppType (TyApp "ForeignObj#" []) = "foreignObjPrimTy"
619 ppType (TyApp "BCO#" []) = "bcoPrimTy"
620 ppType (TyApp "()" []) = "unitTy" -- unitTy is TysWiredIn's name for ()
622 ppType (TyVar "a") = "alphaTy"
623 ppType (TyVar "b") = "betaTy"
624 ppType (TyVar "c") = "gammaTy"
625 ppType (TyVar "s") = "deltaTy"
626 ppType (TyVar "o") = "openAlphaTy"
627 ppType (TyApp "State#" [x]) = "mkStatePrimTy " ++ ppType x
628 ppType (TyApp "MutVar#" [x,y]) = "mkMutVarPrimTy " ++ ppType x
630 ppType (TyApp "MutableArray#" [x,y]) = "mkMutableArrayPrimTy " ++ ppType x
633 ppType (TyApp "MutableByteArray#" [x]) = "mkMutableByteArrayPrimTy "
636 ppType (TyApp "Array#" [x]) = "mkArrayPrimTy " ++ ppType x
639 ppType (TyApp "Weak#" [x]) = "mkWeakPrimTy " ++ ppType x
640 ppType (TyApp "StablePtr#" [x]) = "mkStablePtrPrimTy " ++ ppType x
641 ppType (TyApp "StableName#" [x]) = "mkStableNamePrimTy " ++ ppType x
643 ppType (TyApp "MVar#" [x,y]) = "mkMVarPrimTy " ++ ppType x
645 ppType (TyApp "TVar#" [x,y]) = "mkTVarPrimTy " ++ ppType x
647 ppType (TyUTup ts) = "(mkTupleTy Unboxed " ++ show (length ts)
649 ++ listify (map ppType ts) ++ ")"
651 ppType (TyF s d) = "(mkFunTy (" ++ ppType s ++ ") (" ++ ppType d ++ "))"
654 = error ("ppType: can't handle: " ++ show other ++ "\n")
656 listify :: [String] -> String
657 listify ss = "[" ++ concat (intersperse ", " ss) ++ "]"
659 flatTys :: Ty -> ([Ty],Ty)
660 flatTys (TyF t1 t2) = case flatTys t2 of (ts,t) -> (t1:ts,t)
661 flatTys other = ([],other)
663 tvsIn :: Ty -> [TyVar]
664 tvsIn (TyF t1 t2) = tvsIn t1 ++ tvsIn t2
665 tvsIn (TyApp _ tys) = concatMap tvsIn tys
666 tvsIn (TyVar tv) = [tv]
667 tvsIn (TyUTup tys) = concatMap tvsIn tys
669 tyconsIn :: Ty -> [TyCon]
670 tyconsIn (TyF t1 t2) = tyconsIn t1 `union` tyconsIn t2
671 tyconsIn (TyApp tc tys) = foldr union [tc] $ map tyconsIn tys
672 tyconsIn (TyVar _) = []
673 tyconsIn (TyUTup tys) = foldr union [] $ map tyconsIn tys
676 arity = length . fst . flatTys