tricu

An interpreted language for exploring Tree Calculus
Log | Files | Refs | README | LICENSE

language-reference.tri (4021B)


      1 !import bd2c14d6db6b69308a5252a8e839705a55d30e815f012900b094315263e08d81 *
      2 !import 7f87549d587fa8c3be88af0ea988db3adcc3b35f1ffb48973553491b075614db as Math
      3 !export *
      4 
      5 -- The open base import supplies contract helpers and unqualified names.
      6 -- Math is a qualified import: its names start with Math.
      7 -- A syntax sampler: independent examples, rather than one application.
      8 -- Start with answer / Natural number: 42. Choose a definition for its preset.
      9 -- Full contracts: follow For AI assistants in the footer to Language features.
     10 -- Each comment below gives an expression and its interpretation.
     11 
     12 -- Names, lambdas, named parameters, application and grouping.
     13 identity = (x : x)
     14 first a b = a
     15 answer = identity (first 42 7)
     16 -- identity "hello" / Text -> hello; first 7 9 / Natural number -> 7.
     17 
     18 -- The kernel constructors: Leaf, Stem and Fork. Use tricu for arbitrary trees.
     19 leaf = t
     20 shapes = [t (t t) (t t t)]
     21 -- leaf / tricu -> t; shapes / tricu shows a list containing all three shapes.
     22 literals = ["hello" 42 [1 2 3]]
     23 -- literals / tricu shows mixed values. Strings support Unicode; names are ASCII.
     24 -- Lists use spaces. Group application items: [(identity 1) (identity 2)].
     25 
     26 -- Partial application: provide one of a function's arguments now.
     27 addTwo = Math.add 2
     28 -- addTwo 5 / Natural number -> 7.
     29 
     30 -- Ordered equations: literals, a qualified pin, exact lists, cons and wildcard.
     31 -- A pin (^true) compares against an existing value.
     32 describe 7 = "seven"
     33 describe "hi" = "greeting"
     34 describe ^true = "true"
     35 describe [a b] = "two items"
     36 describe [head | tail] = "nonempty list"
     37 describe [] = "empty list"
     38 describe _ = "other"
     39 -- Try each with Text: describe 7, describe "hi", describe (t t),
     40 -- describe [4 5], describe [1 2 3], describe [], describe (t (t t)).
     41 
     42 -- Pair patterns and whole-value capture. Construct a pair with t left right.
     43 keep whole{left, right} = whole
     44 keep _ = t
     45 -- keep (t t t) / tricu -> (t t t); keep (t t) / tricu -> t.
     46 
     47 -- Plain local bindings are sequential and nonrecursive.
     48 local x = (let
     49   double n = Math.multiply n 2
     50   in double x)
     51 viaWhere x = step x where step y = Math.add y 1
     52 -- local 6 -> 12; viaWhere 6 -> 7, both Natural number.
     53 
     54 -- Direct self-recursion, with a final fallback equation.
     55 copy [] = []
     56 copy [head | tail] = t head (copy tail)
     57 copy _ = []
     58 -- copy [1 2 3] / Bytes -> [1, 2, 3]. Bytes displays naturals from 0 through 255.
     59 -- Use tricu for a general list; copy [] and copy (t t) both return [].
     60 
     61 -- case examines an intermediate value; arms align and finish with a fallback.
     62 firstOr fallback xs = case xs of
     63   [head | _] = head
     64   _ = fallback
     65 -- firstOr 99 [5] -> 5; firstOr 99 [] -> 99, both Natural number.
     66 
     67 -- Local self-recursion is explicit: let rec (also available with where rec).
     68 localCopy xs = (let rec
     69   walk [] = []
     70   walk [head | tail] = t head (walk tail)
     71   walk _ = []
     72   in walk xs)
     73 -- localCopy [4 5] / Bytes -> [4, 5]; localCopy [] -> [].
     74 
     75 -- Parameter and result contracts check values at runtime.
     76 checked x@nat? =@nat? x
     77 -- checked 42 / Natural number -> 42. A failed contract produces a Result.
     78 -- Keep annotations on plain function heads; see Contracts for Result handlers.
     79 
     80 -- Pipes and do use an explicit sequencing operator. Here a lambda passes a value
     81 -- to the next continuation. It is ordinary pure code; IO uses IO.bind instead.
     82 sequenced input = (do (value next : next value)
     83   value <- input |> identity
     84   identity value
     85   identity <| value)
     86 -- sequenced 9 / Natural number -> 9.
     87 -- Both pipe chains associate left-to-right; group nested applications explicitly.
     88 
     89 |- Block comments are another way to annotate source. They do not nest. -|
     90 -- !export * selects local definitions. Use !export name for a smaller interface.
     91 -- An open import uses !import MANIFEST *; a reexport uses !export * from MANIFEST.
     92 -- Replace MANIFEST with the full identity from Add import or the library guide.
     93 -- Reexports select the public interface; imports introduce names into scope.
     94 -- See Conversation for IO, and the docs for Maybe/Result/parser protocols.