From b8e2743103d0cbe5cedb96b15331b8de9527851c Mon Sep 17 00:00:00 2001 From: James Eversole Date: Wed, 16 Apr 2025 14:23:53 -0500 Subject: [PATCH] Updates to demos --- demos/equality.tri | 15 ++++++-------- demos/levelOrderTraversal.tri | 4 ++-- demos/patternMatching.tri | 37 +++++++++++++++++++++++++++++++++++ demos/size.tri | 14 ++++++------- demos/toSource.tri | 8 ++++---- lib/list.tri | 2 ++ lib/patterns.tri | 20 ++++--------------- 7 files changed, 61 insertions(+), 39 deletions(-) create mode 100644 demos/patternMatching.tri diff --git a/demos/equality.tri b/demos/equality.tri index 16b1ccc..28f5383 100644 --- a/demos/equality.tri +++ b/demos/equality.tri @@ -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? diff --git a/demos/levelOrderTraversal.tri b/demos/levelOrderTraversal.tri index 9474cdb..a2cf4db 100644 --- a/demos/levelOrderTraversal.tri +++ b/demos/levelOrderTraversal.tri @@ -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)) [] diff --git a/demos/patternMatching.tri b/demos/patternMatching.tri new file mode 100644 index 0000000..88f733d --- /dev/null +++ b/demos/patternMatching.tri @@ -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!" diff --git a/demos/size.tri b/demos/size.tri index 12e0a25..9d2d176 100644 --- a/demos/size.tri +++ b/demos/size.tri @@ -3,11 +3,9 @@ main = size size -size = (x : - (y (self x : - compose succ - (triage - (x : x) - self - (x y : compose (self x) (self y)) - x)) x 0)) +size = x : y (self x : compose succ (triage + id + self + (x y : compose (self x) (self y)) + x) +) x 0 diff --git a/demos/toSource.tri b/demos/toSource.tri index 0141382..8028ce3 100644 --- a/demos/toSource.tri +++ b/demos/toSource.tri @@ -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 : diff --git a/lib/list.tri b/lib/list.tri index b888b6b..ccddfa1 100644 --- a/lib/list.tri +++ b/lib/list.tri @@ -1,5 +1,7 @@ !import "base.tri" !Local +_ = t + matchList = a b : triage a _ b emptyList? = matchList true (_ _ : false) diff --git a/lib/patterns.tri b/lib/patterns.tri index bad463b..da55395 100644 --- a/lib/patterns.tri +++ b/lib/patterns.tri @@ -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!")]])