tricu

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

consumer_success_patterns.tri (1406B)


      1 !import "consumers.lowerings.success" U
      2 !import "consumers.lowerings.specialize" P
      3 !import "consumers.structural" S
      4 !import "prelude" !Local
      5 rule output = S.makeConsumer t [(S.makeRule (S.patCapture 0) S.patAny output)]
      6 same output = equal? (P.runDirect (P.compileIndexed (rule output)) 9) (P.runDirect (U.compile (rule output)) 9)
      7 constructor = S.outFork (S.outCapture 0) (S.outStem (S.outFork (S.outLiteral 7) (S.outCapture 0)))
      8 identity = U.compile (rule (S.outCapture 0))
      9 failed = U.compile (S.makeConsumer t t)
     10 returned = S.outCall (S.outLiteral identity) (S.outLiteral 42) 0 constructor
     11 badCall = S.outCall (S.outLiteral failed) (S.outLiteral 42) 0 constructor
     12 checks = foldl and? true [(same constructor)
     13  (same (S.outCapture 99))
     14  (same (S.outFork (S.outCapture 99) constructor))
     15  (same (S.outFork constructor (S.outCapture 99)))
     16  (same (S.outStem (S.outCapture 99)))
     17  (same (S.outEqual constructor constructor))
     18  (equal? (P.runDirect (U.compile (rule returned)) 9) (ok (pair 42 (t (pair 7 42))) t))
     19  (equal? (P.runDirect (U.compile (rule badCall)) 9) (err "no matching consumer rule" t))
     20  (same (S.outFork (S.outCapture 99) (pair 99 t)))
     21  (lazyMaybe (_ : false) (_ : true) (U.plain constructor [0]))
     22  (lazyMaybe (_ : true) (_ : false) (U.plain constructor t))
     23  (lazyMaybe (_ : true) (_ : false) (U.plain returned [0]))]
     24 main = matchBool "success-patterns-ok" "success-patterns-DIFFER" checks
     25 main