View type-inhabitants.markdown

This is material to go along with a 2014 Boston Haskell talk.

We are going to look at a series of type signatures in Haskell and explore how parametricity (or lack thereof) lets us constrain what a function is allowed to do.

Let's start with a decidedly non-generic function signature. What are the possible implementations of this function which typecheck?

wrangle :: Int -> Int
View gist:cb780644c86df1e988ee
1 2 3 4 5 6 7 8 9 10
import scalaz._, Scalaz._
import argonaut._, Argonaut._
 
/**
* Main prints:
* -\/(Double: [--\(d)])
* \/-(Dubble(56.45))
* \/-(Pint(56))
* \/-(Pint(56))
*/
View gist:b404b0539f96fbc6ebd2
1 2 3 4 5 6 7 8 9 10
src/L/Compiler.hs:178:54:
Could not deduce (Show o) arising from a use of ‘go’
from the context (FromSExprShow i, FromSExprShow j)
bound by a pattern with constructor
Constrained :: forall (p :: * -> Constraint)
(f :: * -> * -> *)
i
o.
(p i, p o) =>
f i o -> (:=>) p f i o,
View gist:a04fd86d448af69be4d1
1 2 3 4 5 6 7 8 9 10
withCompiler :: Extension -> (forall i. Thrist (FromSExprShow :=> Compiler1) i X86 -> a) -> a
withCompiler ".L1" go = go (mapThrist Constrained l1Compiler)
 
executables/LC.hs:51:39:
No instance for (FromSExpr y) arising from a use of ‘Constrained’
Possible fix:
add (FromSExpr y) to the context of
a type expected by the context:
Compiler1 x y -> (:=>) FromSExprShow Compiler1 x y
or the type signature for
View gist:a03846a49e5238896177
1 2 3 4 5 6 7 8 9 10
src/L/ReplTools.hs:70:39:
Could not deduce (Show y) arising from a use of ‘Constrained’
from the context (FromSExpr i, Show i, Show o)
bound by the type signature for
f :: (FromSExpr i, Show i, Show o) => Compiler i o -> IO ()
at src/L/ReplTools.hs:69:8-61
Possible fix:
add (Show y) to the context of
a type expected by the context:
Compiler1 x y -> (:=>) Show Compiler1 x y
View gist:003f7fb6fb832d6d14ac
1 2 3 4 5 6 7 8 9 10
-- compile from i all the way to o, writing any intermediate files
compileTurtles ::
Show o =>
Thrist (Show :=> Compiler1) i o ->
CompilationOptions ->
FilePath -> --the original input file, also serves as the program name
i ->
IO ()
compileTurtles n@(Nil _) opts inputFile input =
compileAndWriteResult (mapThrist (\(Constrained f) -> f) n) opts inputFile input >> return ()
View gist:12893c23f437901cce4a
1 2 3 4 5 6 7 8 9 10
OLD:
-- native execution
compileAndRunNative ::
Language i o ->
CompilationOptions ->
FilePath -> --the original input file, also serves as the program name
i ->
IO (Val Output)
compileAndRunNative l@(Language _ _ _ _ subLang) opts inputFile input = do
code <- compileAndWriteResult l opts inputFile input
View gist:e6eaf6b3cf52c1da4911
1 2 3 4 5
extT :: Thrist Compiler1 i o -> Extension
extT t = extT' t "X86" where
extT' :: Thrist Compiler1 i o -> String -> Extension
extT' Nil e = e
extT' (Cons (Compiler1 _ ext) t) _ = extT' t ext
View gist:4cb4f415001f0bf517c4
1 2 3 4 5 6 7 8 9 10
import spray.json._
import DefaultJsonProtocol._
 
case class Color(name: String, red: Int, green: Int, blue: Int)
 
object MyJsonProtocol extends DefaultJsonProtocol {
implicit val colorFormat = jsonFormat4(Color)
}
 
object Main {
View gist:68aec1ee21ad2f67e594
1 2 3 4 5 6 7 8 9
MacBook-Pro:ermine-scala joshcough$ ./sbt
Detected sbt version 0.13.5
Starting sbt: invoke with -help for other options
:ownloading sbt launcher 0.13.5
From http://typesafe.artifactoryonline.com/typesafe/ivy-releases/org.scala-sbt/sbt-launch.jar3.5
/sbt-launch.jar0.13.5
/sbt-launch.jar. Obtain the jar manually and place it at ./.lib/0.13.5
MacBook-Pro:ermine-scala joshcough$ ls .lib/
0.13.5 0.13.5?
Something went wrong with that request. Please try again.