Implementing an efficient data structure like a zipper in Haskell with access to O (1) elements

Question

I want to create a data type that allows me to quickly access and change its elements. Is it possible to create in Haskell a structure and functions that will execute as fast as a simple C ++ implementation?

Problem Details

I am writing a compiler in Haskell. I have an AST represented by a data type, consider the following:

import Prelude hiding (id)

-- this is a sample data type, the real one has got a lot of constructors
data AST = A { id :: Int, x :: AST, y :: AST, z :: AST }
         | B { id :: Int }
         | C { id :: Int, x :: AST, y :: AST }
         | D { id :: Int, u :: AST, v :: AST, w :: AST}

Each AST node has a unique identifier. I would like to implement the following functions in Haskell:

  • function getByIdthat will return the AST node of the selected id in O(1)time complexity.
  • "" . , O(1) .

Zippers, 3 :

  • ( ) , , , "" "". , ?
  • , getById O(1), ?
  • , . , ( O(1)).

++

++ AST nodePtrs. nodeById O(1), *(nodePtrs[id]). ++ , - O(1).

+4
1

, , differentiation?

, getById, , . , Haskell, getById :: Int -> IO AST, - . ( , ), getById AST AST, ? . , , haskell.

, . , ZAST - AST. - .

makeFocus :: ZAST -> Focus ZAST

type Focus =
  (ZAST -> ZAST) -> -- The modifier of the "below part"
  ZAST ->           -- The new "above part", you have to provide it again as it might have changed
  ZAST              -- The Result

, , ++.


, , , , ( , ..), . , , ++ Haskell.

+1

All Articles