1 {-# OPTIONS -XModalTypes -XMultiParamTypeClasses #-}
2 module GHC.HetMet.CodeTypes (
6 GuestIntegerLiteral, guestIntegerLiteral,
7 GuestStringLiteral, guestStringLiteral,
8 GuestCharLiteral, guestCharLiteral,
9 GuestLanguageMult, <[ (*) ]>,
10 GuestLanguageAdd, <[ (+) ]>,
11 GuestLanguageSub, <[ (-) ]>, <[ negate ]>,
12 GuestLanguageFromInteger, <[ fromInteger ]>,
13 GuestLanguageBool, <[ (||) ]>, <[ (&&) ]>, <[ true ]>, <[ false ]>, <[ ifThenElse ]>
15 import Prelude (Integer, String, Char, Bool, error)
16 import GHC.HetMet.GArrow
18 hetmet_brak :: forall c. forall a. a -> <[a]>@c
19 hetmet_brak = Prelude.error "hetmet_brak should never be evaluated; did you forget to compile with -fcoqpass?"
21 hetmet_esc :: forall c. forall a. <[a]>@c -> a
22 hetmet_esc = Prelude.error "hetmet_esc should never be evaluated; did you forget to compile with -fcoqpass?"
24 hetmet_csp :: forall c. forall a. a -> a
25 hetmet_csp = Prelude.error "hetmet_csp should never be evaluated; did you forget to compile with -fcoqpass?"
28 -- After the flattening pass the argument and result types of this
29 -- function are identical (for any instantiation), so the flattener
30 -- simply turns it into the identity function. Its only purpose is to
31 -- act as a "safe type cast" during pre-flattening
32 -- type-inference/checking:
44 hetmet_flatten _ = Prelude.error "hetmet_flatten should never be evaluated; did you forget to compile with -fcoqpass?"
47 class GuestIntegerLiteral c where
48 guestIntegerLiteral :: Integer -> <[ Integer ]>@c
50 class GuestStringLiteral c where
51 guestStringLiteral :: String -> <[ String ]>@c
53 class GuestCharLiteral c where
54 guestCharLiteral :: Char -> <[ Char ]>@c
56 -- Note that stringwise-identical identifiers at different syntactic
57 -- depths are different identifiers; for this reason the operators
58 -- below can have a different type at syntactical depth 1 than at
59 -- syntactical depth 0.
61 class GuestLanguageMult c t where
62 <[ (*) ]> :: <[ t -> t -> t ]>@c
64 class GuestLanguageAdd c t where
65 <[ (+) ]> :: <[ t -> t -> t ]>@c
67 class GuestLanguageSub c t where
68 <[ (-) ]> :: <[ t -> t -> t ]>@c
69 <[ negate ]> :: <[ t -> t ]>@c -- used for unary (-)
71 class GuestLanguageFromInteger c t where
72 <[ fromInteger ]> :: <[ Integer -> t ]>@c
74 class GuestLanguageBool c where
75 <[ (||) ]> :: <[ Bool -> Bool -> Bool ]>@c
76 <[ (&&) ]> :: <[ Bool -> Bool -> Bool ]>@c
77 <[ true ]> :: <[ Bool ]>@c
78 <[ false ]> :: <[ Bool ]>@c
79 <[ ifThenElse ]> :: <[ Bool -> t -> t -> t ]>@c
81 -- For heterogeneous metaprogramming, the meaning of "running" a
82 -- program is fairly ambiguous, and moreover is highly sensitive to
83 -- which subclasses of GuestLanguage the expression assumes it is
84 -- dealing with. For example, in homogeneous metaprogramming, "run"
87 -- ga_run :: forall a. (forall c. <[a]>@c) -> a
89 -- However, an expression which uses, say (*) at level 1 will never
90 -- be able to be passed to this expression, since
92 -- square :: forall c t. GuestLanguageMult ct => <[t]>@c -> <[t]>@c
93 -- square x = <[ ~~x * ~~x ]>
96 -- So even though this expression is polymorphic in the environment
97 -- classifier "c", it isn't "polymorphic enough". This isn't merely a
98 -- technical obstacle -- the more features you assume the guest
99 -- language has, the more work the "run" implementation is obligated
100 -- to perform, and the type system must track that obligation.
102 -- The upshot is that we can define special-purpose "run" classes such as:
104 -- class GuestLanguageRunMult t where
105 -- ga_runMult :: forall a. (forall c. GuestLanguageMult c t => <[a]>@c) -> a
107 -- Any implementation of this class will need to know how to interpret
108 -- the (*) operator. Unfortunately, to my knowledge, there is no way
109 -- to quantify over type classes in the Haskell type system, which is
110 -- what we would need to define a type-class-indexed version of the
111 -- GuestLanguageRun class; if we could do that, then we would have:
113 -- class GuestLanguageRun ( t ::: * -> TYPECLASS ) where
114 -- ga_runMult :: forall a. (forall c. TYPECLASS c => <[a]>@c) -> a
116 -- It might be possible to pull this of using type families; I need to