# Associative tuples Classic List Threaded 5 messages Open this post in threaded view
|

## Associative tuples

 I was interested to read in http://lua-users.org/wiki/SimpleTuplesabout defining pairing functions satisfying the mathematically necessary condition   pair (x,x') == pair (y,y') <==> x == y and x' == y' To recapitulate, we have: local pair do -------------------  local m = function (a) return {          __index = function (t,b)                 local p = function ( ) return a,b end                 t[b] = p                 return p end }  local meta = { __index = function (t,a)                    local v = setmetatable ({ }, m (a))                    t[a] = v                    return v end }  local P = setmetatable ({ }, meta)  pair = function (x, y) return P[x][y] end end ----------------- If we then define:   tuple = function (...)     local n = select ('#', ...)     if n < 1 then return end     if n < 2 then return ... end     return pair (..., tuple (select (2, ...))) end we have    tuple (1,2,3) == tuple (1, tuple (2,3)) --> true by definition but, alas,    tuple (1,2,3) == tuple (tuple (1,2), 3) --> false So are there any associative tupling functions? We probably need to be able to hash not just single items on the stack but tuples of items, in an associative way. I tried googling "hashing and tupling" but that particular cast of the net brought up no relevant research papers as far as I could see. As a good category theorist I should not be asking for equalities but for invertible associator functions that satisfy naturality and MacLane's pentagonal condition; but then monoidal categories are monoidally equivalent to strict monoidal categories. -- Gavin Wraith ([hidden email]) Home page: http://www.wra1th.plus.com/
Open this post in threaded view
|

## Re: Associative tuples

 On 01/11/2018 01:09 AM, Gavin Wraith wrote: > [...] > we have >    tuple (1,2,3) == tuple (1, tuple (2,3)) --> true > by definition but, alas, >    tuple (1,2,3) == tuple (tuple (1,2), 3) --> false > So are there any associative tupling functions? > [...]> -- > Gavin Wraith ([hidden email]) > Home page: http://www.wra1th.plus.com/Why not implement tuples via lists?   tuple =     function(...)       local result = {type = 'tuple'}       local process       process =         function(list)           for i, el in ipairs(list) do             if (type(el) == 'table') and (el.type == 'tuple') then               process(el)             else               table.insert(result, el)             end           end         end       process({...})       return result     end   is_equal =     function(t_a, t_b)       if (#t_a == #t_b) then         for i = 1, #t_a do           if (t_a[i] ~= t_b[i]) then             return false           end         end         return true       else         return false       end     end   -- Verification:   print(     'true',     is_equal(       tuple(1, tuple(2, 3)),       tuple(tuple(1, 2), 3)     ),     is_equal(       tuple(1, tuple(2, 3)),       tuple(1, 2, 3)     ),     is_equal(       tuple(1, 2, 3),       tuple(tuple(1, 2, 3))     )   )   print(     'false',     is_equal(       tuple(1, 2, 3),       tuple(1, 2)     ),     is_equal(       tuple(1, tuple(2, 3)),       tuple(1, tuple(2))     )   ) -- Martin
Open this post in threaded view
|

## Re: Associative tuples

 In message <[hidden email]>           dyngeccetor8 <[hidden email]> wrote: > Why not implement tuples via lists? > >  tuple = >    function(...) >      local result = {type = 'tuple'} > >      local process >      process = >        function(list) >          for i, el in ipairs(list) do >            if (type(el) == 'table') and (el.type == 'tuple') then >              process(el) >            else >              table.insert(result, el) >            end >          end >        end > >      process({...}) > >      return result >    end > >  is_equal = >    function(t_a, t_b) >      if (#t_a == #t_b) then >        for i = 1, #t_a do >          if (t_a[i] ~= t_b[i]) then >            return false >          end >        end >        return true >      else >        return false >      end >    end I was looking for something that worked with ==.  But thanks anyway. -- Gavin Wraith ([hidden email]) Home page: http://www.wra1th.plus.com/