Skip to content

Commit

Permalink
Use enums
Browse files Browse the repository at this point in the history
  • Loading branch information
kamil-adam committed May 7, 2024
1 parent 7d43700 commit 7d5a949
Show file tree
Hide file tree
Showing 12 changed files with 474 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ package pl.writeonly.catculator.core.adt.calculus

import pl.writeonly.catculator.core.adt.tree.*

enum Combinator:
case S
case K
case I

object Combinator {

type CombinatorT = Tree[Combinator]
Expand All @@ -17,10 +22,4 @@ object Combinator {
}

def generateC(c: Combinator): String = c.toString

case object S extends Combinator
case object K extends Combinator
case object I extends Combinator
}

sealed trait Combinator
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package pl.writeonly.catculator.core.adt.calculus

import pl.writeonly.catculator.core.adt.calculus.Combinator.CombinatorBT
import pl.writeonly.catculator.core.adt.tree.BinaryTree.*

object Constants {
val sCom: CombinatorBT = com(Combinator.S)
val kCom: CombinatorBT = com(Combinator.K)
val iCom: CombinatorBT = com(Combinator.I)
val trueCom: CombinatorBT = kCom
val falseCom: CombinatorBT = Node(kCom, iCom)

val appKS: CombinatorBT = Node(kCom, sCom)

val bCom: CombinatorBT = app3(sCom, appKS, kCom)

def app3SI(a: CombinatorBT): CombinatorBT = app3(sCom, iCom, a)

def appK(a: CombinatorBT): Node[Combinator] = Node(kCom, a)

def app4(
c1: CombinatorBT,
c2: CombinatorBT,
c3: CombinatorBT,
c4: CombinatorBT,
): CombinatorBT = Node(c1, app3(c2, c3, c4))

def app3(c1: CombinatorBT, c2: CombinatorBT, c3: CombinatorBT): CombinatorBT =
Node(c1, Node(c2, c3))

def com(c: Combinator): CombinatorBT = Leaf(c)

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package pl.writeonly.catculator.core.adt.calculus

import cats.implicits.catsSyntaxEq
import pl.writeonly.catculator.core.adt.calculus.Combinator.CombinatorBT
import pl.writeonly.catculator.core.adt.calculus.Constants.*
import spire.math.Natural

object InputEncoder {

def readInput(input: String): CombinatorBT = encodeInput(
input
.toList
.map { c =>
Natural(c.toLong)
},
)

private def encodeInput(input: List[Natural]): CombinatorBT = input
.foldRight(falseCom) { case (n, l) =>
cons(church(n), l)
}

def cons(a: CombinatorBT, b: CombinatorBT): CombinatorBT =
app3(sCom, Constants.app3SI(appK(a)), appK(b))

def church(n: Natural): CombinatorBT = n.toBigInt match {
case n if n === BigInt(0) => falseCom
case n => succChurch(Natural(n - BigInt(1)))
}

private def succChurch(n: Natural): CombinatorBT = successor(church(n))

def successor(c: CombinatorBT): CombinatorBT = app3(sCom, bCom, c)

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@ import cats.data.NonEmptyList
import pl.writeonly.catculator.core.Extras.fix
import spire.math.Natural

enum Lambda:
case Com(c: Combinator)
case Var(name: String)
case Abs(param: String, body: Lambda)
case App(f: Lambda, x: Lambda)
case Let(name: String, expression: Lambda, body: Lambda)
case MultiAbs(params: List[String], body: Lambda)
case MultiApp(fs: NonEmptyList[Lambda])
case MultiLet(ps: NonEmptyList[(String, Lambda)], body: Lambda)
case LocalScope(xs: NonEmptyList[Lambda])
case NilList(xs: List[Lambda])
case CharStr(s: String)
case NatNum(n: Natural)
case IntNum(s: Sign, n: Natural)

object Lambda {

def generateParams(params: List[String]): String = params
Expand Down Expand Up @@ -39,21 +54,4 @@ object Lambda {

def intNumFromString(sing: Sign, s: String): Lambda = IntNum(sing, Natural(s))

final case class Com(c: Combinator) extends Lambda
final case class Var(name: String) extends Lambda
final case class Abs(param: String, body: Lambda) extends Lambda
final case class App(f: Lambda, x: Lambda) extends Lambda
final case class Let(name: String, expression: Lambda, body: Lambda)
extends Lambda
final case class MultiAbs(params: List[String], body: Lambda) extends Lambda
final case class MultiApp(fs: NonEmptyList[Lambda]) extends Lambda
final case class MultiLet(ps: NonEmptyList[(String, Lambda)], body: Lambda)
extends Lambda
final case class LocalScope(xs: NonEmptyList[Lambda]) extends Lambda
final case class NilList(xs: List[Lambda]) extends Lambda
final case class CharStr(s: String) extends Lambda
final case class NatNum(n: Natural) extends Lambda
final case class IntNum(s: Sign, n: Natural) extends Lambda
}

sealed trait Lambda
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package pl.writeonly.catculator.core.adt.calculus

enum Sign:
case Plus
case Minus

object Sign {

def generate(s: Sign): String = s match {
case Plus => "+"
case Minus => "-"
}

case object Plus extends Sign
case object Minus extends Sign
}

trait Sign
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package pl.writeonly.catculator.core.adt.tree

object BinaryTree {

case class Leaf[A](leaf: A) extends BinaryTree[A]
enum BinaryTree[+A]:
case Leaf(leaf: A)
case Node(first: BinaryTree[A], follow: BinaryTree[A])

case class Node[A](first: BinaryTree[A], follow: BinaryTree[A])
extends BinaryTree[A]
object BinaryTree {
}

sealed trait BinaryTree[+A]
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ package pl.writeonly.catculator.core.adt.tree

import cats.data.NonEmptyList

enum Tree[A]:
case Leaf(leaf: A)
case Node(children: NonEmptyList[Tree[A]])

object Tree {
def node[A](head: Tree[A], tail: Tree[A]*): Tree[A] =
Node(NonEmptyList(head, tail.toList))

case class Leaf[A](leaf: A) extends Tree[A]

case class Node[A](children: NonEmptyList[Tree[A]]) extends Tree[A]
}

sealed trait Tree[+A]
87 changes: 87 additions & 0 deletions docs/_site/404.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<!DOCTYPE html>
<html lang="en"><head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1"><!-- Begin Jekyll SEO tag v2.8.0 -->
<title>Your awesome title | Write an awesome description for your new site here. You can edit this line in _config.yml. It will appear in your document head meta (for Google search results) and in your feed.xml site description.</title>
<meta name="generator" content="Jekyll v4.3.2" />
<meta property="og:title" content="Your awesome title" />
<meta property="og:locale" content="en_US" />
<meta name="description" content="Write an awesome description for your new site here. You can edit this line in _config.yml. It will appear in your document head meta (for Google search results) and in your feed.xml site description." />
<meta property="og:description" content="Write an awesome description for your new site here. You can edit this line in _config.yml. It will appear in your document head meta (for Google search results) and in your feed.xml site description." />
<link rel="canonical" href="http://localhost:4000/404.html" />
<meta property="og:url" content="http://localhost:4000/404.html" />
<meta property="og:site_name" content="Your awesome title" />
<meta property="og:type" content="website" />
<meta name="twitter:card" content="summary" />
<meta property="twitter:title" content="Your awesome title" />
<script type="application/ld+json">
{"@context":"https://schema.org","@type":"WebPage","description":"Write an awesome description for your new site here. You can edit this line in _config.yml. It will appear in your document head meta (for Google search results) and in your feed.xml site description.","headline":"Your awesome title","url":"http://localhost:4000/404.html"}</script>
<!-- End Jekyll SEO tag -->
<link rel="stylesheet" href="/assets/main.css"><link type="application/atom+xml" rel="alternate" href="http://localhost:4000/feed.xml" title="Your awesome title" /></head>
<body><header class="site-header" role="banner">

<div class="wrapper"><a class="site-title" rel="author" href="/">Your awesome title</a><nav class="site-nav">
<input type="checkbox" id="nav-trigger" class="nav-trigger" />
<label for="nav-trigger">
<span class="menu-icon">
<svg viewBox="0 0 18 15" width="18px" height="15px">
<path d="M18,1.484c0,0.82-0.665,1.484-1.484,1.484H1.484C0.665,2.969,0,2.304,0,1.484l0,0C0,0.665,0.665,0,1.484,0 h15.032C17.335,0,18,0.665,18,1.484L18,1.484z M18,7.516C18,8.335,17.335,9,16.516,9H1.484C0.665,9,0,8.335,0,7.516l0,0 c0-0.82,0.665-1.484,1.484-1.484h15.032C17.335,6.031,18,6.696,18,7.516L18,7.516z M18,13.516C18,14.335,17.335,15,16.516,15H1.484 C0.665,15,0,14.335,0,13.516l0,0c0-0.82,0.665-1.483,1.484-1.483h15.032C17.335,12.031,18,12.695,18,13.516L18,13.516z"/>
</svg>
</span>
</label>

<div class="trigger"><a class="page-link" href="/about/">About</a></div>
</nav></div>
</header>
<main class="page-content" aria-label="Content">
<div class="wrapper">
<style type="text/css" media="screen">
.container {
margin: 10px auto;
max-width: 600px;
text-align: center;
}
h1 {
margin: 30px 0;
font-size: 4em;
line-height: 1;
letter-spacing: -1px;
}
</style>

<div class="container">
<h1>404</h1>

<p><strong>Page not found :(</strong></p>
<p>The requested page could not be found.</p>
</div>

</div>
</main><footer class="site-footer h-card">
<data class="u-url" href="/"></data>

<div class="wrapper">

<h2 class="footer-heading">Your awesome title</h2>

<div class="footer-col-wrapper">
<div class="footer-col footer-col-1">
<ul class="contact-list">
<li class="p-name">Your awesome title</li><li><a class="u-email" href="mailto:[email protected]">[email protected]</a></li></ul>
</div>

<div class="footer-col footer-col-2"><ul class="social-media-list"><li><a href="https://github.com/jekyll"><svg class="svg-icon"><use xlink:href="/assets/minima-social-icons.svg#github"></use></svg> <span class="username">jekyll</span></a></li><li><a href="https://www.twitter.com/jekyllrb"><svg class="svg-icon"><use xlink:href="/assets/minima-social-icons.svg#twitter"></use></svg> <span class="username">jekyllrb</span></a></li></ul>
</div>

<div class="footer-col footer-col-3">
<p>Write an awesome description for your new site here. You can edit this line in _config.yml. It will appear in your document head meta (for Google search results) and in your feed.xml site description.</p>
</div>
</div>

</div>

</footer>
</body>

</html>
88 changes: 88 additions & 0 deletions docs/_site/about/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<!DOCTYPE html>
<html lang="en"><head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1"><!-- Begin Jekyll SEO tag v2.8.0 -->
<title>About | Your awesome title</title>
<meta name="generator" content="Jekyll v4.3.2" />
<meta property="og:title" content="About" />
<meta property="og:locale" content="en_US" />
<meta name="description" content="Write an awesome description for your new site here. You can edit this line in _config.yml. It will appear in your document head meta (for Google search results) and in your feed.xml site description." />
<meta property="og:description" content="Write an awesome description for your new site here. You can edit this line in _config.yml. It will appear in your document head meta (for Google search results) and in your feed.xml site description." />
<link rel="canonical" href="http://localhost:4000/about/" />
<meta property="og:url" content="http://localhost:4000/about/" />
<meta property="og:site_name" content="Your awesome title" />
<meta property="og:type" content="website" />
<meta name="twitter:card" content="summary" />
<meta property="twitter:title" content="About" />
<script type="application/ld+json">
{"@context":"https://schema.org","@type":"WebSite","description":"Write an awesome description for your new site here. You can edit this line in _config.yml. It will appear in your document head meta (for Google search results) and in your feed.xml site description.","headline":"About","name":"Your awesome title","url":"http://localhost:4000/about/"}</script>
<!-- End Jekyll SEO tag -->
<link rel="stylesheet" href="/assets/main.css"><link type="application/atom+xml" rel="alternate" href="http://localhost:4000/feed.xml" title="Your awesome title" /></head>
<body><header class="site-header" role="banner">

<div class="wrapper"><a class="site-title" rel="author" href="/">Your awesome title</a><nav class="site-nav">
<input type="checkbox" id="nav-trigger" class="nav-trigger" />
<label for="nav-trigger">
<span class="menu-icon">
<svg viewBox="0 0 18 15" width="18px" height="15px">
<path d="M18,1.484c0,0.82-0.665,1.484-1.484,1.484H1.484C0.665,2.969,0,2.304,0,1.484l0,0C0,0.665,0.665,0,1.484,0 h15.032C17.335,0,18,0.665,18,1.484L18,1.484z M18,7.516C18,8.335,17.335,9,16.516,9H1.484C0.665,9,0,8.335,0,7.516l0,0 c0-0.82,0.665-1.484,1.484-1.484h15.032C17.335,6.031,18,6.696,18,7.516L18,7.516z M18,13.516C18,14.335,17.335,15,16.516,15H1.484 C0.665,15,0,14.335,0,13.516l0,0c0-0.82,0.665-1.483,1.484-1.483h15.032C17.335,12.031,18,12.695,18,13.516L18,13.516z"/>
</svg>
</span>
</label>

<div class="trigger"><a class="page-link" href="/about/">About</a></div>
</nav></div>
</header>
<main class="page-content" aria-label="Content">
<div class="wrapper">
<article class="post">

<header class="post-header">
<h1 class="post-title">About</h1>
</header>

<div class="post-content">
<p>This is the base Jekyll theme. You can find out more info about customizing your Jekyll theme, as well as basic Jekyll usage documentation at <a href="https://jekyllrb.com/">jekyllrb.com</a></p>

<p>You can find the source code for Minima at GitHub:
<a href="https://github.com/jekyll">jekyll</a> /
<a href="https://github.com/jekyll/minima">minima</a></p>

<p>You can find the source code for Jekyll at GitHub:
<a href="https://github.com/jekyll">jekyll</a> /
<a href="https://github.com/jekyll/jekyll">jekyll</a></p>


</div>

</article>

</div>
</main><footer class="site-footer h-card">
<data class="u-url" href="/"></data>

<div class="wrapper">

<h2 class="footer-heading">Your awesome title</h2>

<div class="footer-col-wrapper">
<div class="footer-col footer-col-1">
<ul class="contact-list">
<li class="p-name">Your awesome title</li><li><a class="u-email" href="mailto:[email protected]">[email protected]</a></li></ul>
</div>

<div class="footer-col footer-col-2"><ul class="social-media-list"><li><a href="https://github.com/jekyll"><svg class="svg-icon"><use xlink:href="/assets/minima-social-icons.svg#github"></use></svg> <span class="username">jekyll</span></a></li><li><a href="https://www.twitter.com/jekyllrb"><svg class="svg-icon"><use xlink:href="/assets/minima-social-icons.svg#twitter"></use></svg> <span class="username">jekyllrb</span></a></li></ul>
</div>

<div class="footer-col footer-col-3">
<p>Write an awesome description for your new site here. You can edit this line in _config.yml. It will appear in your document head meta (for Google search results) and in your feed.xml site description.</p>
</div>
</div>

</div>

</footer>
</body>

</html>
17 changes: 17 additions & 0 deletions docs/_site/feed.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="4.3.2">Jekyll</generator><link href="http://localhost:4000/feed.xml" rel="self" type="application/atom+xml" /><link href="http://localhost:4000/" rel="alternate" type="text/html" /><updated>2023-09-16T00:27:06+02:00</updated><id>http://localhost:4000/feed.xml</id><title type="html">Your awesome title</title><subtitle>Write an awesome description for your new site here. You can edit this line in _config.yml. It will appear in your document head meta (for Google search results) and in your feed.xml site description.</subtitle><entry><title type="html">Welcome to Jekyll!</title><link href="http://localhost:4000/jekyll/update/2023/09/16/welcome-to-jekyll.html" rel="alternate" type="text/html" title="Welcome to Jekyll!" /><published>2023-09-16T00:15:34+02:00</published><updated>2023-09-16T00:15:34+02:00</updated><id>http://localhost:4000/jekyll/update/2023/09/16/welcome-to-jekyll</id><content type="html" xml:base="http://localhost:4000/jekyll/update/2023/09/16/welcome-to-jekyll.html"><![CDATA[<p>You’ll find this post in your <code class="language-plaintext highlighter-rouge">_posts</code> directory. Go ahead and edit it and re-build the site to see your changes. You can rebuild the site in many different ways, but the most common way is to run <code class="language-plaintext highlighter-rouge">jekyll serve</code>, which launches a web server and auto-regenerates your site when a file is updated.</p>

<p>Jekyll requires blog post files to be named according to the following format:</p>

<p><code class="language-plaintext highlighter-rouge">YEAR-MONTH-DAY-title.MARKUP</code></p>

<p>Where <code class="language-plaintext highlighter-rouge">YEAR</code> is a four-digit number, <code class="language-plaintext highlighter-rouge">MONTH</code> and <code class="language-plaintext highlighter-rouge">DAY</code> are both two-digit numbers, and <code class="language-plaintext highlighter-rouge">MARKUP</code> is the file extension representing the format used in the file. After that, include the necessary front matter. Take a look at the source for this post to get an idea about how it works.</p>

<p>Jekyll also offers powerful support for code snippets:</p>

<figure class="highlight"><pre><code class="language-ruby" data-lang="ruby"><span class="k">def</span> <span class="nf">print_hi</span><span class="p">(</span><span class="nb">name</span><span class="p">)</span>
<span class="nb">puts</span> <span class="s2">"Hi, </span><span class="si">#{</span><span class="nb">name</span><span class="si">}</span><span class="s2">"</span>
<span class="k">end</span>
<span class="n">print_hi</span><span class="p">(</span><span class="s1">'Tom'</span><span class="p">)</span>
<span class="c1">#=&gt; prints 'Hi, Tom' to STDOUT.</span></code></pre></figure>

<p>Check out the <a href="https://jekyllrb.com/docs/home">Jekyll docs</a> for more info on how to get the most out of Jekyll. File all bugs/feature requests at <a href="https://github.com/jekyll/jekyll">Jekyll’s GitHub repo</a>. If you have questions, you can ask them on <a href="https://talk.jekyllrb.com/">Jekyll Talk</a>.</p>]]></content><author><name></name></author><category term="jekyll" /><category term="update" /><summary type="html"><![CDATA[You’ll find this post in your _posts directory. Go ahead and edit it and re-build the site to see your changes. You can rebuild the site in many different ways, but the most common way is to run jekyll serve, which launches a web server and auto-regenerates your site when a file is updated.]]></summary></entry></feed>
Loading

0 comments on commit 7d5a949

Please sign in to comment.