Scale By The Bay 2018 Unconference: Gabriel Claramunt
Recording: Scale By The Bay 2018 Unconference: Gabriel Claramunt
you this presentation is about the library I've been working on for a couple of years it's more like an libraries can experiment and more like a presentation is like a plea for help the series I started like in 2013 I took I went back to school because they have a very cool fresh repairing department I took this from the category theory course that has half category theory and then genetic programming it was mind-blowing and was really interesting and part of the part of the part of the course was a generic Brahman kind of a library was a small Haskell program that does generic programming using the techniques that we saw in the course it was kind of bored and a year later I was kind of four more and I decided to translate this to Scala and then I started like run against all with criminal cases find out that I did I know barely about the Scala compiler but I somehow managed to get it work but it has a cup of ugly casts because type information got lost in the middle one day I showed that to my science and up my saw me it should be able to get rid of those cast I couldn't that's what my goal for the last two years but something like every a couple of months I go back to these thinking about it try something something doesn't work I put again in the freezer but anyway still I like the ideas and I wanted to present a little for what I could cool understand is pretty it's almost the same as matryoshka just with different names also almost the whole thing is very there's a couple of things first this is kind of the categorical definition of fault that based on on one initial algebra you have the fault is the isomorphous between that and all the other algebras that means basically we are the initial our algebra the Constructors with four we are replacing all structures with different functions but one thing we can do also is we can create a large abre that describe the data types if an so if I recall correctly it is there's an evolution you can take every constructor you have and extract something called Sigma algebra the basic is an algebra that counts how many parameters you have an IVA constructor and then from those cost structures from that Sigma algebra is off of that into a font or algebra basically you add a parameter and then now you have fun tours and then you can apply for the decomposition for the regular cases like this all these are actually fun tours but you need one unit for the empty values constant to hold values I have an identity filter for the recursion recursive type the left injection when you have alternatives this is represent the left side the writing section of the sum when you have alternatives that represent a the right side and when you want to put two things together you have the product that's the thing missing is seeing an example we have the I think I have the list definition here we're gonna have well they should be easier let's say we have a definitive list this of a Haskell dish we have near or least of the response of a list of a kind of right well we want is we want to describe this with these founders so first we have the end the thing that doesn't have any value does the you does the the left side of something so this is actually a some and and the right side we have a combination of one element with the recursive type so it's one one element we need the constant to keep the element and combine it with a recursive with the recursive calls i we can do the same with the without three three of a they say you have the leaf of a or three branch of three of a and three of a the way we describe this is okay in one side we have the constant holiday the value otherwise we have a recursive type combine it with another recursive type the thing is with this with this algebra we can describe a lot of kind of is like regular types what you're going to do now is we can define a phone for this this algebra so if we have a way to go from any of those regular draggable types to this algebra and then we have a fall for this algebra we can have only one fall that falls all kind of these kind of other structures and that's what's kind of what this Haskell code does doesn't use first describe the great data types for the for the algebra and then it has the interesting that for me was complicated a user type families to go from the regular type to this decomposition and from the decomposition to the regular type and then it defines so here is the the generic form this is actually I check it once and this is the same code as the because the way they move yeah fix fix is in colors fixed because I was trying to compare because master's class seems way better I was trying to extract some trips some pigs some tips and tricks and I check that way anyway so I said one day I started writing this color code for that first my first problem was color doesn't have time families but by the way we are using here I actually can combine the I can use type class that actually has a type and that will be the the use of the type family in the indigo but I didn't want to depend because this was an experiment that they want to happen in any external library so I like everything is bespoke hand coded custom-made and locally salsa I go in my own font or class I start to plate the case classes for the elements I want constant and the the front or class for constant the unit the photo class for units the identity the sum with left and right the filter for the sum and the product and the filter for the sum this one I'm actually not using it but this is the composition of different filters I'm not using for the color habit so far this this part work help relatively good this is kind of the core of the whole thing when I defined my regular class data type that takes your type and creates a generic version of that we have I need the actual description of the type the filter for that and the functions that go from back and forth on that with that I can write my generic fault that takes and this is what I got really crazy because I was trying to emulate I was because I was following the Haskell code I may end with a in a painting myself into a corner but we never have the regular for the data type and then I need the actual this called the pattern filter that is the description in in our small components but I need to protect also I need to pass the the value that will be output of a fault for that I just need to ask that is a I need to have the regular for that and this is this is the function are going to fall from the pattern description of that of the of our type generic into an a and that's what it is called does this kind of works surprisingly for me at least so then I went to data types I create a couple of simple things I create a a tree that is not part of the trial that is fixed on it doesn't have any parameter so I create a at the regular type class for that tree with a description of the the font or ideally it should be like this the type of the tree is the type of the left plus like some identity product identity oh yes sorry yeah forgot that yeah that's good enough but turns out this works scholar recognized it I was worried I didn't recognize it but if I write it like this it doesn't find the implicit filter for that I showed this to Daniel's three when they say oh yeah this is a bag in the implicit or a solution that doesn't work it turns out if you do it like this it works if you go step by step it works anyway what's good for me then I have to write how do how do from the this tree of integers to this pattern filter so if I have a leaf I have the left of a constant of that value we have if I have a node branch had the right side of the product of about two identities so there sighs it's just the same thing in the opposite way right from left constant go to the leaf and from right product go to the node one thing I actually work is also I can use the regulars skull at least I don't have to create my own time I just can plug the skull at least again at least as the unit or the product of a constant and the regular type we do the same trick we decompose that into the compress of the implicit world Neil is the left side of unit right is X X X X like have dances the right side of the product of the head with the tail back and forth I defined here my binary tree just just to try different things right we have the leaf and the node this is almost the same as a tree before but this is parametrized again it works I mean I can write this I would not say it works but I can write this it compiles this is something I was still working on I can use by phone tours to work on types that has different types inside like the example is a rose tree that is a tree of list and a tree of list of trees here with the simple regular phone tours we cannot represent that but we want ours yes because one side you hold the parameter and the other side you had the the different type and then you go recursively but I wanted I was started walking to that I got I got block I said okay let me try to first make the simplest case work so yeah I know a dish of that but then we have kind of all the elements in place so in right now to make it work I have an example I've had this intention tree we just to live one and to have the other tree that has parameters this again parameterize it to integer defined here like no no leaf whatever and just a plain old scala list and I'm going to define in this case I want to find a sum sum of all the elements of that of the datatype using my components so the sum of the empty thing is 0 the sum of the constant is a content of the constant the sum of the the identity is just a it did solve this basically as a marker to just keep recursing the sum of left side is again the sum of what is inside the sample right side the same and the combination is in this case is just a sum of the result of some in both sides that's this kind of thing I'm not happy with this is get really ugly and you have to put the types otherwise so what is inferring any maybe as I'm asking too much from the Scala compiler I don't know I just trying to to make it a little neither I can do the same this is my some function here I use fold with some just I use just a different data type but the same fall function in the same sum function I can try the same four count basically the count of empty 0 the count of K is 1 and all the other things are kind of the same I can use it for max that's priest almost like the Sun except that when I go a combination as just select the max 1 I was trying also flatten that takes any data type and writes it into a list because again the for the empty cases Neil is a empty list when I have one element is the list with an element and when I combine two elements to two types I just got a the list there's something I was trying this writes everything into a string this is checks I can use to check boolean properties but let me run it I mean basically you see the code is the same shape always and I can run the same thing for for this different of the types so if I run it I get a lot of warnings saying okay you're explicitly saying the types here you're lying to me let me put that bigger when in spite of the complaints and the lying and whatever it works I can calculate the sum of the trained of the three parameters parameterized the need of a list I can count the elements I can get the max I can flatten it to at least I can share lives into a string I can get all the I can check our property for each element I can check a property that comprise all the elements because I quit I define a lot like for all and exist I can check yeah this is the all and this is the exist so yeah so beside that this this is probably 70 everything here is like duct tape and glue and whatever is kind of horrible to look but I think the the the idea itself is really amazing I think is what the masters Colossus with only one fault only one function to fold your types you can use any other type and have only one function it will work you don't have if you change your another type you don't have to change your fault function yeah that's what I wanted to show and if somebody had I has ideas I'm totally open to that thank you anyone questions or whatever yeah one of the ideas I had once to make it once I make this previous like this step is pretty mechanic I mean you can write it when you're close I was trying to going to look at if we can write a macro that generate this because this like it's pretty mechanic you can describe a Sunday on the Constructors you can analyze the Constructors generate your regular components and also write their the functions well probably that we um before like fixing those catch would be interesting too that was yeah smart guy cool well thank you