Code Monkey home page Code Monkey logo

present's Introduction

present Hackage

Make presentations for data types.

Breadth-first unpacking of data structures

> import Data.Maybe
> import Data.Data
> import Data.Typeable
> import Data.ID
> :set -XDeriveDataTypeable
> data Bar = Bar () Bool deriving (Typeable,Data)
> data Foo = Foo Bar [Bar] Char Int deriving (Typeable,Data)

The data type can be queried like this:

> let x = Foo (Bar () True) [(Bar () True),(Bar () False)] 'a' 5
> present (fromJust (fromList [0])) x
Just (Alg "Foo" "Foo" [("Bar","@0→0"),("[Bar]","@0→1"),("Char","@0→2"),("Int","@0→3")])
> present (fromJust (fromList [0,1])) x
Just (List "[Bar]" [("Bar","@0→1→0"),("[Bar]","@0→1→1")])
> present (fromJust (fromList [0,1,0])) x
Just (Alg "Bar" "Bar" [("()","@0→1→0→0"),("Bool","@0→1→0→1")])
> present (fromJust (fromList [0,1,0,0])) x
Just (Tuple "()" [])
> present (fromJust (fromList [0,1,0,1])) x
Just (Alg "Bool" "True" [])
> present (fromJust (fromList [0,2])) x
Just (Char "Char" "'a'")
> present (fromJust (fromList [0,3])) x
Just (Integer "Int" "5")

Lazy infinite data structures

Data structures can also be unpacked lazily.

> present (fromJust (fromList [0])) [1..]
Just (List "[Integer]" [("Integer","@0→0"),("[Integer]","@0→1")])
> present (fromJust (fromList [0,0])) [1..]
Just (Integer "Integer" "1")
> present (fromJust (fromList [0,1])) [1..]
Just (List "[Integer]" [("Integer","@0→1→0"),("[Integer]","@0→1→1")])
> present (fromJust (fromList [0,1,0])) [1..]
Just (Integer "Integer" "2")
> present (fromJust (fromList [0,1,1,0])) [1..]
Just (Integer "Integer" "3")
> present (fromJust (fromList [0,1,1,1,0])) [1..]
Just (Integer "Integer" "4")

Lazy memoization

Sharing allows you to inspect a data structure and only evaluate parts of it once, subsequent calls will be immediate.

> let ack 0 n = n+1; ack m 0 = ack (m-1) 1; ack m n = ack (m-1) (ack m (n-1))
> :set +s
> let x = ack 3 8; xs = [x,x]
(0.00 secs, 1028952 bytes)
> present (fromJust (fromList [0])) xs
Just (List "[Integer]" [("Integer","@0→0"),("[Integer]","@0→1")])
(0.00 secs, 1035472 bytes)
> present (fromJust (fromList [0,0])) xs
Just (Integer "Integer" "2045")
(2.20 secs, 803045032 bytes)
> present (fromJust (fromList [0,1,0])) xs
Just (Integer "Integer" "2045")
(0.00 secs, 1070976 bytes)

Representations for common editor-compatible formats

Support for JSON:

> fmap Data.Aeson.encode (present (fromJust (fromList [0])) (Foo (Bar () True) [] 'a' 6))
Just (Chunk "{\"slots\":[[\"Bar\",[0,0]],[\"[Bar]\",[0,1]],
[\"Char\",[0,2]],[\"Int\",[0,3]]],\"text\":\"Foo\",
\"rep\":\"alg\",\"type\":\"Foo\"}" Empty)

And for s-expressions (Emacs):

> fmap Data.AttoLisp.encode (present (fromJust (fromList [0])) (Foo (Bar () True) [] 'a' 6))
Just (Chunk "((rep \"alg\") (type \"Foo\") (text \"Foo\")
(slots ((\"Bar\" (0 0)) (\"[Bar]\" (0 1))
(\"Char\" (0 2)) (\"Int\" (0 3)))))" Empty)

present's People

Contributors

chrisdone avatar

Watchers

Alan Zimmerman avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.