Replace genprimopcode's parsec parser with an alex+happy parser
[ghc-hetmet.git] / utils / genprimopcode / Main.hs
1 {-# OPTIONS -cpp #-}
2 ------------------------------------------------------------------
3 -- A primop-table mangling program                              --
4 ------------------------------------------------------------------
5
6 module Main where
7
8 import Parser
9 import Syntax
10
11 import Monad
12 import Char
13 import List
14 import System ( getArgs )
15 import Maybe ( catMaybes )
16
17 main :: IO ()
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)
23                   )
24        else
25        do s <- getContents
26           case parse s of
27              Left err -> error ("parse error at " ++ (show err))
28              Right p_o_specs
29                 -> seq (sanityTop p_o_specs) (
30                    case head args of
31
32                       "--data-decl" 
33                          -> putStr (gen_data_decl p_o_specs)
34
35                       "--has-side-effects" 
36                          -> putStr (gen_switch_from_attribs 
37                                        "has_side_effects" 
38                                        "primOpHasSideEffects" p_o_specs)
39
40                       "--out-of-line" 
41                          -> putStr (gen_switch_from_attribs 
42                                        "out_of_line" 
43                                        "primOpOutOfLine" p_o_specs)
44
45                       "--commutable" 
46                          -> putStr (gen_switch_from_attribs 
47                                        "commutable" 
48                                        "commutableOp" p_o_specs)
49
50                       "--needs-wrapper" 
51                          -> putStr (gen_switch_from_attribs 
52                                        "needs_wrapper" 
53                                        "primOpNeedsWrapper" p_o_specs)
54
55                       "--can-fail" 
56                          -> putStr (gen_switch_from_attribs 
57                                        "can_fail" 
58                                        "primOpCanFail" p_o_specs)
59
60                       "--strictness" 
61                          -> putStr (gen_switch_from_attribs 
62                                        "strictness" 
63                                        "primOpStrictness" p_o_specs)
64
65                       "--usage" 
66                          -> putStr (gen_switch_from_attribs 
67                                        "usage" 
68                                        "primOpUsg" p_o_specs)
69
70                       "--primop-primop-info" 
71                          -> putStr (gen_primop_info p_o_specs)
72
73                       "--primop-tag" 
74                          -> putStr (gen_primop_tag p_o_specs)
75
76                       "--primop-list" 
77                          -> putStr (gen_primop_list p_o_specs)
78
79                       "--make-haskell-wrappers" 
80                          -> putStr (gen_wrappers p_o_specs)
81                         
82                       "--make-haskell-source" 
83                          -> putStr (gen_hs_source p_o_specs)
84
85                       "--make-latex-doc"
86                          -> putStr (gen_latex_doc p_o_specs)
87
88                       _ -> error "Should not happen, known_args out of sync?"
89                    )
90
91 known_args :: [String]
92 known_args 
93    = [ "--data-decl",
94        "--has-side-effects",
95        "--out-of-line",
96        "--commutable",
97        "--needs-wrapper",
98        "--can-fail",
99        "--strictness",
100        "--usage",
101        "--primop-primop-info",
102        "--primop-tag",
103        "--primop-list",
104        "--make-haskell-wrappers",
105        "--make-haskell-source",
106        "--make-latex-doc"
107      ]
108
109 ------------------------------------------------------------------
110 -- Code generators -----------------------------------------------
111 ------------------------------------------------------------------
112
113 gen_hs_source :: Info -> String
114 gen_hs_source (Info defaults entries) =
115            "-----------------------------------------------------------------------------\n"
116         ++ "-- |\n"
117         ++ "-- Module      :  GHC.Arr\n"
118         ++ "-- \n"
119         ++ "-- Maintainer  :  cvs-ghc@haskell.org\n"
120         ++ "-- Stability   :  internal\n"
121         ++ "-- Portability :  non-portable (GHC extensions)\n"
122         ++ "--\n"
123         ++ "-- GHC\'s primitive types and operations.\n"
124         ++ "--\n" 
125         ++ "-----------------------------------------------------------------------------\n"
126         ++ "module GHC.Prim (\n"
127         ++ unlines (map (("\t" ++) . hdr) entries)
128         ++ ") where\n\n{-\n"
129         ++ unlines (map opt defaults) ++ "-}\n"
130         ++ unlines (map ent entries) ++ "\n\n\n"
131      where opt (OptionFalse n)    = n ++ " = False"
132            opt (OptionTrue n)     = n ++ " = True"
133            opt (OptionString n v) = n ++ " = { " ++ v ++ "}"
134
135            hdr s@(Section {})                    = sec s
136            hdr (PrimOpSpec { name = n })         = wrapOp n ++ ","
137            hdr (PseudoOpSpec { name = n })       = wrapOp n ++ ","
138            hdr (PrimTypeSpec { ty = TyApp n _ }) = wrapTy n ++ ","
139            hdr (PrimTypeSpec {})                 = error "Illegal type spec"
140
141            ent   (Section {})      = ""
142            ent o@(PrimOpSpec {})   = spec o
143            ent o@(PrimTypeSpec {}) = spec o
144            ent o@(PseudoOpSpec {}) = spec o
145
146            sec s = "\n-- * " ++ escape (title s) ++ "\n"
147                         ++ (unlines $ map ("-- " ++ ) $ lines $ unlatex $ escape $ "|" ++ desc s) ++ "\n"
148
149            spec o = comm ++ decl
150              where decl = case o of
151                         PrimOpSpec { name = n, ty = t }   -> wrapOp n ++ " :: " ++ pty t
152                         PseudoOpSpec { name = n, ty = t } -> wrapOp n ++ " :: " ++ pty t
153                         PrimTypeSpec { ty = t }   -> "data " ++ pty t
154                         Section { } -> ""
155
156                    comm = case (desc o) of
157                         [] -> ""
158                         d -> "\n" ++ (unlines $ map ("-- " ++ ) $ lines $ unlatex $ escape $ "|" ++ d)
159
160                    pty (TyF t1 t2) = pbty t1 ++ " -> " ++ pty t2
161                    pty t           = pbty t
162
163                    pbty (TyApp tc ts) = tc ++ (concat (map (' ':) (map paty ts)))
164                    pbty (TyUTup ts)   = "(# " ++ (concat (intersperse "," (map pty ts))) ++ " #)"
165                    pbty t             = paty t
166
167                    paty (TyVar tv)      = tv
168                    paty t               = "(" ++ pty t ++ ")"
169
170            wrapOp nm | isAlpha (head nm) = nm
171                      | otherwise         = "(" ++ nm ++ ")"
172            wrapTy nm | isAlpha (head nm) = nm
173                      | otherwise         = "(" ++ nm ++ ")"
174            unlatex s = case s of
175                 '\\':'t':'e':'x':'t':'t':'t':'{':cs -> markup "@" "@" cs
176                 '{':'\\':'t':'t':cs -> markup "@" "@" cs
177                 '{':'\\':'i':'t':cs -> markup "/" "/" cs
178                 c : cs -> c : unlatex cs
179                 [] -> []
180            markup s t xs = s ++ mk (dropWhile isSpace xs)
181                 where mk ""        = t
182                       mk ('\n':cs) = ' ' : mk cs
183                       mk ('}':cs)  = t ++ unlatex cs
184                       mk (c:cs)    = c : mk cs
185            escape = concatMap (\c -> if c `elem` special then '\\':c:[] else c:[])
186                 where special = "/'`\"@<"
187
188 gen_latex_doc :: Info -> String
189 gen_latex_doc (Info defaults entries)
190    = "\\primopdefaults{" 
191          ++ mk_options defaults
192          ++ "}\n"
193      ++ (concat (map mk_entry entries))
194      where mk_entry (PrimOpSpec {cons=constr,name=n,ty=t,cat=c,desc=d,opts=o}) =
195                  "\\primopdesc{" 
196                  ++ latex_encode constr ++ "}{"
197                  ++ latex_encode n ++ "}{"
198                  ++ latex_encode (zencode n) ++ "}{"
199                  ++ latex_encode (show c) ++ "}{"
200                  ++ latex_encode (mk_source_ty t) ++ "}{"
201                  ++ latex_encode (mk_core_ty t) ++ "}{"
202                  ++ d ++ "}{"
203                  ++ mk_options o
204                  ++ "}\n"
205            mk_entry (Section {title=ti,desc=d}) =
206                  "\\primopsection{" 
207                  ++ latex_encode ti ++ "}{"
208                  ++ d ++ "}\n"
209            mk_entry (PrimTypeSpec {ty=t,desc=d,opts=o}) =
210                  "\\primtypespec{"
211                  ++ latex_encode (mk_source_ty t) ++ "}{"
212                  ++ latex_encode (mk_core_ty t) ++ "}{"
213                  ++ d ++ "}{"
214                  ++ mk_options o
215                  ++ "}\n"
216            mk_entry (PseudoOpSpec {name=n,ty=t,desc=d,opts=o}) =
217                  "\\pseudoopspec{"
218                  ++ latex_encode (zencode n) ++ "}{"
219                  ++ latex_encode (mk_source_ty t) ++ "}{"
220                  ++ latex_encode (mk_core_ty t) ++ "}{"
221                  ++ d ++ "}{"
222                  ++ mk_options o
223                  ++ "}\n"
224            mk_source_ty typ = pty typ
225              where pty (TyF t1 t2) = pbty t1 ++ " -> " ++ pty t2
226                    pty t = pbty t
227                    pbty (TyApp tc ts) = tc ++ (concat (map (' ':) (map paty ts)))
228                    pbty (TyUTup ts) = "(# " ++ (concat (intersperse "," (map pty ts))) ++ " #)"
229                    pbty t = paty t
230                    paty (TyVar tv) = tv
231                    paty t = "(" ++ pty t ++ ")"
232            
233            mk_core_ty typ = foralls ++ (pty typ)
234              where pty (TyF t1 t2) = pbty t1 ++ " -> " ++ pty t2
235                    pty t = pbty t
236                    pbty (TyApp tc ts) = (zencode tc) ++ (concat (map (' ':) (map paty ts)))
237                    pbty (TyUTup ts) = (zencode (utuplenm (length ts))) ++ (concat ((map (' ':) (map paty ts))))
238                    pbty t = paty t
239                    paty (TyVar tv) = zencode tv
240                    paty (TyApp tc []) = zencode tc
241                    paty t = "(" ++ pty t ++ ")"
242                    utuplenm 1 = "(# #)"
243                    utuplenm n = "(#" ++ (replicate (n-1) ',') ++ "#)"
244                    foralls = if tvars == [] then "" else "%forall " ++ (tbinds tvars)
245                    tvars = tvars_of typ
246                    tbinds [] = ". " 
247                    tbinds ("o":tbs) = "(o::?) " ++ (tbinds tbs)
248                    tbinds (tv:tbs) = tv ++ " " ++ (tbinds tbs)
249            tvars_of (TyF t1 t2) = tvars_of t1 `union` tvars_of t2
250            tvars_of (TyApp _ ts) = foldl union [] (map tvars_of ts)
251            tvars_of (TyUTup ts) = foldr union [] (map tvars_of ts)
252            tvars_of (TyVar tv) = [tv]
253            
254            mk_options o =
255              "\\primoptions{"
256               ++ mk_has_side_effects o ++ "}{"
257               ++ mk_out_of_line o ++ "}{"
258               ++ mk_commutable o ++ "}{"
259               ++ mk_needs_wrapper o ++ "}{"
260               ++ mk_can_fail o ++ "}{"
261               ++ latex_encode (mk_strictness o) ++ "}{"
262               ++ latex_encode (mk_usage o)
263               ++ "}"
264
265            mk_has_side_effects o = mk_bool_opt o "has_side_effects" "Has side effects." "Has no side effects."
266            mk_out_of_line o = mk_bool_opt o "out_of_line" "Implemented out of line." "Implemented in line."
267            mk_commutable o = mk_bool_opt o "commutable" "Commutable." "Not commutable."
268            mk_needs_wrapper o = mk_bool_opt o "needs_wrapper" "Needs wrapper." "Needs no wrapper."
269            mk_can_fail o = mk_bool_opt o "can_fail" "Can fail." "Cannot fail."
270
271            mk_bool_opt o opt_name if_true if_false =
272              case lookup_attrib opt_name o of
273                Just (OptionTrue _) -> if_true
274                Just (OptionFalse _) -> if_false
275                Just (OptionString _ _) -> error "String value for boolean option"
276                Nothing -> ""
277            
278            mk_strictness o = 
279              case lookup_attrib "strictness" o of
280                Just (OptionString _ s) -> s  -- for now
281                Just _ -> error "Boolean value for strictness"
282                Nothing -> "" 
283
284            mk_usage o =
285              case lookup_attrib "usage" o of
286                Just (OptionString _ s) -> s  -- for now
287                Just _ -> error "Boolean value for usage"
288                Nothing -> "" 
289
290            zencode xs =
291              case maybe_tuple xs of
292                 Just n  -> n            -- Tuples go to Z2T etc
293                 Nothing -> concat (map encode_ch xs)
294              where
295                maybe_tuple "(# #)" = Just("Z1H")
296                maybe_tuple ('(' : '#' : cs) = case count_commas (0::Int) cs of
297                                                 (n, '#' : ')' : _) -> Just ('Z' : shows (n+1) "H")
298                                                 _                  -> Nothing
299                maybe_tuple "()" = Just("Z0T")
300                maybe_tuple ('(' : cs)       = case count_commas (0::Int) cs of
301                                                 (n, ')' : _) -> Just ('Z' : shows (n+1) "T")
302                                                 _            -> Nothing
303                maybe_tuple _                 = Nothing
304                
305                count_commas :: Int -> String -> (Int, String)
306                count_commas n (',' : cs) = count_commas (n+1) cs
307                count_commas n cs          = (n,cs)
308                
309                unencodedChar :: Char -> Bool    -- True for chars that don't need encoding
310                unencodedChar 'Z' = False
311                unencodedChar 'z' = False
312                unencodedChar c   = isAlphaNum c
313                
314                encode_ch :: Char -> String
315                encode_ch c | unencodedChar c = [c]      -- Common case first
316                
317                -- Constructors
318                encode_ch '('  = "ZL"    -- Needed for things like (,), and (->)
319                encode_ch ')'  = "ZR"    -- For symmetry with (
320                encode_ch '['  = "ZM"
321                encode_ch ']'  = "ZN"
322                encode_ch ':'  = "ZC"
323                encode_ch 'Z'  = "ZZ"
324                
325                -- Variables
326                encode_ch 'z'  = "zz"
327                encode_ch '&'  = "za"
328                encode_ch '|'  = "zb"
329                encode_ch '^'  = "zc"
330                encode_ch '$'  = "zd"
331                encode_ch '='  = "ze"
332                encode_ch '>'  = "zg"
333                encode_ch '#'  = "zh"
334                encode_ch '.'  = "zi"
335                encode_ch '<'  = "zl"
336                encode_ch '-'  = "zm"
337                encode_ch '!'  = "zn"
338                encode_ch '+'  = "zp"
339                encode_ch '\'' = "zq"
340                encode_ch '\\' = "zr"
341                encode_ch '/'  = "zs"
342                encode_ch '*'  = "zt"
343                encode_ch '_'  = "zu"
344                encode_ch '%'  = "zv"
345                encode_ch c    = 'z' : shows (ord c) "U"
346                        
347            latex_encode [] = []
348            latex_encode (c:cs) | c `elem` "#$%&_^{}" = "\\" ++ c:(latex_encode cs)
349            latex_encode ('~':cs) = "\\verb!~!" ++ (latex_encode cs)
350            latex_encode ('\\':cs) = "$\\backslash$" ++ (latex_encode cs)
351            latex_encode (c:cs) = c:(latex_encode cs)
352
353 gen_wrappers :: Info -> String
354 gen_wrappers (Info _ entries)
355    = "{-# OPTIONS -fno-implicit-prelude #-}\n" 
356         -- Dependencies on Prelude must be explicit in libraries/base, but we
357         -- don't need the Prelude here so we add -fno-implicit-prelude.
358      ++ "module GHC.PrimopWrappers where\n" 
359      ++ "import qualified GHC.Prim\n" 
360      ++ unlines (map f (filter (not.dodgy) (filter is_primop entries)))
361      where
362         f spec = let args = map (\n -> "a" ++ show n) [1 .. arity (ty spec)]
363                      src_name = wrap (name spec)
364                  in "{-# NOINLINE " ++ src_name ++ " #-}\n" ++ 
365                     src_name ++ " " ++ unwords args 
366                      ++ " = (GHC.Prim." ++ name spec ++ ") " ++ unwords args
367         wrap nm | isLower (head nm) = nm
368                 | otherwise = "(" ++ nm ++ ")"
369
370         dodgy spec
371            = name spec `elem` 
372              [-- C code generator can't handle these
373               "seq#", 
374               "tagToEnum#",
375               -- not interested in parallel support
376               "par#", "parGlobal#", "parLocal#", "parAt#", 
377               "parAtAbs#", "parAtRel#", "parAtForNow#" 
378              ]
379
380 gen_primop_list :: Info -> String
381 gen_primop_list (Info _ entries)
382    = unlines (
383         [      "   [" ++ cons first       ]
384         ++
385         map (\p -> "   , " ++ cons p) rest
386         ++ 
387         [     "   ]"     ]
388      ) where (first:rest) = filter is_primop entries
389
390 gen_primop_tag :: Info -> String
391 gen_primop_tag (Info _ entries)
392    = unlines (max_def : zipWith f primop_entries [1 :: Int ..])
393      where
394         primop_entries = filter is_primop entries
395         f i n = "tagOf_PrimOp " ++ cons i 
396                 ++ " = _ILIT(" ++ show n ++ ") :: FastInt"
397         max_def = "maxPrimOpTag = " ++ show (length primop_entries) ++ " :: Int"
398
399 gen_data_decl :: Info -> String
400 gen_data_decl (Info _ entries)
401    = let conss = map cons (filter is_primop entries)
402      in  "data PrimOp\n   = " ++ head conss ++ "\n"
403          ++ unlines (map ("   | "++) (tail conss))
404
405 gen_switch_from_attribs :: String -> String -> Info -> String
406 gen_switch_from_attribs attrib_name fn_name (Info defaults entries)
407    = let defv = lookup_attrib attrib_name defaults
408          alternatives = catMaybes (map mkAlt (filter is_primop entries))
409
410          getAltRhs (OptionFalse _)    = "False"
411          getAltRhs (OptionTrue _)     = "True"
412          getAltRhs (OptionString _ s) = s
413
414          mkAlt po
415             = case lookup_attrib attrib_name (opts po) of
416                  Nothing -> Nothing
417                  Just xx -> Just (fn_name ++ " " ++ cons po ++ " = " ++ getAltRhs xx)
418
419      in
420          case defv of
421             Nothing -> error ("gen_switch_from: " ++ attrib_name)
422             Just xx 
423                -> unlines alternatives
424                   ++ fn_name ++ " other = " ++ getAltRhs xx ++ "\n"
425
426 ------------------------------------------------------------------
427 -- Create PrimOpInfo text from PrimOpSpecs -----------------------
428 ------------------------------------------------------------------
429
430 gen_primop_info :: Info -> String
431 gen_primop_info (Info _ entries)
432    = unlines (map mkPOItext (filter is_primop entries))
433
434 mkPOItext :: Entry -> String
435 mkPOItext i = mkPOI_LHS_text i ++ mkPOI_RHS_text i
436
437 mkPOI_LHS_text :: Entry -> String
438 mkPOI_LHS_text i
439    = "primOpInfo " ++ cons i ++ " = "
440
441 mkPOI_RHS_text :: Entry -> String
442 mkPOI_RHS_text i
443    = case cat i of
444         Compare 
445            -> case ty i of
446                  TyF t1 (TyF _ _) 
447                     -> "mkCompare " ++ sl_name i ++ ppType t1
448                  _ -> error "Type error in comparison op"
449         Monadic
450            -> case ty i of
451                  TyF t1 _
452                     -> "mkMonadic " ++ sl_name i ++ ppType t1
453                  _ -> error "Type error in monadic op"
454         Dyadic
455            -> case ty i of
456                  TyF t1 (TyF _ _)
457                     -> "mkDyadic " ++ sl_name i ++ ppType t1
458                  _ -> error "Type error in dyadic op"
459         GenPrimOp
460            -> let (argTys, resTy) = flatTys (ty i)
461                   tvs = nub (tvsIn (ty i))
462               in
463                   "mkGenPrimOp " ++ sl_name i ++ " " 
464                       ++ listify (map ppTyVar tvs) ++ " "
465                       ++ listify (map ppType argTys) ++ " "
466                       ++ "(" ++ ppType resTy ++ ")"
467
468 sl_name :: Entry -> String
469 sl_name i = "FSLIT(\"" ++ name i ++ "\") "
470
471 ppTyVar :: String -> String
472 ppTyVar "a" = "alphaTyVar"
473 ppTyVar "b" = "betaTyVar"
474 ppTyVar "c" = "gammaTyVar"
475 ppTyVar "s" = "deltaTyVar"
476 ppTyVar "o" = "openAlphaTyVar"
477 ppTyVar _   = error "Unknown type var"
478
479 ppType :: Ty -> String
480 ppType (TyApp "Bool"        []) = "boolTy"
481
482 ppType (TyApp "Int#"        []) = "intPrimTy"
483 ppType (TyApp "Int32#"      []) = "int32PrimTy"
484 ppType (TyApp "Int64#"      []) = "int64PrimTy"
485 ppType (TyApp "Char#"       []) = "charPrimTy"
486 ppType (TyApp "Word#"       []) = "wordPrimTy"
487 ppType (TyApp "Word32#"     []) = "word32PrimTy"
488 ppType (TyApp "Word64#"     []) = "word64PrimTy"
489 ppType (TyApp "Addr#"       []) = "addrPrimTy"
490 ppType (TyApp "Float#"      []) = "floatPrimTy"
491 ppType (TyApp "Double#"     []) = "doublePrimTy"
492 ppType (TyApp "ByteArr#"    []) = "byteArrayPrimTy"
493 ppType (TyApp "RealWorld"   []) = "realWorldTy"
494 ppType (TyApp "ThreadId#"   []) = "threadIdPrimTy"
495 ppType (TyApp "ForeignObj#" []) = "foreignObjPrimTy"
496 ppType (TyApp "BCO#"        []) = "bcoPrimTy"
497 ppType (TyApp "()"          []) = "unitTy"      -- unitTy is TysWiredIn's name for ()
498
499 ppType (TyVar "a")               = "alphaTy"
500 ppType (TyVar "b")               = "betaTy"
501 ppType (TyVar "c")               = "gammaTy"
502 ppType (TyVar "s")               = "deltaTy"
503 ppType (TyVar "o")               = "openAlphaTy"
504 ppType (TyApp "State#" [x])      = "mkStatePrimTy " ++ ppType x
505 ppType (TyApp "MutVar#" [x,y])   = "mkMutVarPrimTy " ++ ppType x 
506                                    ++ " " ++ ppType y
507 ppType (TyApp "MutArr#" [x,y])   = "mkMutableArrayPrimTy " ++ ppType x 
508                                    ++ " " ++ ppType y
509
510 ppType (TyApp "MutByteArr#" [x]) = "mkMutableByteArrayPrimTy " 
511                                    ++ ppType x
512
513 ppType (TyApp "Array#" [x])      = "mkArrayPrimTy " ++ ppType x
514
515
516 ppType (TyApp "Weak#"  [x])      = "mkWeakPrimTy " ++ ppType x
517 ppType (TyApp "StablePtr#"  [x])      = "mkStablePtrPrimTy " ++ ppType x
518 ppType (TyApp "StableName#"  [x])      = "mkStableNamePrimTy " ++ ppType x
519
520 ppType (TyApp "MVar#" [x,y])     = "mkMVarPrimTy " ++ ppType x 
521                                    ++ " " ++ ppType y
522 ppType (TyApp "TVar#" [x,y])     = "mkTVarPrimTy " ++ ppType x 
523                                    ++ " " ++ ppType y
524 ppType (TyUTup ts)               = "(mkTupleTy Unboxed " ++ show (length ts)
525                                    ++ " "
526                                    ++ listify (map ppType ts) ++ ")"
527
528 ppType (TyF s d) = "(mkFunTy (" ++ ppType s ++ ") (" ++ ppType d ++ "))"
529
530 ppType other
531    = error ("ppType: can't handle: " ++ show other ++ "\n")
532
533 listify :: [String] -> String
534 listify ss = "[" ++ concat (intersperse ", " ss) ++ "]"
535
536 flatTys :: Ty -> ([Ty],Ty)
537 flatTys (TyF t1 t2) = case flatTys t2 of (ts,t) -> (t1:ts,t)
538 flatTys other       = ([],other)
539
540 tvsIn :: Ty -> [TyVar]
541 tvsIn (TyF t1 t2)    = tvsIn t1 ++ tvsIn t2
542 tvsIn (TyApp _ tys)  = concatMap tvsIn tys
543 tvsIn (TyVar tv)     = [tv]
544 tvsIn (TyUTup tys)   = concatMap tvsIn tys
545
546 arity :: Ty -> Int
547 arity = length . fst . flatTys
548