From bd923a99767440b0ed297875d8715a77c705d970 Mon Sep 17 00:00:00 2001 From: Julin S Date: Fri, 29 Sep 2023 17:19:59 +0530 Subject: [PATCH] [cpp] try some template metaprogramming --- cpp/tmp/hello.cpp | 27 ++++++++++++++++ cpp/tmp/nat.cpp | 32 ++++++++++++++++++ haskell/Hlist.hs | 57 +++++++++++++++++++++++++++++++++ haskell/{types.hs => Vector.hs} | 0 4 files changed, 116 insertions(+) create mode 100644 cpp/tmp/hello.cpp create mode 100644 cpp/tmp/nat.cpp create mode 100644 haskell/Hlist.hs rename haskell/{types.hs => Vector.hs} (100%) diff --git a/cpp/tmp/hello.cpp b/cpp/tmp/hello.cpp new file mode 100644 index 0000000..4095b29 --- /dev/null +++ b/cpp/tmp/hello.cpp @@ -0,0 +1,27 @@ +#include + +template +struct Nat { + enum { val = 1 + Nat::val }; +}; + +template<> +struct Nat<0> { + enum { val = 0 }; +}; + +/* + * 'class' has variables 'private' by default. 'struct' has it 'public'. More + * convenient here. + */ + +int main() { + std::cout << Nat<2>::val << std::endl; + std::cout << Nat<23>::val << std::endl; +} + +// Output +/* +2 +23 +*/ diff --git a/cpp/tmp/nat.cpp b/cpp/tmp/nat.cpp new file mode 100644 index 0000000..85cfe73 --- /dev/null +++ b/cpp/tmp/nat.cpp @@ -0,0 +1,32 @@ +#include + +struct Zero { + enum { val = 0 }; +}; + +template +struct Succ { + enum { val = 1 + N::val}; +}; + +int main() { + // O = 0 + std::cout << Zero::val << std::endl; + + // S O = 1 + std::cout << Succ::val << std::endl; + + // S (S O) = 2 + std::cout << Succ>::val << std::endl; + + // S (S (S O) = 3 + std::cout << Succ>>::val << std::endl; +} + +// Output +/* +0 +1 +2 +3 +*/ diff --git a/haskell/Hlist.hs b/haskell/Hlist.hs new file mode 100644 index 0000000..e5964af --- /dev/null +++ b/haskell/Hlist.hs @@ -0,0 +1,57 @@ +{-# LANGUAGE DataKinds #-} +{-# LANGUAGE GADTs #-} +{-# LANGUAGE TypeOperators #-} +{-# LANGUAGE TypeFamilies #-} + + +-- -- Method 1 +-- data family Hlist (l :: [*]) +-- data instance Hlist '[] = HNil +-- data instance Hlist (t ': ts) = HCons t (Hlist ts) + +-- hd :: Hlist (t ': ts) -> t +-- hd (HCons x _) = x + +-- tl :: Hlist (t ': ts) -> Hlist ts +-- tl (HCons _ xs) = xs + +-- idx :: Hlist ts -> (n :: Int) -> (ts '!! n) + +-- -- Can't make it work +-- -- app :: Hlist ts1 -> Hlist ts2 -> Hlist (ts1 '++ ts2) +-- -- app HNil hls2 = hls2 + +-- -- Can't make it work +-- -- len :: Hlist ts -> Int +-- -- len HNil = 0 +-- -- len (HCons _ xs) = 1 + (len xs) + + +-- -- Method 2 ?? +-- data HNil = HNil +-- data HCons a b = HCons a b + +-- a = HCons 3 HNil +-- b = HCons 3 (HCons True HNil) + + + +-- Method 3 +data Hlist :: [*] -> * where + HNil :: Hlist '[] + HCons :: t -> Hlist ts -> Hlist (t : ts) + +data Member x (l':ls) where + First :: Member x (l:ls) + Next :: Member x ls -> Member x (l:ls) + +-- data Member x (l':ls) where +-- First :: Member x (l:ls) +-- Next :: Member x ls -> Member x (l:ls) + +-- len :: Hlist ts -> Int +-- len HNil = 0 +-- len (HCons _ xs) = 1 + (len xs) + +-- a = HCons 3 HNil +-- b = HCons 3 (HCons True HNil) diff --git a/haskell/types.hs b/haskell/Vector.hs similarity index 100% rename from haskell/types.hs rename to haskell/Vector.hs