Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ocramz/bound-simple
A lightweight utility library for implementing lambda abstraction and beta reduction in embedded languages.
https://github.com/ocramz/bound-simple
lambda-calculus
Last synced: 22 days ago
JSON representation
A lightweight utility library for implementing lambda abstraction and beta reduction in embedded languages.
- Host: GitHub
- URL: https://github.com/ocramz/bound-simple
- Owner: ocramz
- Created: 2021-10-18T14:52:00.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2021-10-18T19:47:47.000Z (about 3 years ago)
- Last Synced: 2024-09-23T19:17:32.601Z (about 1 month ago)
- Topics: lambda-calculus
- Language: Haskell
- Homepage:
- Size: 15.6 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: Changelog.md
Awesome Lists containing this project
README
# bound-simple
A lightweight implementation of 'bound'. Provides much of the functionality of Bound.Scope.Simple, without the large dependency footprint.
## Example
The function `whnf` beta-reduces a term of the untyped lambda calculus.
In the code below, we first declare a type `Exp` for terms, using the `Scope` type within the constructor for a lambda abstraction. To this we add a few instances necessary for showing and traversing the terms. The Monad instance takes care of variable substitution.
After that, abstraction and application are implemented in terms of abstract1 and instantiate1.
The `test` function declares a term `(\x . x) y` , then prints it and its reduced form.```haskell
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DeriveFoldable #-}
{-# LANGUAGE DeriveTraversable #-}import Control.Monad (ap)
import Bound.Simple (Scope, Bound(..), abstract1, instantiate1)
import Data.Functor.Classes.Generic (Generically(..))import GHC.Generics (Generic1)
infixl 9 :
data Exp a = V a | Exp a : Exp a | Lam (Scope () Exp a)
deriving (Show, Functor, Foldable, Traversable, Generic1)
deriving (Show1) via Generically Expinstance Applicative Exp where pure = V; k <*> m = ap k m
instance Monad Exp where
return = V
V a >>= f = f a
(x :@ y) >>= f = (x >>= f) :@ (y >>= f)
Lam e >>= f = Lam (e >>>= f)lam :: Eq a => a -> Exp a -> Exp a
lam v b = Lam (abstract1 v b)whnf :: Exp a -> Exp a
whnf (e1 :@ e2) = case whnf e1 of
Lam b -> whnf (instantiate1 e2 b)
f' -> f' :@ e2
whnf e = etest :: IO ()
test = do
let term = lam x (V x) :@ V y
print term -- Lam (Scope (V (B ()))) :@ V y
print $ whnf term -- V y
```