Random Note 2024-06-10

It's a random note from 2024-06-10.
I don't plan on writing every day for now. I'll write when I feel like it.
Today, I feel like writing.

Spire

Introduction to the Spire library for performing accurate numerical computations in Scala

じゃあ何すか、COBOL以外では4.8 - 4.7 - 0.1できないってことっすか / ScalaとSpireで安心安全な計算ライフを実現しよう - Lambdaカクテル
先日こういうツイートが流れてきた。 Q:なぜ金融系では未だにCOBOLが使われるんですか?A:お手元にExcelがありましたら任意のセルに「=4.8-4.7-0.1」って入れてみてください。— 遊撃部長F/S&RWAs (@fstora) 2024年6月6日 Q:なぜ金融系では未だにCOBOLが使われるんですか? A:お手元にExcelがありましたら任意のセルに「=4.8-4.7-0.1」って入れてみてください。 普段我々がゴリゴリ馬車馬のように使っているソフトウェアでよく利用されている浮動小数点型、すなわちfloatやdoubleなどは特定の算術に弱いことが知られている。というかもうこの手の…
じゃあ何すか、COBOL以外では4.8 - 4.7 - 0.1できないってことっすか / ScalaとSpireで安心安全な計算ライフを実現しよう - Lambdaカクテル favicon https://blog.3qe.us/entry/2024/06/10/090000
じゃあ何すか、COBOL以外では4.8 - 4.7 - 0.1できないってことっすか / ScalaとSpireで安心安全な計算ライフを実現しよう - Lambdaカクテル
Spire
docs
Spire favicon https://typelevel.org/spire/

Spire is a numeric library for Scala which is intended to be generic, fast, and precise.

Using features such as specialization, macros, type classes, and implicits, Spire works hard to defy conventional wisdom around performance and precision trade-offs.

A major goal is to allow developers to write efficient numeric code without having to "bake in" particular numeric representations.

In most cases, generic implementations using Spire's specialized type classes perform identically to corresponding direct implementations.

Dealing with a type that represents rational numbers called Rational

import spire._
import spire.math._
import spire.implicits._

val r1 = Rational(42)
val r2 = Rational(1, 3)

println(r1 * r2) // 14

Spire retains rational numbers as they are. Thanks to this, calculations can be conducted accurately.

println(Rational(7, 9)) // 7/9

As mentioned at the beginning, Spire is designed to handle various numerical types effectively, including Algebraic, Complex, Natural, Real, FixedPoint, and more.

Spire
docs
Spire favicon https://typelevel.org/spire/#number-types

It seems to have operations related to groups.

Spire
docs
Spire favicon https://typelevel.org/spire/#semigroup-monoid-and-group

And also rings.

Spire
docs
Spire favicon https://typelevel.org/spire/#rings-co

And even vector spaces.

Spire
docs
Spire favicon https://typelevel.org/spire/#vectorspaces-co

Interesting~.

Kory&hsjoihs @ ScalaMatsuri 2024

This is a Speaks of @Kory__3 & @hsjoihs at ScalaMatsuri.

Learning Extensible Effects by Building One

This Speaks about "Eff: Extensible Effects" simplified implementation.

Extensible Effects are one means of effectively combining multiple effects, with Monad Transformer being another means. This presentation focuses on the simple implementation of Eff.

The implementation of effect systems is explained from a fundamental modeling perspective.

Fundamental Modeling:

  • The machine reads the program and interacts with the world
  • Causing effects during program evaluation is premature concretization

Implementing a "machine that reads executable forms" in Scala.

But what computational model to refer to? (There are many...)

Referring to assembly language actually naturally leads to Eff! (wtf)

... From this perspective, a brief explanation of assembly (specifically RISC-V) and JVM bytecode follows.

Then, the drawbacks of the non-structural model of assembly are discussed, leading to the introduction of graph theory to consider better models. An argument is made that single instructions cannot build graphs, prompting the creation of container types shaped like graphs.

Premise: Many processor instructions consist of fundamental instructions + extension instructions.

As a special case, when instructions themselves have programmatic structure, executable files can be consolidated into a single instruction

-> compactify function

If instructions have programmatic structure, a dedicated runtime can reuse Eff runtime. Otherwise, transpilation is done before execution.

Considering the union and decompose of instruction sets.

Using a structure called EitherK to union instructions.

case class EitherK[Instr1[_], Instr2[_], BIdx](
  value: Either[Instr1[BIdx], Instr2[BIdx]]
)

infix type mix[Instr1[_], Instr2[_]] = [BI] =>>
  EitherK[Instr1, Instr2, BI]

Decomposing unionized instructions into fundamental and extension instructions using the Decompose type class.

trait Decompose[SubInstr[_], Instr[_]] {

  type Complement[_]

  def classify[A](
    instr: Instr[A]
  ): Either[SubInstr[A], Complement[A]]
}

Considering functionality extension in the transpiler.

transpile allows conversion of executable forms based on translation rules. Step-wise transpilation can also be described using this.

Executable[Instr1 mix Instr2 mix Instr3, EC]
↓
Executable[Instr1 mix Instr2, EC]
↓
Executable[Instr1, EC]
↓
Executable[EC]

Viewing assembly programs as composable Executables, a machine for Executable is built by stacking transpilers.

This Executable is commonly known as Eff.

Hmm, it's complicated. Let's summarize Eff in a separate scrap.

© 2024-PRESENT ubugeeei. All rights reserved.