DEV Community

Yufan Lou
Yufan Lou

Posted on

3

Today I Learned (Not) Reflection

An intuition for reflection | QA9

This reflection is not the one for metaprogramming, but the property that if you fold a piece of data with its constructors, you get back the piece of data itself. In terms of list in Haskell, with constructors:

[] :: [a]
(:) :: a -> [a] -> [a]
Enter fullscreen mode Exit fullscreen mode

and a folding function like this:

foldr :: (a -> b -> b) -> b -> [a] -> b
Enter fullscreen mode Exit fullscreen mode

The property of reflection is then defined as this equation:

foldr (:) [] = id
Enter fullscreen mode Exit fullscreen mode

Another "Today I Learned" quote:

It is known (although I am not sure where you can find this in a paper) that if you have parametricity and reflection, you can derive induction.

A man feeling mindblown

Especially revelatory quotes:

With induction, we are trying to summarize an infinite number of cases in some finite way. This finite way is with the constructors for the datatype.

not taking constructors and datatypes for granted, we might ask: but how do you know you correctly summarized that infinite set of values using your functions you call constructors

My intuition about reflection is that it contributes an essential property towards knowing you have the correct summary of your infinite set of data.

A philosophical quote:

All it does is go through the list and interpret cons as cons, nil as nil.

Data transformation is all about reinterpreting the constructors of data.

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

πŸ‘‹ Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay