lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


It was thus said that the Great Sergey Kovalev once stated:
> Let inroduce new reserved word "pure_function" into lua.
> It should work similar to usual function except
> all variables used inside are implicitly local
> no access to any upvalues or global namespace _G
> So it could interact with arguments it was passed.

  So having read the rest of the messages in this thread, I think I
following what you are trying to do.  So instead of the following:

function fromjulianday(jd)
  local a = jd + 32044
  local b = (4 * a + 3) // 146097
  local c = a - (b * 146097) // 4
  local d = (4 * c + 3) // 1461
  local e = c - (1461 * d) // 4
  local m = (5 * e + 2) // 153
  
  return {
    day   = e - (153 * m + 2) // 5 + 1,
    month = m + 3 - 12 * (m // 10),
    year  = b * 100 + d - 4800 + m // 10
  }
end

You could instead write this as:

pure_function fromjulianday(jd)
  a = jd + 32044
  b = (4 * a + 3) // 146097      
  c = a - (b * 146097) // 4       
  d = (4 * c + 3) // 1461      
  e = c - (1461 * d) // 4      
  m = (5 * e + 2) // 153      
     
  return { 
    day   = e - (153 * m + 2) // 5 + 1,      
    month = m + 3 - 12 * (m // 10),      
    year  = b * 100 + d - 4800 + m // 10      
  }
end

Or would it have to be?

pure_function fromjulianday(_ENV,jd) -- I don't use _ENV
  a = jd + 32044
  b = (4 * a + 3) // 146097            
  c = a - (b * 146097) // 4             
  d = (4 * c + 3) // 1461            
  e = c - (1461 * d) // 4            
  m = (5 * e + 2) // 153            
     
  return { 
    day   = e - (153 * m + 2) // 5 + 1,      
    month = m + 3 - 12 * (m // 10),      
    year  = b * 100 + d - 4800 + m // 10      
  }
end  

  Is this what you are proposing?
  
  To me, a "pure" function is one that only relies upon the parameters given
to it, it has no access to any data outside itself.  So for Lua, no globals
(or _ENV[]) or upvalues.  It a compiled langauge like Haskell such pure
functions can be replaced at compile time in certain circumstances with its
result.  For example:

	x = sin(math.pi)

at compile time x could be replaced with 0 with math.pi is a constant. [1]

  -spc

[1]	There's more to this optimization than what I've given here.
	Languages that support this type of optimization can track the
	contents of variables and find constants as parameters even if used
	through a variable, for example:

		pure function foo(x)
		  a = 5
		  b = 6
		  x = x * a + math.sin(b)
		  return x
		end
		
	The compiler can determine that a and b are not modified after
	assignment, so the function can be reduced to:
	
		-- replace variables with constants
		pure function foo(x)
		  x = x * 5 + math.sin(6)
		  return x
		end
		
		-- substitute results of pure functions
		pure function foo(x)
		  x = x * 5 + -0.27941549819893
		  return x
		end
		
		-- remove reassignment of x
		pure function foo(x)
		  return x * 5 - 0.27941549819893
		end
		
	Furthermore, if foo() is ever called with a constant, it too could
	be called at compile time, since it's marked as "pure" so it only
	depends upon values given to it.