<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Molossus Spondee</title>
    <description>The latest articles on DEV Community by Molossus Spondee (@mspondee).</description>
    <link>https://dev.to/mspondee</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F345366%2F0ebc4919-fa98-47ee-ab37-a9d6178b58f9.jpg</url>
      <title>DEV Community: Molossus Spondee</title>
      <link>https://dev.to/mspondee</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mspondee"/>
    <language>en</language>
    <item>
      <title>Free monoidal functors in arbitrary categories</title>
      <dc:creator>Molossus Spondee</dc:creator>
      <pubDate>Mon, 05 Oct 2020 04:58:22 +0000</pubDate>
      <link>https://dev.to/mspondee/free-monoidal-functors-in-arbitrary-categories-3kbp</link>
      <guid>https://dev.to/mspondee/free-monoidal-functors-in-arbitrary-categories-3kbp</guid>
      <description>&lt;p&gt;It is often joked "a monad is a monoid in the category of endofunctors."&lt;/p&gt;

&lt;p&gt;Little known fact, an Applicative functor is a monoid in the category of functors with day convolution as the product.&lt;/p&gt;

&lt;p&gt;And day convolution unlike endofunctor composition can be defined over any strong monoidal category (basically just has tuples and functions.)&lt;/p&gt;

&lt;p&gt;Hence&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{-# LANGUAGE ExistentialQuantification, RankNTypes #-}
import Prelude hiding (Functor (..), Applicative (..))
class Functor f where
  fmap :: Hom a b -&amp;gt; f a -&amp;gt; f b
class Functor f =&amp;gt; Monoidal f where
  pure :: Id a -&amp;gt; f a
  join :: Day f f a -&amp;gt; f a
data Day f g a = forall x y. Day (f x) (g y) (Hom (PRODUCT x y) a)
instance Functor (Day f g) where
   fmap f (Day h x y) = Day (f :.: h) x y
data Id a = Id (forall x. Hom x a)
instance Functor Id where
    fmap f (Id x) = Id (f :.: x)
data Free f a = Pure (Id a) | Ap (Day f (Free f) a)
instance Functor (Free f) where
   fmap f (Pure x) = Pure (fmap f x)
   fmap f (Ap x) = Ap (fmap f x)
instance Monoidal (Free f) where
   pure = Pure
   join (Day h (Pure x) y) = Ap h x y
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Where &lt;code&gt;PRODUCT&lt;/code&gt;and &lt;code&gt;Hom&lt;/code&gt; are up to you to define.&lt;/p&gt;

&lt;p&gt;In order to have full applicative functors you need functions which your category might not have.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Codata/Data with Type Classes and Tagless Final Style</title>
      <dc:creator>Molossus Spondee</dc:creator>
      <pubDate>Tue, 28 Jul 2020 16:10:37 +0000</pubDate>
      <link>https://dev.to/mspondee/codata-data-with-type-classes-and-tagless-final-style-179m</link>
      <guid>https://dev.to/mspondee/codata-data-with-type-classes-and-tagless-final-style-179m</guid>
      <description>&lt;p&gt;Lately I have been playing with tagless final style, a way of writing data structures in Haskell which is supposedly more extensible. I don't really see it as all that advantageous myself.&lt;/p&gt;

&lt;p&gt;A trivial linked list example.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class List t where
    nil :: t
    cons :: Int -&amp;gt; t -&amp;gt; t
instance List AsList where
    nil = AsList []
    cons h (AsList t) = AsList (h : t)
mylist :: List t =&amp;gt; t
mylist = cons 3 (cons 6 nil)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this way we transform our data structures into generic computer programs that are interpreted to output our data structures.&lt;/p&gt;

&lt;p&gt;This post isn't really about tagless final style so I won't go into too much more detail. Really this post is about Scott encodings.&lt;/p&gt;

&lt;p&gt;There is a funny little Combinator known as Mu which can be used to encode recursion in a tricky way.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;newtype Mu f = Mu (forall a. (f a -&amp;gt; a) -&amp;gt; a)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Of course in Haskell you don't really need such a cumbersome encoding. This is just theory.&lt;/p&gt;

&lt;p&gt;I have found it useful on occassion to box up tagless final data on occasion even though you are not really supposed to.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;newtype Box k = Box (forall a. k a =&amp;gt; a)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Notice something? In the core language type classes are desugared to record parameters implicitly passed in. So Box is just a funny variant of Mu!&lt;/p&gt;

&lt;p&gt;There is another well known way of encoding recursion.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;data Nu f = forall a. Nu a (a -&amp;gt; f a)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This is suggestive of another style of encoding.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Stream t where
    hd :: t -&amp;gt; Int
    tl :: t -&amp;gt; t
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;data CoBox k = forall a. k a =&amp;gt; CoBox a
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And indeed this works fine.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;toList :: CoBox Stream -&amp;gt; [Into]
toList (CoBox x) = loop x where
    loop x = hd x : tl x
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Tagless "final" style is revealed for what it really is. A style of open recursion based around functors.&lt;/p&gt;

&lt;p&gt;This is suggestive of a new approach.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Stream f where
   hd :: f a -&amp;gt; Int
   tl :: f a -&amp;gt; a
   cons :: Int -&amp;gt; a -&amp;gt; f a

newtype Fix f = Fix (f (Fix))
toList :: Stream f =&amp;gt; Fix f -&amp;gt; [a]
toList (Fix x) = hd x : toList (tl x)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;But this is a digression for another time.&lt;/p&gt;

</description>
      <category>haskell</category>
    </item>
    <item>
      <title>Recursion, corecursion and thunks</title>
      <dc:creator>Molossus Spondee</dc:creator>
      <pubDate>Thu, 18 Jun 2020 15:16:16 +0000</pubDate>
      <link>https://dev.to/mspondee/recursion-corecursion-and-thunks-2ok9</link>
      <guid>https://dev.to/mspondee/recursion-corecursion-and-thunks-2ok9</guid>
      <description>&lt;p&gt;Java does not support tail calls.&lt;/p&gt;

&lt;p&gt;The usual workaround is a type like the following.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Thunk&amp;lt;A&amp;gt; {
     boolean isDone();
     Thunk step();
     A finish();
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Looked at mathematically this type is really a corecursive definition sort of like a stream datatype.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;exists b. (b -&amp;gt; (b + a))
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;It happens there exists a dual recursive datatype.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;forall b. ((a -&amp;gt; b) -&amp;gt; (b -&amp;gt; b) -&amp;gt; b
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You can apply the math to a simple factorial function like so.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt; &amp;lt;C&amp;gt; C fact (int m, int n, Function&amp;lt;Supplier&amp;lt;C&amp;gt;,C&amp;gt; y, Function&amp;lt;Integer, C&amp;gt; result) {
     if (n &amp;lt; 1){
          return h.apply(m);
     }
     return y.apply(() -&amp;gt; fact(m *n, n -1, y, h));
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;At first it appears we have gained nothing from this approach except a degree of indirection. But in fact this lets us delay and thunk an evaluation step or just evaluate it directly.&lt;/p&gt;

&lt;p&gt;I'm still not sure this is the best approach for tail calls on the JVM but I remain hopeful recursion schemes will bring a better answer than the ordinary one.&lt;/p&gt;

</description>
      <category>java</category>
    </item>
    <item>
      <title>If you could add one new instruction to mainstream hardware what would it be?</title>
      <dc:creator>Molossus Spondee</dc:creator>
      <pubDate>Fri, 12 Jun 2020 17:01:05 +0000</pubDate>
      <link>https://dev.to/mspondee/if-you-could-add-one-new-instruction-to-mainstream-hardware-what-would-it-be-4oi5</link>
      <guid>https://dev.to/mspondee/if-you-could-add-one-new-instruction-to-mainstream-hardware-what-would-it-be-4oi5</guid>
      <description>&lt;p&gt;Similar to Agner Fog's idea of a new instruction set architecture but much less limited in scope &lt;a href="https://www.agner.org/optimize/blog/read.php?i=421"&gt;https://www.agner.org/optimize/blog/read.php?i=421&lt;/a&gt; if you could add one entirely new instruction to mainstream hardware what would add? I'll write some of my own thoughts below as a comment but I'm interested in what the rest of the developer community would want. Are there things you just can't do without adding a dedicated instruction or things that are just too slow? Or perhaps it is more of a convenience thing or some other factor I have not considered?&lt;/p&gt;

</description>
      <category>discuss</category>
    </item>
    <item>
      <title>Java polymorphism workaround</title>
      <dc:creator>Molossus Spondee</dc:creator>
      <pubDate>Thu, 21 May 2020 20:44:33 +0000</pubDate>
      <link>https://dev.to/mspondee/java-polymorphism-workaround-1nhc</link>
      <guid>https://dev.to/mspondee/java-polymorphism-workaround-1nhc</guid>
      <description>&lt;p&gt;One limitation of Java generics is that you can't have polymorphic data within a method. But there is a workaround of declaring a static class within a method. The Java record syntax let's you do this without creating a new inner class object. Hopefully the inner class limitation on classes declared inside a method is lifted.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int myfunction(int x) {
   record Id() {
       static &amp;lt;A&amp;gt; id(A input) {
           A value = input;
           return value;
       }
   }
   return Id.id(x);
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;It's a bit awkward but sometimes useful in heavily polymorphic code.&lt;/p&gt;

</description>
      <category>java</category>
    </item>
    <item>
      <title>Equality Constraints With Java</title>
      <dc:creator>Molossus Spondee</dc:creator>
      <pubDate>Sun, 17 May 2020 14:52:26 +0000</pubDate>
      <link>https://dev.to/mspondee/equality-constraints-with-java-2nom</link>
      <guid>https://dev.to/mspondee/equality-constraints-with-java-2nom</guid>
      <description>&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;final class Subclasses&amp;lt;A extends B, A&amp;gt;{
}
record Equals&amp;lt;A, B&amp;gt;(Subclasses&amp;lt;? super A,B&amp;gt; left, Subclasses&amp;lt;? super B, A&amp;gt; right){}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Reifies equality constraints&lt;/p&gt;

</description>
      <category>java</category>
    </item>
    <item>
      <title>Well and truly uninstantiable Java classes</title>
      <dc:creator>Molossus Spondee</dc:creator>
      <pubDate>Fri, 15 May 2020 03:38:33 +0000</pubDate>
      <link>https://dev.to/mspondee/well-and-truly-uninstantiable-java-classes-117m</link>
      <guid>https://dev.to/mspondee/well-and-truly-uninstantiable-java-classes-117m</guid>
      <description>&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Privileged {
    public Privileged(){
        super();
        checkPermissions();
    }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Is insufficient due to tricks with race conditions.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Privileged {
    public Privileged(){
        this(checkPermission());
    }
    private Privileged(Void dummy) {}
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Is the proper procedure&lt;/p&gt;

</description>
      <category>java</category>
    </item>
    <item>
      <title>Teleogical Programming</title>
      <dc:creator>Molossus Spondee</dc:creator>
      <pubDate>Sun, 03 May 2020 17:10:57 +0000</pubDate>
      <link>https://dev.to/mspondee/teleogical-programming-11gm</link>
      <guid>https://dev.to/mspondee/teleogical-programming-11gm</guid>
      <description>&lt;p&gt;I would comment an exceptional use of exceptions but probably would not stress over such a decision otherwise.&lt;/p&gt;

&lt;p&gt;I try to think about programming concepts in a descriptive rather than a prescriptive fashion.&lt;/p&gt;

&lt;p&gt;For example, I prefer "const variables in C permit the type checker to disallow writes and possibly link static constants into read only sections" over "a const variable in C is prescribed for read only purposes."&lt;/p&gt;

&lt;p&gt;Compilers do not understand the intent behind variable names and they do not understand the intent behind for loops, goto, exceptions and all the rest of program structure. Compilers do not even understand the intent of the language designer. Compilers only do what they were programmed to do.&lt;/p&gt;

&lt;p&gt;Sometimes teleogical thinking is important such as for understanding what uses of an API are supported but I try not to get locked into it.&lt;/p&gt;

&lt;p&gt;Being locked into a language or API designer's vision also locks you into their mistakes. Dennis Ritchie is not an authority on the correct or permitted use of for loops. There is no correct or prescribed use of for loops just only what for loops actually do. What classes or any other language construct ought to be used for is a similarly pointless discussion.&lt;/p&gt;

&lt;p&gt;I try to see things for what they are not what they ought to be.&lt;/p&gt;

&lt;p&gt;Exceptions are not for exceptional conditions. Exceptions are a syntax construct. Teleogical thinking mistakes programming languages for human languages. Describing what a program is intended to do is for comments.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Never Inline Hack Java</title>
      <dc:creator>Molossus Spondee</dc:creator>
      <pubDate>Fri, 03 Apr 2020 23:50:59 +0000</pubDate>
      <link>https://dev.to/mspondee/never-inline-hack-java-53m6</link>
      <guid>https://dev.to/mspondee/never-inline-hack-java-53m6</guid>
      <description>&lt;p&gt;As of occasion you may need to ensure some Java code is never inlined when debugging performance problems.&lt;/p&gt;

&lt;p&gt;The following hack works around this by abusing a special exception for the Throwable constructor.&lt;/p&gt;

&lt;p&gt;static  T dontinline(Supplier supplier) {&lt;br&gt;
    return (T) new Throwable() {&lt;br&gt;
       Object sneak = supplier.get();&lt;br&gt;
    }.sneak;&lt;br&gt;
}&lt;/p&gt;

</description>
      <category>java</category>
      <category>performance</category>
    </item>
    <item>
      <title>Nevertheless, Molossus Spondee Coded</title>
      <dc:creator>Molossus Spondee</dc:creator>
      <pubDate>Tue, 03 Mar 2020 20:19:18 +0000</pubDate>
      <link>https://dev.to/mspondee/nevertheless-molossus-spondee-coded-1nll</link>
      <guid>https://dev.to/mspondee/nevertheless-molossus-spondee-coded-1nll</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;be me&lt;br&gt;
be born in Vancouver&lt;br&gt;
high cost of living&lt;br&gt;
dirt poor&lt;br&gt;
second son of the family&lt;br&gt;
my dad was a failed starving artist&lt;br&gt;
my mother worked at data entry at a bank&lt;br&gt;
she was fat with a broken back&lt;br&gt;
and at home they didn't do much but eat and watch TV&lt;br&gt;
I mostly grew up alone on books and the computer&lt;br&gt;
at school I was never really bullied&lt;br&gt;
I'd come home and my parents would ask&lt;br&gt;
Were you bullied today at school?&lt;br&gt;
Nothing happened at school&lt;br&gt;
I just hid behind the back at lunch and did nothing&lt;br&gt;
all of my life just nothing&lt;br&gt;
I got a job below my skills and paid my way through college&lt;br&gt;
but the rage ate away at me&lt;br&gt;
I would walk along the streets for hours&lt;br&gt;
just walking away the rage&lt;br&gt;
I had violent and suicidal and sexual and jealous thoughts&lt;br&gt;
I began to suffer at college&lt;br&gt;
I sat in a haze at the library through exams for courses I hadn't been to all semester&lt;br&gt;
but I pushed through&lt;br&gt;
I got an Associate's Degree in CS&lt;br&gt;
I worked part time 10 hours a week&lt;br&gt;
barely&lt;br&gt;
are a job that didn't really use my skills&lt;br&gt;
at night I would feel like I was choking on the smell of burning garbage&lt;br&gt;
so I would go for long walks&lt;br&gt;
I started writing poetry&lt;br&gt;
just sitting at the library and writing poetry all day&lt;br&gt;
I developed weird pains through out my body&lt;br&gt;
I always had body issues&lt;br&gt;
and when I was 24 I got badly sunburned&lt;br&gt;
I freaked out and went to the emergency room&lt;br&gt;
fully convinced I was dying of skin cancer&lt;br&gt;
I kept going on in a fog&lt;br&gt;
in pain and suicidal everyday&lt;br&gt;
I was convinced my face was swelling up oddly&lt;br&gt;
it was round about a year after that&lt;br&gt;
I began meditating&lt;br&gt;
and it really saved my life&lt;br&gt;
I became really accomplished&lt;br&gt;
I meditated a lot&lt;br&gt;
I had to because I was in so much pain&lt;br&gt;
but soon the choking rage came back&lt;br&gt;
eventually I bought a skirt&lt;br&gt;
and it was when I put it on I had to admit I was trans&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;There's no conclusion here except maybe things didn't have to be like this and you don't really know what handicaps people are struggling with just by looking at their skin or apparent gender and I don't really believe in bad guys and punishment anymore. When you've been to that point where you're a danger to yourself and others you know it's already punishment enough.&lt;/p&gt;

&lt;p&gt;Things are getting better. I'm working more. I hope I'll be able to get a job making real use of my coding skills eventually.&lt;/p&gt;

</description>
      <category>wecoded</category>
    </item>
  </channel>
</rss>
