Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Standard ML help datatype problem

  1. Feb 4, 2010 #1
    Standard ML help!! datatype problem

    I have to create a function about peano numbers defined as the following datatype:

    datatype 'a peano = P of ('a -> 'a) * 'a -> 'a


    I have to create a function about peano numbers defined as the following datatype:
    datatype 'a peano = P of ('a -> 'a) * 'a -> 'a
    val zero = P(fn (f, x) => x)


    The function that i have to implement finds the succesive peano number of the peano parameter P(p).
    This is what I have written:

    fun suc (P(p)) = case P(p) of P(fn(f,x)=>x) => P(fn(f,x)=>f(x));

    The problem is that i get these errors:
    stdIn:4.33-4.36 Error: syntax error: deleting FN LPAREN
    stdIn:4.43 Error: syntax error found at RPAREN

    I don't know what Im doing wrong please help!!
     
  2. jcsd
  3. Feb 10, 2010 #2
    Re: Standard ML help!! datatype problem

    So... it has been a very long time since I have done any S/ML and I don't have an interpreter handy, but at least maybe I can help you decode the error message.

    When it says 4.33-4.36 it's referring to line 4, characters 33 through 36. What it says is the tokens "FN LPAREN" are not recognized in the place where you put them. This of course is "fn" followed by a "left parenthesis", which is indeed at the 33rd character position in your line of code.

    Then when it gets to character 43 it hits a right parenthesis. Because it previously decided to ignore the lparen, the rparen of course also becomes a syntax error.

    Are you absolutely sure it is legal to put an fn in that place, or in general on the right-hand side of a "case of"? The ml type system is very smart but it has limits. You seem to be wanting it to pattern match against a function that takes (f,x) and returns x. I really don't see how the pattern matcher could possibly know whether a function fits that pattern without evaluating the function (and really not even then). Maybe try to check your documentation or textbook and see exactly what kinds of things are legal after that "of". (And come to think of it, why are you pattern matching at all?-- that is, what is the point of a "case" statement with only one "of"?)
     
  4. Feb 17, 2010 #3
    Re: Standard ML help!! datatype problem

    Sorry, I didn't notice you had replied until now. But thank you so much!
    If anyone has a problem like this I solved it, here's my code :
    datatype 'a peano = P of ('a -> 'a) * 'a -> 'a

    val zero = P(fn (f, x) => x)


    (* create : int -> 'a peano
    * create(n) = the Peano number representing n
    * Precondition: n >= 0)

    fun create 0=zero
    |create num =
    let
    fun helper (1,f,x) = f(x)
    | helper (num,f,x) = helper (num-1,f,f(x))
    in
    P(fn(f,x)=>helper(num,f,x))
    end;

    (* peanoToInt : int peano -> int
    * Postcondition: result >= 0
    *)
    fun peanoToInt (P(p)) = let fun f(x)=x+1 in p(f,0) end;

    (* suc : peano -> 'a peano
    * suc(p) = successor of the Peano number p
    *)
    fun suc (P(p)) = P(fn(f,x)=>p(f,f(x)));

    * double : 'a peano -> 'a peano
    * double p = (Peano number p) * 2
    *)
    fun double (P(p)) = P(fn(f,x)=> p(f,p(f,x)));
     
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook