Skip to content

Instantly share code, notes, and snippets.

@kcsongor
Last active April 16, 2018 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kcsongor/caf10e696514e24fd70c1cd829c50c2d to your computer and use it in GitHub Desktop.
Save kcsongor/caf10e696514e24fd70c1cd829c50c2d to your computer and use it in GitHub Desktop.
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE KindSignatures #-}
module Fold where
data Nat = Z | S Nat
data Vect (n :: Nat) a where
Nil :: Vect 'Z a
Cons :: a -> Vect n a -> Vect ('S n) a
instance Foldable (Vect 'Z) where
foldr _ b _ = b
instance Foldable (Vect n) => Foldable (Vect ('S n)) where
foldr f b (Cons x xs) = f x (foldr f b xs)
test :: Int
test = foldr (+) 0 (Cons 1 (Cons 2 (Cons 3 (Cons 4 Nil))))
-- $ ghc -O1 Fold.hs -ddump-simpl
-- test :: Int
-- [GblId,
-- Caf=NoCafRefs,
-- Str=m,
-- Unf=Unf{Src=<vanilla>, TopLvl=True, Value=True, ConLike=True,
-- WorkFree=True, Expandable=True, Guidance=IF_ARGS [] 10 20}]
-- test = GHC.Types.I# 10#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment