<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Priyanka Mondal on Medium]]></title>
        <description><![CDATA[Stories by Priyanka Mondal on Medium]]></description>
        <link>https://medium.com/@priyankamondal-11139?source=rss-d178cdc8589------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*x31-PvVmxCYN8uA_.</url>
            <title>Stories by Priyanka Mondal on Medium</title>
            <link>https://medium.com/@priyankamondal-11139?source=rss-d178cdc8589------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 00:51:46 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@priyankamondal-11139/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Progress sensitive security]]></title>
            <link>https://priyankamondal-11139.medium.com/progress-sensitive-security-4910643c2ad6?source=rss-d178cdc8589------2</link>
            <guid isPermaLink="false">https://medium.com/p/4910643c2ad6</guid>
            <dc:creator><![CDATA[Priyanka Mondal]]></dc:creator>
            <pubDate>Thu, 15 Oct 2020 23:13:08 GMT</pubDate>
            <atom:updated>2020-10-16T00:57:53.696Z</atom:updated>
            <content:encoded><![CDATA[<p>This is also known as termination sensitive security. Termination is considered a covert channel as a terminating program might reveal some information to the attacker. A program should be progress/termination sensitive to prevent information leakage in this channel. This is enforced via the language semantics and type system. A progress/termination insensitive language reveals arbitrary information at termination.</p><blockquote>Termination Insensitive Noninterference (TINI)</blockquote><p>TINI treats a diverging trace indistinguishable from any other trace. This indistinguishability is called weak indistinguishability (which is also timing insensitive). While, on other hand, TSNI (termination sensitive NI) distinguish between nonterminating and terminating traces.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4910643c2ad6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Faceted Values]]></title>
            <link>https://priyankamondal-11139.medium.com/faceted-values-61e80dacb55c?source=rss-d178cdc8589------2</link>
            <guid isPermaLink="false">https://medium.com/p/61e80dacb55c</guid>
            <dc:creator><![CDATA[Priyanka Mondal]]></dc:creator>
            <pubDate>Thu, 15 Oct 2020 22:50:11 GMT</pubDate>
            <atom:updated>2020-10-15T22:50:11.249Z</atom:updated>
            <content:encoded><![CDATA[<p>This post is based on the <a href="https://users.soe.ucsc.edu/~cormac/papers/popl12b.pdf">paper </a>by Thomas Austin and Cormac Flanagan.</p><p>A value can be read by some entity who has the proper authority to read it. A value <em>&lt;k ? s : p&gt; </em>means an entity with authority label <em>k or higher </em>will see the value s, otherwise it will see <em>p.</em></p><p>They have evaluation semantics for faceted values. And they also have a concept of view.</p><blockquote>L: Faceted_Value → value</blockquote><p>Equivalnce of view, if V1 eqv V2 then L(V1) = L(V2)</p><p>start from section 3.2</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=61e80dacb55c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Secure Compilation]]></title>
            <link>https://priyankamondal-11139.medium.com/secure-compilation-ddf9df8c9082?source=rss-d178cdc8589------2</link>
            <guid isPermaLink="false">https://medium.com/p/ddf9df8c9082</guid>
            <dc:creator><![CDATA[Priyanka Mondal]]></dc:creator>
            <pubDate>Thu, 15 Oct 2020 22:44:36 GMT</pubDate>
            <atom:updated>2020-10-15T22:48:30.348Z</atom:updated>
            <content:encoded><![CDATA[<p>Compilers convert source language to target language. Sometimes they take one pass, and sometimes they take multiple passes. We need to make sure the compiled language in each pass retains all the security properties of the source language : The goal of secure compilation techniques is to ensure this particular thing. Secure compilation is generally represented with an equivalence relation. Source language e_s is equivalent to target language e_t is written as e_s~e_t. if there is an intermediate compiler step then e_s ~ e_i and e_i ~ e_t should hold and from transitivity we get e_s ~ e_i ~ e_t .</p><p>To prove the equivalence we need to be prove (i) e_s~e_t and (ii) e_t~e_s. To prove the first one, closure conversion is done for all lambda functions. To prove the both (i) and (ii) logical relations and translation types are used. If some target term is of translation type then only it can be back-translated to source term. Amal’s paper on “Noninterference for Free” gives a good insight where they convert DCC to System F. Her OPLSS lectures are worth listening to.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ddf9df8c9082" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Byzantine Fault Tolerance in Long-lived Systems]]></title>
            <link>https://priyankamondal-11139.medium.com/byzantine-fault-tolerance-in-long-lived-systems-a1ea25253e88?source=rss-d178cdc8589------2</link>
            <guid isPermaLink="false">https://medium.com/p/a1ea25253e88</guid>
            <dc:creator><![CDATA[Priyanka Mondal]]></dc:creator>
            <pubDate>Thu, 15 Oct 2020 17:54:47 GMT</pubDate>
            <atom:updated>2020-10-15T17:54:47.501Z</atom:updated>
            <content:encoded><![CDATA[<p>This paper is written by Rodrigous and Liskov.</p><p>There are two important liveness aspects in BFT. Fault detection and recovery. With remote attestation, i.e. secure hardware, fault detection is easy but recovery is not possible. With proactive recovery detecting faults is not possible but recovery from certain attacks is possible. Proactive recovery is when each node is monitored by a watch-dog periodically and restarted with reinitialised code upon some failure.</p><p>Their approach is to have a CM (config manager) that checks the finger prints. Upon receiving wrong signature or wrong messages they take action.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a1ea25253e88" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Haskell IO Monad]]></title>
            <link>https://priyankamondal-11139.medium.com/haskell-io-monad-b4a825b59269?source=rss-d178cdc8589------2</link>
            <guid isPermaLink="false">https://medium.com/p/b4a825b59269</guid>
            <dc:creator><![CDATA[Priyanka Mondal]]></dc:creator>
            <pubDate>Thu, 07 May 2020 06:01:00 GMT</pubDate>
            <atom:updated>2023-11-04T16:45:40.548Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="https://www.schoolofhaskell.com/school/starting-with-haskell/introduction-to-haskell/6-laziness">6: Laziness</a></p><h3><a href="https://www.schoolofhaskell.com/school/starting-with-haskell/introduction-to-haskell/6-laziness#side-effects-and-purity">Side effects and purity</a></h3><p>So, what’s really at issue here is the presence or absence of <em>side effects</em>. By “side effect” we mean <em>anything that causes evaluation of an expression to interact with something outside itself</em>. The root issue is that such outside interactions are time-sensitive. For example:</p><ul><li>Modifying a global variable — it matters when this happens since it may affect the evaluation of other expressions</li><li>Printing to the screen — it matters when this happens since it may need to be in a certain order with respect to other writes to the screen</li><li>Reading from a file or the network — it matters when this happens since the contents of the file can affect the outcome of the expression</li></ul><p>As we have seen, lazy evaluation makes it hard to reason about when things will be evaluated; hence including side effects in a lazy language would be extremely unintuitive. Historically, this is the reason Haskell is pure: initially, the designers of Haskell wanted to make a <em>lazy</em> functional language, and quickly realized it would be impossible unless it also disallowed side effects.</p><p>But… a language with <em>no</em> side effects would not be very useful. The only thing you could do with such a language would be to load up your programs in an interpreter and evaluate expressions. (Hmm… that sounds familiar…) You would not be able to get any input from the user, or print anything to the screen, or read from a file. The challenge facing the Haskell designers was to come up with a way to allow such effects in a principled, restricted way that did not interfere with the essential purity of the language. They finally did come up with something (namely, the IO monad). Forget about side-effects and monads for a moment and lets just see how IO works for Haskell. In haskell inputs/outputs are commands. Now, what do we mean by commands ? And, how is it different from expressions ?</p><p>Expressions are something that computes something. We can name the result returned from an expression by “let”. E.g. “let x = z+y”. Here “z+y” is an expression, and we are naming the return value of this expression as “x”. Where as “print (x)” is a command. This command prints the value of x on the screen. But how do we take inputs in Haskell ? It is done via the getline command. “in &lt;- getline”.</p><pre>main = do<br>  in &lt;- getline<br>  print(in)</pre><p>But in Haskell any command (IO or non-IO) should be inside a do block. See the example below:</p><pre>iofunc = do<br>  in1 &lt;- getline<br>  in2 &lt;- getline<br>  return (in1,in2)<br><br>main = do <br>  getio &lt;- iofunc<br>  print(&quot;the pair is:&quot; ++ getio)</pre><p>The “return” command is used to return the value from commands.</p><p>The do/return can be used for non-IO commands also. See the following example:</p><pre>noio x = do<br>  let y = x+x<br>  return y<br><br>main = do<br> y &lt;- nonio 100<br> print(y)</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b4a825b59269" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Haskell Lazy Evaluation]]></title>
            <link>https://priyankamondal-11139.medium.com/haskell-lazy-evaluation-4cfa20abbec5?source=rss-d178cdc8589------2</link>
            <guid isPermaLink="false">https://medium.com/p/4cfa20abbec5</guid>
            <dc:creator><![CDATA[Priyanka Mondal]]></dc:creator>
            <pubDate>Sun, 03 May 2020 22:00:40 GMT</pubDate>
            <atom:updated>2020-05-03T22:00:40.821Z</atom:updated>
            <content:encoded><![CDATA[<pre><strong>let </strong>x = z^2 <strong>in<br>    if</strong> cond<br>     <strong>then</strong> x+y<br>     <strong>else</strong> 1</pre><p>In the above code <strong>x</strong> is used only in <strong>then</strong> branch and not it <strong>else</strong> branch, and which branch is chosen depends on <strong>cond. </strong>So, it is totally unnecessary and waste of resources to evaluate x beforehand. It is evaluated only when the <strong>then </strong>branch is evaluated. The following code shows exactly when it is evaluated lazily.</p><pre><strong>let </strong>x = z^2 <strong>in<br>    if</strong> cond<br>     <strong>then</strong> z^2+y<br>     <strong>else</strong> 1</pre><p>Haskell uses lazy evaluation, i.e. arguments to a function are evaluated only when they are actually used. This is exactly opposite to <strong>strict evaluation</strong>. In strict evaluation, function arguments are fully evaluated before passing them to the function. Lazy evaluation can be thought of as call-by-name evaluation in lambda world. In Haskell, the unevaluated parameter to a function is called the <strong>“thunk”</strong>.</p><p>Lazy evaluation saves memory if memory allocation is done lazily. Means, memory is allocated only when something is stored in memory not when the allocation is declared (e.g. malloc in C). But at the same time, with lazy evaluation, it is hard to predict how much memory the program is going to use throughout the program.</p><pre>foldl :: (b -&gt; a -&gt; b) -&gt; b -&gt; t a -&gt; b<br>foldl _ z []     = z<br>foldl f z (x:xs) = foldl f (f z x) xs </pre><p>If we evaluate foldl (+) 0 [1,2,3] , it takes the following steps.</p><pre>foldl (+) 0 [1,2,3]<br>    = foldl (+) (0+1) [2,3]<br>    = foldl (+) ((0+1)+2) [3]<br>    = foldl (+) (((0+1)+2)+3) []<br>    = (((0+1)+2)+3)<br>    = ((1+2)+3)<br>    = (3+3)<br>    = 6</pre><p>The value of the accumulator is not required until the end. Thus we get the thunk(((0+1)+2)+3). After which it gets evaluated only when answer is required to be printed on the screen. The problem with this is in every time the program needs to remember a number. With a very large list (...(((0+1)+2)+3).....), this can result into a stack overflow. Haskell has <strong>foldl’</strong> function to get rid of this big thunk problem, which requires the second argument to be evaluated before every next step.</p><p>Lazy evaluation enables us to work with infinite data structures.</p><blockquote><strong>“Defining an infinite data structure actually only creates a thunk, which we can think of as a “seed” out of which the entire data structure can <em>potentially</em> grow, depending on what parts actually are used/needed.”</strong></blockquote><p>In the following code snippet, two lists are added element by element until one list is empty. <strong>repeat 3 </strong>creates an infinite list containing only 3.</p><pre>adds :: (Num a) =&gt; [a] -&gt;[a] -&gt; [a]<br>adds [] [] = []<br>adds [] _ = []<br>adds _ [] = []<br>adds (x:xs) (y:ys) = (x+y):(adds xs ys)</pre><pre>*Main&gt; adds [1,2] (repeat 3)<br>[4,5]<br>*Main&gt; adds (repeat 4) [2,5,6]<br>[6,9,10]<br>*Main&gt; adds (repeat 4) (reteat 3)<br><strong>**stack overflow**</strong></pre><p>The thunks are evaluated only enough so that the Haskell pattern match goes through.</p><pre>func1: [Int] -&gt; Int<br>func1 [] = 0<br>func x::xs = x + func1 xs</pre><pre>func2 : Int -&gt; Bool<br>func2 0 = True<br>func2 1= False<br>func2 x = func2 (x-2)</pre><p>For example, in the above example if we call <strong>(func2 (func1 [1,3,6,90]))</strong>, to pattern match it will be evaluated to <strong>(func2 100)</strong>, and will not be evaluated to <strong>True</strong>. It will be carried forward as <strong>(func2 100)</strong>, a thunk. So in a nutshell evaluation is done until it pattern matches and not more than that.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4cfa20abbec5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Haskell Higher Order Fu]]></title>
            <link>https://priyankamondal-11139.medium.com/haskell-higher-order-fu-7d3a3aaa15a3?source=rss-d178cdc8589------2</link>
            <guid isPermaLink="false">https://medium.com/p/7d3a3aaa15a3</guid>
            <dc:creator><![CDATA[Priyanka Mondal]]></dc:creator>
            <pubDate>Wed, 29 Apr 2020 04:25:44 GMT</pubDate>
            <atom:updated>2020-04-29T04:25:44.746Z</atom:updated>
            <content:encoded><![CDATA[<h3>Haskell Higher Order Functions</h3><p>A higher order function is a function that can take a function as a parameter and/or can return a function as its end result. In this post we will discuss a few popular Haskell higher order functions.</p><h3>Filter</h3><p>As the name suggests this function filters out data that satisfy some condition.</p><pre>filter :: (a -&gt; Bool) -&gt; [a] -&gt; [a]<br> filter _ [] = []<br> filter p (x:xs)<br>  | p x = x : filter p xs<br>  | otherwise = filter p xs</pre><p>p is a predicate function that checks whether a condition is true or not.</p><pre>Prelude&gt; filter (==3) [1,2,3]<br>[3]<br>Prelude&gt; filter (==3) [1,2,33]<br>[]</pre><h3>Map</h3><p>Map applies a function to every element of a given list</p><pre>map :: (a -&gt; b) -&gt; [a] -&gt; [b]<br> map _ [] = []<br> map f (x:xs) = f x : map f xs</pre><p>function <strong>f </strong>takes one parameter of type <strong>a </strong>and the elements input list that <strong>map</strong> function takes has to have type <strong>[a]</strong></p><pre>Prelude&gt; map (*5) [1,2,3]<br>[5,10,15]</pre><h3>Lambda</h3><p>Lambdas are the functions that are made on the go in the program and are anonymous. These functions are written as <strong>\x -&gt; body</strong></p><pre>Prelude&gt; map (\x-&gt;x*5) [1,2,3]<br>[5,10,15]</pre><pre>Prelude&gt; (\x-&gt;x [11,2,3,4]) head<br>11</pre><h3>Foldl</h3><p>If we check the type of <strong>foldl</strong> function in ghci, we get the following.</p><pre>Prelude&gt; :t foldl<br>foldl :: Foldable t =&gt; (b -&gt; a -&gt; b) -&gt; b -&gt; t a -&gt; b</pre><p>Foldable is a class in Haskell. A data structure is foldable when its an instance of Foldable class, i.e. it implements all the functions in Foldable class. Foldable data structures can be folded into a summary value.</p><p><strong>foldl </strong>function takes three arguments. The first one is a function of type (b -&gt; a -&gt; b). The second argument is called the accumulator. The third argument is in the context <strong>t</strong> (think of this as a wrapper). foldl folds <strong>(t a)</strong> left associatively with the help of the function argument and the accumulator.</p><pre>Prelude&gt; foldl (+) 4 [1,2,3]<br>10<br>Prelude&gt; foldl (+) 3 (Just 5)<br>8</pre><p>In the first example <strong>t a</strong> is [1,2,3]. The first argument is (+) (of type say Int-&gt;Int -&gt;Int ). foldl first takes 4 (the accumulator) and 1 and feeds it to (+), then again feeds the result (i.e. 5) as the accumulator and the second element, 2, to (+). This goes on until the list is empty. For the second example <strong>t a </strong>is <strong>Just 5.</strong></p><h3>Foldr</h3><pre>Prelude&gt; :t foldr<br>foldr :: Foldable t =&gt; (a -&gt; b -&gt; b) -&gt; b -&gt; t a -&gt; b</pre><p>foldr works just like foldl. But there are two differences.</p><ol><li>The accumulator is the second argument to the function argument.</li><li>Folding is done starting from the last element if <strong>(t a) </strong>(makes more sense when <strong>(t a)</strong> is a list)<strong>.</strong></li></ol><pre>Prelude&gt; foldr (+) 4 [1,2,3]<br>10<br>Prelude&gt; foldr (+) 3 (Just 5)<br>8</pre><p>The difference between foldl and foldr can be explained with the following example.</p><pre>Prelude&gt; foldl (-) 0 [1,2,3,4,5]<br>-15<br>Prelude&gt; foldr (-) 0 [1,2,3,4,5]<br>3</pre><p>The foldl and foldr happens the following way —</p><ol><li>foldl : (((((0-1)-2)-3)-4)-5)</li><li>foldr : (1-(2-(3-(4-(5-0)))))</li></ol><p>scanl and scanr functions are like foldl and foldr resepectively, but they return a list of all the intermediate results instead of just returning the folded end result.</p><h3>($)</h3><pre>($) :: (a -&gt; b) -&gt; a -&gt; b<br> f $ x = f x</pre><p>In Haskell when we write a (<strong>func x y z) </strong>it is same as writing <strong>(((func x) y) z).</strong> So, function written with spaces are left associative. If <strong>($) </strong>applies lefthand side of it to the righthand side. So, if we want to write <strong>(func (x (y z)), </strong>we can write it like <strong>func $ x $ y z. </strong>So, mainly its just work as same as parentheses. Python users are familiar with this already.</p><pre>Prelude&gt; (+) 100 $ foldl (+) 2 $ map (*5) [1,2,3,4,5]<br>177<br>Prelude&gt; (+) 100 ( foldl (+) 2 ( map (*5) [1,2,3,4,5]))<br>177</pre><h3>(.)</h3><p>This function is used for function composition. The function definition is as simple as the following.</p><pre>(.) :: (b -&gt; c) -&gt; (a -&gt; b) -&gt; a -&gt; c<br> f . g = \x -&gt; f (g x)</pre><p>Let us see examples</p><pre>Prelude&gt; let addmul = ((+3).(*2))<br>Prelude&gt; addmul 4<br>11<br>Prelude&gt; let plus3 = (+3)<br>Prelude&gt; let plus2 = (+2)<br>Prelude&gt; let plus1 = (+1)<br>Prelude&gt; (plus1 . plus2 . plus3) 4<br>10</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7d3a3aaa15a3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Haskell Curry ( and Uncurry)]]></title>
            <link>https://priyankamondal-11139.medium.com/haskell-curry-and-uncurry-31d52d642a1e?source=rss-d178cdc8589------2</link>
            <guid isPermaLink="false">https://medium.com/p/31d52d642a1e</guid>
            <dc:creator><![CDATA[Priyanka Mondal]]></dc:creator>
            <pubDate>Sun, 26 Apr 2020 02:11:32 GMT</pubDate>
            <atom:updated>2020-04-29T02:03:41.940Z</atom:updated>
            <content:encoded><![CDATA[<h3>Haskell Curry (and Uncurry)</h3><p>When we write that a function <strong>F </strong>has type a → b → c, we can say two things about it.</p><ol><li>the function takes two parameters of type a and b and returns a type c. [i.e. (a → b) → c ]</li><li>Or, the function takes a parameter of type <strong>a</strong> and returns a function of type b → c . [i.e. a → (b → c) ]</li></ol><p>By default Haskell function types are curried, meaning the types are all right associative, i.e. its like option 2.( Although the function application itself is left associative, and is discussed in Haskell Higher Order function post.) This helps Haskell to support partial forms. If a function is called with fewer parameters than actually required then we get back a function (called partially applied function) that takes the rest of the parameters that were not provided while calling the initial function. If we call function <strong>F</strong> with a parameter of type <strong>a</strong> then we get back the <strong>partially applied </strong>function of type<strong> b → c.</strong></p><pre>add :: Int -&gt; Int -&gt; Int<br>add x y = x+y</pre><pre>ghci&gt; let add2 = add 3<br>ghci&gt; add2 4<br>7</pre><pre>ghci&gt; add 3 4<br>7 </pre><p>In the above example, when we call <strong>add</strong> function with only one parameter, 3, it returns a function (we named it add2). When that function is fed another parameter 4, we get back the result. We could have just fed the parameters together, but internally it would do the same thing.</p><p>Haskell has inbuilt curry and uncurry functions, to convert uncurried functions to curried functions, and vice versa.</p><pre>curry :: ((a, b) -&gt; c) -&gt; a -&gt; b -&gt; c<br>uncurry :: (a -&gt; b -&gt; c) -&gt; ((a, b) -&gt; c)</pre><p>Let us see an example of uncurrying a curried function.</p><pre>cur1 :: (Num a) =&gt; a -&gt; a -&gt; a                                                           cur1 x y = x+y</pre><pre>ghci&gt;add1 = uncurry cur1<br>ghci&gt; add1 (1,2)<br>3<br>ghci&gt; add1 1<br><strong>**ERROR**</strong></pre><p>Now let&#39;s see the other way around</p><pre>uncur1 :: (Num a) =&gt; (a,a) -&gt; a<br>uncur1 x = fst x + snd x</pre><pre>ghci&gt; add1 = curry uncur1<br>ghci&gt; add1 1 2<br>3<br>ghci&gt; let add2 = add1 1<br>ghci&gt; add2 2<br>3</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=31d52d642a1e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Haskell Monads]]></title>
            <link>https://priyankamondal-11139.medium.com/haskell-monads-ddc333ef5efe?source=rss-d178cdc8589------2</link>
            <guid isPermaLink="false">https://medium.com/p/ddc333ef5efe</guid>
            <dc:creator><![CDATA[Priyanka Mondal]]></dc:creator>
            <pubDate>Sat, 25 Apr 2020 01:04:32 GMT</pubDate>
            <atom:updated>2020-04-26T00:28:44.700Z</atom:updated>
            <content:encoded><![CDATA[<p>Let us talk about the most popular typeclass in Haskell, i.e. monads.</p><p>The main usage of monads in Haskell is to make effects wrap inside a context, so that it has a type. Thus Haskell is a pure language not because it does not have side-effects but because every effect is typed in Haskell.</p><pre><strong>class</strong> Monad m <strong>where<br> <br> </strong>return :: a -&gt; m a<br>  <br> (&gt;&gt;=) :: m a -&gt; (a -&gt; m b) -&gt; m b<br>  <br> (&gt;&gt;) :: m a -&gt; m b -&gt; m b<br>  x &gt;&gt; y = x &gt;&gt;= \_ -&gt; y</pre><pre>fail :: String -&gt; m a<br> fail msg = error msg</pre><p>Monad class in Haskell has four functions. <strong>return</strong> converts an element of type <strong>a</strong> to a monad (recall <strong>pure</strong> in Applicatives). <strong>(&gt;&gt;=)</strong> function(read as “bind”) takes a monadic term, and a function (that takes a non monadic term and returns a monadic term), and returns a monadic term. <strong>(&gt;&gt;) </strong>and <strong>fail</strong> functions come with their default implementations. That’s why while creating monad instances it is not mandatory to implement <strong>(&gt;&gt;) </strong>and <strong>fail</strong> function.</p><p>An instance of Maybe monad can be like the following.</p><pre><strong>instance</strong> Monad Maybe <strong>where<br>  </strong>return x = Just x<br>  Nothing &gt;&gt;= f = Nothing<br>  Just x &gt;&gt;= f = f x<br>  fail _ = Nothing</pre><blockquote><strong>Maybe</strong> is functor, applicative and a monad as its an instance of all these three typeclasses.</blockquote><p>One can create their own monads. It is not absolutely necessary to be an instance Monad typeclass for it to be a monad. In fact anything that obeys the monad laws is a monad. These laws are as follows.</p><p>a) Left Identity — applying <strong>return </strong>to a value <strong>x</strong>, then applying the resulting value to (<strong>&gt;&gt;=</strong>) with a function <strong>f</strong> will give us the result same as <strong>f x</strong>, i.e.</p><blockquote><strong>return x &gt;&gt;= f </strong>is same as <strong>f x</strong></blockquote><p>b) Right Identity — this law is very simple. Say we have a monadic term (M a), and <strong>bind</strong> is of type <strong>M a → (a → M b) → M b . </strong>Then if we apply bind with <strong>M a</strong> and <strong>return </strong>(whisch is of type <strong>a → M a</strong>)<strong> </strong>then the result is <strong>M a, </strong>i.e. the result is same as the first input parameter. To put it in one sentence</p><blockquote><strong>M&gt;&gt;= return</strong> is same as <strong>M</strong></blockquote><p>c) Associativity — bind function is associative.</p><blockquote><strong>(m &gt;&gt;= f) &gt;&gt;= g</strong> is same as <strong>m &gt;&gt;= (\x -&gt; f x &gt;&gt;= g)</strong></blockquote><p>The following discussion is inspired from the paper <a href="https://page.mi.fu-berlin.de/scravy/realworldhaskell/materialien/the-essence-of-functional-programming.pdf">The essence of functional programming</a> by Philip Wadler. The following monads are not instances of Haskell Monad typeclass. Each of them implements three functions: unit, bind and show. They are monads because they follow the three monad laws.</p><ol><li>Identity Monad —</li></ol><pre>type I a = a</pre><pre>unitI a = a <br>a ‘bindI‘ k = k a <br>showI a = showval a</pre><p>2. Monad to add error messages to the interpreter of fig 1. in the paper.</p><pre>data E a = Success a | Error String</pre><pre>unitE a = Success a <br>errorE s = Error s <br>(Success a) ‘bindE‘ k = k a <br>(Error s) ‘bindE‘ k = Error s<br>showE (Success a) = “Success: “ ++ showval a <br>showE (Error s) = “Error: “ ++ s</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ddc333ef5efe" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Haskell Monoids]]></title>
            <link>https://priyankamondal-11139.medium.com/haskell-monoids-8b0c8f1d8c45?source=rss-d178cdc8589------2</link>
            <guid isPermaLink="false">https://medium.com/p/8b0c8f1d8c45</guid>
            <dc:creator><![CDATA[Priyanka Mondal]]></dc:creator>
            <pubDate>Sat, 25 Apr 2020 01:03:12 GMT</pubDate>
            <atom:updated>2020-04-25T01:03:12.666Z</atom:updated>
            <content:encoded><![CDATA[<p>First, let us see what algebraic structures are. An algebraic structure comprises of three parts: a set of elements, a set of operations on the elements with finite arity, and a set of identity elements. Monoid is an algebraic structure with an associative binary operation and an identity element. Examples of monoids would be (N,+,1), (Z,*,0), ([N],++,[]) etc.</p><pre><strong>class</strong> Monoid a <strong>where</strong>  mempty :: a<br>  mappend :: a -&gt; a -&gt; a<br>  mconcat :: [a] -&gt; a<br>  mconcat = foldr mappend mempty</pre><p>The Haskell implementation of Monoid is shown above. Unlike functors and applicatives it takes a base type,<strong>a</strong>, instead of a type constructor as a parameter. <strong>mempty</strong> is the identity element, and <strong>mappend </strong>is the associative binary function. mconcat can be ignored for the moment, as it comes with a default implementation and is not implemented for every monoid instance. If <strong>A </strong>is the set of elements of type a, then the monoid is (A,mappend,mempty).</p><blockquote><strong><em>mappend</em></strong><em> sounds like append but it can be any binary operation.</em></blockquote><p>Lists can be instance of Monoid.</p><pre><strong>instance</strong> Monoid [a] <strong>where<br>  </strong>mempty = []<br>  mappend = (++)</pre><p>The identity element is the empty list and the binary function is the append function which takes two lists of type [a] and returns a list of type [a].</p><p><strong>Ordering</strong> type is also an instance of Monoid. Ordering type can have three values, EQ, LT, GT.</p><pre><strong>instance</strong> Monoid Ordering <strong>where</strong><br> mempty = EQ<br> mappend LT _  = LT<br> mappend GT _  = GT<br> mappend EQ x  = x</pre><p>The result of mappend depends on the value of the first parameter. GT is greater than everything, LT is less that everything, and EQ of the second parameter is the second parameter itself.</p><p>Maybe is also an instance of Monoid. ({Maybe a}, mappend, Nothing)</p><pre><strong>instance</strong> Monoid a =&gt; Monoid (Maybe a) <strong>where<br>  </strong>mempty = Nothing<br>  mappend Nothing m = m<br>  mappend m Nothing = m<br>  mappend (Just m1) (Just m2) = Just (mappend m1 m2)</pre><p>To test on ghci</p><pre>ghci&gt; mappend (Just &quot;mom&quot;)  (Just &quot;dad&quot; )<br>Just &quot;momdad&quot;</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8b0c8f1d8c45" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>