Category Theory via C# (4) Natural Transformation

[FP & LINQ via C# series]

[Category Theory via C# series]

Natural transformation and naturality

If F: C → D and G: C → D are both functors from categories C to category D, the mapping from F to G is called natural transformation and denoted α: F ⇒ G. α: F ⇒ G is actually family of morphisms from F to G, For each object X in category C, there is a specific morphism αX: F(X) → G(X) in category D, called the component of α at X. For each morphism m: X → Y in category C and 2 functors F: C → D, G: C → D, there is a naturality square in D:

image

In another word, for m: X → Y in category C, there must be αY ∘ F(m) ≡ G(m) ∘ αX , or equivalently αY ∘ SelectF(m) ≡ SelectG(m) ∘ αX in category D.

In DotNet category, the following ToLazy<> generic method transforms Func<> functor to Lazy<> functor:

public static partial class NaturalTransformations
{
    // ToLazy: Func<> -> Lazy<>
    public static Lazy<T> ToLazy<T>(this Func<T> function) => new Lazy<T>(function);
}

Apparently, for above natural transformation: ToLazy<>: Func<> ⇒ Lazy<>:

  • for each specific object T, there is an object Func<T>, an object Lazy<T>, and a morphism ToFunc<T>: Func<T> → Lazy<T>.
  • For each specific morphism selector: TSource → TResult, there is a naturality square, which consists of 4 morphisms:
    • ToLazy<TResult>: Func<TResult> → Lazy<TResult>, which is the component of ToLazy<> at TResult
    • FuncExtensions.Select(selector): Func<TSource> → Func<TResult>
    • LazyExtensions.Select(selector): Lazy<TSource> → Lazy<TResult>
    • ToLazy<TSource>: Func<TSource> → Lazy<TSource>, which is the component of ToLazy<> at TSource

image

The following example is a simple naturality square that commutes for ToLazy<>:

internal static void Naturality()
{
    Func<int, string> selector = int32 => Math.Sqrt(int32).ToString("0.00");

    // Naturality square:
    // ToFunc<string>.o(LazyExtensions.Select(selector)) == FuncExtensions.Select(selector).o(ToFunc<int>)
    Func<Func<string>, Lazy<string>> funcStringToLazyString = ToLazy<string>;
    Func<Func<int>, Func<string>> funcInt32ToFuncString = FuncExtensions.Select(selector);
    Func<Func<int>, Lazy<string>> leftComposition = funcStringToLazyString.o(funcInt32ToFuncString);
    Func<Lazy<int>, Lazy<string>> lazyInt32ToLazyString = LazyExtensions.Select(selector);
    Func<Func<int>, Lazy<int>> funcInt32ToLazyInt32 = ToLazy<int>;
    Func<Func<int>, Lazy<string>> rightComposition = lazyInt32ToLazyString.o(funcInt32ToLazyInt32);

    Func<int> funcInt32 = () => 2;
    Lazy<string> lazyString = leftComposition(funcInt32);
    lazyString.Value.WriteLine(); // 1.41
    lazyString = rightComposition(funcInt32);
    lazyString.Value.WriteLine(); // 1.41
}

And the following are a few more examples of natural transformations:

// ToFunc: Lazy<T> -> Func<T>
public static Func<T> ToFunc<T>(this Lazy<T> lazy) => () => lazy.Value;

// ToEnumerable: Func<T> -> IEnumerable<T>
public static IEnumerable<T> ToEnumerable<T>(this Func<T> function)
{
    yield return function();
}

// ToEnumerable: Lazy<T> -> IEnumerable<T>
public static IEnumerable<T> ToEnumerable<T>(this Lazy<T> lazy)
{
    yield return lazy.Value;
}

Functor Category

Now there are functors, and mappings between functors, which are natural transformations. Naturally, they lead to category of functors. Given 2 categories C and D, there is a functor category, denoted DC:

  • Its objects ob(DC) are the functors from category C to D .
  • Its morphisms hom(DC) are the natural transformations between those functors.
  • The composition of natural transformations α: F ⇒ G and β: G ⇒ H, is natural transformations (β ∘ α): F ⇒ H.
  • The identity natural transformation idF: F ⇒ F maps each functor to itself

image_thumb1

Regarding the category laws:

  • Associativity law: As fore mentioned, natural transformation’s components are morphisms in D, so natural transformation composition in DC can be viewed as morphism composition in D: (β ∘ α)X: F(X) → H(X) = (βX: G(X) → H(X)) ∘ (αX: F(X) → G(X)). Natural transformations’ composition in DC is associative, since all component morphisms’ composition in D is associative
  • Identity law: similarly, identity natural transform’s components are the id morphisms idF(X): F(X) → F(X) in D. Identity natural transform satisfy identity law, since all its components satisfy identity law.

Here is an example of natural transformations composition:

// ToFunc: Lazy<T> -> Func<T>
public static Func<T> ToFunc<T>(this Lazy<T> lazy) => () => lazy.Value;
#endif

// ToOptional: Func<T> -> Optional<T>
public static Optional<T> ToOptional<T>(this Func<T> function) =>
    new Optional<T>(() => (true, function()));

// ToOptional: Lazy<T> -> Optional<T>
public static Optional<T> ToOptional<T>(this Lazy<T> lazy) =>
    // new Func<Func<T>, Optional<T>>(ToOptional).o(new Func<Lazy<T>, Func<T>>(ToFunc))(lazy);
    lazy.ToFunc().ToOptional();
}

Endofunctor category

Given category C, there is a endofunctors category, denoted CC, or End(C), where the objects are the endofunctors from category C to C itself, and the morphisms are the natural transformations between those endofunctors.

image3_thumb

All the functors in C# are endofunctors from DotNet category to DotNet. They are the objects of endofunctor category DotNetDotNet or End(DotNet).

70 Comments

  • Amazon.com/mytv - enter the 6 digit amazon mytv code you receive at screen at www.amazon.com/mytv to regiter your device. contact amazon support for hel

  • thanks for this post i love it

  • ازی انفجار می تواند یک شبه شما را ثروتمند کند و برعکس! ممکن است شما همه ی سرمایه خود را در این بازی قمار کنید و در چند ثانیه سرمایتان را از دست دهید. آموزش بازی انفجار و نحوه برنده شدن در آن بیش از هر چیزی نیاز به تجربه دارد.

  • The training was excellent. Thank you

  • Really nice and interesting post. I was looking for this

  • thanks

  • Really nice and interesting post. I was looking for this

  • thanks a lot

  • شرکت نکو تجارت سپند نتها نمایندگی انحصاری ریچانگ در ایران در سال 76 به منظور تولید ورق های ترموپلاستیک صنعتی قابل مصرف در صنایع مختلف کشور با بهره گیری از مشاورین اروپایی و زیر نظر متخصصین مجرب داخلی پلیمری کشور تاسیس گردید. از محصولات ما می توان به ورقه های اکلریک (پلکسی گلس)، ورق هایم پک، پلی کربنات لکسان وطلق رنگی اشاره کرد.

  • داشتن ظاهری زیباتر و جوانتر هم مو جب خرسندی و رضایت خود فرد می شود و هم باعث موفقیت او در امور شخصی، خانوادگی و اجتماعی نیز می شود. مر کز زیبایی دکتر مهدیانی فوق تخصص جراحی پلاستیک و زیبایی به عنوان بهترین مراکز جراحی پلاستیک و زیبایی در تهران کلیه خدمات زیبایی و جوانسازی را با بهترین کیفیت و مناسب ترین قیمت به شما عزیزان ارائه می کند.
    https://drmahdiani.com/

  • یکی از جست و جو های افراد در گوگل و دیگر موتور های جست و جو برندینگ و ساخت برند است، چرا که به دنبال پاسخی برای این که چطور می توانند به این مهم دست پیدا کنند می گردند. چرا که می توانند درآمد بالایی را با برندینگ، برندسازی و ساخت و برند حرفه ای به دست بیاورند. با هویت دادن به محصولتان در کسب و کار و نوعی برند سازی، به کالای مورد نظر خود شخصیتی ثابت می دهید که این امر باعث پیدا کردن جایگاه ویژه و مخصوص در ذهن مشتری می شود.
    https://amina-group.com/branding

  • <a href="https://tahvienovin.com/">شرکت تهویه نوین ایرانیان</a> با بهره گیری از کادری مجرب و حرفه ای، متشکل از مهندسین با تجربه و نیروهای متخصص بر آن است تا در مسیر تحقق مشتری مداری گامهایی مؤثرتر بردارد. در این راستا با ارائه محصولاتی با کیفیت، عملکردی مطلوب، هزینه ای بهینه و نیز خدمات پس از فروش، در پی جلب رضایت مشتریان گرامی است.

  • This article is really fantastic and thanks for sharing the valuable post.

  • awesome post. I’m a normal visitor of your web site and appreciate you taking the time to maintain the nice site. I’ll be a frequent visitor for a long time.

  • Great Article it its really informative and innovative keep us posted with new updates. its was really valuable. 

  • Thanks for writing such a good article, I stumbled onto your blog and read a few post. I like your style of writing...

  • This post is truly inspiring. I like your post and everything you share with us is current and very informative

  • Very interesting topic will bookmark your site to check if you Post more about in the future.

  • بلاک چین

  • تحلیل تکنیکال

  • رمز ارزها

  • یکی از بهترین مهد کودک در پاسداران (مهد کودک فرزندان آفتاب) می باشد. برای اطلاعات بیشتر روی لینک کلیک کنید.

  • اگر به دنبال خرید گل برای مناسبت ها هستید آن هم به شکل آنلاین فقط کافیه سری به سایت ما بزنید.

  • شرکت ارمغان تجارت رهسام با مقصود تامین نیازهای گسترده جامعه مهندسی و صنعت تاسیسات کشور در زمینه طراحی، مهندسی و مستند سازی تجهیزات تنظیم هوا مشغول به فعالیت می باشد. برای اطلاعات بیشتر و آشنایی با محصولات شرکت رهسام می توانید به وبسایت شرکت رهسام مراجعه کنید.

  • https://ma-study.blogspot.com/

  • That's a great article! The neatly organized content is good to see. Can I quote a blog and write it on my blog? My blog has a variety of communities including these articles. Would you like to visit me later? <a href="https://fortmissia.com/">토토사이트추천</a>

  • بسیاری از افراد هنگام انتقال پول از چین به ایران با مشکل مواجه می شوند. یکی از خدمات اصلی شرکت چینیران حواله یوان به چین است. برای استعلام <a href="https://www.chinatejarat.com/%d8%ad%d9%88%d8%a7%d9%84%d9%87-%db%8c%d9%88%d8%a7%d9%86-%da%86%db%8c%d9%86/">قیمت حواله یوان به چین</a> روی لینک مقابل کلیک فرمایید.

  • خرید لاک ژل و انواع محصولات ناخن با بهترین قیمت

  • برخی از بازی های  شرکت بلیزارد بصورت رایگان دردسترس گیمرها و کاربران نخواهد بود. و این کاربران برای استفاده از بازی  گیم تایم یا همان گیم کارت خریداری کنند. یکی از این بازی ها،‌ بازی محبوب و پرطرفدار ورلدآف وارکرافت است. به شارژ ماهیانه بازی وارکرافت در سرورهای بازی بلیزارد  گیم تایم می گویند ، که در فروشگاه جت گیم موجود می باشد.

    خرید گیم تایم 60 روزه ازفروشگاه جت گیم:

    در واقع گیم تایم 60 روزه نمونه ای جدید است از گیم تایم ها برای استفاده دربازی World of Warcraft  . که در ادامه بیشتر در مورد این محصول و نحوه استفاده از آن توضیح می دهیم .

    شما با خرید گیم تایم 60 روزه در مدت زمان آن گیم تایم ( 60 روز ) به امکاناتی در بازی World of Warcraft درسترسی پیدا خواهید کرد که این امکانات شامل موارد زیر میباشند :

    1 - اجازه لول آپ کردن تا لول 50 ( بدون گیم تایم فقط می توانید تا لول 20 بازی کنید )

    2 - اجازه  چت کردن با دیگران درون بازی ( بدون گیم تایم نمی توانید در بازی  چت کنید )

    3 - دسترسی به بازی World of Warcraft Classic

    در نتیجه برای بازی در World of Warcraft حتمآ به تهیه گیم تایم نیاز دارید.

    نکته 1 : گیم تایم یا همان زمان بازی ورد اف وارکرفت برای توانایی انلاین بازی کردن استفاده می شود و بدون گیم تایم امکان بازی کردن بازی محبوب ورد اف وارکرفت را نخواهید داشت.

    نکته 2 : درصورتی که گیم تایم نداشته باشید امکان بازی ورد اف وارکرفت کلاسیک را ندارید و شما میتوانید جهت خرید این محصول از وبسایت ما اقدام نمایید

    نکته 3 : نیازی به وارد کردن مشخصات اکانت بلیزارد شما نمی باشد زیرا کد گیم تایم  توسط خود شما و پس از دریافت کد، وارد می شود  ( آموزش وارد کردن در پایین صفحه قرار دارد )

  • صندلی حالت دهنده: برای حالت دادن، رنگ کردن و کوتاه کردن موهای مشتری استفاده می شود. معمولاً پشت سر و گزینه کاملاً خوابیده ندارند. صندلی آرایشگر: دارای ویژگی خوابیدن کامل برای اصلاح است. معمولا شامل یک پشتی سر، زیرپایی برای تجربه راحت تر است.

  • Fine way of describing, and nice article to take facts about my presentation topic,

  • I want to say that this article is amazing, nice written and come with almost all significant infos. I'd like to look more posts like this

  • Popularity of Gametime for two months:
    As mentioned above, the 60-day game time has been more popular than other game times for several months. This is because it has both the right time and the right price. The reason why World of Warcraft players use this type of game time is the duration. Because the game time of 60 days is an average game time and most people use the days of this game time. One advantage that this game time has over other game times is its duration.

    All kinds of game time regions
    In general, the two-month game time is made from 2 regions, Europe and America. But an important point is that it is recommended to get a region of Gametime that is compatible with your Shadowland region. If you are looking for our advice, we recommend you to buy Region Europe. Because it is close to Middle East servers and usually you get better ping. Obtained from Jet Game site

  • such a great post.

  • such a great post.

  • such a great post.

  • very nice post.

  • such a great post.

  • Your writing is perfect and complete. casinocommunity However, I think it will be more wonderful if your post includes additional topics that I am thinking of. I have a lot of posts on my site similar to your topic. Would you like to visit once?

  • سریع‌ترین رشد اقتصادی اتحادیه اروپا در سال 2021، ایرلند بود که طبق آخرین پیش‌بینی در سال 2022، تولید ناخالص داخلی این کشور در آن سال 13.5 درصد رشد کرد. انتظار می‌رفت که اقتصاد کرواسی با اقتصاد مالت، 10.2 درصد رشد کند. سومین سریعترین رشد با 9.4 درصد است.

  • I've been looking for photos and articles on this topic over the past few days due to a school assignment, <and I'm really happy to find a post with the material I was looking for! I bookmark and will come often! Thanks :D

  • Betsson es reconocida como una de las principales plataformas de apuestas en diversos países en América Latina, en especial en Perú donde opera desde hace más de una década de forma online.

  • <p style="text-align: justify;"><strong>سرم ضدجوش<a href="https://orizenshop.com/"> لاکچری کوین</a></strong> در حین پاکسازی پوست را بدون احساس چربی مرطوب می کند.</p>

  • What Could have helped it to make this big

  • I appreciate this wonderful information on this wonderful website. I hope that there's more to come. This is the perfect post. It helped me a lot. I hope you come to my site and share your opinions if you have time. Have a nice day.

  • I appreciate this wonderful information on this wonderful website. I hope that there's more to come. This is the perfect post. It helped me a lot. I hope you come to my site and share your opinions if you have time. Have a nice day.

  • Wheel spinner is a game that I am sure that anyone who loves spins of fortune will be addicted to sitting in front of the computer playing this game for hours on end. The game has no age limit for players, suitable for those who want healthy entertainment and improved hand reflexes.

  • Amazing! Its really awesome article, I have got much clear idea regarding from this article

  • I am overwhelmed by your post with such a nice topic.

  • Simply unadulterated brilliance from you here. I have never expected something not as much as this from you.

  • I am really grateful for your blog post for giving a lot of information

  • THIS IS GREAT BLOG FOR TODAY THAT I READ, THANKS FOR THIS!

  • I AM REALLY THANKFUL FOR THIS, I'M SURE THAT THE INFORMATION YOU SHARED TO US IS VERY USEFUL ESPECIALLY NOWADAYS.

  • GREAT BLOG! I REALLY APPRECIATED IT, THANKS FOR SHARING!

  • I AM VERY SATISFIED HERE, BECAUSE I READ THE ARTICLE THAT IS SO VERY USEFULL. THANKS FOR THIS.

  • Your article is amazing. I wish I could read more.

  • Now the sign-in page opens, click on the create account button. This will open the sign-up page now.

  • پارچه باما یک فروشگاه <a href="https://parchebama.com/">خرید آنلاین پارچه</a> است که به ارائه پارچه‌های با کیفیت بالا به مشتریان در ایران معروف شده است. با مجموعه‌ای گسترده از پارچه‌های مناسب برای مصارف مختلف

  • ChatGPT kann viel mehr als nur Inhalte produzieren; Es kann auch bei der Prüfung, Überprüfung, Ergänzung und Recherche von Texten und Daten hilfreich sein. Obwohl Word-Dokumente noch nicht auf ChatGPT hochgeladen wurden, https://pavzi.com/de/so-laden-sie-ein-dokument-auf-chatgpt-hoch/ kann das Ghostwriter-Add-in ChatGPT-Funktionen in Word bereitstellen. Sie können auch Dateien und Dokumente in Google Docs hochladen, die KI von Google Labs nutzen, Ihr Dokument in PDF umwandeln und es dann in Bing Chat oder ChatPDF hochladen.

  • از کاربردهای عایق الاستومری می توان به موارد زیر اشاره نمود:

    از عایق الاستومری می توان جهت عایق کاری تأسیسات و سیستم تأسیسات خورشیدی، سیستم های مکانیکی در صنایع کشتی سازی، صنایع دارویی، صنعت نفت و گاز و صنایع پتروشیمی و همچنین در داکت ها استفاده نمود.
    امکان عایق کاری چیلرهای بزرگ و انواع لوله های ساختمانی با این عایق وجود دارد.
    با توجه به خاصیت ضد حریق این محصولات، می توان از آنها در شرایط مورد نیاز بهره گرفت.
    همچنین با کمک این عایق می توان از انتقال صدا جلوگیری نمود. لازم به ذکر است که امکان کاهش شدت صوت با بکارگیری عایق الاستومری به ضخامت 2 سانتی متری، به میزان 35 دسی بل وجود دارد.

  • THIS IS A GOOD POST WITHOUT ANY DOUBTS. YOU REALLY DOING A GREAT JOB. I INSPIRED FROM YOU. SO KEEP IT UP!!

  • دیمو لینگو شما را در سفری هیجان‌انگیز به دنیای آموزش زبان آلمانی همراهی می‌کند

  • nice post.

  • I saw your article well. We can help you enjoy more fun. Welcome anytime.

  • Tak mungkin kamu menemukan situs terbaik selain di <a href="https://bursa188.pro/"rel="dofollow">BURSA188</a> <a href="https://bursa188.store/"rel="dofollow">BURSA188</a>

  • THANKS FOR SHARING THIS INFORMATIVE ARTICLE.

  • THIS POST IS VERY USEFUL. THANK YOU FOR THIS USEFULL INFORMATION.

  • WONDERFUL POST AND NICE WORDS IN THIS ARTICLE. THANKS FOR SHARING.

  • NICE ARTICLE MAN KEEP UP THE GOOD WORK. I WOULD LIKE TO BE HERE AGAIN TO FIND ANOTHER MASTERPIECE ARTICLE. THANKS FOR SHARING

  • پرستار بیمار فردی متخصص و آموزش دیده است که در منزل از افراد دچار معلولیت و بیماری مراقبت می کند، پرستار بیمار با استفاده از روش های تخصصی به مراقبت از بیمار پرداخته و با توجه به نیازهای بیمار به ارائه خدمات تخصصی می پردازد.
    https://parastartehran.com/sick-nurse/

Add a Comment

As it will appear on the website

Not displayed

Your website