Skip to content
This repository has been archived by the owner on Jun 18, 2023. It is now read-only.

Commit

Permalink
feat: basic ast stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
prescientmoon committed Sep 4, 2020
0 parents commit 19f3043
Show file tree
Hide file tree
Showing 7 changed files with 315 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/bower_components/
/node_modules/
/.pulp-cache/
/output/
/generated-docs/
/.psc-package/
/.psc*
/.purs*
/.psa*
/.spago
128 changes: 128 additions & 0 deletions packages.dhall
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
{-
Welcome to your new Dhall package-set!
Below are instructions for how to edit this file for most use
cases, so that you don't need to know Dhall to use it.
## Warning: Don't Move This Top-Level Comment!
Due to how `dhall format` currently works, this comment's
instructions cannot appear near corresponding sections below
because `dhall format` will delete the comment. However,
it will not delete a top-level comment like this one.
## Use Cases
Most will want to do one or both of these options:
1. Override/Patch a package's dependency
2. Add a package not already in the default package set
This file will continue to work whether you use one or both options.
Instructions for each option are explained below.
### Overriding/Patching a package
Purpose:
- Change a package's dependency to a newer/older release than the
default package set's release
- Use your own modified version of some dependency that may
include new API, changed API, removed API by
using your custom git repo of the library rather than
the package set's repo
Syntax:
Replace the overrides' "{=}" (an empty record) with the following idea
The "//" or "⫽" means "merge these two records and
when they have the same value, use the one on the right:"
-------------------------------
let overrides =
{ packageName =
upstream.packageName // { updateEntity1 = "new value", updateEntity2 = "new value" }
, packageName =
upstream.packageName // { version = "v4.0.0" }
, packageName =
upstream.packageName // { repo = "https://www.example.com/path/to/new/repo.git" }
}
-------------------------------
Example:
-------------------------------
let overrides =
{ halogen =
upstream.halogen // { version = "master" }
, halogen-vdom =
upstream.halogen-vdom // { version = "v4.0.0" }
}
-------------------------------
### Additions
Purpose:
- Add packages that aren't already included in the default package set
Syntax:
Replace the additions' "{=}" (an empty record) with the following idea:
-------------------------------
let additions =
{ package-name =
{ dependencies =
[ "dependency1"
, "dependency2"
]
, repo =
"https://example.com/path/to/git/repo.git"
, version =
"tag ('v4.0.0') or branch ('master')"
}
, package-name =
{ dependencies =
[ "dependency1"
, "dependency2"
]
, repo =
"https://example.com/path/to/git/repo.git"
, version =
"tag ('v4.0.0') or branch ('master')"
}
, etc.
}
-------------------------------
Example:
-------------------------------
let additions =
{ benchotron =
{ dependencies =
[ "arrays"
, "exists"
, "profunctor"
, "strings"
, "quickcheck"
, "lcg"
, "transformers"
, "foldable-traversable"
, "exceptions"
, "node-fs"
, "node-buffer"
, "node-readline"
, "datetime"
, "now"
]
, repo =
"https://github.com/hdgarrood/purescript-benchotron.git"
, version =
"v7.0.0"
}
}
-------------------------------
-}


let upstream =
https://github.com/purescript/package-sets/releases/download/psc-0.13.8-20200831/packages.dhall sha256:cdb3529cac2cd8dd780f07c80fd907d5faceae7decfcaa11a12037df68812c83

let overrides = {=}

let additions = {=}

in upstream // overrides // additions
19 changes: 19 additions & 0 deletions spago.dhall
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{-
Welcome to a Spago project!
You can edit this file as you like.
-}
{ name = "my-project"
, dependencies =
[ "console"
, "effect"
, "free"
, "generics-rep"
, "ordered-collections"
, "psci-support"
, "record"
, "transformers"
, "tuples"
]
, packages = ./packages.dhall
, sources = [ "src/**/*.purs", "test/**/*.purs" ]
}
137 changes: 137 additions & 0 deletions src/Ast.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
module Lunarflow.Ast
( AstChunk(..)
, Ast(..)
, Expression
, IndexedAst
, GroupedExpression
, WithId
, IndexedLambdaData
, groupExpression
, indexExpression
) where

import Prelude
import Control.Monad.Reader (ReaderT, asks, local, runReaderT)
import Control.Monad.State (State, evalState, modify)
import Data.Generic.Rep (class Generic)
import Data.Generic.Rep.Show (genericShow)
import Data.List as List
import Data.Map (Map)
import Data.Map as Map
import Data.Maybe (maybe)
import Data.Symbol (SProxy(..))
import Prim.Row as Row
import Record as Record

-- | The meat and potatoes of representing an expression.
-- |
-- | - a represents the type we use as childre.
-- | - l represents the type lambdas carry around.
-- |
-- | Thre reason we take an argument for a is so we don't have to
-- | take an extra type argumet and write `Ast l r` on each occurence.
data AstChunk l a
= Call a a
| Lambda l a
| Var String

derive instance genericAstChunk :: Generic (AstChunk l a) _

instance showAstChunk :: (Show l, Show a) => Show (AstChunk l a) where
show = genericShow

-- | Generic Ast type
-- |
-- | This is extensible so we don't have to create a different dsl
-- | each time we want to augument a tree with new information.
newtype Ast l r
= Ast
{ term :: AstChunk l (Ast l r)
| r
}

derive instance genericAst :: Generic (Ast l r) _

instance showAst :: Show l => Show (Ast l r) where
show (Ast { term }) = genericShow term

-- | Helper for packing an ast
mkAst :: forall r l. Row.Lacks "term" r => AstChunk l (Ast l r) -> Record r -> Ast l r
mkAst inner = Ast <<< Record.insert (SProxy :: _ "term") inner

-- | Basic lambda calculus expressions
type Expression
= Ast String ()

-- | Basic exmple of how to encode `\a b. a`
const' :: Expression
const' = lambda
where
lambda :: Expression
lambda = mkAst (Lambda "x" lambda') {}

lambda' :: Expression
lambda' = mkAst (Lambda "y" varX) {}

varX :: Expression
varX = mkAst (Var "x") {}

-- | Basic extensible record for stuff which has an unique id represented as an int.
type WithId r
= ( id :: Int | r )

-- | Data carried around by lambdas.
-- | The argumentId is an unique code variables
-- | can use to reference that argument (basically removeing the need for shadowing).
-- | Thre reason we do this instead of changing names to indices is for ease of generating the layouts.
-- I made this into a separate type because I use it in the GroupedExpression type as well
type IndexedLambdaData
= { argumentName :: String, argumentId :: Int }

-- | Indexed lambda calculus expressions
type IndexedAst
= Ast IndexedLambdaData
(WithId ())

-- | Add incidces for all nodes in an ast
indexExpression :: Expression -> IndexedAst
indexExpression = flip evalState 0 <<< flip runReaderT Map.empty <<< go
where
-- | Internal version of fromExpression which runs in an actual monad
go :: Expression -> ReaderT (Map String Int) (State Int) IndexedAst
go (Ast { term }) = do
indexed <- case term of
Call func argument -> Call <$> go func <*> go argument
Lambda name body -> do
argumentId <- getId
Lambda { argumentName: name, argumentId } <$> local (Map.insert name argumentId) (go body)
Var name -> pure $ Var name
id <- case term of
Var name -> asks (Map.lookup name) >>= maybe getId pure
_ -> getId
pure
$ Ast
{ id
, term: indexed
}

-- | Helper for getting a new unique id
getId = modify ((+) 1)

-- | Ast which doesn't allow consecutive lambdas.
type GroupedExpression
= Ast { arguments :: List.List IndexedLambdaData } (WithId ())

-- | Group multiple consecutive lambdas into 1 as visual sugar.
-- | This is the textual equivalent of suagring \f. \a. \b. f b a into \f a b. f b a
groupExpression :: IndexedAst -> GroupedExpression
groupExpression ast@(Ast astData) = Ast $ astData { term = term }
where
term = case astData.term of
Lambda lambdaData body -> case nestedTerm of
Lambda { arguments } nestedBody -> Lambda { arguments: List.Cons lambdaData arguments } nestedBody
_ -> Lambda { arguments: pure lambdaData } nestedAst
where
nestedAst@(Ast { term: nestedTerm }) = groupExpression body
Call func arg -> Call (groupExpression func) (groupExpression arg)
Var a -> Var a
Empty file added src/Layout.purs
Empty file.
10 changes: 10 additions & 0 deletions src/Main.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Main where

import Prelude

import Effect (Effect)
import Effect.Console (log)

main :: Effect Unit
main = do
log "🍝"
11 changes: 11 additions & 0 deletions test/Main.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Test.Main where

import Prelude

import Effect (Effect)
import Effect.Class.Console (log)

main :: Effect Unit
main = do
log "🍝"
log "You should add some tests."

0 comments on commit 19f3043

Please sign in to comment.