MatrixAI / Emergence

Distributed Infrastructure Orchestration
Apache License 2.0
1 stars 0 forks source link

Discussion about hnix #24

Closed CMCDragonkai closed 6 years ago

CMCDragonkai commented 6 years ago

hnix (https://github.com/haskell-nix/hnix) is a Haskell re-implementation of the Nix language.

This can be useful for artifact bindings when binding to Nix artifacts.

It is also useful as a foundation to write the Architect language parser and compiler.

CMCDragonkai commented 6 years ago

First thing I discovered is that hnix has Nix's expressions defined in https://github.com/haskell-nix/hnix/blob/master/src/Nix/Expr/Types.hs

Specifically: https://github.com/haskell-nix/hnix/blob/738ef09bf9add0f0f21d7a72dd5ce31dbd200f67/src/Nix/Expr/Types.hs#L93-L140

The comments mention that this data type is polymorphic in order to make use of Functor and other typeclasses. This is an interesting pattern called the type-level Fix pattern. It is explained here: http://mainisusuallyafunction.blogspot.com.au/2010/12/type-level-fix-and-generic-folds.html and https://www.schoolofhaskell.com/user/bartosz/understanding-algebras

Later:

https://github.com/haskell-nix/hnix/blob/738ef09bf9add0f0f21d7a72dd5ce31dbd200f67/src/Nix/Expr/Types.hs#L172-L173

I believe this is just done to make the Nix expression algebraic data type to be more generic so they can use generic higher order functions like fmap and other things to work with the abstract evaluation tree.

CMCDragonkai commented 6 years ago

A nix atom is something that evaluates to themselves. They are the "literals". These are defined here: https://github.com/haskell-nix/hnix/blob/738ef09bf9add0f0f21d7a72dd5ce31dbd200f67/src/Nix/Atoms.hs#L18-L32

These atoms include only ints, floats, booleans and null.

CMCDragonkai commented 6 years ago

Note that Nix is an dynamically/unityped language. So the Types.hs used in hnix don't mean Nix types, but the algebraic data types used to represent Nix constructs for the hnix compiler/evaluator to deal with.

CMCDragonkai commented 6 years ago

Should find out what all the strictness used in constructors for.

This commit added in the strictness annotations https://github.com/haskell-nix/hnix/commit/77bc4cb334be6d2970186e43fd006128f0b0eb69

With the comment that while it doesn't improve speed, it appears to reduce memory usage. For further reference: http://blog.johantibell.com/2011/06/memory-footprints-of-some-common-data.html

CMCDragonkai commented 6 years ago

Continuing integration into hnix here: https://github.com/MatrixAI/Architect/issues/14