Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I specialise with an explicit type application?

Tags:

haskell

With GHC 8.0, I can write an ambiguous function that's overloaded over some type not mentioned in the main part of its type signature, and then call it using an explicit type application:

{-# LANGUAGE ScopedTypeVariables, RankNTypes,
             AllowAmbiguousTypes, TypeApplications, TypeFamilies #-}

showRead :: forall t . (Read t, Show t) => String -> String
showRead x = show (read x :: t)

showReadInt = showRead @Int

I'd like to use a SPECIALIZE pragma to force a specialisation of showRead for Int (my real code has the actual call site in a different module). However, the normal SPECIALIZE syntax is based on writing the main part of the type signature, e.g.:

{-# SPECIALIZE showRead :: String -> String #-}

and in this case that doesn't allow me to specify what t should be, and predictably gives an error about it being ambiguous.

I tried using an equality constraint:

{-# SPECIALISE showRead :: forall t . (Read t, Show t, t ~ Int) => String -> String #-}

but that just gave the error:

• Could not deduce (Read t0) a SPECIALISE pragma for ‘showRead’
  from the context: (Read t, Show t, t ~ Int)
    bound by the type signature for:
               showRead :: (Read t, Show t, t ~ Int) => String -> String
    at foo.hs:4:1-76
  The type variable ‘t0’ is ambiguous

Is there any way I can do this? Of course I could just use a Proxy, but it seems a shame not to use the shiny new way instead.

like image 575
GS - Apologise to Monica Avatar asked Sep 07 '16 21:09

GS - Apologise to Monica


People also ask

What is the best way to implement explicit types?

Comprehensibility – when adding explicit types, you have to think more about implementation logic, because you should know what comes in and what comes out before you implement it. This approach is very similar to the TDD approach. Typing your signature first, before implementing a logic of the function.

What is implicit and explicit typing?

Implicit and explicit typing, also known as strong or weak typing, is the amount of assumption that a programming language will apply to type assertion. Implicit typing can get you out of a pickle, and make programming a little more smooth in many situations.

What is implicit and explicit casting in Java?

Type Casting in Java – Implicit and Explicit Casting. Type Casting in Java is nothing but converting a primitive or interface or class in Java into other type. There is a rule in Java Language that classes or interface which shares the same type hierrachy only can be typecasted.

How to declare two explicit specializations for the primary template?

This example declared two explicit specializations for the primary template (the template which is being specialized) class A. Object x uses the constructor of the primary template. Object y uses the explicit specialization A<>::A (). Object z uses the explicit specialization A<double, 10>::A ().


1 Answers

This isn't supported in GHC 8.0, but is now the subject of a proposal to support it in future GHCs.

like image 166
GS - Apologise to Monica Avatar answered Oct 23 '22 06:10

GS - Apologise to Monica