How to create pattern matching for Int in function with type?

I have a function that accepts Type:

data MyType = IntT | BoolT | OtherT

typeToMyType :: Type -> MyType

How can I write a template that will indicate that I want to match the type Int?

What have i tried?

1

[t| GHC.Types.Int |]will create a type value Q Type, but despite the frequent fights, I could not get to merge with the template.

I tried to use it in the guard:

| $( do tI <- [t| GHC.Types.Int |]; [| t == tI |] ) = ...

But this complains:

No instance for (Language.Haskell.TH.Syntax.Lift Type)
  arising from a use of `Language.Haskell.TH.Syntax.lift'
In the expression: Language.Haskell.TH.Syntax.lift tI
In a stmt of a 'do' block:
  [| t == tI |]
  pending(rn) [tI]
In the expression:
  do { tI <- [t| Int |];
       [| t == tI |]
       pending(rn) [tI] }

2

Perhaps a simpler approach would be as follows:

typeToMyType :: Type -> MyType
typeToMyType (ConT $(''Int)) = NumberT 

But then:

Couldn't match type `Name' with `Q Pat'
Expected type: PatQ
  Actual type: Name
In the expression: ''Int
In the splice: $(''Int)

I have no idea what that means.

Why do I (even) need?

I want to parse the signature of the function to create Q Dec. I used Stringfor the prototype:

    gen ["Int", "Int"] "luaOpMinus" '-
  ======>
    Eval.hs:24:3-38
    luaOpMinus [Number x_a98F, Number x_a98G]
      = (return $ [Number ((-) x_a98F x_a98G)])

, Int -> Int -> Int, ["Int", "Int"] ( ), [LuaValue] -> LuaValue - , Lua . - , reify.

, , , reify :

Prelude Language.Haskell.TH Data.Bits> $(reify 'bit >>= stringE . show)
"ClassOpI Data.Bits.bit (ForallT [PlainTV a_1627400821] [ClassP Data.Bits.Bits [VarT a_1627400821]] (AppT (AppT
ArrowT (ConT GHC.Types.Int)) (VarT a_1627400821))) Data.Bits.Bits (Fixity 9 InfixL)"

(ConT GHC.Types.Int).

+4
1
typeToMyType :: Type -> MyType
typeToMyType (ConT $(''Int)) = NumberT 

$(...), ''Int Name.

typeToMyType :: Type -> MyType
typeToMyType (ConT t) | t == ''Int  = IntT
                      | t == ''Bool = BoolT
typeToMyType _ = OtherT

- GHC ''Int , .

+2

Source: https://habr.com/ru/post/1612070/


All Articles