Pattern matching compile failure

34 views
Skip to first unread message

Mike Jones

unread,
Sep 27, 2015, 12:54:24 PM9/27/15
to ats-lang-users
  This code compiles with an error: this pattern match clause is redundant

But this code is like the example in the book. What led to this code was when the second case "idle" was missing, things behaved like the the first case "transacting" evaluated true no matter what s.ps was. So I added the second case just to debug that, and got the compile error, which kind of confirms it is always matching.

fun isWeekday
  (x: wday): bool = case x of
  | Saturday() => false | Sunday() => false | _ => true
// end of [isWeekday]

CODE

datatype pstate =
    | Idle of ()
    | Transacting of ()
    | Hang of ()

  typedef state = @{ addr= uint8, ps= pstate }

fun process (c:int, s:state) : state = let
    val  sn  = case 0 of
              | _ when ch = 'p' => let
                val i:int = case s.ps of
                  | transacting => let
                     val _ = i2c_stop()
                     in 1 end
                  | idle => let
                     val _ = i2c_start()
                     in 2 end
                val () = print! i
                in @{ addr = s.addr, ps = idle } end  

Hongwei Xi

unread,
Sep 27, 2015, 1:01:33 PM9/27/15
to ats-lan...@googlegroups.com
If a nullary constructor Foo is used as a pattern, it needs to be
written as 'Foo()'; 'Foo' is considered a variable when used as a pattern.


--
You received this message because you are subscribed to the Google Groups "ats-lang-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ats-lang-user...@googlegroups.com.
To post to this group, send email to ats-lan...@googlegroups.com.
Visit this group at http://groups.google.com/group/ats-lang-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/ats-lang-users/4176c280-dd32-4328-9f92-96b9d61f6e53%40googlegroups.com.

Mike Jones

unread,
Sep 27, 2015, 3:00:48 PM9/27/15
to ats-lang-users
I will store that one next to:

if (i = j)
{
  be_subtle_ and_cause_trouble();
}

Solved problem...

Thanks

gmhwxi

unread,
Sep 27, 2015, 4:19:23 PM9/27/15
to ats-lang-users
Unlike using '=' for '==' in C, using 'Foo' for 'Foo()' causes no trouble:

1) If 'Foo' is the pattern for the last clause, then 'Foo()' and 'Foo' are
    really the same (assuming pattern matching is exhaustive)
2) If 'Foo' is not the last one, then you always get a compilation error.
Reply all
Reply to author
Forward
0 new messages