Updates to demos
This commit is contained in:
parent
25bfe139e8
commit
b8e2743103
@ -12,19 +12,16 @@ not_TC? = t (t (t t) (t t t)) (t t (t t t))
|
||||
|
||||
-- /demos/toSource.tri contains an explanation of `triage`
|
||||
demo_triage = a b c : t (t a b) c
|
||||
demo_matchBool = (ot of : demo_triage
|
||||
of
|
||||
(_ : ot)
|
||||
(_ _ : ot)
|
||||
)
|
||||
demo_matchBool = a b : demo_triage b (_ : a) (_ _ : a)
|
||||
|
||||
-- Lambda representation of the Boolean `not` function
|
||||
not_Lambda? = demo_matchBool demo_false demo_true
|
||||
|
||||
-- Since tricu eliminates Lambda terms to SKI combinators, the tree form of many
|
||||
-- As tricu eliminates Lambda terms to SKI combinators, the tree form of many
|
||||
-- functions defined via Lambda terms are larger than the most efficient TC
|
||||
-- representation. Between different languages that evaluate to tree calculus
|
||||
-- terms, the exact implementation of Lambda elimination may differ and lead
|
||||
-- to different tree representations even if they share extensional behavior.
|
||||
-- representation possible. Between different languages that evaluate to tree
|
||||
-- calculus terms, the exact implementation of Lambda elimination may differ
|
||||
-- and lead to different trees even if they share extensional behavior.
|
||||
|
||||
-- Let's see if these are the same:
|
||||
lambdaEqualsTC = equal? not_TC? not_Lambda?
|
||||
|
@ -20,13 +20,13 @@ main = exampleTwo
|
||||
|
||||
label = node : head node
|
||||
|
||||
left = (node : if (emptyList? node)
|
||||
left = node : (if (emptyList? node)
|
||||
[]
|
||||
(if (emptyList? (tail node))
|
||||
[]
|
||||
(head (tail node))))
|
||||
|
||||
right = (node : if (emptyList? node)
|
||||
right = node : (if (emptyList? node)
|
||||
[]
|
||||
(if (emptyList? (tail node))
|
||||
[]
|
||||
|
37
demos/patternMatching.tri
Normal file
37
demos/patternMatching.tri
Normal file
@ -0,0 +1,37 @@
|
||||
!import "../lib/patterns.tri" !Local
|
||||
|
||||
-- We can do conditional pattern matching by providing a list of lists, where
|
||||
-- each sublist contains a boolean expression and a function to return if said
|
||||
-- boolean expression evaluates to true.
|
||||
|
||||
value = 42
|
||||
main = match value [[(equal? "Hello") (_ : ", world!")] [(equal? 42) (_ : "The answer.")]]
|
||||
|
||||
-- < main
|
||||
-- > "The answer."
|
||||
|
||||
matchExample = (x : match x
|
||||
[[(equal? 1) (_ : "one")]
|
||||
[(equal? 2) (_ : "two")]
|
||||
[(equal? 3) (_ : "three")]
|
||||
[(equal? 4) (_ : "four")]
|
||||
[(equal? 5) (_ : "five")]
|
||||
[(equal? 6) (_ : "six")]
|
||||
[(equal? 7) (_ : "seven")]
|
||||
[(equal? 8) (_ : "eight")]
|
||||
[(equal? 9) (_ : "nine")]
|
||||
[(equal? 10) (_ : "ten")]
|
||||
[ otherwise (_ : "I ran out of fingers!")]])
|
||||
|
||||
-- < matchExample 3
|
||||
-- > "three"
|
||||
-- < matchExample 5
|
||||
-- > "five"
|
||||
-- < matchExample 9
|
||||
-- > "nine"
|
||||
-- < matchExample 11
|
||||
-- > "I ran out of fingers!"
|
||||
-- < matchExample "three"
|
||||
-- > "I ran out of fingers!"
|
||||
-- < matchExample [("hello") ("world")]
|
||||
-- > "I ran out of fingers!"
|
@ -3,11 +3,9 @@
|
||||
|
||||
main = size size
|
||||
|
||||
size = (x :
|
||||
(y (self x :
|
||||
compose succ
|
||||
(triage
|
||||
(x : x)
|
||||
size = x : y (self x : compose succ (triage
|
||||
id
|
||||
self
|
||||
(x y : compose (self x) (self y))
|
||||
x)) x 0))
|
||||
x)
|
||||
) x 0
|
||||
|
@ -18,22 +18,22 @@ main = toSource not?
|
||||
sourceLeaf = t (head "t")
|
||||
|
||||
-- Stem case
|
||||
sourceStem = (convert : (a rest :
|
||||
sourceStem = convert : (a 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.
|
||||
(t (head ")") rest))))) -- Close with ")" and append the rest.
|
||||
|
||||
-- Fork case
|
||||
sourceFork = (convert : (a b rest :
|
||||
sourceFork = convert : (a b 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.
|
||||
(t (head ")") rest))))))) -- Close with ")" and append the rest.
|
||||
|
||||
-- Wrapper around triage
|
||||
toSource_ = y (self arg :
|
||||
|
@ -1,5 +1,7 @@
|
||||
!import "base.tri" !Local
|
||||
|
||||
_ = t
|
||||
|
||||
matchList = a b : triage a _ b
|
||||
|
||||
emptyList? = matchList true (_ _ : false)
|
||||
|
@ -1,4 +1,5 @@
|
||||
!import "list.tri" !Local
|
||||
!import "base.tri" !Local
|
||||
!import "list.tri" List
|
||||
|
||||
match_ = y (self value patterns :
|
||||
triage
|
||||
@ -16,21 +17,8 @@ match_ = y (self value patterns :
|
||||
patterns)
|
||||
|
||||
match = (value patterns :
|
||||
match_ value (map (sublist :
|
||||
pair (head sublist) (head (tail sublist)))
|
||||
match_ value (List.map (sublist :
|
||||
pair (List.head sublist) (List.head (List.tail sublist)))
|
||||
patterns))
|
||||
|
||||
otherwise = const (t t)
|
||||
|
||||
matchExample = (x : match x
|
||||
[[(equal? 1) (_ : "one")]
|
||||
[(equal? 2) (_ : "two")]
|
||||
[(equal? 3) (_ : "three")]
|
||||
[(equal? 4) (_ : "four")]
|
||||
[(equal? 5) (_ : "five")]
|
||||
[(equal? 6) (_ : "six")]
|
||||
[(equal? 7) (_ : "seven")]
|
||||
[(equal? 8) (_ : "eight")]
|
||||
[(equal? 9) (_ : "nine")]
|
||||
[(equal? 10) (_ : "ten")]
|
||||
[ otherwise (_ : "I ran out of fingers!")]])
|
||||
|
Loading…
x
Reference in New Issue
Block a user