Regarding point 3:
You can make code run exactly in the same way in both ghc
and ghci
:
use only pure and safe functions. By doing this you know that optimizations or side-effects will not show any different behaviour due to the implementation (as in the question you linked).
No special flag is required. If you do not follow this advice then there is no flag that allows to obtain what you want.
Regarding point 2:
You can make ghci recompile every file and use the interpreter version instead of loading the binary compiled by ghc so you can be sure the code will act as if typed by ghci instead of compiled by ghc. Use the :reload
command to force recompilation.
Regarding the types and defaulting (point 1):
GHCi by default uses the extended defaulting rules, which are described here.
Standard defaulting rules basically default only "numbers" (i.e. Num
, Fractional
etc.). Extended rules are able to default types for things like Eq
, Ord
and Show
.
Note that the default declaration implicit when using ExtendedDefaultRules
is:
default ((), Integer, Double)
So, basically, any "free" Eq
or Ord
constraint is simply defaulted to ()
.
So you can simply specify ExtendedDefaultRules
or NoExtendedDefaultRules
to specify exactly which kind of defaulting you want.
This can be done putting the pragmas in the source files {-# LANGUAGE <extension-name> #-}
or by specifying -X<extension-name>
when compiling with ghc
or by using the GHCi directive :set -X<extension-name>
.
As noted by dfeur there is a fundamental difference between ghci and ghc: ghci must be able to completely infer the type of the input text when it is inserted. So, obviously, you have to arrange the declarations so that a function doesn't reference an undefined name.
Moreover type defaulting will be applied to each statement which means that, together with the monomorphism restriction (see: What is the monomorphism restriction? and https://wiki.haskell.org/Monomorphism_restriction) ghci may infer more monomorphic types than the ones inferred by ghc
when compiling the same code.
You probably want to use NoMonomorphismRestriction
when using GHCi (though recent versions of ghci have it disabled by default) and probably in your files too.