Warning: this is an htmlized version!
The original is here, and
the conversion rules are here.
-- This file:
--   http://angg.twu.net/LUA/Lazy1.lua.html
--   http://angg.twu.net/LUA/Lazy1.lua
--           (find-angg "LUA/Lazy1.lua")
-- Author: Eduardo Ochs <eduardoochs@gmail.com>
-- 2022jul16
--
-- (defun l () (interactive) (find-angg "LUA/Lazy1.lua"))
-- Obsolete! Superseded by:  (find-angg "LUA/Lazy2.lua")

require "C2Subst1"    -- (find-angg "LUA/C2Subst1.lua")

-- (find-angg "LUA/C2Subst1.lua" "LExpr")

tolua = function (o)
    if type(o) == "number" then return tostring(o) end
    if type(o) == "string" then return format("%q", o) end
    if not o.tolua then PPP(o); error "No :tolua" end
    return o:tolua()
  end

LazyFun = Class {
  type    = "LazyFun",
  __tostring = function (la) return la:tolua() end,
  __index = {
    tolua = function (la)
        local args = mapconcat(tolua, la, ", ")
        return format("%s(%s)", la[0], args)
      end,
    totree = function (la) return SynTree.from(la) end,
  },
}

LazyVar = Class {
  type    = "LazyVar",
  __tostring = function (la) return la:tolua() end,
  __index = {
    tolua = function (la) return la[0] end,
    totree = function (la) return SynTree.from(la) end,
  },
}

fun = function (name)
    _G[name] = function (...) return LazyFun {[0]=name, ...} end
  end
var = function (name)
    _G[name] = LazyVar {[0]=name}
  end

funs = function (bigstr)
    for _,name in ipairs(split(bigstr)) do fun(name) end
  end
vars = function (bigstr)
    for _,name in ipairs(split(bigstr)) do var(name) end
  end

vareq = function (o1, o2)
    return otype(o1) == "LazyVar"
       and otype(o2) == "LazyVar"
       and o1[0] == o2[0]   -- vars with the same name
  end
funeq = function (o1, o2)
    return otype(o1) == "LazyFun"
       and otype(o2) == "LazyFun"
       and o1[0] == o2[0]   -- apps of the same function
  end
funarg = function (o, n)
    if otype(o) ~= "LazyFun" then error "Not a function" end
    return o[n or 1]
  end

--[==[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "Lazy1.lua"
f = LazyFun {[0]="f", 22, "foo"}
= f

funs " ddx eq mul f g fp gp und "
= f(22, "foo")

vars  " x y "
RC = eq(ddx(f(g(x))), mul(fp(g(x)), gp(x)))
= RC

= vareq(x,x), vareq(x,y)
= funeq(f(22),f(33)), funeq(f(22),g(33))
= funarg(f(22))
= funarg(f(22, 33), 2)

foo = f(und(g(x)))
= foo
= PP(              foo  )
= PP(     tostring(foo) )
= PP(expr(tostring(foo)))
foo = expr(tostring(foo))
= PP(              foo  )
= PP(     tostring(foo) )
= PP(expr(tostring(foo)))
und = id
foo = expr(tostring(foo))
= PP(              foo  )
= PP(     tostring(foo) )
= PP(expr(tostring(foo)))

-- (find-angg "LUA/C2Subst1.lua" "basic-ops")


--]==]

makesubst_v1 = function (S_core)
    local S
    S = function (o)
        if type(o) == "number" then return o end
        if type(o) == "string" then return o end
        local oisvar = function (v) return    vareq(o, v) end
        local oisapp = function (f) return    funeq(o, f("_")) end
        local oarg   = function (n) return   funarg(o, n)  end
        local oSarg  = function (n) return S(funarg(o, n)) end
        local Sresult = S_core(o,oisvar,oisapp,oarg,oSarg)
	if Sresult then return Sresult end
        local o2 = shallowcopy(o)
        for i=1,#o2 do
          o2[i] = S(o2[i])
        end
        return o2
      end
    return S
  end

Subst = Class {
  type = "Subst",
  fmt0 = "function (o,isvar,isapp,arg,Sarg)\n%s\n  end",
  fmt1 = "makesubst_v1(%s)",
  from = function (bigstr0) return
      Subst({bigstr=rtrim(bigstr0)}):compile()
    end,
  __call = function (s, ...) return s.S(...) end,
  __tostring = function (s) return s.bigstr end,
  __index = {
    code = function (s)
        return format(Subst.fmt1, format(Subst.fmt0, s.bigstr))
      end,
    compile = function (s) s.S = expr(s:code()); return s end,
  },
}

--[==[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "Lazy1.lua"

S1 = Subst.from [[
    if isapp(f)  then return sen(Sarg()) end
    if isapp(fp) then return cos(Sarg()) end
    if isapp(g)  then return mul(42,Sarg()) end
    if isapp(gp) then return 42 end
    if isvar(x)  then return t end
  ]]

= S1
= S1:code()
=    RC :totree()
= S1(RC):totree()

und0 = function (o)
    if funeq(o, und("_")) then return und0(funarg(o)) end
    return o
  end

S2unds = Subst.from [[
    if isapp(f)  then return und(f (und(Sarg()))) end
    if isapp(fp) then return und(fp(und(Sarg()))) end
    if isapp(g)  then return und(g (und(Sarg()))) end
    if isapp(gp) then return und(gp(und(Sarg()))) end
    if isvar(x)  then return und(x) end
  ]]

S2und1 = Subst.from [[
    if isapp(und) then return und(und0(Sarg())) end
  ]]

=               RC  :totree()
=        S2unds(RC) :totree()
= S2und1(S2unds(RC)):totree()

S3u = Subst.from [[
    return und(o)
  ]]

= S3u(RC):totree()

= und0(und(und(x)))


foo = mul(und(und(x)), und(y))
= foo:totree()


--]==]



funs " ddx ddvar eq mul sen cos f g fp gp und "
vars " x y t "
RC = eq(ddx  (   f(g(x))), mul(fp(g(x)), gp(x)))
RC = eq(ddvar(x, f(g(x))), mul(fp(g(x)), gp(x)))

S1_core = function (o,isvar,isapp,arg,Sarg)
    if isvar(x)  then return y end
  end
S2_core = function (o,isvar,isapp,arg,Sarg)
    if isapp(und) then return t end
  end
S3_core = function (o,isvar,isapp,arg,Sarg)
    if isapp(f)  then return sen(Sarg()) end
    if isapp(fp) then return cos(Sarg()) end
    if isapp(g)  then return mul(42,Sarg()) end
    if isapp(gp) then return 42 end
    if isvar(x)  then return t end
  end


--[==[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "Lazy1.lua"

S1 = makesubst_v1(
    function (o,isvar,isapp,arg,Sarg)
        if isvar(x) then return t end
      end)
= RC
= S1(RC)

S2 = makesubst_v1(function (o,isvar,isapp,arg,Sarg)
    if isapp(f)  then return sen(Sarg()) end
    if isapp(fp) then return cos(Sarg()) end
    if isapp(g)  then return mul(42,Sarg()) end
    if isapp(gp) then return 42 end
    if isvar(x)  then return t end
  end)
=    RC
= S2(RC)
=    RC :totree()
= S2(RC):totree()


funs " u "
RCU = eq(ddvar(u(x), u(f(u(g(u(x)))))), mul(fp(u(g(u(x)))), u(gp(u(x)))))
= RCU

S1 = makesubst_v1(S1_core)
S3 = makesubst_v1(S3_core)
= S1("foo")
= S1(x)
= S1(RC)
= S3(RC)
= S3(f(42))



= RC
foo = x
PPP(foo)
= x




isvar = function (o, v)
  return otype 



-- (
c2m221dp1p 2 "introducao")
-- (c2m221dp1a    "introducao")

RC_example1 = applysubst(RC, S1)
  end

--]==]




-- Local Variables:
-- coding:  utf-8-unix
-- End: