Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yes, I'm posting another answer. And it still might not be exactly what you're looking for. But I think it might be of use nonetheless. It's in Haskell.</p> <pre><code>data LExpr = Lambda Char LExpr | Atom Char | App LExpr LExpr instance Show LExpr where show (Atom c) = [c] show (App l r) = "(" ++ show l ++ " " ++ show r ++ ")" show (Lambda c expr) = "(λ" ++ [c] ++ ". " ++ show expr ++ ")" </code></pre> <p>So here I cooked up a basic algebraic data type for expressing lambda calculus. I added a simple, but effective, custom Show instance.</p> <pre><code>ghci&gt; App (Lambda 'a' (Atom 'a')) (Atom 'b') ((λa. a) b) </code></pre> <p>For fun, I threw in a simple <code>reduce</code> method, with helper <code>replace</code>. Warning: not carefully thought out or tested. Do not use for industrial purposes. Cannot handle certain nasty expressions. :P</p> <pre><code>reduce (App (Lambda c target) expr) = reduce $ replace c (reduce expr) target reduce v = v replace c expr av@(Atom v) | v == c = expr | otherwise = av replace c expr ap@(App l r) = App (replace c expr l) (replace c expr r) replace c expr lv@(Lambda v e) | v == c = lv | otherwise = (Lambda v (replace c expr e)) </code></pre> <p>It seems to work, though that's really just me getting sidetracked. (<code>it</code> in ghci refers to the last value evaluated at the prompt)</p> <pre><code>ghci&gt; reduce it b </code></pre> <p>So now for the fun part, <code>rotate</code>. So I figure I can just peel off the first layer, and if it's a Lambda, great, I'll save the identifier and keep drilling down until I hit a non-Lambda. Then I'll just put the Lambda and identifier right back in at the "last" spot. If it wasn't a Lambda in the first place, then do nothing.</p> <pre><code>rotate (Lambda c e) = drill e where drill (Lambda c' e') = Lambda c' (drill e') -- keep drilling drill e' = Lambda c e' -- hit a non-Lambda, put c back rotate e = e </code></pre> <p>Forgive the unimaginative variable names. Sending this through ghci shows good signs:</p> <pre><code>ghci&gt; Lambda 'a' (Atom 'a') (λa. a) ghci&gt; rotate it (λa. a) ghci&gt; Lambda 'a' (Lambda 'b' (App (Atom 'a') (Atom 'b'))) (λa. (λb. (a b))) ghci&gt; rotate it (λb. (λa. (a b))) ghci&gt; Lambda 'a' (Lambda 'b' (Lambda 'c' (App (App (Atom 'a') (Atom 'b')) (Atom 'c')))) (λa. (λb. (λc. ((a b) c)))) ghci&gt; rotate it (λb. (λc. (λa. ((a b) c)))) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload