
    
        
        
        
                
        
        
        
            
{"version":"https:\/\/jsonfeed.org\/version\/1","title":"mathspp.com feed","home_page_url":"https:\/\/mathspp.com\/blog\/tags\/haskell","feed_url":"https:\/\/mathspp.com\/blog\/tags\/haskell.json","description":"Stay up-to-date with the articles on mathematics and programming that get published to mathspp.com.","author":{"name":"Rodrigo Gir\u00e3o Serr\u00e3o"},"items":[{"title":"Verifying tautologies with Haskell","date_published":"2019-06-14T00:00:00+02:00","id":"https:\/\/mathspp.com\/blog\/verifying-tautologies-with-haskell","url":"https:\/\/mathspp.com\/blog\/verifying-tautologies-with-haskell","content_html":"<p>This post gives the source code for a small Haskell program that finds if a formula is a tautology.<\/p>\n\n<p>A <a href=\"https:\/\/en.wikipedia.org\/wiki\/Tautology_(logic)\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">tautology<\/a> is a formula that is true regardless of the logical value we attribute to the variables it contains. For example, the propositional formula <span class=\"mathjax mathjax--inline\">\\(P \\vee \\neg P\\)<\/span> is <strong>always<\/strong> true, regardless of <span class=\"mathjax mathjax--inline\">\\(P\\)<\/span> being true or false. But not every tautology is as simple as the one I just showed. For example, showing that<\/p>\n<p class=\"mathjax mathjax--block\">\\[\n(P \\implies Q) \\implies [(Q \\implies R) \\implies (P \\implies R)]\\]<\/p>\n<p>is a tautology requires a bit more thought. But thinking is too tiring, so let us write a Haskell program that takes a formula and decides if the formula is a tautology or not! (By the way, said program is available <a href=\"https:\/\/github.com\/rodrigogiraoserrao\/projects\/blob\/master\/misc\/tautologies.hs\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">in my GitHub<\/a>...) First, we need to be able to represent a proposition, and we will create a data type just for that. We will call our type \"Prop,\" from proposition, from propositional logic.<\/p>\n<script src=\"https:\/\/gist.github.com\/rodrigogiraoserrao\/4b3f30b5e77584a354e1037ef7d1566e.js\"><\/script><p>where we provided data constructors for all sorts of propositional formulas. The idea is that now the formula <span class=\"mathjax mathjax--inline\">\\(P \\vee \\neg P\\)<\/span> is represented as <code>Or (Var \"P\") (Neg (Var \"P\"))<\/code>.<\/p>\n<p>Now we have a way of representing formulas. What we are left with is having a way of evaluating those formulas to check if they are tautologies... Propositions will have variables, so we will need to collect the variables of a proposition, generate all possible combinations of values to attribute to all the variables, and then evaluate the logical value of the proposition with the given attribution. Notice that most of our functions will make use of the structure of the data <code>Prop<\/code> in order to do some sort of \"recursion,\" but on the structure of the proposition. For example, if you have a proposition <code>p = Or p1 p2<\/code>, then the variables in <span class=\"mathjax mathjax--inline\">\\(p\\)<\/span> are the variables in <span class=\"mathjax mathjax--inline\">\\(p_1\\)<\/span> concatenated with the variables in <span class=\"mathjax mathjax--inline\">\\(p_2\\)<\/span>.<\/p>\n<script src=\"https:\/\/gist.github.com\/rodrigogiraoserrao\/f064060e9524cc3ed41b2ac3e50f5950.js\"><\/script><p>Now that we have all variables, we can generate all the possible combinations of values they can take. If we only have one variable, that is <span class=\"mathjax mathjax--inline\">\\(T, F\\)<\/span>. If we have two variables, that's <span class=\"mathjax mathjax--inline\">\\(TT\\)<\/span>, <span class=\"mathjax mathjax--inline\">\\(TF\\)<\/span>, <span class=\"mathjax mathjax--inline\">\\(FT\\)<\/span>, <span class=\"mathjax mathjax--inline\">\\(FF\\)<\/span>. If we have three variables, ... I define the function that generates all possible combinations of true\/false values and then put them together with our variables.<\/p>\n<script src=\"https:\/\/gist.github.com\/rodrigogiraoserrao\/c327f8ae1e58aa547f9616087dc3e001.js\"><\/script><p>And now that we can define attributions for our variables, we can write a function that takes a proposition and an attribution, and evaluates the proposition according to the attribution! Our function will have the signature <code>Prop -&gt; Attribution -&gt; Maybe Bool<\/code> because we cannot know for sure that the attribution that is passed in contains all the variables present in the proposition.<\/p>\n<script src=\"https:\/\/gist.github.com\/rodrigogiraoserrao\/7d0aee9510ff02dcac955c56851a6f32.js\"><\/script><p>We are very close to the finish line! Given a proposition <span class=\"mathjax mathjax--inline\">\\(p\\)<\/span>, how do we check if it is a tautology? Well, we should generate all of the possible attributions for it, evaluate each one of them, and check if all the attributions made...<\/p>","summary":"This post gives the source code for a small Haskell program that finds if a formula is a tautology.","date_modified":"2025-07-23T16:49:02+02:00","tags":["haskell","logic","programming"],"image":"\/user\/pages\/02.blog\/verifying-tautologies-with-haskell\/thumbnail.webp"},{"title":"Egyptian multiplication with Haskell","date_published":"2017-10-25T00:00:00+02:00","id":"https:\/\/mathspp.com\/blog\/egyptian-multiplication","url":"https:\/\/mathspp.com\/blog\/egyptian-multiplication","content_html":"<p>Progress is great and new things are always exciting... but that doesn't mean old things don't have any value!<\/p>\n\n<figure class=\"image-caption\"><img title=\"Photo by Crissy Jarvis on Unsplash\" alt=\"A picture of an abacus\" src=\"\/images\/9\/4\/6\/4\/9\/946498f9c4af0c00e8c324b15c21066b6dd99b8d-abacus.jpg\"><figcaption class=\"\">Photo by Crissy Jarvis on Unsplash<\/figcaption><\/figure>\n<p>Very recently I watched a <a href=\"https:\/\/www.youtube.com\/watch?v=CwT9oZtfRYw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">Youtube video<\/a> posted by my friend <a href=\"https:\/\/www.youtube.com\/channel\/UC5RV_s1Jh-jQI4HfexEIb2Q\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">MathGurl<\/a>, in which she explained the ancient Egyptian multiplication method. At the same time, and for no particular reason, I remembered Haskell, so I decided to implement the method. It was not a big feat of programming, but I did enjoy relearning the basics of Haskell I once knew. You can find the file with the implementation in <a href=\"https:\/\/github.com\/RodrigoGiraoSerrao\/projects\/blob\/master\/misc\/egyptianMult.hs\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">this GH file<\/a> or right here:<\/p>\n<script src=\"https:\/\/gist.github.com\/RodrigoGiraoSerrao\/6df9bd9c9e0f16e2e6a7a4948f2020e3.js\"><\/script>\n<p>(The implementation only works for non-negative integers)<\/p>\n<p>The method is quite simple and works because of the binary expansion of a number. Basically, if you want to calculate <span class=\"mathjax mathjax--inline\">\\(a \\times b\\)<\/span>, either <span class=\"mathjax mathjax--inline\">\\(b\\)<\/span> is even or odd. If <span class=\"mathjax mathjax--inline\">\\(b\\)<\/span> is even, just cut <span class=\"mathjax mathjax--inline\">\\(b\\)<\/span> in half and duplicate <span class=\"mathjax mathjax--inline\">\\(a\\)<\/span> to compute <span class=\"mathjax mathjax--inline\">\\((2a)\\times(\\frac{b}2)\\)<\/span>. If <span class=\"mathjax mathjax--inline\">\\(b\\)<\/span> is odd, then <span class=\"mathjax mathjax--inline\">\\(ab = a + (2a)\\times\\frac{b-1}2\\)<\/span>. Another way of thinking about this is by writing <span class=\"mathjax mathjax--inline\">\\(b\\)<\/span> in the form<\/p>\n<p class=\"mathjax mathjax--block\">\\[\nb = 2^{k_1} + 2^{k_2} + \\cdots + 2^{k_n}\\]<\/p>\n<p>and then having <\/p>\n<p class=\"mathjax mathjax--block\">\\[\na\\times b = a(2^{k_1} + 2^{k_2} + \\cdots + 2^{k_n}) = a2^{k_1} + a2^{k_2} + \\cdots + a2^{k_n}.\\]<\/p>\n<p>If I muster the courage to do it, I might also redo the functions in the <a href=\"\/blog\/recursion-by-kleene\">post about Kleene recursive functions<\/a>, in Haskell...<\/p>","summary":"A short post about an ancient multiplication method.","date_modified":"2024-08-13T12:47:46+02:00","tags":["mathematics","algorithms","haskell","programming"],"image":"\/user\/pages\/02.blog\/egyptian-multiplication\/abacus.jpg"}]}
