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`
|
-- /demos/toSource.tri contains an explanation of `triage`
|
||||||
demo_triage = a b c : t (t a b) c
|
demo_triage = a b c : t (t a b) c
|
||||||
demo_matchBool = (ot of : demo_triage
|
demo_matchBool = a b : demo_triage b (_ : a) (_ _ : a)
|
||||||
of
|
|
||||||
(_ : ot)
|
|
||||||
(_ _ : ot)
|
|
||||||
)
|
|
||||||
-- Lambda representation of the Boolean `not` function
|
-- Lambda representation of the Boolean `not` function
|
||||||
not_Lambda? = demo_matchBool demo_false demo_true
|
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
|
-- functions defined via Lambda terms are larger than the most efficient TC
|
||||||
-- representation. Between different languages that evaluate to tree calculus
|
-- representation possible. Between different languages that evaluate to tree
|
||||||
-- terms, the exact implementation of Lambda elimination may differ and lead
|
-- calculus terms, the exact implementation of Lambda elimination may differ
|
||||||
-- to different tree representations even if they share extensional behavior.
|
-- and lead to different trees even if they share extensional behavior.
|
||||||
|
|
||||||
-- Let's see if these are the same:
|
-- Let's see if these are the same:
|
||||||
lambdaEqualsTC = equal? not_TC? not_Lambda?
|
lambdaEqualsTC = equal? not_TC? not_Lambda?
|
||||||
|
@ -20,13 +20,13 @@ main = exampleTwo
|
|||||||
|
|
||||||
label = node : head node
|
label = node : head node
|
||||||
|
|
||||||
left = (node : if (emptyList? node)
|
left = node : (if (emptyList? node)
|
||||||
[]
|
[]
|
||||||
(if (emptyList? (tail node))
|
(if (emptyList? (tail node))
|
||||||
[]
|
[]
|
||||||
(head (tail node))))
|
(head (tail node))))
|
||||||
|
|
||||||
right = (node : if (emptyList? node)
|
right = node : (if (emptyList? node)
|
||||||
[]
|
[]
|
||||||
(if (emptyList? (tail 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
|
main = size size
|
||||||
|
|
||||||
size = (x :
|
size = x : y (self x : compose succ (triage
|
||||||
(y (self x :
|
id
|
||||||
compose succ
|
|
||||||
(triage
|
|
||||||
(x : x)
|
|
||||||
self
|
self
|
||||||
(x y : compose (self x) (self y))
|
(x y : compose (self x) (self y))
|
||||||
x)) x 0))
|
x)
|
||||||
|
) x 0
|
||||||
|
@ -18,22 +18,22 @@ main = toSource not?
|
|||||||
sourceLeaf = t (head "t")
|
sourceLeaf = t (head "t")
|
||||||
|
|
||||||
-- Stem case
|
-- Stem case
|
||||||
sourceStem = (convert : (a rest :
|
sourceStem = convert : (a rest :
|
||||||
t (head "(") -- Start with a left parenthesis "(".
|
t (head "(") -- Start with a left parenthesis "(".
|
||||||
(t (head "t") -- Add a "t"
|
(t (head "t") -- Add a "t"
|
||||||
(t (head " ") -- Add a space.
|
(t (head " ") -- Add a space.
|
||||||
(convert a -- Recursively convert the argument.
|
(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
|
-- Fork case
|
||||||
sourceFork = (convert : (a b rest :
|
sourceFork = convert : (a b rest :
|
||||||
t (head "(") -- Start with a left parenthesis "(".
|
t (head "(") -- Start with a left parenthesis "(".
|
||||||
(t (head "t") -- Add a "t"
|
(t (head "t") -- Add a "t"
|
||||||
(t (head " ") -- Add a space.
|
(t (head " ") -- Add a space.
|
||||||
(convert a -- Recursively convert the first arg.
|
(convert a -- Recursively convert the first arg.
|
||||||
(t (head " ") -- Add another space.
|
(t (head " ") -- Add another space.
|
||||||
(convert b -- Recursively convert the second arg.
|
(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
|
-- Wrapper around triage
|
||||||
toSource_ = y (self arg :
|
toSource_ = y (self arg :
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
!import "base.tri" !Local
|
!import "base.tri" !Local
|
||||||
|
|
||||||
|
_ = t
|
||||||
|
|
||||||
matchList = a b : triage a _ b
|
matchList = a b : triage a _ b
|
||||||
|
|
||||||
emptyList? = matchList true (_ _ : false)
|
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 :
|
match_ = y (self value patterns :
|
||||||
triage
|
triage
|
||||||
@ -16,21 +17,8 @@ match_ = y (self value patterns :
|
|||||||
patterns)
|
patterns)
|
||||||
|
|
||||||
match = (value patterns :
|
match = (value patterns :
|
||||||
match_ value (map (sublist :
|
match_ value (List.map (sublist :
|
||||||
pair (head sublist) (head (tail sublist)))
|
pair (List.head sublist) (List.head (List.tail sublist)))
|
||||||
patterns))
|
patterns))
|
||||||
|
|
||||||
otherwise = const (t t)
|
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