Ad
  • Custom User Avatar

    I don't think there's anything secret in there. Here it is:

    {-# Language RankNTypes #-}
    
    module Preloaded ( List(List), foldr
                     , Number, zero, Preloaded.succ, Preloaded.pred, plus, times
                     , Boolean, (?), true, false, or, and, not
                     , Option, option, nothing, just, fmap
                     , Pair, uncurry, pair, fst, snd, first, second, both, double
                     , ($), undefined, undefinedHelper
                     ) where
    
    import Prelude (Word,succ,pred,(+),(*),(.),($),errorWithoutStackTrace,(<>),String)
    
    newtype List a = List { foldr :: forall z. (a -> z -> z) -> z -> z }
    
    type Number = Word
    
    zero :: Number -> Boolean
    zero 0 = true
    zero _ = false
    
    succ,pred :: Number -> Number
    succ 18446744073709551615 = 18446744073709551615
    succ x = Prelude.succ x
    pred 0 = 0
    pred x = Prelude.pred x
    
    plus,times :: Number -> Number -> Number
    plus = (+)
    times = (*)
    
    newtype Boolean = Boolean { (?) :: forall a. a -> a -> a }
    
    true,false :: Boolean
    true = Boolean $ \ t _ -> t
    false = Boolean $ \ _ f -> f
    
    or,and :: Boolean -> Boolean -> Boolean
    or x y  = x ? x $ y
    and x y = x ? y $ x
    
    not :: Boolean -> Boolean
    not x = x ? false $ true
    
    newtype Option a = Option { option :: forall z. z -> (a -> z) -> z }
    
    nothing :: Option a
    nothing = Option $ \ z _ -> z
    
    just :: a -> Option a
    just a = Option $ \ _ fn -> fn a
    
    fmap :: (a -> b) -> Option a -> Option b
    fmap f x = option x nothing (just . f)
    
    newtype Pair a b = Pair { uncurry :: forall z. (a -> b -> z) -> z }
    
    pair :: a -> b -> Pair a b
    pair a b = Pair $ \ fn -> fn a b
    
    fst :: Pair a b -> a
    fst pair = uncurry pair $ \ a _ -> a
    
    snd :: Pair a b -> b
    snd pair = uncurry pair $ \ _ b -> b
    
    first :: (a -> z) -> Pair a b -> Pair z b
    first f pair = Pair $ \ g -> g (f $ fst pair) (snd pair)
    
    second :: (b -> z) -> Pair a b -> Pair a z
    second f pair = Pair $ \ g -> g (fst pair) (f $ snd pair)
    
    both :: (a -> z) -> Pair a a -> Pair z z
    both f = double f f
    
    double :: (a -> x) -> (b -> y) -> Pair a b -> Pair x y
    double f g pair = Pair $ \ h -> h (f $ fst pair) (g $ snd pair)
    
    undefined :: error
    undefined = errorWithoutStackTrace "undefined"
    
    undefinedHelper :: String -> error
    undefinedHelper name = errorWithoutStackTrace $ "undefined: " <> name