Skip to content

Instantly share code, notes, and snippets.

@ajkovar
Last active March 31, 2021 04:18
Show Gist options
  • Save ajkovar/d1b2d757c9ef5d3343a73f1ad2d2426f to your computer and use it in GitHub Desktop.
Save ajkovar/d1b2d757c9ef5d3343a73f1ad2d2426f to your computer and use it in GitHub Desktop.
Haskell
-- get types of an expression
:t (fail)
-- Get info about type classes that this type implements
:i Int
-- Get info about the 'kind' of a type (somoething like the type of the type)
:k Int
-- See contents of a module
:browse Graphics.PDF.Typesetting
-- Show the bindings made at the prompt and their types.
:show bindings
-- Show the list of modules currently loaded
:show modules
-- Show the imports that are currently in force, as created by import and :module commands.
:show imports
-- Show the currently active language flags for source files.
:show language
-- Show the currently active language flags for expressions typed at the prompt
:showi language
-- set extensions
-- http://downloads.haskell.org/~ghc/7.4.1/docs/html/users_guide/flag-reference.html
:set -XOverloadedStrings
-- reload loaded modules
:reload
-- print out info about object
:print something
-- get unevaluated view of something
:sprint something
-- load modules
:m + Data.List Data.Map Data.Set
-- exit cli
:quit
-- show the class instances available for a type. (requires 8.10)
:instances
# start shell
ghci
# compile using the make option, which tells it to resolve any dependencies
# the -o option, which changes the output file name
# the -i option, which tells it to include for other files in the compiling
# and the -Wall option which tells it to print warnings (all)
ghc --make Main.hs -i some/path/to/File.hs -o test -Wall
ghc-pkg list - shows the list of global ghc packages (what are those? I;m not sure exactly)
# view version dependencies
stack ls dependencies
# init a cabal project in non interactive mode (with the root in the current directory)
cabal init -n
# load a package in the repl
cabal repl -b multiset
# OR
stack repl --package multiset Main.hs
fmap :: (a -> b) -> f a -> f b
For emphasis, we can write fmap’s type with extra parentheses: fmap :: (a -> b) -> (f a -> f b)
can we get rid of the parenthesis in (a -> b).. i.e.
could we call fmap like `fmap a b fa` ?
I'm currently trying to understand monads as part of my haskell journey. I think more or less I have the idea but am a bit confused by this example that is given in the haskell.org tutorial section.
https://www.haskell.org/tutorial/monads.html#:~:text=data%20SM%20a%20%3D%20SM%20(S,%3D%20%20c%20s0
This section states that "we are trying to do is define an overall computation as a series of steps (functions with type SM a), sequenced using >>= and return. These steps may interact with the state (via readSM or updateSM) or may ignore the state. " I don't really understand how the read and update are supposed to be used within the context of the sequencing operations. They both seem to completely ignore the value of type 'a'. The first argument to the bind operation (>>=) is a monad and second argument is a lambda with the value as the argument. I thought the idea of I don't see how I can use either
type Resource = Integer
data R a = R (Resource -> (Resource, Either a (R a)))
-- this didn't work:
-- deriving (Show)
instance Monad R where
R c1 >>= fc2 = R (\r -> case c1 r of
(r', Left v) -> let R c2 = fc2 v in
c2 r'
(r', Right pc1) -> (r', Right (pc1 >>= fc2)))
return v = R (\r -> (r, (Left v)))
infixl 1 >>, >>=
class Monad m where
(>>=) :: m a -> (a -> m b) -> m b
(>>) :: m a -> m b -> m b
return :: a -> m a
-- fail :: String -> m a
m >> k = m >>= \_ -> k
type S = String
data SM a = SM (S -> (a,S)) -- The monadic type
instance Monad SM where
-- defines state propagation
SM c1 >>= fc2 = SM (\s0 -> let (r,s1) = c1 s0
SM c2 = fc2 r in
c2 s1)
return k = SM (\s -> (k,s))
-- extracts the state from the monad
readSM :: SM S
readSM = SM (\s -> (s,s))
-- updates the state of the monad
updateSM :: (S -> S) -> SM () -- alters the state
updateSM f = SM (\s -> ((), f s))
-- run a computation in the SM monad
runSM :: S -> SM a -> (a,S)
runSM s0 (SM c) = c s0
let m = readSM >>= (\s -> updateSM (\s -> (s ++ "2"))) in runSM "1" m
let m = do
s <- readSM
updateSM (\s -> s ++ "2")
in
runSM "1" m
-- what is the point in using a tuple with state and a value? What is the point in having a type a?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment