sml,polyml , Check if function is defined in SML

Check if function is defined in SML


Tag: sml,polyml

Given an SML source file, is it possible to check (using Poly/ML) whether or not a list of function/value names are defined? If so, how?

Alternatively, I've noticed that you can do the following. Suppose we have a source file to be inspected named somefile.sml. Suppose we create the file test.sml, with the following contents:

use "somefile"
f; (* defined in somefile.sml *)
g; (* undefined *)

And then we run:

use "test" handle e as SyntaxError => (print (exnMessage e));

Unfortunately, this only prints out "Static Errors". Is there any way, similar to that described above, to determine (in code) which functions in "test.sml" are not defined?


There's probably no way to do this portably but in Poly/ML you can find out whether a value, or anything else, is defined using PolyML.globalNameSpace.

To test for a value (e.g. a function) use

#lookupVal PolyML.globalNameSpace

This takes a name and returns a option type which is SOME if the value has been defined and NONE if it has not. So

#lookupVal PolyML.globalNameSpace "f";

will return



#lookupVal PolyML.globalNameSpace "g";

will return NONE.


mlton gives library-related error

When I try to compile a program with mlton, I get an error. ~/projects/serve-sml $ mlton In file included from /usr/lib/mlton/include/platform.h:13:0, from /usr/lib/mlton/include/common-main.h:16, from /usr/lib/mlton/include/amd64-main.h:11, from /tmp/file86PWQJ.1.c:110: /usr/lib/mlton/include/cenv.h:137:17: fatal error: gmp.h: No such file or directory #include "gmp.h" ^ compilation terminated. call to system failed with exit status 1:...

SML error in printing off length of largest number in a list

I'm using 3 functions to find the largest number in a list and print it's length. Sample input would be maxL [5, 199, 3000, 63]; fun max(a,b) = if a>b then a else b; fun maxL(L) = if L=[] then 0 else let val largest = max(hd(L), maxL(tl(L))) in num_digits...

Trying to write a function in SML

I am trying to write a function in sml that takes a list as its first argument and a number as its second. The result should be: greaterT [3,5,2,4,7]3; val it = [5,4,7] : int list This is my work so far but doesn't work yet. fun greaterT ([],k) =...

How turn list of pair in list of int, where result int is sum of pair

I try to define function with the following protocol: [(1,2), (6,5), (9,10)] -> [3, 11, 19] Here is what I have now: fun sum_pairs (l : (int * int) list) = if null l then [] else (#1 hd(l)) + (#2 hd(l))::sum_pairs(tl(l)) According to type checker I have some type...

How to make a block in SML inside of pattern matching construction

I'm a beginner in SML and still its syntax is not quite friendly to me (I'm a C# programmer). So this question may seem to you dumb. So, I have a recursive function with pattern matching (for example factorial), which I'm going to debug and I can't get how to...

How to optimize a list search in SML/NJ?

I am writing a piece of code in SML/NJ and need, at some point, to access a list, which I have already created. I know that, in C, for example, accessing an array takes constant time. So, I thought that that would be the case for ML as well. Apparently,...

Check if function is defined in SML

Given an SML source file, is it possible to check (using Poly/ML) whether or not a list of function/value names are defined? If so, how? Alternatively, I've noticed that you can do the following. Suppose we have a source file to be inspected named somefile.sml. Suppose we create the file...

Apply a list of parameters to a curried function

simple task: all I want is a function to apply a list of parameters to a curried function. Let's say our function is the famous add one: fun add a b = a + b; Now all I want is a function to apply a list (say [1, 5]) to...

Type error calling select Standard ML

I'm trying to call to deal with some sockets. It seems like the following minimal snippet from my code should work... fun descs ss = map Socket.sockDesc ss fun selecting server clients = let val { ss, ws, cs } = { rds = descs [server], wds =...

Printing only print output with SML/NJ

I'm trying to use SML/NJ, and I use sml < source.sml to run the code, but it prints out too much information. For example, this is the source.sml: fun fac 0 = 1 | fac n = n * fac (n - 1) val r = fac 10 ; print(Int.toString(r));...

SML - Find element in a list and substitute it

I'm trying to build a function which takes as input two list of type (string*string) list and returns one list of the same type. The first list is like a "lookup" list in which the second element is the element to search and the first element is the element to...

Making an integer value from a list that represents that integer

Hello i am attempting to write a function in SML. My goal is to create a function which takes a list that represents a value such as [2,3,4] and would return its integer representation i.e 432 (in the list the first element is the ones place). so what my train...

Convert Array to List in SML

How do i convert an array type to a list type in sml. I have searched the list and array structure functions but have not found one that does this (there is a list to array function though). Description of List structure: Description of Array structure:

Pattern match in binding of lambda?

In Haskell, I often do something like this: f $ \x -> case x of A a1 a2 -> ... B b1 b2 -> ... C c1 c2 -> ... But I don't want x, I just want to deconstruct it. In Standard ML I can do something like this:...

Standard ML: Getting Last in List

I'm trying to get the last element of a list of integers in SML but I want to indicate (somehow) when the user passes in an unacceptable list (such as an empty one). My code is here: fun lastInList [] = ~1 | lastInList [x] = x | lastInList (x::xs)...

Operator and operand don't agree can't find why

I'm working on this code and can't make it run. I've checked it few times and still can't figure out why it's not working. fun date_to_string (date : (int * int * int)) = let val months = ["January", "February","March", "April", "May", "June","July", "August", "September", "October", "November", "December"]; fun get_nth...

How is structure sharing broken in Standard ML?

In a 2013 presentation about the future of Standard ML, Bob Harper says, on slide 9, that "structure sharing is broken". Can someone give more detail on that? I don't have enough experience with sharing to understand what he meant.

Is there an include-like command for 'struct', similar to 'include' for 'sig'?

This question is related to how sectioning and Sidekick can be used with Isabelle/ML in Isabelle/jEdit. Consider two Isar commands, section and ML. These commands act as sectioning commands in the tree of the jEdit Sidekick plugin. One consequence is that I can use multiple ML{*...*} statements in a THY...

SML - Unzip tuples in one list

I need to "unzip" a list of tuples and even though I tried with the unzip function it doesn't do what I want to. In fact, as I understood the unzip function returns you a tuple of list, but what i want is this. Giving a list of tuple like...

Handling Keyboard Interrupts

I've got a minimal TCP server running in the SML/NJREPL, and I'm wondering how to gracefully close the listener socket on a keyboard interrupt. A stripped-down version of the server is fun sendHello sock = let val res = "HTTP/1.1 200 OK\r\nContent-Length: 12\r\n\r\nHello world!\r\n\r\n" val slc = Word8VectorSlice.full (Byte.stringToBytes res)...

How to change the datatype of an existing tree in SML?

I need to solve this problem. I really have no clue. Any help would be greatly appreciated. I guess a traversal needs to be done but I have no idea how to attack this problem. Thanks in advance. Consider again the following 1st datatype definition of a binary tree: datatype...

What do “continuations” mean in functional programming?(Specfically SML)

I have read a lot about continuations and a very common definition I saw is, it returns the control state. I am taking a functional programming course taught in SML. Our professor defined continuations to be: "What keeps track of what we still have to do" ; "Gives us control...

On signature matching of Standard ML

To my understanding, a CANDIDATE signature matches a TARGET signature if CANDIDATE is stronger than TARGET. Then if structure some_structure implements signature CANDIDATE, it implements signature TARGET too. But according to page 155 of Robert Haper's Programming in Standard ML (Version 1.2 of 11.02.11): signature MERGEABLE_QUEUE = sig include QUEUE...

Context-Free Grammar for Custom Programming Language

After having completed the Compiler Design course at my university I have been playing around with making a compiler for a simple programming language, but I'm having trouble with the parser. I'm making the compiler in mosml and using its builtin parser mosmlyac for constructing the parser. Here is an...

How to check recursive call results in CPS codes

So I'm working on a function to find some valid arithmetic operations to a target number from an int list. It's not allowed to use throw/callac. Only add and mul are valid arithmetic operations here and they are left associative. datatype operation = ADD | MULT (* find_op: int ->...

SML: How can I simulate a counter in SML without having an additional argument

I have a recursive function in SML that does a certain computation that doesn't really matter for my question. What I want to do is I want to track the number of times the recursion has taken place, as in I want to count the iterations of my algorithm. I...

isSubstring method İn ML

fun appear(x:string,y:string)= if String.isSubstring x y then print("APPEAR ") else print("NOT APPEAR "); appear("abc","asabcbc"); I wrote this function and compiled in Moskow ML.But there is an error like this: ! Unbound value component: String.isSubstring Can anybody help me ?...

Trying to Define `$` Type

Using Standard ML of New Jersey (v110.77), I'm trying to define the $ data type from Purely Functional Data Structures: datatype alpha susp = $ of alpha But I get an error: - datatype alpha susp = $ of alpha; stdIn:1.11-2.7 Error: syntax error: deleting ID ID EQUALOP What am...

Functors with multiple inputs in Standard ML

High level question: How do I use functors with multiple arguments in SML? I've looked at this, this, this and this(PDF). All of them seem to conflict in terms of structure or functor definition syntax, and none of them show anything other than a unary functor. Specifics: I'm trying to...

Wildcards in Standard ML

I just came across the following function in ML for the Working Programmer: fun null [] = true | null (_::_) = false 1) Can't both wildcards be empty lists? If not, how does ML prevent this? 2) Could the function be shortened to be: fun null [] = true...

SML - Concatenating strings in a list within a list

So I've got this list of lists of strings: [["#@","**","#@"],["##","*%","##"]] What I want to do is transform each inner list into a single string like this: ["#@**#@","##*%##"] Resulting in a list of strings. I've tried various combinations of map, foldr, and anonymous functions, but I can't for the life of...

Curried function for evaluating an equation in SML

The exact problem I need to work out is below. Basically, I am given a list of real numbers, representing coefficients. The first element is the constant value, and all that follow it are the coefficients of the polynomial equation. So, eval[1.0, 5.0, 3.0] 2.0 would be constructing the equation...

Functional Breadth First Search

Functional depth first search is lovely in directed acyclic graphs. In graphs with cycles however, how do we avoid infinite recursion? In a procedural language I would mark nodes as I hit them, but let's say I can't do that. A list of visited nodes is possible, but will be...

Error occuring in sml syntax of real.fromstring

I am new to sml and ml-lex. To convert string to real numbers we use the function real.fromstring. This is my code for conversion where yytext is an array of characters or a string. getOpt ((Real.fromString(yytext)), 0.0); I am using the above syntax in ml-lex for tokenising real numbers. {real}...

ML currying and anonymous functions

Could someone please explain to me what is going on in these three bindings? What is the significance of the parens? What is the meaning of g and why does it have to be a g as an argument in the anonymous function? And overall what is actually happening in...

Recursive shunting yard algorithm

everybody I am trying to implement simple algorithm "Shunting yard" (to parse infix to postfix notation) using SML (Standart ML) fun parseToRPN(input:string) = let val _input = explode input val digits = [#"0", #"1", #"2", #"3", #"4", #"5", #"6", #"7", #"8", #"9"] val stack = [] val output = []...

Standard ML / NJ: Loading in file of functions

I'm trying to write a bunch of functions in an SML file and then load them into the interpreter. I've been googling and came across this: Which has this section: Loading ML source text from a file The function use: string -> unit interprets its argument as a file...

SML - Recursive function for list string

I need your help! I'm trying to create a function that takes as input two elements of type (string*string*string) list and (string*string) list and return an element of type (string*string) list manipulating in a specific way. I need something like: returnString(([("s0","l0","s1"),("s1","l1","s0")]),([("s0","phi1"),("l0","chi1"),("l1","chi2"),("s1","phi2")])); the function that takes these inputs should return me:...

SML adding two pairs of numbers in a list

I'm trying to take a list, for example [1,2,3,4], and add each pair of in the list to get a new list [3,7]. I keep getting an error and I don't know what it means. Any help would be appreciated. fun listsum x = if null x then 0 else...

Using TCP in Standard ML

I'm trying to write a minimal TCP server in Standard ML and getting some type errors I don't understand. What I've got so far is fun sendHello sock = let val res = "HTTP/1.1 200 OK\r\nContent-Length: 12\r\n\r\nHello world!\r\n\r\n" val wds = map (fn c => Word8.fromInt (Char.ord c)) (String.explode res)...