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.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay