 Well, good evening and welcome to the session on pure functional programming on JVM with Freak. My name is Nareesha and I work for a company called Channel Bridge. Well, Freak, you can call it as, you know, I mean there are two ways to look at it. One has Haskell on JVM and another one is somebody who is working on the JVM and want to want a tool or a platform where you can do pure functional programming, then what capability do you have or what are the options available. So I'm mostly presenting from the second point of view because that's my background and especially because if you're already in Haskell programmer then you'd have a certain existing code. So you want all those code exactly to be run on JVM etc. But as you wouldn't have that constraint if you are on JVM and you want a better tool for functional programming. Before I get started I just want to get a feel of how many are actually already programming in closure Haskell anything of that sort. Many are mostly new to functional programming or programming in functional style. Well, I'll mix up that. Well, so interestingly the reason how I got led to this is can we do functional programming or with discipline essentially something like immutability versus when it is enforced by the platform. Some of the typical perceived benefits like ability to keep on changing the variable. When you sacrifice that obviously you are forced to think in a different direction and you get a lot of benefits for your programming. With that effort, I mean as a platform for platform sensor tool on JVM for programming in pure functional way got discovered free which is interestingly initially it got started as if I use the way back machine on June 1st, 2014. So it's a programming language in the spirit of Haskell but if you today it's mostly free is Haskell for JVM that's the kind of evolution it went through. With that I'll straight away get started. So let me start with the simple program first. Let's get started. So this is my structure right now I just have a hello and a leap. So I'm going to compile my free program. Let me show the structure before. So I created my program in hello.fr and when I compile essentially which will call you know there is no free C as such executable available but it's calling Java program which Java intern we will use the free library which will internally call the Java compiler to translate this. So what you see is that a file called hello.java got created and which then got compiled into hello.class and then I ran hello.class so that essentially that's the structure. So if you just want to take a look at our site work see so that's the generated Java code which then gets compiled into byte code. So essentially the way Freak works is unlike many of the languages on JVM first it compiles into Java source code then it you know using the typical Java compiler it gets compiled into byte code and from there you run it that's how it's going to work. It comes with the REPL there are quite a few options for the for the REPL. So we have the you know REPL which you can download from the Freak GitHub page and you can use that that's one option. You also get Java effects REPL. So if you want you can use that. Also there is online REPL available at try.freaklang.org so you can use any of these options. So for most of my talk I'll stick to the REPL what I have which is simple idea. So any questions so essentially I mean if you look at the Java code it has a lot of it can have other classes also. So a lot of things are created by Freak itself. So your class files would represent that structure. All right. So if you look at the structure so in my file I gave fc.hello. So under my build you would see fc got created directory and hello.class. So the last part will be converted to your class file will be named the last part. Previous will be your package structure. Okay so let's get it to REPL and let me create a function. So you would see Freak is a statically compiled language I mean since it's an implementation of a scholar that would be obvious but for somebody who is coming from any other Java or any other dynamic languages on Java background. So it's a statically typed and it's the type you don't have to specify the type you know the compiler can infer the type here. So you would see it inferred it as a number. So let me just call say 1020 that's going to work and say so that's essentially going to work. So having said that if you want to specify the type you always have the option to specify that say add int right. So I can say it's from int to int I can say yeah now it's going to give a compilation error because it has to be an integer. So you have the option to specify it or a compiler can infer that. So next one of the very important you know part for somebody who is coming from any other functional style language could be about the purity. So let me just create one more file let me create same then I'll create one more function here I would say which I would call from. So by default I mean it will not allow you to introduce side effects in your code if you want you have to enforce it explicit explicit about that you want your function to have side effects. So yeah now it is compiled so I can so by default you know functions cannot introduce side effects so which will give you additional advantage you know to make sure things are. Now let's get back to the repel so we have already wrote the function add you know you can call something like this and interestingly if you call the function just with one argument instead of multiple one it's going to you know create a function I mean it's a partial application of function so where you just specify you know single argument is going to create on the function so I can just create say add 10 now I say add 10 and say 20 so by default even when I wrote add 10 20 so first it's going to apply add on 10 so right as a result you will get a function which will be then applied to you know 20 so function application will have a higher precedence so if you want to change it you will have to you know explicitly you can use so parents is for example say now I want to add this to one more right so instead of you want to say add 10 result or there's another way to I mean if you go on doing this the code will not look very good so instead of that you can be you know use a dollar same result a bit wherein you don't have to really specify that parents questions yeah I mean they have implemented the whole you know the since JVM by default doesn't have way to lazy execute and all those things they have implemented in Java then again they have implemented preload in the language it's free itself yes it is lazy you know there's a basic implement implementation of the language in Java the libraries are implemented in the language free case yeah mostly yeah they implement Haskell 2010 report but it may not be hundred percent compatible with GHC as of now so if you go to the GitHub there's a page where they list these are the differences so especially for Haskell programmers okay so let's get into function of composition maybe let's make another function all increment 10 11 so now say I want to increment twice right increment of increment so if you want to create a function by composing two functions in this case I can just use the same function twice it's going to give me another function now to 10 that will give me so you can take two functions composed to get another function which you can then apply that's how you would do composition so we have seen the current so interestingly the name free girl you know it's a it's named after a German mathematician called Gottlob Freak who was the first one to you know propose some Korean well so let's get into working with the list so which will also explore a bit laziness and we would see laziness in detail also so now say you can always give the complete numbers like this specify list 1 2 3 4 or say I just want now specify a range and you also have the option of you know having infinitely so if I don't use it nothing will be evaluated so this are lazy similarly you can specify say so if you just want even numbers first in this way I'm not pretty sure what is the implementation whole languages so so again I mean as the function previously we have seen any of those functions so let me just do this that will give me the Java code so you would see final lazy being used so to implement that essentially some of you know these are some of the structures they use so you would see there's a lazy interface so you know most most of them are it's smaller so you would see the same you know you should see that the method signatures it would have all of them so it would not be a you know your regular int or that's because it has to be lazy okay so let's just combine some of these principles and let's say we let's do the famous fizz bus in a smaller way so I mean I hope you know every third word will be fizz and every fifth word will be buzz and wherever you know they combine will have fizz bus that's how the pattern again so we can start with the same fizz and it will be of the pattern fizz so I can generate you know that pattern make it infinite by saying cycle similarly buzz I can create say and bus so still nothing has happened so now I want to create fizz bus so I can do say combine these two together with zip what I want to do is concatenate those two into one and so that will give me a list so now so you would see the third one is fizz fifth one is buzz and 15th one is fizz bus so next level would be you know putting a number instead of empty so essentially we have combined you know these things together okay so having since Frig is running on JVM an important aspect for the language is to interoperate with the Java language so if you have a Java library how do we use it from Frig and the way also so let's explore in an example on that create that's a very simple class which I mean which I mean rather a function increment which we want to use from Frig so here now you would see the helper class is available so let me write the free code so essentially say I want to create increment just want to use that assets so here I'll how to define that Java function or the signature which takes an integer and returns integer so since you know the function doesn't have any side effects let me put it as pure so let's assume the exact exactly I'm coming into that so let's let's I mean instead of writing on the function let's assume that it's having a side effect so I am not giving it as pure I'll go back and try to compile that so it's not having to do that because it's a IO unit right so if you're integrating with Java there is no magic way to figure out if the function is pure or not so it's up to the developer to specify that I'm writing a pure function or not writing the pure function and rest of the system will go with that whatever increment has illegal yeah that's error E no it is here right I can see it's directly assigned so I mean if I have to use it I have to use it in a context where all right yeah that yeah that seems like a good idea I'm not sure I'm not a hard of any implementation of that such I believe from the effort wise that will be more or less same I mean you have to specify it somewhere that this is a pure or not well interesting I don't try it out I think some but essentially if it doesn't return I mean it can be and now you know I mean IO unit it can't be a pure function so that's some IO unit yeah so this is about you know integrate using Java functions or Java code in from your free programs the other way is also possible but still the lot of work is going on so if you look at the so there are two versions of you know calling free code from Java you would see from 3.24 onwards they have something new and so essentially it will not be very straightforward you should be aware of some implementation or typically they call it as a free run type system so these type systems you need to be aware so that you know instead of passing simple types you would have to use wrap them in lazy and all these kind of implementations but yeah so with that effort it would be possible so mostly if your free implementation I mean whatever code you have written in free if you want to use it typically would require some sort of you know wrapper being written that is going to be extensively used so I think that would be the approach for yeah so I was talking about the differences yeah so here you have the difference between friggin has come so just mainly for people who are coming from the Haskell background kind of let me show that is what yeah you will have to use so you know Gradle or something like that so this will you know spit out I mean create Java files and then the class files then finally you can create a jar of it which can be done through Gradle yeah so essentially you know what I'm using you know is I mean this is the jar file so along with this jar file whatever jar file you have created if you can put that so essentially from Java you are accessing these things or at least Tomcat would have the bootstrapping code that's how it works by default so the whole the approach they have taken is your free code gets converted into Java code which you can it is possible but it would require some sort of boilerplate code as of now you will have to you know adopt those free type systems yes it's a jar since it gets converted into Java there should be binary combined yes because entropy at the Java level well some function signatures got changed or something like that right but but Scala doesn't go that way so since this is going through Java so that has to be maintained I mean unless you change the whole library definition itself different versions of Java or then you would have the new jar integrated I mean mostly if you have a jar that should be pretty much compatible I don't see any issue why because Java version has changed it will not become uncomfortable compatible but if a free version has changed and you are writing new code then obviously unless there is some breaking changes also well so the this repel what you saw this is written in freegeson with some JavaScript front-end so sometimes in case of errors you may get some jQuery errors I'm not aware of company who has I don't think anybody has admitted we are using the free but I believe you know the way to go would be to start a small since it digits well with the Java that we approach before going for a full flash oh yeah so I mean in this so see the it's essentially immutable so you can pass the same structure to any number of threads I'm not sure you know Haskell is just a can layer on Java I mean sort of not Haskell free or you can use Java I'm not sure Haskell directly I believe yeah so it's already using some sort of I mean laziness means you may not have to create a lot of things first of all the case would be if you you know do not have a structural sharing then say you take an object you try to change something so essentially it will create a new object that case but I mean there were some you know you know numbers on the garbage collection we showed is very less there should be some reference within I think so not really but that would also depend on the implementation so just because of language unless your implementation is good that there may not be 100% accurate but yeah that's I'm not sure yeah in terms of maturity I think it's already there for a few years and been slowly evolving and I think people are more confidently saying that it's a Haskell implementation on JVM than just saying that okay it's influenced by Haskell so I think yeah mostly it is compatible so that should be possible I'm not sure so I mean I'm focused on that so this page should list differences I'm sure that gap has reduced over a period of time and there are some additional things like I mean if you have so instead of seeing length so this type of notation supported here a lot of suggestions etc. this one? like this? yeah so I think there's already a plugin for Eclipse right now and there is a feature request on IntelliJ which has been recently accepted so maybe an year back or so there may be 10 or 20 people who might have voted for that it's crossed 100 and sure yeah so I mean in terms of the one limit I would see is there you can have only 65k methods per class so if you know this auto-generating stuff is adding more to that then that could be a problem but I mean the same problem exists in Android also and they use that you can use where some of the functions can be inlined and also I'm not sure of the complete feasibility because it's lazy and all but that's one option to make it feasible even after that well so yeah so essentially I want to end with saying that by you know sacrificing some of the perceived conveniences like you know mutability and all we are able to explore the whole new world which can lead to a lot of innovation and with the JVM being a proven target so you can always have the convenience of programming in the tool you want and finally run it on your preferred platform so that's all what I had for today thank you