Rework module system
Don't require/allow naming a module, instead require that the importer names it. Allow importing into the local scope with the name !Local. Simplify namespacing logic. Updates all tests to reflect these changes.
This commit is contained in:
@ -1,6 +1,4 @@
|
||||
!module Equality
|
||||
|
||||
!import "lib/base.tri" Lib
|
||||
!import "lib/base.tri" !Local
|
||||
|
||||
main = lambdaEqualsTC
|
||||
|
||||
@ -28,7 +26,7 @@ not_Lambda? = demo_matchBool demo_false demo_true
|
||||
-- to different tree representations even if they share extensional behavior.
|
||||
|
||||
-- Let's see if these are the same:
|
||||
lambdaEqualsTC = Lib.equal? not_TC? not_Lambda?
|
||||
lambdaEqualsTC = equal? not_TC? not_Lambda?
|
||||
|
||||
-- Here are some checks to verify their extensional behavior is the same:
|
||||
true_TC? = not_TC? demo_false
|
||||
@ -37,5 +35,5 @@ false_TC? = not_TC? demo_true
|
||||
true_Lambda? = not_Lambda? demo_false
|
||||
false_Lambda? = not_Lambda? demo_true
|
||||
|
||||
bothTrueEqual? = Lib.equal? true_TC? true_Lambda?
|
||||
bothFalseEqual? = Lib.equal? false_TC? false_Lambda?
|
||||
bothTrueEqual? = equal? true_TC? true_Lambda?
|
||||
bothFalseEqual? = equal? false_TC? false_Lambda?
|
||||
|
@ -1,6 +1,4 @@
|
||||
!module LOT
|
||||
|
||||
!import "lib/base.tri" Lib
|
||||
!import "lib/base.tri" !Local
|
||||
|
||||
main = exampleTwo
|
||||
-- Level Order Traversal of a labelled binary tree
|
||||
@ -19,41 +17,41 @@ main = exampleTwo
|
||||
-- / / \
|
||||
-- 4 5 6
|
||||
|
||||
label = \node : Lib.head node
|
||||
label = \node : head node
|
||||
|
||||
left = (\node : Lib.if (Lib.emptyList? node)
|
||||
left = (\node : if (emptyList? node)
|
||||
[]
|
||||
(Lib.if (Lib.emptyList? (Lib.tail node))
|
||||
(if (emptyList? (tail node))
|
||||
[]
|
||||
(Lib.head (Lib.tail node))))
|
||||
(head (tail node))))
|
||||
|
||||
right = (\node : Lib.if (Lib.emptyList? node)
|
||||
right = (\node : if (emptyList? node)
|
||||
[]
|
||||
(Lib.if (Lib.emptyList? (Lib.tail node))
|
||||
(if (emptyList? (tail node))
|
||||
[]
|
||||
(Lib.if (Lib.emptyList? (Lib.tail (Lib.tail node)))
|
||||
(if (emptyList? (tail (tail node)))
|
||||
[]
|
||||
(Lib.head (Lib.tail (Lib.tail node))))))
|
||||
(head (tail (tail node))))))
|
||||
|
||||
processLevel = Lib.y (\self queue : Lib.if (Lib.emptyList? queue)
|
||||
processLevel = y (\self queue : if (emptyList? queue)
|
||||
[]
|
||||
(Lib.pair (Lib.map label queue) (self (Lib.filter
|
||||
(\node : Lib.not? (Lib.emptyList? node))
|
||||
(Lib.lconcat (Lib.map left queue) (Lib.map right queue))))))
|
||||
(pair (map label queue) (self (filter
|
||||
(\node : not? (emptyList? node))
|
||||
(lconcat (map left queue) (map right queue))))))
|
||||
|
||||
levelOrderTraversal_ = \a : processLevel (t a t)
|
||||
|
||||
toLineString = Lib.y (\self levels : Lib.if (Lib.emptyList? levels)
|
||||
toLineString = y (\self levels : if (emptyList? levels)
|
||||
""
|
||||
(Lib.lconcat
|
||||
(Lib.lconcat (Lib.map (\x : Lib.lconcat x " ") (Lib.head levels)) "")
|
||||
(Lib.if (Lib.emptyList? (Lib.tail levels)) "" (Lib.lconcat (t (t 10 t) t) (self (Lib.tail levels))))))
|
||||
(lconcat
|
||||
(lconcat (map (\x : lconcat x " ") (head levels)) "")
|
||||
(if (emptyList? (tail levels)) "" (lconcat (t (t 10 t) t) (self (tail levels))))))
|
||||
|
||||
levelOrderToString = \s : toLineString (levelOrderTraversal_ s)
|
||||
|
||||
flatten = Lib.foldl (\acc x : Lib.lconcat acc x) ""
|
||||
flatten = foldl (\acc x : lconcat acc x) ""
|
||||
|
||||
levelOrderTraversal = \s : Lib.lconcat (t 10 t) (flatten (levelOrderToString s))
|
||||
levelOrderTraversal = \s : lconcat (t 10 t) (flatten (levelOrderToString s))
|
||||
|
||||
exampleOne = levelOrderTraversal [("1")
|
||||
[("2") [("4") t t] t]
|
||||
|
@ -1,24 +1,22 @@
|
||||
!module Size
|
||||
|
||||
!import "lib/base.tri" Lib
|
||||
!import "lib/base.tri" !Local
|
||||
|
||||
main = size size
|
||||
|
||||
compose = \f g x : f (g x)
|
||||
|
||||
succ = Lib.y (\self :
|
||||
Lib.triage
|
||||
succ = y (\self :
|
||||
triage
|
||||
1
|
||||
t
|
||||
(Lib.triage
|
||||
(triage
|
||||
(t (t t))
|
||||
(\_ Lib.tail : t t (self Lib.tail))
|
||||
(\_ tail : t t (self tail))
|
||||
t))
|
||||
|
||||
size = (\x :
|
||||
(Lib.y (\self x :
|
||||
(y (\self x :
|
||||
compose succ
|
||||
(Lib.triage
|
||||
(triage
|
||||
(\x : x)
|
||||
self
|
||||
(\x y : compose (self x) (self y))
|
||||
|
@ -1,8 +1,6 @@
|
||||
!module ToSource
|
||||
!import "lib/base.tri" !Local
|
||||
|
||||
!import "lib/base.tri" Lib
|
||||
|
||||
main = toSource Lib.not?
|
||||
main = toSource not?
|
||||
-- Thanks to intensionality, we can inspect the structure of a given value
|
||||
-- even if it's a function. This includes lambdas which are eliminated to
|
||||
-- Tree Calculus (TC) terms during evaluation.
|
||||
@ -16,29 +14,29 @@ main = toSource Lib.not?
|
||||
-- triage = (\leaf stem fork : t (t leaf stem) fork)
|
||||
|
||||
-- Base case of a single Leaf
|
||||
sourceLeaf = t (Lib.head "t")
|
||||
sourceLeaf = t (head "t")
|
||||
|
||||
-- Stem case
|
||||
sourceStem = (\convert : (\a rest :
|
||||
t (Lib.head "(") -- Start with a left parenthesis "(".
|
||||
(t (Lib.head "t") -- Add a "t"
|
||||
(t (Lib.head " ") -- Add a space.
|
||||
(convert a -- Recursively convert the argument.
|
||||
(t (Lib.head ")") rest)))))) -- Close with ")" and append the rest.
|
||||
t (head "(") -- Start with a left parenthesis "(".
|
||||
(t (head "t") -- Add a "t"
|
||||
(t (head " ") -- Add a space.
|
||||
(convert a -- Recursively convert the argument.
|
||||
(t (head ")") rest)))))) -- Close with ")" and append the rest.
|
||||
|
||||
-- Fork case
|
||||
sourceFork = (\convert : (\a b rest :
|
||||
t (Lib.head "(") -- Start with a left parenthesis "(".
|
||||
(t (Lib.head "t") -- Add a "t"
|
||||
(t (Lib.head " ") -- Add a space.
|
||||
(convert a -- Recursively convert the first arg.
|
||||
(t (Lib.head " ") -- Add another space.
|
||||
(convert b -- Recursively convert the second arg.
|
||||
(t (Lib.head ")") rest)))))))) -- Close with ")" and append the rest.
|
||||
t (head "(") -- Start with a left parenthesis "(".
|
||||
(t (head "t") -- Add a "t"
|
||||
(t (head " ") -- Add a space.
|
||||
(convert a -- Recursively convert the first arg.
|
||||
(t (head " ") -- Add another space.
|
||||
(convert b -- Recursively convert the second arg.
|
||||
(t (head ")") rest)))))))) -- Close with ")" and append the rest.
|
||||
|
||||
-- Wrapper around triage
|
||||
toSource_ = Lib.y (\self arg :
|
||||
Lib.triage
|
||||
toSource_ = y (\self arg :
|
||||
triage
|
||||
sourceLeaf -- `triage` "a" case, Leaf
|
||||
(sourceStem self) -- `triage` "b" case, Stem
|
||||
(sourceFork self) -- `triage` "c" case, Fork
|
||||
@ -47,5 +45,5 @@ toSource_ = Lib.y (\self arg :
|
||||
-- toSource takes a single TC term and returns a String
|
||||
toSource = \v : toSource_ v ""
|
||||
|
||||
exampleOne = toSource Lib.true -- OUT: "(t t)"
|
||||
exampleTwo = toSource Lib.not? -- OUT: "(t (t (t t) (t t t)) (t t (t t t)))"
|
||||
exampleOne = toSource true -- OUT: "(t t)"
|
||||
exampleTwo = toSource not? -- OUT: "(t (t (t t) (t t t)) (t t (t t t)))"
|
||||
|
Reference in New Issue
Block a user