3dec80feda126a9b9ff5217689b037de9d275477
[coq-hetmet.git] / src / Extraction-prefix.hs
1 {-# OPTIONS_GHC -fno-warn-unused-binds  #-}
2 module CoqPass ( coqPassCoreToString, coqPassCoreToCore )
3 where
4 import qualified Unique
5 import qualified UniqSupply
6 import qualified MkCore
7 import qualified TysWiredIn
8 import qualified TysPrim
9 import qualified Outputable
10 import qualified PrelNames
11 import qualified OccName
12 import qualified Name
13 import qualified Literal
14 import qualified Type
15 import qualified TypeRep
16 import qualified DataCon
17 import qualified TyCon
18 import qualified Coercion
19 import qualified Var
20 import qualified Id
21 import qualified FastString
22 import qualified BasicTypes
23 import qualified DataCon
24 import qualified CoreSyn
25 import qualified CoreUtils
26 import qualified Class
27 import qualified Data.Char 
28 import qualified Data.List
29 import qualified Data.Ord
30 import qualified Data.Typeable
31 import Data.Bits ((.&.), shiftL, (.|.))
32 import Prelude ( (++), (+), (==), Show, show, Char, (.) )
33 import qualified Prelude
34 import qualified GHC.Base
35
36 -- used for extracting strings
37 bin2ascii =
38   (\ b0 b1 b2 b3 b4 b5 b6 b7 ->
39      let f b i = if b then 1 `shiftL` i else 0
40      in Data.Char.chr (f b0 0 .|. f b1 1 .|. f b2 2 .|. f b3 3 .|. f b4 4 .|. f b5 5 .|. f b6 6 .|. f b7 7))
41
42 getTyConTyVars :: TyCon.TyCon -> [Var.TyVar]
43 getTyConTyVars tc =
44   if TyCon.isFunTyCon tc
45   then []
46   else if TyCon.isPrimTyCon tc
47        then []
48        else TyCon.tyConTyVars tc
49
50 sortAlts :: [(CoreSyn.AltCon,[Var.Var],CoreSyn.Expr Var.Var)] -> [(CoreSyn.AltCon,[Var.Var],CoreSyn.Expr Var.Var)]
51 sortAlts x = Data.List.sortBy (\(a1,_,_) -> \(a2,_,_) -> Data.Ord.compare a1 a2) x
52
53 -- to do: this could be moved into Coq
54 coreVarToWeakVar :: Var.Var -> WeakVar
55 coreVarToWeakVar v | Id.isId     v = WExprVar (WeakExprVar v (errOrFail (coreTypeToWeakType (Var.varType v))))
56  where
57   errOrFail (OK x)    = x
58   errOrFail (Error s) = Prelude.error s
59 coreVarToWeakVar v | Var.isTyVar v = WTypeVar (WeakTypeVar v (coreKindToKind (Var.varType v)))
60 coreVarToWeakVar v | Var.isCoVar v = WCoerVar (WeakCoerVar v (Prelude.error "FIXME") 
61                                                              (Prelude.error "FIXME") (Prelude.error "FIXME"))
62 coreVarToWeakVar _                 =
63    Prelude.error "Var.Var that is neither an expression variable, type variable, nor coercion variable!"
64
65 tyConOrTyFun :: TyCon.TyCon -> Prelude.Either TyCon.TyCon TyCon.TyCon
66 tyConOrTyFun n =
67    if n == TysPrim.statePrimTyCon     -- special-purpose hack treat State# as a type family since it has kind *->* but no tyvars
68    then Prelude.Right n
69    else if TyCon.isFamInstTyCon n
70         then Prelude.Right n
71         else Prelude.Left n
72
73 nat2int :: Nat -> Prelude.Int
74 nat2int O     = 0
75 nat2int (S x) = 1 + (nat2int x)
76
77 natToString :: Nat -> Prelude.String
78 natToString n = show (nat2int n)
79
80 -- only needs to sanitize characters which might appear in Haskell identifiers
81 sanitizeForLatex :: Prelude.String -> Prelude.String
82 sanitizeForLatex []      = []
83 sanitizeForLatex ('_':x) = "\\_"++(sanitizeForLatex x)
84 sanitizeForLatex ('$':x) = "\\$"++(sanitizeForLatex x)
85 sanitizeForLatex ('#':x) = "\\#"++(sanitizeForLatex x)
86 sanitizeForLatex (c:x)   = c:(sanitizeForLatex x)
87
88 kindToCoreKind :: Kind -> TypeRep.Kind
89 kindToCoreKind KindStar          = TypeRep.liftedTypeKind
90 kindToCoreKind (KindArrow k1 k2) = Coercion.mkArrowKind (kindToCoreKind k1) (kindToCoreKind k2)
91 kindToCoreKind _                 = Prelude.error "kindToCoreKind does not know how to handle that"
92
93 coreKindToKind :: TypeRep.Kind -> Kind
94 coreKindToKind k =
95   case Coercion.splitKindFunTy_maybe k of
96       Prelude.Just (k1,k2) -> KindArrow (coreKindToKind k1) (coreKindToKind k2)
97       Prelude.Nothing -> 
98                       if (Coercion.isLiftedTypeKind k)   then KindStar
99                  else if (Coercion.isUnliftedTypeKind k) then KindStar
100                  else if (Coercion.isArgTypeKind k)      then KindStar
101                  else if (Coercion.isUbxTupleKind k)     then KindStar
102                  else if (Coercion.isOpenTypeKind k)     then KindStar
103 --                 else if (Coercion.isUnliftedTypeKind k) then KindUnliftedType
104 --                 else if (Coercion.isOpenTypeKind k)     then KindOpenType
105 --                 else if (Coercion.isArgTypeKind k)      then KindArgType
106 --                 else if (Coercion.isUbxTupleKind k)     then KindUnboxedTuple
107                  else if (Coercion.isTySuperKind k)      then Prelude.error "coreKindToKind got the kind-of-the-kind-of-types"
108                  else if (Coercion.isCoSuperKind k)      then Prelude.error "coreKindToKind got the kind-of-the-kind-of-coercions"
109                  else                                         Prelude.error ((Prelude.++) "coreKindToKind got an unknown kind: "
110                                                                                (Outputable.showSDoc (Outputable.ppr k)))
111 outputableToString :: Outputable.Outputable a => a -> Prelude.String
112 outputableToString = (\x -> Outputable.showSDoc (Outputable.ppr x))
113
114 -- I'm leaving this here (commented out) in case I ever need it again)
115 --checkTypeEquality :: Type.Type -> Type.Type -> Prelude.Bool
116 --checkTypeEquality t1 t2 = Type.tcEqType (Type.expandTypeSynonyms t1) (Type.expandTypeSynonyms t2)
117
118 coreViewDeep :: Type.Type -> Type.Type
119 coreViewDeep t =
120     case t of
121       TypeRep.TyVarTy tv       -> TypeRep.TyVarTy tv
122       TypeRep.FunTy arg res    -> TypeRep.FunTy (coreViewDeep arg) (coreViewDeep res)
123       TypeRep.AppTy fun arg    -> TypeRep.AppTy (coreViewDeep fun) (coreViewDeep arg)
124       TypeRep.ForAllTy fun arg -> TypeRep.ForAllTy fun (coreViewDeep arg)
125       TypeRep.TyConApp tc tys  -> let t' = TypeRep.TyConApp tc (Prelude.map coreViewDeep tys)
126                         in case Type.coreView t' of
127                                Prelude.Nothing     -> t'
128                                Prelude.Just    t'' -> t''
129       TypeRep.PredTy p         -> case Type.coreView t of
130                                Prelude.Nothing     -> TypeRep.PredTy p
131                                Prelude.Just    t'  -> t'
132
133 -- FIXME: cotycon applications may be oversaturated
134 coreCoercionToWeakCoercion :: Type.Type -> WeakCoercion
135 coreCoercionToWeakCoercion c =
136  case c of
137   TypeRep.TyVarTy  v     -> WCoVar (WeakCoerVar v (Prelude.error "FIXME") (Prelude.error "FIXME") (Prelude.error "FIXME"))
138   TypeRep.AppTy    t1 t2 -> WCoApp   (coreCoercionToWeakCoercion t1) (coreCoercionToWeakCoercion t2)
139   TypeRep.TyConApp tc t  ->
140       case TyCon.isCoercionTyCon_maybe tc of
141         Prelude.Nothing -> Prelude.error ((Prelude.++) "coreCoercionToWeakCoercion got " (outputableToString c))
142         Prelude.Just (_, ctcd) ->
143             case (ctcd,t) of
144               (TyCon.CoTrans , [x,y]     ) -> WCoComp   (coreCoercionToWeakCoercion x) (coreCoercionToWeakCoercion y)
145               (TyCon.CoSym   , [x]       ) -> WCoSym    (coreCoercionToWeakCoercion x)
146               (TyCon.CoLeft  , [x]       ) -> WCoLeft   (coreCoercionToWeakCoercion x)
147               (TyCon.CoRight , [x]       ) -> WCoLeft   (coreCoercionToWeakCoercion x)
148 -- FIXME      (TyCon.CoUnsafe, [t1, t2 ] ) -> WCoUnsafe (coreTypeToWeakType t1) (coreTypeToWeakType t2)
149               (TyCon.CoTrans , []        ) -> Prelude.error "CoTrans is not in post-publication-appendix SystemFC1"
150               (TyCon.CoCsel1 , []        ) -> Prelude.error "CoCsel1 is not in post-publication-appendix SystemFC1"
151               (TyCon.CoCsel2 , []        ) -> Prelude.error "CoCsel2 is not in post-publication-appendix SystemFC1"
152               (TyCon.CoCselR , []        ) -> Prelude.error "CoCselR is not in post-publication-appendix SystemFC1"
153               (TyCon.CoInst  , []        ) -> Prelude.error "CoInst  is not in post-publication-appendix SystemFC1"
154 --              (TyCon.CoAxiom , []        ) -> Prelude.error "CoAxiom is not yet implemented (FIXME)"
155               _ -> Prelude.error ((Prelude.++) "coreCoercionToWeakCoercion got " (outputableToString c))
156   _ -> Prelude.error ((Prelude.++) "coreCoercionToWeakCoercion got " (outputableToString c))
157 --  TypeRep.ForAllTy v t   -> WCoAll  (Prelude.error "FIXME") (coreTypeToWeakType t)
158 -- FIXME   x y                                  -> WCoAppT    (coreCoercionToWeakCoercion x) (coreCoercionToWeakType y)
159 --  CoreSyn.Type t                            -> WCoType   (coreTypeToWeakType t)
160
161 {-
162 weakCoercionToCoreCoercion :: CoreCoercion -> Type.Type
163 | WCoVar     (weakCoerVar _ _ t1 t2) => (t1,t2)
164 | WCoType    t                       => Prelude_error "FIXME WCoType"
165 | WCoApp     c1 c2                   => Prelude_error "FIXME WCoApp"
166 | WCoAppT    c t                     => Prelude_error "FIXME WCoAppT"
167 | WCoAll     k f                     => Prelude_error "FIXME WCoAll"
168 | WCoSym     c                       => let (t2,t1) := weakCoercionTypes c in (t1,t2)
169 | WCoComp    c1 c2                   => Prelude_error "FIXME WCoComp"
170 | WCoLeft    c                       => Prelude_error "FIXME WCoLeft"
171 | WCoRight   c                       => Prelude_error "FIXME WCoRight"
172 | WCoUnsafe  t1 t2                   => (t1,t2)
173 -}